feat: add create group page (#601)

Fixes: #212
This commit is contained in:
Karan Takalkar
2020-05-22 17:39:09 +05:30
committed by GitHub
parent fd2c5f61ac
commit 68c30cfa4d
9 changed files with 395 additions and 5 deletions
@@ -0,0 +1,131 @@
<div class="container">
<mat-card>
<form [formGroup]="groupForm">
<mat-card-content>
<div fxLayout="column">
<mat-form-field>
<mat-label>Name</mat-label>
<input matInput required formControlName="name">
<mat-error *ngIf="groupForm.controls.name.hasError('required')">
Group Name is <strong>required</strong>
</mat-error>
<mat-error *ngIf="groupForm.controls.name.hasError('pattern')">
Group Name <strong>cannot</strong> begin with a special character or number
</mat-error>
</mat-form-field>
<mat-form-field>
<mat-label>Office</mat-label>
<mat-select required formControlName="officeId">
<mat-option *ngFor="let office of officeData" [value]="office.id">
{{ office.name }}
</mat-option>
</mat-select>
<mat-error *ngIf="groupForm.controls.officeId.hasError('required')">
Office is <strong>required</strong>
</mat-error>
</mat-form-field>
<mat-form-field>
<mat-label>Staff</mat-label>
<mat-select formControlName="staffId">
<mat-option *ngFor="let staff of staffData" [value]="staff.id">
{{ staff.displayName }}
</mat-option>
</mat-select>
</mat-form-field>
<mat-checkbox labelPosition="before" formControlName="active">
Active
</mat-checkbox>
<mat-form-field *ngIf="this.groupForm.controls.active.value">
<mat-label>Activation Date</mat-label>
<input matInput [min]="minDate" [max]="maxDate" [matDatepicker]="activationDatePicker" required formControlName="activationDate">
<mat-datepicker-toggle matSuffix [for]="activationDatePicker"></mat-datepicker-toggle>
<mat-datepicker #activationDatePicker></mat-datepicker>
<mat-error *ngIf="groupForm.controls.activationDate.hasError('required')">
Activation Date is <strong>required</strong>
</mat-error>
</mat-form-field>
<mat-form-field>
<mat-label>External id</mat-label>
<input matInput formControlName="externalId">
</mat-form-field>
<mat-form-field>
<mat-label>Submitted on</mat-label>
<input matInput [min]="minDate" [max]="maxDate" [matDatepicker]="submittedOnDatePicker" required formControlName="submittedOnDate">
<mat-datepicker-toggle matSuffix [for]="submittedOnDatePicker"></mat-datepicker-toggle>
<mat-datepicker #submittedOnDatePicker></mat-datepicker>
<mat-error *ngIf="groupForm.controls.submittedOnDate.hasError('required')">
Submission Date is <strong>required</strong>
</mat-error>
</mat-form-field>
<mat-form-field>
<mat-label>Add Clients</mat-label>
<input matInput [formControl]="clientChoice" [matAutocomplete]="clientsAutocomplete">
</mat-form-field>
</div>
<mat-autocomplete autoActiveFirstOption #clientsAutocomplete="matAutocomplete" [displayWith]="displayClient">
<mat-option *ngFor="let client of clientsData" [value]="client">
{{ client.displayName }}
</mat-option>
</mat-autocomplete>
<div fxLayout="row">
<div class="mat-table" *ngIf="clientChoice.value">
<div class="mat-header-row">
<div class="mat-header-cell">Client Details</div>
<button mat-icon-button (click)="addClient()">
<fa-icon icon="plus"></fa-icon>
</button>
</div>
<div class="mat-row">
<div class="mat-cell">Name</div>
<div class="mat-cell">{{clientChoice.value.displayName}}</div>
</div>
<div class="mat-row">
<div class="mat-cell">Id</div>
<div class="mat-cell">{{clientChoice.value.id}}</div>
</div>
<div class="mat-row">
<div class="mat-cell">Office</div>
<div class="mat-cell">{{clientChoice.value.officeName}}</div>
</div>
</div>
<mat-nav-list *ngIf="clientMembers.length">
<h3 matSubheader>Selected Clients</h3>
<div mat-list-item *ngFor="let client of clientMembers index as i">
<button mat-icon-button (click)="removeClient(i)">
<fa-icon icon="trash"></fa-icon>
</button>
<span matLine>{{client.displayName}}</span>
</div>
</mat-nav-list>
</div>
</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]="!groupForm.valid" (click)="submit()">Submit</button>
</mat-card-actions>
</form>
</mat-card>
</div>
@@ -0,0 +1,26 @@
.container {
max-width: 37rem;
}
.mat-table {
display: block;
font-family: Tahoma, Verdana;
width: 50%;
}
.mat-row,
.mat-header-row {
display: flex;
border-bottom-width: 1px;
border-bottom-style: solid;
align-items: center;
min-height: 48px;
padding: 0 24px;
}
.mat-cell,
.mat-header-cell {
flex: 1;
overflow: hidden;
word-wrap: break-word;
}
@@ -0,0 +1,25 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { CreateGroupComponent } from './create-group.component';
describe('CreateGroupComponent', () => {
let component: CreateGroupComponent;
let fixture: ComponentFixture<CreateGroupComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ CreateGroupComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(CreateGroupComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
@@ -0,0 +1,167 @@
/** Angular Imports */
import { Component, OnInit, AfterViewInit } from '@angular/core';
import { FormGroup, FormBuilder, FormControl, Validators } from '@angular/forms';
import { Router, ActivatedRoute } from '@angular/router';
import { DatePipe } from '@angular/common';
/** Custom Services */
import { GroupsService } from '../groups.service';
import { ClientsService } from '../../clients/clients.service';
/**
* Create Group component.
*/
@Component({
selector: 'mifosx-create-group',
templateUrl: './create-group.component.html',
styleUrls: ['./create-group.component.scss']
})
export class CreateGroupComponent implements OnInit, AfterViewInit {
/** Minimum date allowed. */
minDate = new Date(2000, 0, 1);
/** Maximum date allowed. */
maxDate = new Date();
/** Group form. */
groupForm: FormGroup;
/** Office data. */
officeData: any;
/** Client data. */
clientsData: any = [];
/** Staff data. */
staffData: any;
/** Client Members. */
clientMembers: any[] = [];
/** ClientChoice. */
clientChoice = new FormControl('');
/**
* Retrieves the offices data from `resolve`.
* @param {FormBuilder} formBuilder Form Builder.
* @param {ActivatedRoute} route Activated Route.
* @param {Router} router Router for navigation.
* @param {ClientsService} clientsService CentersService.
* @param {GroupsService} groupService GroupsService.
* @param {DatePipe} datePipe Date Pipe to format date.
*/
constructor(private formBuilder: FormBuilder,
private route: ActivatedRoute,
private router: Router,
private clientsService: ClientsService,
private groupService: GroupsService,
private datePipe: DatePipe) {
this.route.data.subscribe( (data: {offices: any} ) => {
this.officeData = data.offices;
});
}
/**
* Creates and sets the group form.
*/
ngOnInit() {
this.createGroupForm();
}
/**
* Subscribes to Clients search filter:
*/
ngAfterViewInit() {
this.clientChoice.valueChanges.subscribe( (value: string) => {
if (value.length >= 2) {
this.clientsService.getFilteredClients('displayName', 'ASC', true, value, this.groupForm.get('officeId').value)
.subscribe( (data: any) => {
this.clientsData = data.pageItems;
});
}
});
}
/**
* Creates the group form.
*/
createGroupForm() {
this.groupForm = this.formBuilder.group({
'name': ['', [Validators.required, Validators.pattern('(^[A-z]).*')]],
'officeId': ['', Validators.required],
'submittedOnDate': ['', Validators.required],
'staffId': [''],
'externalId': [''],
'active': [false],
});
this.buildDependencies();
}
/**
* Sets the staff and clients data each time the user selects a new office.
* Adds form control Activation Date if active.
*/
buildDependencies() {
this.groupForm.get('officeId').valueChanges.subscribe((option: any) => {
this.groupService.getStaff(option).subscribe(data => {
this.staffData = data['staffOptions'];
if (this.staffData === undefined) {
this.groupForm.controls['staffId'].disable();
} else {
this.groupForm.controls['staffId'].enable();
}
});
});
this.groupForm.get('active').valueChanges.subscribe((bool: boolean) => {
if (bool) {
this.groupForm.addControl('activationDate', new FormControl('', Validators.required));
} else {
this.groupForm.removeControl('activationDate');
}
});
}
/**
* Add client.
*/
addClient() {
if (!this.clientMembers.includes(this.clientChoice.value)) {
this.clientMembers.push(this.clientChoice.value);
}
}
/**
* Remove client.
* @param index Client's array index.
*/
removeClient(index: number) {
this.clientMembers.splice(index, 1);
}
/**
* Displays Client name in form control input.
* @param {any} client Client data.
* @returns {string} Client name if valid otherwise undefined.
*/
displayClient(client: any): string | undefined {
return client ? client.displayName : undefined;
}
/**
* Submits the group form and creates group,
* if successful redirects to groups.
*/
submit() {
const submittedOnDate: Date = this.groupForm.value.submittedOnDate;
const activationDate: Date = this.groupForm.value.activationDate;
// TODO: Update once language and date settings are setup
const dateFormat = 'dd MMMM yyyy';
this.groupForm.patchValue({
submittedOnDate: this.datePipe.transform(submittedOnDate, dateFormat),
activationDate: this.datePipe.transform(activationDate, dateFormat)
});
const group = this.groupForm.value;
group.locale = 'en';
group.dateFormat = dateFormat;
group.clientMembers = [];
this.clientMembers.forEach((client: any) => group.clientMembers.push(client.id));
this.groupService.createGroup(group).subscribe((response: any) => {
this.router.navigate(['../groups']);
});
}
}