From e40158bd6e01b03e3be30d3c31e5c727b658a715 Mon Sep 17 00:00:00 2001 From: pwai Date: Sun, 9 Dec 2018 13:59:55 +0000 Subject: [PATCH] feat: add manage surveys component (#343) Fixes #186 --- .../manage-surveys.component.html | 71 +++++++++++++++++ .../manage-surveys.component.scss | 16 ++++ .../manage-surveys.component.spec.ts | 25 ++++++ .../manage-surveys.component.ts | 79 +++++++++++++++++++ .../manage-surveys/manage-surveys.resolver.ts | 30 +++++++ src/app/system/system-routing.module.ts | 13 ++- src/app/system/system.component.html | 2 +- src/app/system/system.module.ts | 4 +- src/app/system/system.service.ts | 7 ++ 9 files changed, 244 insertions(+), 3 deletions(-) create mode 100644 src/app/system/manage-surveys/manage-surveys.component.html create mode 100644 src/app/system/manage-surveys/manage-surveys.component.scss create mode 100644 src/app/system/manage-surveys/manage-surveys.component.spec.ts create mode 100644 src/app/system/manage-surveys/manage-surveys.component.ts create mode 100644 src/app/system/manage-surveys/manage-surveys.resolver.ts diff --git a/src/app/system/manage-surveys/manage-surveys.component.html b/src/app/system/manage-surveys/manage-surveys.component.html new file mode 100644 index 000000000..4ab7395a1 --- /dev/null +++ b/src/app/system/manage-surveys/manage-surveys.component.html @@ -0,0 +1,71 @@ +
+ +
+ +
+ +
+ + Filter + + +
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Key {{ survey.key }} Name {{ survey.name }} Description {{ survey.description }} Country Code {{ survey.countryCode }} Status +
+ +
+
Action + + +
+ + + +
+ +
diff --git a/src/app/system/manage-surveys/manage-surveys.component.scss b/src/app/system/manage-surveys/manage-surveys.component.scss new file mode 100644 index 000000000..bdeabc553 --- /dev/null +++ b/src/app/system/manage-surveys/manage-surveys.component.scss @@ -0,0 +1,16 @@ +table { + width: 100%; + + .select-row:hover { + cursor: pointer; + } +} + +// TODO: CONFIGURE THESE CLASSES GLOBALLY +.enabled { + color: #32cd32; +} + +.disabled { + color: #f44366; +} diff --git a/src/app/system/manage-surveys/manage-surveys.component.spec.ts b/src/app/system/manage-surveys/manage-surveys.component.spec.ts new file mode 100644 index 000000000..5385671fe --- /dev/null +++ b/src/app/system/manage-surveys/manage-surveys.component.spec.ts @@ -0,0 +1,25 @@ +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; + +import { ManageSurveysComponent } from './manage-surveys.component'; + +describe('SurveysComponent', () => { + let component: ManageSurveysComponent; + let fixture: ComponentFixture; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [ ManageSurveysComponent ] + }) + .compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(ManageSurveysComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/app/system/manage-surveys/manage-surveys.component.ts b/src/app/system/manage-surveys/manage-surveys.component.ts new file mode 100644 index 000000000..1273dfbc0 --- /dev/null +++ b/src/app/system/manage-surveys/manage-surveys.component.ts @@ -0,0 +1,79 @@ +/** Angular Imports */ +import { Component, OnInit, ViewChild } from '@angular/core'; +import { MatTableDataSource, MatPaginator, MatSort } from '@angular/material'; +import { ActivatedRoute } from '@angular/router'; + +/** + * Manage Surveys component. + */ +@Component({ + selector: 'mifosx-manage-surveys', + templateUrl: './manage-surveys.component.html', + styleUrls: ['./manage-surveys.component.scss'], +}) +export class ManageSurveysComponent implements OnInit { + + /* Surveys data */ + surveysData: any; + /* Columns to be displayed in manage surveys data table */ + displayedColumns: string[] = ['key', 'name', 'description', 'countryCode', 'status', 'action']; + /* Data source for manage surveys data table */ + dataSource: MatTableDataSource; + + /** Paginator for manage surveys table. */ + @ViewChild(MatPaginator) paginator: MatPaginator; + /** Sorter for manage surveys table. */ + @ViewChild(MatSort) sort: MatSort; + + /** + * Retrieves the surveys data from `resolve`. + * @param {ActivatedRoute} route Activated Route. + */ + constructor(private route: ActivatedRoute) { + this.route.data.subscribe(( data: { surveys: any }) => { + this.surveysData = data.surveys; + }); + } + + /** + * Returns whether an survey is active based on its duration + * @param {number} validFrom Date valid from + * @param {number} validTo Date valid to + */ + isActive(validFrom: number, validTo: number) { + const curdate = new Date().getTime(); + return (curdate > validFrom && curdate < validTo); + } + + /** + * Sets the manage surveys table. And initiates button and action sort + */ + ngOnInit() { + this.setSurveys(); + this.dataSource.sortingDataAccessor = (item, property) => { + switch (property) { + case 'status': return this.isActive(item.validFrom, item.validTo); + case 'action': return this.isActive(item.validFrom, item.validTo); + default: return item[property]; + } + }; + } + + /** + * Initializes the data source, paginator and sorter for surveys table. + */ + setSurveys() { + this.dataSource = new MatTableDataSource(this.surveysData); + this.dataSource.paginator = this.paginator; + this.dataSource.sort = this.sort; + } + + /** + * Filters data in surveys table based on passed value. + * @param {string} filterValue Value to filter data. + */ + applyFilter(filterValue: string) { + this.dataSource.filter = filterValue.trim().toLowerCase(); + } + +} diff --git a/src/app/system/manage-surveys/manage-surveys.resolver.ts b/src/app/system/manage-surveys/manage-surveys.resolver.ts new file mode 100644 index 000000000..b41e45946 --- /dev/null +++ b/src/app/system/manage-surveys/manage-surveys.resolver.ts @@ -0,0 +1,30 @@ +/** Angular Imports */ +import { Injectable } from '@angular/core'; +import { Resolve } from '@angular/router'; + +/** rxjs Imports */ +import { Observable } from 'rxjs'; + +/** Custom Services */ +import { SystemService } from '../system.service'; + +/** + * Surveys data resolver. + */ +@Injectable() +export class ManageSurveysResolver implements Resolve { + + /** + * @param {SystemService} systemService System service. + */ + constructor(private systemService: SystemService) {} + + /** + * Returns the Surveys data. + * @returns {Observable} + */ + resolve(): Observable { + return this.systemService.getSurveys(); + } + +} diff --git a/src/app/system/system-routing.module.ts b/src/app/system/system-routing.module.ts index 629f19410..a24568aec 100644 --- a/src/app/system/system-routing.module.ts +++ b/src/app/system/system-routing.module.ts @@ -13,12 +13,14 @@ import { ExternalServicesComponent } from './external-services/external-services import { ManageDataTablesComponent } from './manage-data-tables/manage-data-tables.component'; import { ManageHooksComponent } from './manage-hooks/manage-hooks.component'; import { RolesAndPermissionsComponent } from './roles-and-permissions/roles-and-permissions.component'; +import { ManageSurveysComponent } from './manage-surveys/manage-surveys.component'; /** Custom Resolvers */ import { CodesResolver } from './codes/codes.resolver'; import { ManageDataTablesResolver } from './manage-data-tables/manage-data-tables.resolver'; import { ManageHooksResolver } from './manage-hooks/manage-hooks.resolver'; import { RolesAndPermissionsResolver } from './roles-and-permissions/roles-and-permissions.resolver'; +import { ManageSurveysResolver } from './manage-surveys/manage-surveys.resolver'; const routes: Routes = [ Route.withShell([ @@ -78,6 +80,14 @@ const routes: Routes = [ }, data: { title: extract('Roles and Permissions'), breadcrumb: 'Roles and Permissions' }, }, + { + path: 'surveys', + component: ManageSurveysComponent, + resolve: { + surveys: ManageSurveysResolver + }, + data: { title: extract('Manage Surveys'), breadcrumb: 'Manage Surveys' }, + } ] } ]) @@ -90,7 +100,8 @@ const routes: Routes = [ CodesResolver, ManageDataTablesResolver, ManageHooksResolver, - RolesAndPermissionsResolver + RolesAndPermissionsResolver, + ManageSurveysResolver ] }) export class SystemRoutingModule { } diff --git a/src/app/system/system.component.html b/src/app/system/system.component.html index 22a4d9de6..47b88041f 100644 --- a/src/app/system/system.component.html +++ b/src/app/system/system.component.html @@ -56,7 +56,7 @@

Define or modify entity to entity mappings

- + diff --git a/src/app/system/system.module.ts b/src/app/system/system.module.ts index 639d31d36..bbc1f954f 100644 --- a/src/app/system/system.module.ts +++ b/src/app/system/system.module.ts @@ -13,6 +13,7 @@ import { ExternalServicesComponent } from './external-services/external-services import { ManageDataTablesComponent } from './manage-data-tables/manage-data-tables.component'; import { ManageHooksComponent } from './manage-hooks/manage-hooks.component'; import { RolesAndPermissionsComponent } from './roles-and-permissions/roles-and-permissions.component'; +import { ManageSurveysComponent } from './manage-surveys/manage-surveys.component'; @NgModule({ imports: [ @@ -26,7 +27,8 @@ import { RolesAndPermissionsComponent } from './roles-and-permissions/roles-and- ExternalServicesComponent, ManageDataTablesComponent, ManageHooksComponent, - RolesAndPermissionsComponent + RolesAndPermissionsComponent, + ManageSurveysComponent ] }) export class SystemModule { } diff --git a/src/app/system/system.service.ts b/src/app/system/system.service.ts index 6695dafdb..e085f62a5 100644 --- a/src/app/system/system.service.ts +++ b/src/app/system/system.service.ts @@ -46,6 +46,13 @@ export class SystemService { return this.http.get('/codes'); } + /** + * @returns {Observable} Fetches Surveys. + */ + getSurveys(): Observable { + return this.http.get('/surveys'); + } + /** * @param {any} code Code to be created. * @returns {Observable}