Skip to content

Commit

Permalink
Refactors dashboard state to a component
Browse files Browse the repository at this point in the history
In support of the longer term fix brough on by #1372
  • Loading branch information
AllenBW committed Jan 25, 2018
1 parent 8bc4411 commit 46fc4b7
Show file tree
Hide file tree
Showing 12 changed files with 285 additions and 323 deletions.
1 change: 1 addition & 0 deletions client/app/components/_components.sass
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
// @import all of your component styles here
@import 'notifications/notifications'
@import './dashboard/dashboard'
10 changes: 7 additions & 3 deletions client/app/components/components.module.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { SharedModule } from '../shared/shared.module.js'
import { DashboardComponent } from './dashboard/dashboard.component.js'
import { DashboardComponentFactory } from './dashboard/dashboard.component.service.js'

export default angular
.module('app.components', [
SharedModule
])
.module('app.components', [
SharedModule
])
.component('dashboardComponent', DashboardComponent)
.factory('DashboardsState', DashboardComponentFactory)
.name
File renamed without changes.
74 changes: 74 additions & 0 deletions client/app/components/dashboard/dashboard.component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import templateUrl from './dashboard.html'

export const DashboardComponent = {
templateUrl,
controller: ComponentController,
controllerAs: 'vm'
}

/** @ngInject */
function ComponentController ($state, CatalogsState, EventNotifications, lodash, Chargeback, RBAC) {
const vm = this

const retiredTitle = __('Retire Status')
const allRequestTypes = ['pending', 'approved', 'denied']

vm.$onInit = function () {
vm.permissions = {
'monthlyCharges': RBAC.has(RBAC.FEATURES.DASHBOARD.VIEW.MONTHLY_CHARGES)
}

angular.extend(vm, {
requestsFeature: false,
servicesFeature: false,
servicesCount: {
total: 0,
current: 0,
retired: 0,
soon: 0
},
requestsCount: {
total: 0
},
navigateToRetiringSoonServicesList: navigateToRetiringSoonServicesList,
navigateToRetiredServicesList: navigateToRetiredServicesList,
navigateToCurrentServicesList: navigateToCurrentServicesList
})
}

function navigateToRetiredServicesList () {
$state.go('services', {
'filter': [{
'id': 'retired',
'title': retiredTitle,
'value': {id: true, title: __('Retired')}
}]
})
}

function navigateToRetiringSoonServicesList () {
const currentDate = new Date()
const filters = []

filters.push({'id': 'retired', 'title': retiredTitle, 'value': {id: false, title: __('Retires between')}})
filters.push({'id': 'retires_on', 'operator': '>', 'value': {id: currentDate.toISOString(), title: __('Now')}})
const days30 = currentDate.setDate(currentDate.getDate() + 30)
filters.push({
'id': 'retires_on',
'operator': '<',
'value': {id: new Date(days30).toISOString(), title: __('30 Days')}
})

$state.go('services', {'filter': filters})
}

function navigateToCurrentServicesList () {
$state.go('services', {
'filter': [{
'id': 'retired',
'title': retiredTitle,
'value': {id: false, title: __('Not Retired')}
}]
})
}
}
89 changes: 89 additions & 0 deletions client/app/components/dashboard/dashboard.component.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/** @ngInject */
export function DashboardComponentFactory (CollectionsApi, RBAC) {

return {
definedServiceIdsServices: resolveServicesWithDefinedServiceIds,
retiredServices: resolveRetiredServices,
expiringServices: resolveExpiringServices,
allRequests: resolveAllRequests
}

function pendingRequestsForServiceTemplateProvisionRequest () {
var filterValues = ['approval_state=pending_approval']
var options = {hide: 'resources', filter: filterValues}

return CollectionsApi.query('requests', options)
}

function pendingRequestsForServiceReconfigureRequest () {
var filterValues = ['type=ServiceReconfigureRequest', 'approval_state=pending_approval']
var options = {hide: 'resources', filter: filterValues}

return CollectionsApi.query('requests', options)
}

function approvedRequestsForServiceTemplateProvisionRequest () {
var filterValues = ['type=ServiceTemplateProvisionRequest', 'approval_state=approved']
var options = {hide: 'resources', filter: filterValues}

return CollectionsApi.query('requests', options)
}

function approvedRequestsForServiceReconfigureRequest () {
var filterValues = ['type=ServiceReconfigureRequest', 'approval_state=approved']
var options = {hide: 'resources', filter: filterValues}

return CollectionsApi.query('requests', options)
}

function deniedRequestsForServiceTemplateProvisionRequest () {
var filterValues = ['type=ServiceTemplateProvisionRequest', 'approval_state=denied']
var options = {hide: 'resources', filter: filterValues}

return CollectionsApi.query('requests', options)
}

function deniedRequestsForServiceReconfigureRequest () {
var filterValues = ['type=ServiceReconfigureRequest', 'approval_state=denied']
var options = {hide: 'resources', filter: filterValues}

return CollectionsApi.query('requests', options)
}

function resolveExpiringServices () {
if (RBAC.has('service_view') && RBAC.has(RBAC.FEATURES.SERVICES.VIEW)) {
const currentDate = new Date()
const date1 = 'retires_on>' + currentDate.toISOString()
const days30 = currentDate.setDate(currentDate.getDate() + 30)
const date2 = 'retires_on<' + new Date(days30).toISOString()
const options = {hide: 'resources', filter: ['retired=false', date1, date2]}

return CollectionsApi.query('services', options)
}

return true
}

function resolveRetiredServices () {
if (RBAC.has('service_view') && RBAC.has(RBAC.FEATURES.SERVICES.VIEW)) {
const options = {hide: 'resources', filter: ['service_id=nil', 'retired=true']}

return CollectionsApi.query('services', options)
}
return true
}

function resolveServicesWithDefinedServiceIds () {
if (RBAC.has('service_view') && RBAC.has(RBAC.FEATURES.SERVICES.VIEW)) {
const options = {
expand: 'resources',
filter: ['service_id=nil'],
attributes: ['chargeback_report']
}

return CollectionsApi.query('services', options)
}

return true
}
}
Empty file.
77 changes: 77 additions & 0 deletions client/app/components/dashboard/dashboard.component.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* global $state, readJSON, RBAC, $httpBackend, $controller, CollectionsApi */
/* eslint-disable no-unused-expressions */
describe('State: dashboard', () => {
const permissions = readJSON('tests/mock/rbac/allPermissions.json')
beforeEach(() => {
module('app.core', 'app.states', 'app.orders', 'app.services')
bard.inject('$location', '$rootScope', '$state', '$templateCache', '$httpBackend', '$q', 'RBAC')
RBAC.set(permissions)
})

beforeEach(() => {
let d = new Date()
d.setMinutes(d.getMinutes() + 30)
d = d.toISOString()
d = d.substring(0, d.indexOf('.'))

$httpBackend.whenGET('').respond(200)
})

describe('route', () => {
beforeEach(() => {
bard.inject('$location', '$rootScope', '$state', '$templateCache')
})

it('should work with $state.go', () => {
$state.go('dashboard')
expect($state.is('dashboard'))
})
})

describe('controller', () => {
let controller, dashboardState
const resolveServicesWithDefinedServiceIds = {}
const retiredServices = {}
const resolveNonRetiredServices = {}
const expiringServices = {}
const resolveAllRequests = []

beforeEach(() => {
bard.inject('$controller', '$log', '$state', '$rootScope', 'CollectionsApi', 'RBAC')

const controllerResolves = {
definedServiceIdsServices: resolveServicesWithDefinedServiceIds,
retiredServices: retiredServices,
nonRetiredServices: resolveNonRetiredServices,
expiringServices: expiringServices,
allRequests: resolveAllRequests
}
dashboardState = $state.get('dashboard')
controller = $controller(dashboardState.controller, controllerResolves)
})

it('should be created successfully', () => {
expect(controller).to.be.defined
})

describe('resolveExpiringServices', () => {
it('makes a query request using the CollectionApi', () => {
const clock = sinon.useFakeTimers(new Date('2016-01-01').getTime())
let collectionsApiSpy = sinon.stub(CollectionsApi)

dashboardState.resolve.expiringServices(collectionsApiSpy, RBAC)

expect(collectionsApiSpy.query).to.have.been.calledWith('services', {
hide: 'resources',
filter: [
'retired=false',
'retires_on>2016-01-01T00:00:00.000Z',
'retires_on<2016-01-31T00:00:00.000Z'
]
})

clock.restore()
})
})
})
})
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<div class="container-fluid container-cards-pf ss-dashboard">
<div class="row row-cards-pf">
<div class="col-xs-6 col-sm-6 col-md-6" ng-show="vm.servicesFeature">
<div class="col-xs-6 col-sm-6 col-md-6" >
<div class="card-pf card-pf-accented ss-dashboard__card-primary" ui-sref="services">
<div class="card-pf-body">
<span class="fa fa-file-o"></span>
<div class="ss-dashboard__card-primary__count">
<h2>{{ ::vm.servicesCount.total }}</h2>
<h2>{{ vm.servicesCount.total }}</h2>
<h3 translate>Total Services</h3>
</div>
</div>
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6" ng-show="vm.requestsFeature">
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="card-pf card-pf-accented ss-dashboard__card-primary total-requests">
<div class="card-pf-body">
<span class="fa fa-file-text-o"></span>
Expand All @@ -24,7 +24,7 @@ <h3 translate>Total Requests</h3>
</div>
</div>
<div class="row row-cards-pf">
<div class="col-xs-6 col-sm-6 col-md-6" ng-show="vm.servicesFeature">
<div class="col-xs-6 col-sm-6 col-md-6" ng-if="vm.servicesFeature">
<div class="card-pf">
<div class="row row-cards-pf">
<div class="card-pf card-pf-aggregate-status card-divider">
Expand All @@ -37,7 +37,7 @@ <h2 class="card-pf-title" translate>
<span class="card-pf-aggregate-status-notification">
<span class="pficon pficon-warning-triangle-o" uib-tooltip="{{'The number of services retiring within the next 30 days'|translate}}" tooltip-placement="bottom"></span>
</span>
<span class="card-pf-aggregate-status-notification">{{ ::vm.servicesCount.soon }}</span>
<span class="card-pf-aggregate-status-notification">{{ vm.servicesCount.soon }}</span>
</p>
</div>
</a>
Expand All @@ -52,7 +52,7 @@ <h2 class="card-pf-title" translate>
<span class="card-pf-aggregate-status-notification">
<span class="pficon fa fa-check" uib-tooltip="{{'The number of active services'|translate}}" tooltip-placement="bottom"></span>
</span>
<span class="card-pf-aggregate-status-notification">{{ ::vm.servicesCount.current }}</span>
<span class="card-pf-aggregate-status-notification">{{ vm.servicesCount.current }}</span>
</p>
</div>
</a>
Expand All @@ -67,7 +67,7 @@ <h2 class="card-pf-title" translate>
<span class="card-pf-aggregate-status-notification">
<span class="pficon pficon-close" uib-tooltip="{{'The number of services which have hit their retirement period or been retired'|translate}}" tooltip-placement="bottom"></span>
</span>
<span class="card-pf-aggregate-status-notification">{{ ::vm.servicesCount.retired }}
<span class="card-pf-aggregate-status-notification">{{ vm.servicesCount.retired }}
</span>
</p>
</div>
Expand All @@ -94,47 +94,47 @@ <h2 class="card-pf-title" translate>
</div>
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6" ng-show="vm.requestsFeature">
<div class="col-xs-6 col-sm-6 col-md-6" ng-if="vm.requestsFeature">
<div class="card-pf">
<div class="row row-cards-pf">
<div class="card-pf card-pf-aggregate-status card-divider">
<div class="card-pf-body">
<h2 class="card-pf-title" translate>
Pending Requests
</h2>
<p class="card-pf-aggregate-status-notifications">
<div class="card-pf-body">
<h2 class="card-pf-title" translate>
Pending Requests
</h2>
<p class="card-pf-aggregate-status-notifications">
<span class="card-pf-aggregate-status-notification">
<span class="pficon pficon-warning-triangle-o"></span>
</span>
<span class="card-pf-aggregate-status-notification">{{ ::vm.requestsCount.pending }}</span>
</p>
</div>
<span class="card-pf-aggregate-status-notification">{{ vm.requestsCount.pending }}</span>
</p>
</div>
</div>
<div class="card-pf card-pf-aggregate-status card-divider">
<div class="card-pf-body">
<h2 class="card-pf-title" translate>
Approved Requests
</h2>
<p class="card-pf-aggregate-status-notifications">
<div class="card-pf-body">
<h2 class="card-pf-title" translate>
Approved Requests
</h2>
<p class="card-pf-aggregate-status-notifications">
<span class="card-pf-aggregate-status-notification">
<span class="pficon fa fa-check"></span>
</span>
<span class="card-pf-aggregate-status-notification">{{ ::vm.requestsCount.approved }}</span>
</p>
</div>
<span class="card-pf-aggregate-status-notification">{{ vm.requestsCount.approved }}</span>
</p>
</div>
</div>
<div class="card-pf card-pf-aggregate-status card-divider">
<div class="card-pf-body">
<h2 class="card-pf-title" translate>
Denied Requests
</h2>
<p class="card-pf-aggregate-status-notifications">
<div class="card-pf-body">
<h2 class="card-pf-title" translate>
Denied Requests
</h2>
<p class="card-pf-aggregate-status-notifications">
<span class="card-pf-aggregate-status-notification">
<span class="pficon pficon pficon-close"></span>
</span>
<span class="card-pf-aggregate-status-notification">{{ ::vm.requestsCount.denied }}</span>
</p>
</div>
<span class="card-pf-aggregate-status-notification">{{ vm.requestsCount.denied }}</span>
</p>
</div>
</div>
</div>
</div>
Expand Down
1 change: 0 additions & 1 deletion client/app/states/_states.sass
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@
@import '404/404'
@import 'login/login'
@import 'catalogs/details/details'
@import 'dashboard/dashboard'
@import 'orders/details/details'
Loading

0 comments on commit 46fc4b7

Please sign in to comment.