@@ -8,14 +8,29 @@ import { Route } from '../core/route/route.service';
|
||||
import { extract } from '../core/i18n/i18n.service';
|
||||
|
||||
import { CentersComponent } from './centers.component';
|
||||
import { CreateCenterComponent } from './create-center/create-center.component';
|
||||
import { OfficesResolver } from 'app/accounting/common-resolvers/offices.resolver';
|
||||
|
||||
const routes: Routes = [
|
||||
Route.withShell([
|
||||
|
||||
{
|
||||
path: 'centers',
|
||||
component: CentersComponent,
|
||||
data: { title: extract('Centers'), breadcrumb: 'Centers' }
|
||||
data: { title: extract('Centers'), breadcrumb: 'Centers' },
|
||||
children: [
|
||||
{
|
||||
path: '',
|
||||
component: CentersComponent
|
||||
},
|
||||
{
|
||||
path: 'create',
|
||||
component: CreateCenterComponent,
|
||||
data: { title: extract('Create Center'), breadcrumb: 'Create' },
|
||||
resolve: {
|
||||
offices: OfficesResolver,
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
])
|
||||
];
|
||||
@@ -23,6 +38,6 @@ const routes: Routes = [
|
||||
@NgModule({
|
||||
imports: [RouterModule.forChild(routes)],
|
||||
exports: [RouterModule],
|
||||
providers: []
|
||||
providers: [OfficesResolver]
|
||||
})
|
||||
export class CentersRoutingModule { }
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
<button mat-raised-button color="primary">
|
||||
<fa-icon icon="download"></fa-icon> Import Center
|
||||
</button>
|
||||
<button mat-raised-button color="primary">
|
||||
<button mat-raised-button color="primary" [routerLink]="['create']">
|
||||
<fa-icon icon="plus"></fa-icon> Create Center
|
||||
</button>
|
||||
</div>
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
/** Angular Imports */
|
||||
import { NgModule } from '@angular/core';
|
||||
import { DatePipe } from '@angular/common';
|
||||
|
||||
/** Custom Modules */
|
||||
import { CentersRoutingModule } from './centers-routing.module';
|
||||
@@ -9,6 +8,7 @@ import { PipesModule } from '../pipes/pipes.module';
|
||||
|
||||
/** Custom Components */
|
||||
import { CentersComponent } from './centers.component';
|
||||
import { CreateCenterComponent } from './create-center/create-center.component';
|
||||
|
||||
/**
|
||||
* Centers Module
|
||||
@@ -22,8 +22,8 @@ import { CentersComponent } from './centers.component';
|
||||
PipesModule
|
||||
],
|
||||
declarations: [
|
||||
CentersComponent
|
||||
],
|
||||
providers: [DatePipe]
|
||||
CentersComponent,
|
||||
CreateCenterComponent
|
||||
]
|
||||
})
|
||||
export class CentersModule { }
|
||||
|
||||
@@ -42,4 +42,23 @@ export class CentersService {
|
||||
return this.http.get('/centers', { params: httpParams });
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {any} center Center to be created.
|
||||
* @returns {Observable<any>}
|
||||
*/
|
||||
createCenter(center: any): Observable<any> {
|
||||
return this.http.post('/centers', center);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} officeId Office Id of office to get staff for.
|
||||
* @returns {Observable<any>}
|
||||
*/
|
||||
getStaff(officeId: number): Observable<any> {
|
||||
const httpParams = new HttpParams()
|
||||
.set('officeId', officeId.toString())
|
||||
.set('staffInSelectedOfficeOnly', 'true');
|
||||
return this.http.get('/centers/template', { params: httpParams });
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
<div class="container">
|
||||
|
||||
<mat-card>
|
||||
|
||||
<form [formGroup]="centerForm">
|
||||
|
||||
<mat-card-content>
|
||||
|
||||
<div fxLayout="column">
|
||||
|
||||
<mat-form-field>
|
||||
<mat-label>Name</mat-label>
|
||||
<input matInput required formControlName="name">
|
||||
<mat-error *ngIf="centerForm.controls.name.hasError('required')">
|
||||
Center Name is <strong>required</strong>
|
||||
</mat-error>
|
||||
<mat-error *ngIf="centerForm.controls.name.hasError('pattern')">
|
||||
Center 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="centerForm.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.centerForm.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="centerForm.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="centerForm.controls.submittedOnDate.hasError('required')">
|
||||
Submission Date is <strong>required</strong>
|
||||
</mat-error>
|
||||
</mat-form-field>
|
||||
|
||||
<mat-form-field>
|
||||
<mat-label>Select and Add groups</mat-label>
|
||||
<mat-select [formControl]="groupChoice">
|
||||
<mat-option *ngFor="let group of groupsData" [value]="group">
|
||||
{{ group.name }}
|
||||
</mat-option>
|
||||
</mat-select>
|
||||
</mat-form-field>
|
||||
|
||||
</div>
|
||||
|
||||
<div fxLayout="row">
|
||||
|
||||
<div class="mat-table" *ngIf="groupChoice.value">
|
||||
<div class="mat-header-row">
|
||||
<div class="mat-header-cell">Group Details</div>
|
||||
<button mat-icon-button (click)="addGroup()">
|
||||
<fa-icon icon="plus"></fa-icon>
|
||||
</button>
|
||||
</div>
|
||||
<div class="mat-row">
|
||||
<div class="mat-cell">Name</div>
|
||||
<div class="mat-cell">{{groupChoice.value.name}}</div>
|
||||
</div>
|
||||
<div class="mat-row">
|
||||
<div class="mat-cell">Id</div>
|
||||
<div class="mat-cell">{{groupChoice.value.id}}</div>
|
||||
</div>
|
||||
<div class="mat-row">
|
||||
<div class="mat-cell">Office</div>
|
||||
<div class="mat-cell">{{groupChoice.value.officeName}}</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<mat-nav-list *ngIf="groupMembers.length">
|
||||
<h3 matSubheader>Selected Groups</h3>
|
||||
<div mat-list-item *ngFor="let group of groupMembers index as i">
|
||||
<button mat-icon-button (click)="removeGroup(i)">
|
||||
<fa-icon icon="trash"></fa-icon>
|
||||
</button>
|
||||
<span matLine>{{group.name}}</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]="!centerForm.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 { CreateCenterComponent } from './create-center.component';
|
||||
|
||||
describe('CreateCenterComponent', () => {
|
||||
let component: CreateCenterComponent;
|
||||
let fixture: ComponentFixture<CreateCenterComponent>;
|
||||
|
||||
beforeEach(async(() => {
|
||||
TestBed.configureTestingModule({
|
||||
declarations: [ CreateCenterComponent ]
|
||||
})
|
||||
.compileComponents();
|
||||
}));
|
||||
|
||||
beforeEach(() => {
|
||||
fixture = TestBed.createComponent(CreateCenterComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
it('should create', () => {
|
||||
expect(component).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,151 @@
|
||||
/** Angular Imports */
|
||||
import { Component, OnInit } 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 'app/groups/groups.service';
|
||||
import { CentersService } from '../centers.service';
|
||||
|
||||
/**
|
||||
* Create Center component.
|
||||
*/
|
||||
@Component({
|
||||
selector: 'mifosx-create-center',
|
||||
templateUrl: './create-center.component.html',
|
||||
styleUrls: ['./create-center.component.scss']
|
||||
})
|
||||
export class CreateCenterComponent implements OnInit {
|
||||
|
||||
/** Minimum date allowed. */
|
||||
minDate = new Date(2000, 0, 1);
|
||||
/** Maximum date allowed. */
|
||||
maxDate = new Date();
|
||||
/** Center form. */
|
||||
centerForm: FormGroup;
|
||||
/** Office data. */
|
||||
officeData: any;
|
||||
/** Group data. */
|
||||
groupsData: any;
|
||||
/** Staff data. */
|
||||
staffData: any;
|
||||
/** Group Members. */
|
||||
groupMembers: any[] = [];
|
||||
/** Group Choice. */
|
||||
groupChoice = 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 {CentersService} centerService CentersService.
|
||||
* @param {GroupsService} groupService GroupsService.
|
||||
* @param {DatePipe} datePipe Date Pipe to format date.
|
||||
*/
|
||||
constructor(private formBuilder: FormBuilder,
|
||||
private route: ActivatedRoute,
|
||||
private router: Router,
|
||||
private centerService: CentersService,
|
||||
private groupService: GroupsService,
|
||||
private datePipe: DatePipe) {
|
||||
this.route.data.subscribe( (data: { offices: any }) => {
|
||||
this.officeData = data.offices;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates and sets the center form.
|
||||
*/
|
||||
ngOnInit() {
|
||||
this.createCenterForm();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the center form.
|
||||
*/
|
||||
createCenterForm() {
|
||||
this.centerForm = this.formBuilder.group({
|
||||
'name': ['', [Validators.required, Validators.pattern('(^[A-z]).*')]],
|
||||
'officeId': ['', Validators.required],
|
||||
'submittedOnDate': ['', Validators.required],
|
||||
'staffId': [''],
|
||||
'externalId': [''],
|
||||
'active': [''],
|
||||
});
|
||||
this.buildDependencies();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the staff and groups data each time the user selects a new office.
|
||||
* Adds form control Activation Date if active.
|
||||
*/
|
||||
buildDependencies() {
|
||||
this.centerForm.get('officeId').valueChanges.subscribe( (option: any) => {
|
||||
this.groupService.getGroupsByOfficeId(option).subscribe( (data: any) => {
|
||||
this.groupsData = data;
|
||||
if (!this.groupsData.length) {
|
||||
this.groupChoice.disable();
|
||||
} else {
|
||||
this.groupChoice.enable();
|
||||
}
|
||||
});
|
||||
this.centerService.getStaff(option).subscribe( (data: any) => {
|
||||
this.staffData = data['staffOptions'];
|
||||
if (this.staffData === undefined) {
|
||||
this.centerForm.controls['staffId'].disable();
|
||||
} else {
|
||||
this.centerForm.controls['staffId'].enable();
|
||||
}
|
||||
});
|
||||
});
|
||||
this.centerForm.get('active').valueChanges.subscribe( (bool: boolean) => {
|
||||
if (bool) {
|
||||
this.centerForm.addControl('activationDate', new FormControl('', Validators.required));
|
||||
} else {
|
||||
this.centerForm.removeControl('activationDate');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Add group.
|
||||
*/
|
||||
addGroup() {
|
||||
if (!this.groupMembers.includes(this.groupChoice.value)) {
|
||||
this.groupMembers.push(this.groupChoice.value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove group.
|
||||
*/
|
||||
removeGroup(index: number) {
|
||||
this.groupMembers.splice(index, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Submits the center form and creates center,
|
||||
* if successful redirects to centers.
|
||||
*/
|
||||
submit() {
|
||||
const prevSubmittedOnDate: Date = this.centerForm.value.submittedOnDate;
|
||||
const prevActivationDate: Date = this.centerForm.value.activationDate;
|
||||
// TODO: Update once language and date settings are setup
|
||||
const dateFormat = 'dd MMMM yyyy';
|
||||
this.centerForm.patchValue({
|
||||
submittedOnDate: this.datePipe.transform(prevSubmittedOnDate, dateFormat),
|
||||
activationDate: this.datePipe.transform(prevActivationDate, dateFormat)
|
||||
});
|
||||
const center = this.centerForm.value;
|
||||
center.locale = 'en';
|
||||
center.dateFormat = dateFormat;
|
||||
center.groupMembers = [];
|
||||
this.groupMembers.forEach((group: any) => center.groupMembers.push(group.id));
|
||||
this.centerService.createCenter(center).subscribe((response: any) => {
|
||||
this.router.navigate(['../centers']);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -42,4 +42,13 @@ export class GroupsService {
|
||||
return this.http.get('/groups', { params: httpParams });
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} officeId Office Id of office to get groups for.
|
||||
* @returns {Observable<any>}
|
||||
*/
|
||||
getGroupsByOfficeId(officeId: number): Observable<any> {
|
||||
const httpParams = new HttpParams().set('officeId', officeId.toString());
|
||||
return this.http.get('/groups', { params: httpParams } );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user