feat: create, view, edit, delete for hooks (#403)

- Add create-hook
- Add view-hook
- Add edit-hook

#180
This commit is contained in:
Chirag Gupta
2020-03-22 18:45:36 +05:30
committed by GitHub
parent 85b8e907f8
commit 17197056bb
24 changed files with 1264 additions and 13 deletions
@@ -0,0 +1,159 @@
<div class="container">
<mat-card>
<form [formGroup]="hookForm" (ngSubmit)="submit()">
<mat-card-content>
<div fxLayout="row wrap" fxLayout.lt-md="column" fxLayoutGap="4%">
<mat-form-field fxFlex="40%">
<mat-label>Hook Template</mat-label>
<mat-select required formControlName="name">
<mat-option *ngFor="let hookTemplate of hooksTemplateData.templates" [value]="hookTemplate.name">
{{ hookTemplate.name }}
</mat-option>
</mat-select>
<mat-error *ngIf="hookForm.controls.name.hasError('required')">
Hook Template is <strong>required</strong>
</mat-error>
</mat-form-field>
<mat-form-field fxFlex="40%">
<mat-label>Display Name</mat-label>
<input matInput required formControlName="displayName">
<mat-error *ngIf="hookForm.controls.displayName.hasError('required')">
Display Name is <strong>required</strong>
</mat-error>
</mat-form-field>
<div class="is-active-wrapper">
<mat-checkbox class="is-active" fxFlex="10%" labelPosition="before" formControlName="isActive">
Is Active?
</mat-checkbox>
</div>
</div>
<div fxLayout="row wrap" fxLayout.lt-md="column" fxLayoutGap="4%">
<mat-form-field fxFlex="48%" *ngIf="hookForm.controls.name.value === 'Web'">
<mat-label>Content Type</mat-label>
<mat-select required formControlName="contentType">
<mat-option [value]="'json'">
json
</mat-option>
<mat-option [value]="'form'">
form
</mat-option>
</mat-select>
<mat-error *ngIf="hookForm.controls.contentType.hasError('required')">
Content Type is <strong>required</strong>
</mat-error>
</mat-form-field>
<mat-form-field fxFlex="48%" *ngIf="hookForm.controls.name.value === 'SMS Bridge'">
<mat-label>Phone Number</mat-label>
<input matInput type="tel" #input maxlength="10" required formControlName="phoneNumber">
<mat-hint align="end">{{input.value?.length || 0}}/10</mat-hint>
<mat-error *ngIf="hookForm.controls.phoneNumber.hasError('required')">
Payload URL is <strong>required</strong>
</mat-error>
</mat-form-field>
<mat-form-field fxFlex="48%">
<mat-label>Payload URL</mat-label>
<input matInput required formControlName="payloadUrl">
<mat-error *ngIf="hookForm.controls.payloadUrl.hasError('required')">
Payload URL is <strong>required</strong>
</mat-error>
</mat-form-field>
</div>
<div fxLayout="row wrap" fxLayout.lt-md="column" fxLayoutGap="4%"
*ngIf="hookForm.controls.name.value === 'SMS Bridge'">
<mat-form-field fxFlex="30%">
<mat-label>SMS Provider</mat-label>
<input matInput required formControlName="smsProvider">
<mat-error *ngIf="hookForm.controls.smsProvider.hasError('required')">
SMS Provider is <strong>required</strong>
</mat-error>
</mat-form-field>
<mat-form-field fxFlex="30%">
<mat-label>SMS Provider Account ID</mat-label>
<input matInput required formControlName="smsProviderAccountId">
<mat-error *ngIf="hookForm.controls.smsProviderAccountId.hasError('required')">
SMS Provider Account ID is <strong>required</strong>
</mat-error>
</mat-form-field>
<mat-form-field fxFlex="30%">
<mat-label>SMS Provider Token</mat-label>
<input matInput required formControlName="smsProviderToken">
<mat-error *ngIf="hookForm.controls.smsProviderToken.hasError('required')">
SMS Provider Token is <strong>required</strong>
</mat-error>
</mat-form-field>
</div>
<br>
<mat-divider [inset]="true"></mat-divider>
<br>
<div fxLayout="row wrap" fxLayoutGap="60%" fxLayout.lt-md="column">
<p fxFlex="20%" class="mat-title">Events</p>
<button mat-raised-button fxFlex="20%" type="button" color="primary" (click)="addEvent()">
<fa-icon icon="plus"></fa-icon>&nbsp;&nbsp;
Add Event
</button>
</div>
<table mat-table [dataSource]="dataSource" matSort>
<ng-container matColumnDef="entityName">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Entity Name </th>
<td mat-cell *matCellDef="let event"> {{ event.entityName }} </td>
</ng-container>
<ng-container matColumnDef="actionName">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Action Name </th>
<td mat-cell *matCellDef="let event"> {{ event.actionName }} </td>
</ng-container>
<ng-container matColumnDef="actions">
<th mat-header-cell *matHeaderCellDef> Actions </th>
<td mat-cell *matCellDef="let i = index">
<button type="button" color="warn" mat-icon-button
(click)="deleteEvent(i)">
<fa-icon icon="trash" size="lg"></fa-icon>
</button>
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
</mat-card-content>
<mat-card-actions fxLayout="row" fxLayout.xs="column" fxLayoutAlign="center" fxLayoutGap="5px">
<button type="button" mat-raised-button [routerLink]="['../']">Cancel</button>
<button mat-raised-button color="primary" [disabled]="(!hookForm.valid || hookForm.pristine) && !eventsDataChanged">Submit</button>
</mat-card-actions>
</form>
</mat-card>
</div>
@@ -0,0 +1,17 @@
@media (min-width: 992px) {
.is-active-wrapper {
position: relative;
.is-active {
padding: 0 0 17.5px 0;
position: absolute;
bottom: 0;
right: 0;
}
}
}
table {
width: 100%;
}
@@ -0,0 +1,25 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { EditHookComponent } from './edit-hook.component';
describe('EditHookComponent', () => {
let component: EditHookComponent;
let fixture: ComponentFixture<EditHookComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ EditHookComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(EditHookComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
@@ -0,0 +1,158 @@
/** Angular Imports */
import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { MatTableDataSource, MatPaginator, MatSort, MatDialog } from '@angular/material';
/** Custom Services */
import { SystemService } from '../../system.service';
/** Custom Components */
import { AddEventDialogComponent } from '../add-event-dialog/add-event-dialog.component';
import { DeleteDialogComponent } from 'app/shared/delete-dialog/delete-dialog.component';
/**
* Edit Hook Component.
*/
@Component({
selector: 'mifosx-edit-hook',
templateUrl: './edit-hook.component.html',
styleUrls: ['./edit-hook.component.scss']
})
export class EditHookComponent implements OnInit {
/** Hooks Template Data. */
hooksTemplateData: any;
/** Hook Data. */
hookData: any;
/** Hook Form. */
hookForm: FormGroup;
/** Columns to be displayed in events table. */
displayedColumns: string[] = ['entityName', 'actionName', 'actions'];
/** Data source for events table. */
dataSource: MatTableDataSource<any>;
/** Events Data. */
eventsData: any[] = [];
/** Boolean to check if events data is changed or not. */
eventsDataChanged: Boolean = false;
/** Sorter for events table. */
@ViewChild(MatSort) sort: MatSort;
/**
* Retrieves the hooks template data from `resolve`.
* @param {ActivatedRoute} route Activated Route.
* @param {SystemService} systemService System Service.
* @param {Router} router Router for navigation.
* @param {FormBuilder} formBuilder Form Builder.
* @param {MatDialog} dialog Dialog Reference.
*/
constructor(private route: ActivatedRoute,
private systemService: SystemService,
private router: Router,
private formBuilder: FormBuilder,
private dialog: MatDialog) {
this.route.data.subscribe(( data: { hooksTemplate: any, hook: any }) => {
this.hooksTemplateData = data.hooksTemplate;
this.hookData = data.hook;
this.eventsData = data.hook.events ? data.hook.events : [];
});
}
/**
* Creates the hook form.
*/
ngOnInit() {
this.createHookForm();
this.setEvents();
}
/**
* Initializes the data source, paginator and sorter for events table.
*/
setEvents() {
this.dataSource = new MatTableDataSource(this.eventsData);
this.dataSource.sort = this.sort;
}
/**
* Creates the hook form.
*/
createHookForm() {
this.hookForm = this.formBuilder.group({
'name': [{ value: this.hookData.name, disabled: true }, Validators.required],
'displayName': [this.hookData.displayName, Validators.required],
'isActive': [this.hookData.isActive],
'phoneNumber': [{ value: this.hookData.name === 'SMS Bridge' ? this.hookData.config[1].fieldValue : '', disabled: this.hookData.name !== 'SMS Bridge' }, Validators.required],
'smsProvider': [{ value: this.hookData.name === 'SMS Bridge' ? this.hookData.config[2].fieldValue : '', disabled: this.hookData.name !== 'SMS Bridge' }, Validators.required],
'smsProviderAccountId': [{ value: this.hookData.name === 'SMS Bridge' ? this.hookData.config[3].fieldValue : '', disabled: this.hookData.name !== 'SMS Bridge' }, Validators.required],
'smsProviderToken': [{ value: this.hookData.name === 'SMS Bridge' ? this.hookData.config[4].fieldValue : '', disabled: this.hookData.name !== 'SMS Bridge' }, Validators.required],
'contentType': [{ value: this.hookData.name === 'Web' ? this.hookData.config[0].fieldValue : '', disabled: this.hookData.name !== 'Web' }, Validators.required],
'payloadUrl': [this.hookData.name === 'Web' ? this.hookData.config[1].fieldValue : this.hookData.config[0].fieldValue, Validators.required]
});
}
/**
* Adds the event form to given events form array.
*/
addEvent() {
const addEventDialogRef = this.dialog.open(AddEventDialogComponent, {
data: this.hooksTemplateData
});
addEventDialogRef.afterClosed().subscribe((response: any) => {
if (response) {
this.eventsData.push({ entityName: response.entity,
actionName: response.action });
this.dataSource.connect().next(this.eventsData);
this.eventsDataChanged = true;
}
});
}
/**
* Removes the event form from given events form array at given index.
* @param {number} index Array index from where event form needs to be removed.
*/
deleteEvent(index: number) {
const deleteEventDialogRef = this.dialog.open(DeleteDialogComponent, {
data: { deleteContext: `event with entity name of ${this.eventsData[index].entityName}` }
});
deleteEventDialogRef.afterClosed().subscribe((response: any) => {
if (response.delete) {
this.eventsData.splice(index, 1);
this.dataSource.connect().next(this.eventsData);
this.eventsDataChanged = true;
}
});
}
/**
* Submits the hook form and updates hook,
* if successful redirects to view updated hook.
*/
submit() {
const hook: {
name: string, isActive: boolean, displayName: string, events: any,
config: {
'Payload URL': string, 'Content Type'?: string, 'SMS Provider'?: string,
'SMS Provider Account Id'?: string, 'SMS Provider Token'?: string
}
} = {
name: this.hookForm.get('name').value,
isActive: this.hookForm.get('isActive').value,
displayName: this.hookForm.get('displayName').value,
events: this.eventsData,
config: {
'Payload URL': this.hookForm.get('payloadUrl').value,
'Content Type': this.hookForm.get('contentType').enabled ? this.hookForm.get('contentType').value : undefined,
'SMS Provider': this.hookForm.get('smsProvider').enabled ? this.hookForm.get('smsProvider').value : undefined,
'SMS Provider Account Id': this.hookForm.get('smsProviderAccountId').enabled ? this.hookForm.get('smsProviderAccountId').value : undefined,
'SMS Provider Token': this.hookForm.get('smsProviderToken').enabled ? this.hookForm.get('smsProviderToken').value : undefined
}
};
this.systemService.updateHook(this.hookData.id, hook)
.subscribe((response: any) => {
this.router.navigate(['../'], {relativeTo: this.route});
});
}
}