178 lines
7.0 KiB
TypeScript
178 lines
7.0 KiB
TypeScript
import { Component, OnInit, Input } from '@angular/core';
|
|
import { ActivatedRoute } from '@angular/router';
|
|
import { MatDialog } from '@angular/material/dialog';
|
|
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 { 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 Services */
|
|
import { ClientsService } from '../../../clients.service';
|
|
import { SettingsService } from 'app/settings/settings.service';
|
|
|
|
|
|
@Component({
|
|
selector: 'mifosx-single-row',
|
|
templateUrl: './single-row.component.html',
|
|
styleUrls: ['./single-row.component.scss']
|
|
})
|
|
export class SingleRowComponent implements OnInit {
|
|
|
|
@Input() dataObject: any;
|
|
datatableName: string;
|
|
clientId: string;
|
|
|
|
constructor(private route: ActivatedRoute,
|
|
private datePipe: DatePipe,
|
|
private dialog: MatDialog,
|
|
private clientsService: ClientsService,
|
|
private settingsService: SettingsService) {
|
|
this.clientId = this.route.parent.parent.snapshot.paramMap.get('clientId');
|
|
}
|
|
|
|
ngOnInit() {
|
|
this.route.params.subscribe((routeParams: any) => {
|
|
this.datatableName = routeParams.datatableName;
|
|
});
|
|
}
|
|
|
|
add() {
|
|
let dataTableEntryObject: any = {
|
|
locale: this.settingsService.language.code
|
|
};
|
|
const dateTransformColumns: string[] = [];
|
|
const columns = this.dataObject.columnHeaders.filter((column: any) => {
|
|
return ((column.columnName !== 'id') && (column.columnName !== 'client_id') && (column.columnName !== 'created_at') && (column.columnName !== 'updated_at'));
|
|
});
|
|
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) {
|
|
// format Dates
|
|
dateTransformColumns.forEach((column) => {
|
|
response.data.value[column] = this.datePipe.transform(response.data.value[column], dataTableEntryObject.dateFormat);
|
|
});
|
|
dataTableEntryObject = { ...response.data.value, ...dataTableEntryObject };
|
|
this.clientsService.addClientDatatableEntry(this.clientId, this.datatableName, dataTableEntryObject).subscribe(() => {
|
|
this.clientsService.getClientDatatable(this.clientId, this.datatableName).subscribe((dataObject: any) => {
|
|
this.dataObject = dataObject;
|
|
});
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
edit() {
|
|
let dataTableEntryObject: any = {
|
|
locale: this.settingsService.language.code
|
|
};
|
|
const dateTransformColumns: string[] = [];
|
|
const columns = this.dataObject.columnHeaders.filter((column: any) => {
|
|
return ((column.columnName !== 'id') && (column.columnName !== 'client_id') && (column.columnName !== 'created_at') && (column.columnName !== 'updated_at'));
|
|
});
|
|
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,
|
|
formfields: formfields
|
|
};
|
|
const editDialogRef = this.dialog.open(FormDialogComponent, { data });
|
|
editDialogRef.afterClosed().subscribe((response: any) => {
|
|
if (response.data) {
|
|
// format Dates
|
|
dateTransformColumns.forEach((column) => {
|
|
response.data.value[column] = this.datePipe.transform(response.data.value[column], dataTableEntryObject.dateFormat);
|
|
});
|
|
dataTableEntryObject = { ...response.data.value, ...dataTableEntryObject };
|
|
this.clientsService.editClientDatatableEntry(this.clientId, this.datatableName, dataTableEntryObject).subscribe(() => {
|
|
this.clientsService.getClientDatatable(this.clientId, this.datatableName).subscribe((dataObject: any) => {
|
|
this.dataObject = dataObject;
|
|
});
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
delete() {
|
|
const deleteDataTableDialogRef = this.dialog.open(DeleteDialogComponent, {
|
|
data: { deleteContext: `the contents of ${this.datatableName}` }
|
|
});
|
|
deleteDataTableDialogRef.afterClosed().subscribe((response: any) => {
|
|
if (response.delete) {
|
|
this.clientsService.deleteDatatableContent(this.clientId, this.datatableName)
|
|
.subscribe(() => {
|
|
this.clientsService.getClientDatatable(this.clientId, this.datatableName).subscribe((dataObject: any) => {
|
|
this.dataObject = dataObject;
|
|
});
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
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 InputBase({
|
|
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 InputBase({
|
|
controlName: column.columnName,
|
|
label: column.columnName,
|
|
value: '',
|
|
type: 'datetime-local',
|
|
required: (column.isColumnNullable) ? false : true
|
|
});
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
}
|