feat: add groups view page (datatable-tabs) (#793)

Fixes: #776
Fixes: #777
Fixes: #791
This commit is contained in:
Karan Takalkar
2020-05-23 21:09:51 +05:30
committed by GitHub
parent b333291f08
commit 377c4891e4
23 changed files with 785 additions and 58 deletions
@@ -0,0 +1,4 @@
<div>
<mifosx-multi-row *ngIf="multiRowDatatableFlag" [dataObject]="groupDatatable"></mifosx-multi-row>
<mifosx-single-row *ngIf="!multiRowDatatableFlag" [dataObject]="groupDatatable"></mifosx-single-row>
</div>
@@ -0,0 +1,25 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { DatatableTabsComponent } from './datatable-tabs.component';
describe('DatatableTabsComponent', () => {
let component: DatatableTabsComponent;
let fixture: ComponentFixture<DatatableTabsComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ DatatableTabsComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(DatatableTabsComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
@@ -0,0 +1,31 @@
/** Angular Imports */
import { Component } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
/**
* Groups Datatable Tabs Component
*/
@Component({
selector: 'mifosx-datatable-tabs',
templateUrl: './datatable-tabs.component.html',
styleUrls: ['./datatable-tabs.component.scss']
})
export class DatatableTabsComponent {
/** Group Datatable */
groupDatatable: any;
/** Multi Row Datatable Flag */
multiRowDatatableFlag: boolean;
/**
* Fetches data table data from `resolve`
* @param {ActivatedRoute} route Activated Route.
*/
constructor(private route: ActivatedRoute) {
this.route.data.subscribe((data: { groupDatatable: any }) => {
this.groupDatatable = data.groupDatatable;
this.multiRowDatatableFlag = this.groupDatatable.columnHeaders[0].columnName === 'id' ? true : false;
});
}
}
@@ -0,0 +1,33 @@
<div class="tab-container mat-typography">
<div fxLayout="row" fxLayoutAlign="start">
<div class="m-b-10">
<h3>{{datatableName}}</h3>
</div>
<div class="action-button m-b-5" fxLayoutGap="10px">
<span *mifosxHasPermission="'CREATE_' + datatableName">
<button mat-raised-button color="primary" (click)="add()">
<fa-icon icon="plus"></fa-icon>&nbsp;&nbsp;Add
</button>
</span>
<span *mifosxHasPermission="'DELETE_' + datatableName">
<button class="delete-button" mat-raised-button color="warn" (click)="delete()" *ngIf="showDeleteBotton">
<fa-icon icon="trash"></fa-icon>&nbsp;&nbsp;Delete All
</button>
</span>
</div>
</div>
<table #dataTable mat-table [hidden]="!datatableData[0]" [dataSource]="datatableData" class="mat-elevation-z1 m-b-25">
<ng-container *ngFor="let datatableColumn of datatableColumns;let i = index" [matColumnDef]="datatableColumn">
<th mat-header-cell *matHeaderCellDef> {{datatableColumn}} </th>
<td mat-cell *matCellDef="let data"> {{data.row[i]}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="datatableColumns"></tr>
<tr mat-row *matRowDef="let row; columns: datatableColumns;"></tr>
</table>
</div>
@@ -0,0 +1,10 @@
.tab-container {
padding: 1%;
margin: 1%;
.action-button{
margin-left: auto;
}
table {
width: 100%;
}
}
@@ -0,0 +1,25 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { MultiRowComponent } from './multi-row.component';
describe('MultiRowComponent', () => {
let component: MultiRowComponent;
let fixture: ComponentFixture<MultiRowComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ MultiRowComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(MultiRowComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
@@ -0,0 +1,184 @@
/** Angular Imports */
import { Component, OnChanges, OnInit, Input, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { MatTable } from '@angular/material/table';
import { MatDialog } from '@angular/material';
import { DatePipe } from '@angular/common';
/** Custom Dialogs */
import { FormDialogComponent } from 'app/shared/form-dialog/form-dialog.component';
import { DeleteDialogComponent } from '../../../../shared/delete-dialog/delete-dialog.component';
/** Custom Models */
import { FormfieldBase } from 'app/shared/form-dialog/formfield/model/formfield-base';
import { InputBase } from 'app/shared/form-dialog/formfield/model/input-base';
import { SelectBase } from 'app/shared/form-dialog/formfield/model/select-base';
import { CheckboxBase } from 'app/shared/form-dialog/formfield/model/checkbox-base';
import { DatepickerBase } from 'app/shared/form-dialog/formfield/model/datepicker-base';
/** Custom Services */
import { GroupsService } from '../../../groups.service';
/**
* Group Multi Row Data Tables
*/
@Component({
selector: 'mifosx-multi-row',
templateUrl: './multi-row.component.html',
styleUrls: ['./multi-row.component.scss']
})
export class MultiRowComponent implements OnInit, OnChanges {
/** Data Object */
@Input() dataObject: any;
/** Data Table Name */
datatableName: string;
/** Data Table Columns */
datatableColumns: string[] = [];
/** Data Table Data */
datatableData: any;
/** Group Id */
groupId: string;
/** Toggle button visibility */
showDeleteBotton: boolean;
/** Data Table Reference */
@ViewChild('dataTable') dataTableRef: MatTable<Element>;
/**
* Fetches group Id from parent route params.
* @param {ActivatedRoute} route Activated Route.
* @param {DatePipe} datePipe Date Pipe.
* @param {GroupsService} groupsService Groups Service.
* @param {MatDialog} dialog Mat Dialog.
*/
constructor(private route: ActivatedRoute,
private datePipe: DatePipe,
private groupsService: GroupsService,
private dialog: MatDialog) {
this.groupId = this.route.parent.parent.snapshot.paramMap.get('groupId');
}
/**
* Updates related variables on changes to dataObject.
*/
ngOnChanges() {
this.datatableColumns = this.dataObject.columnHeaders.map((columnHeader: any) => {
return columnHeader.columnName;
});
this.datatableData = this.dataObject.data;
this.showDeleteBotton = this.datatableData[0] ? true : false;
}
/**
* Fetches data table name from route params.
* subscription is required due to asynchronicity.
*/
ngOnInit() {
this.route.params.subscribe((routeParams: any) => {
this.datatableName = routeParams.datatableName;
});
}
/**
* Adds a new row to the given multi row data table.
*/
add() {
let dataTableEntryObject: any = { locale: 'en' };
const dateTransformColumns: string[] = [];
const columns = this.dataObject.columnHeaders.filter((column: any) => {
return ((column.columnName !== 'id') && (column.columnName !== 'group_id'));
});
const formfields: FormfieldBase[] = columns.map((column: any) => {
switch (column.columnDisplayType) {
case 'INTEGER':
case 'STRING':
case 'DECIMAL':
case 'TEXT': return new InputBase({
controlName: column.columnName,
label: column.columnName,
value: '',
type: (column.columnDisplayType === 'INTEGER' || column.columnDisplayType === 'DECIMAL') ? 'number' : 'text',
required: (column.isColumnNullable) ? false : true
});
case 'BOOLEAN': return new CheckboxBase({
controlName: column.columnName,
label: column.columnName,
value: '',
type: 'checkbox',
required: (column.isColumnNullable) ? false : true
});
case 'CODELOOKUP': return new SelectBase({
controlName: column.columnName,
label: column.columnName,
value: '',
options: { label: 'value', value: 'id', data: column.columnValues },
required: (column.isColumnNullable) ? false : true
});
case 'DATE': {
dateTransformColumns.push(column.columnName);
dataTableEntryObject.dateFormat = 'yyyy-MM-dd';
return new DatepickerBase({
controlName: column.columnName,
label: column.columnName,
value: '',
type: 'date',
required: (column.isColumnNullable) ? false : true
});
}
case 'DATETIME': {
dateTransformColumns.push(column.columnName);
dataTableEntryObject.dateFormat = 'yyyy-MM-dd HH:mm';
return new DatepickerBase({
controlName: column.columnName,
label: column.columnName,
value: '',
type: 'datetime-local',
required: (column.isColumnNullable) ? false : true
});
}
}
});
const data = {
title: 'Add ' + this.datatableName,
formfields: formfields
};
const addDialogRef = this.dialog.open(FormDialogComponent, { data });
addDialogRef.afterClosed().subscribe((response: any) => {
if (response.data) {
dateTransformColumns.forEach((column) => {
response.data.value[column] = this.datePipe.transform(response.data.value[column], dataTableEntryObject.dateFormat);
});
dataTableEntryObject = { ...response.data.value, ...dataTableEntryObject };
this.groupsService.addGroupDatatableEntry(this.groupId, this.datatableName, dataTableEntryObject).subscribe(() => {
this.groupsService.getGroupDatatable(this.groupId, this.datatableName).subscribe((dataObject: any) => {
this.datatableData = dataObject.data;
this.dataTableRef.renderRows();
});
});
}
});
}
/**
* Deletes all rows of the given multi row data table.
*/
delete() {
const deleteDataTableDialogRef = this.dialog.open(DeleteDialogComponent, {
data: { deleteContext: `the contents of ${this.datatableName}` }
});
deleteDataTableDialogRef.afterClosed().subscribe((response: any) => {
if (response.delete) {
this.groupsService.deleteDatatableContent(this.groupId, this.datatableName).subscribe(() => {
this.groupsService.getGroupDatatable(this.groupId, this.datatableName).subscribe((dataObject: any) => {
this.datatableData = dataObject.data;
this.showDeleteBotton = false;
this.dataTableRef.renderRows();
});
});
}
});
}
}
@@ -0,0 +1,29 @@
<div class="tab-container mat-typography">
<h3>{{datatableName}}</h3>
<div fxLayout="row" fxLayoutAlign="flex-end">
<span *mifosxHasPermission="'CREATE_' + datatableName">
<button mat-raised-button color="primary" (click)="add()" *ngIf="!dataObject.data[0]">
<fa-icon icon="plus"></fa-icon>&nbsp;&nbsp;Add
</button>
</span>
<span *mifosxHasPermission="'UPDATE_' + datatableName">
<button mat-raised-button color="primary" (click)="edit()" *ngIf="dataObject.data[0]">
<fa-icon icon="edit"></fa-icon>&nbsp;&nbsp;Edit
</button>
</span>
<span *mifosxHasPermission="'DELETE_' + datatableName" class="delete-button">
<button mat-raised-button color="warn" (click)="delete()" *ngIf="dataObject.data[0]">
<fa-icon icon="trash"></fa-icon>&nbsp;&nbsp;Delete
</button>
</span>
</div>
<mat-list role="list" *ngIf="dataObject.data[0]">
<mat-list-item role="listitem" *ngFor="let columnHeader of dataObject.columnHeaders;let i = index">
{{columnHeader.columnName}} : {{dataObject.data[0].row[i]}}
</mat-list-item>
</mat-list>
</div>
@@ -0,0 +1,7 @@
.tab-container {
padding: 1%;
margin: 1%;
.delete-button {
margin-left: 1%;
}
}
@@ -0,0 +1,25 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { SingleRowComponent } from './single-row.component';
describe('SingleRowComponent', () => {
let component: SingleRowComponent;
let fixture: ComponentFixture<SingleRowComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ SingleRowComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(SingleRowComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
@@ -0,0 +1,207 @@
/** Angular Imports */
import { Component, Input, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { MatDialog } from '@angular/material';
import { DatePipe } from '@angular/common';
/** Custom Components */
import { FormDialogComponent } from 'app/shared/form-dialog/form-dialog.component';
import { DeleteDialogComponent } from '../../../../shared/delete-dialog/delete-dialog.component';
/** Custom Models */
import { FormfieldBase } from 'app/shared/form-dialog/formfield/model/formfield-base';
import { InputBase } from 'app/shared/form-dialog/formfield/model/input-base';
import { DatepickerBase } from 'app/shared/form-dialog/formfield/model/datepicker-base';
import { SelectBase } from 'app/shared/form-dialog/formfield/model/select-base';
import { CheckboxBase } from 'app/shared/form-dialog/formfield/model/checkbox-base';
/** Custom Services */
import { GroupsService } from '../../../groups.service';
/**
* Groups Single Row Data Tables
*/
@Component({
selector: 'mifosx-single-row',
templateUrl: './single-row.component.html',
styleUrls: ['./single-row.component.scss']
})
export class SingleRowComponent implements OnInit {
/** Data Object */
@Input() dataObject: any;
/** Data Table Name */
datatableName: string;
/** Group Id */
groupId: string;
/**
* Fetches group Id from parent route params.
* @param {ActivatedRoute} route Activated Route.
* @param {DatePipe} datePipe Date Pipe.
* @param {GroupsService} groupsService Groups Service.
* @param {MatDialog} dialog Mat Dialog.
*/
constructor(private route: ActivatedRoute,
private datePipe: DatePipe,
private dialog: MatDialog,
private groupsService: GroupsService) {
this.groupId = this.route.parent.parent.snapshot.paramMap.get('groupId');
}
/**
* Fetches data table name from route params.
* subscription is required due to asynchronicity.
*/
ngOnInit() {
this.route.params.subscribe((routeParams: any) => {
this.datatableName = routeParams.datatableName;
});
}
/**
* Creates a new instance of the given single row data table.
*/
add() {
let dataTableEntryObject: any = { locale: 'en' };
const dateTransformColumns: string[] = [];
const columns = this.dataObject.columnHeaders.filter((column: any) => {
return ((column.columnName !== 'id') && (column.columnName !== 'group_id'));
});
const formfields: FormfieldBase[] = this.getFormfields(columns, dateTransformColumns, dataTableEntryObject);
const data = {
title: 'Add ' + this.datatableName,
formfields: formfields
};
const addDialogRef = this.dialog.open(FormDialogComponent, { data });
addDialogRef.afterClosed().subscribe((response: any) => {
if (response.data) {
dateTransformColumns.forEach((column) => {
response.data.value[column] = this.datePipe.transform(response.data.value[column], dataTableEntryObject.dateFormat);
});
dataTableEntryObject = { ...response.data.value, ...dataTableEntryObject };
this.groupsService.addGroupDatatableEntry(this.groupId, this.datatableName, dataTableEntryObject).subscribe(() => {
this.groupsService.getGroupDatatable(this.groupId, this.datatableName).subscribe((dataObject: any) => {
this.dataObject = dataObject;
});
});
}
});
}
/**
* Edits the current instance of single row data table.
*/
edit() {
let dataTableEntryObject: any = { locale: 'en' };
const dateTransformColumns: string[] = [];
const columns = this.dataObject.columnHeaders.filter((column: any) => {
return ((column.columnName !== 'id') && (column.columnName !== 'group_id'));
});
let formfields: FormfieldBase[] = this.getFormfields(columns, dateTransformColumns, dataTableEntryObject);
formfields = formfields.map((formfield: FormfieldBase, index: number) => {
formfield.value = (this.dataObject.data[0].row[index + 1]) ? this.dataObject.data[0].row[index + 1] : '';
return formfield;
});
const data = {
title: 'Edit ' + this.datatableName,
layout: { addButtonText: 'Confirm' },
formfields: formfields
};
const editDialogRef = this.dialog.open(FormDialogComponent, { data });
editDialogRef.afterClosed().subscribe((response: any) => {
if (response.data) {
dateTransformColumns.forEach((column) => {
response.data.value[column] = this.datePipe.transform(response.data.value[column], dataTableEntryObject.dateFormat);
});
dataTableEntryObject = { ...response.data.value, ...dataTableEntryObject };
this.groupsService.editGroupDatatableEntry(this.groupId, this.datatableName, dataTableEntryObject).subscribe(() => {
this.groupsService.getGroupDatatable(this.groupId, this.datatableName).subscribe((dataObject: any) => {
this.dataObject = dataObject;
});
});
}
});
}
/**
* Deletes the current instance of single row data table.
*/
delete() {
const deleteDataTableDialogRef = this.dialog.open(DeleteDialogComponent, {
data: { deleteContext: `the contents of ${this.datatableName}` }
});
deleteDataTableDialogRef.afterClosed().subscribe((response: any) => {
if (response.delete) {
this.groupsService.deleteDatatableContent(this.groupId, this.datatableName)
.subscribe(() => {
this.groupsService.getGroupDatatable(this.groupId, this.datatableName).subscribe((dataObject: any) => {
this.dataObject = dataObject;
});
});
}
});
}
/**
* Maps API response to data table form fields.
* @param {any} columns Data Table Columns.
* @param {string[]} dateTransformColumns Columns transformed with date pipe.
* @param {any} dataTableEntryObject Additional data table details.
*/
getFormfields(columns: any, dateTransformColumns: string[], dataTableEntryObject: any) {
return columns.map((column: any) => {
switch (column.columnDisplayType) {
case 'INTEGER':
case 'STRING':
case 'DECIMAL':
case 'TEXT': return new InputBase({
controlName: column.columnName,
label: column.columnName,
value: '',
type: (column.columnDisplayType === 'INTEGER' || column.columnDisplayType === 'DECIMAL') ? 'number' : 'text',
required: (column.isColumnNullable) ? false : true
});
case 'BOOLEAN': return new CheckboxBase({
controlName: column.columnName,
label: column.columnName,
value: '',
type: 'checkbox',
required: (column.isColumnNullable) ? false : true
});
case 'CODELOOKUP': return new SelectBase({
controlName: column.columnName,
label: column.columnName,
value: '',
options: { label: 'value', value: 'id', data: column.columnValues },
required: (column.isColumnNullable) ? false : true
});
case 'DATE': {
dateTransformColumns.push(column.columnName);
dataTableEntryObject.dateFormat = 'yyyy-MM-dd';
return new DatepickerBase({
controlName: column.columnName,
label: column.columnName,
value: '',
type: 'date',
required: (column.isColumnNullable) ? false : true
});
}
case 'DATETIME': {
dateTransformColumns.push(column.columnName);
dataTableEntryObject.dateFormat = 'yyyy-MM-dd HH:mm';
return new DatepickerBase({
controlName: column.columnName,
label: column.columnName,
value: '',
type: 'datetime-local',
required: (column.isColumnNullable) ? false : true
});
}
}
});
}
}