feat: add edit office component (#664) (#664)

Fixes: #662
This commit is contained in:
Benura Abeywardena
2020-05-13 01:20:15 +05:30
committed by GitHub
parent 6b69112b25
commit 7dbb24623b
9 changed files with 241 additions and 2 deletions
@@ -0,0 +1,59 @@
<div class="container">
<mat-card>
<form [formGroup]="officeForm" (ngSubmit)="submit()">
<mat-card-content>
<div fxLayout="column">
<mat-form-field>
<mat-label>Offices</mat-label>
<input matInput required formControlName="name">
<mat-error *ngIf="officeForm.controls.name.hasError('required')">
Office <strong>is required</strong>
</mat-error>
</mat-form-field>
<mat-form-field *ngIf="officeForm.contains('parentId')">
<mat-label>Parent Office</mat-label>
<mat-select required formControlName="parentId">
<mat-option *ngFor="let office of officeData.allowedParents" [value]="office.id">
{{ office.name }}
</mat-option>
</mat-select>
<mat-error *ngIf="officeForm.controls.parentId.hasError('required')">
Parent Office <strong>is required</strong>
</mat-error>
</mat-form-field>
<mat-form-field>
<mat-label>Opened On</mat-label>
<input matInput [min]="minDate" [max]="maxDate" [matDatepicker]="openingDatePicker" required formControlName="openingDate">
<mat-datepicker-toggle matSuffix [for]="openingDatePicker"></mat-datepicker-toggle>
<mat-datepicker #openingDatePicker></mat-datepicker>
<mat-error *ngIf="officeForm.controls.openingDate.hasError('required')">
Date <strong>is required</strong>
</mat-error>
</mat-form-field>
<mat-form-field>
<mat-label>External ID</mat-label>
<input matInput formControlName="externalId" formControlName="externalId">
</mat-form-field>
</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]="!officeForm.valid">Submit</button>
</mat-card-actions>
</form>
</mat-card>
</div>
@@ -0,0 +1,3 @@
.container{
max-width: 37rem;
}
@@ -0,0 +1,25 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { EditOfficeComponent } from './edit-office.component';
describe('EditOfficeComponent', () => {
let component: EditOfficeComponent;
let fixture: ComponentFixture<EditOfficeComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ EditOfficeComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(EditOfficeComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
@@ -0,0 +1,83 @@
/** Angular Imports */
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { DatePipe } from '@angular/common';
/** Custom Services */
import { OrganizationService } from 'app/organization/organization.service';
/**
* Edit Office component.
*/
@Component({
selector: 'mifosx-edit-office',
templateUrl: './edit-office.component.html',
styleUrls: ['./edit-office.component.scss']
})
export class EditOfficeComponent implements OnInit {
/** Selected Data. */
officeData: any;
/** Office form. */
officeForm: FormGroup;
/** Minimum Date allowed. */
minDate = new Date(2000, 0, 1);
/** Maximum Date allowed. */
maxDate = new Date();
/**
* Retrieves the charge data from `resolve`.
* @param {ProductsService} organizationService Organization Service.
* @param {FormBuilder} formBuilder Form Builder.
* @param {ActivatedRoute} route Activated Route.
* @param {Router} router Router for navigation.
* @param {MatDialog} dialog Dialog reference.
* @param {DatePipe} datepipe Convert Date.
*/
constructor(private organizationService: OrganizationService,
private formBuilder: FormBuilder,
private route: ActivatedRoute,
private router: Router,
private datepipe: DatePipe) {
this.route.data.subscribe((data: { officeTemplate: any }) => {
this.officeData = data.officeTemplate;
});
}
ngOnInit() {
this.createOfficeForm();
}
/**
* Create Edit Office Form.
*/
createOfficeForm() {
this.officeForm = this.formBuilder.group({
'name': [this.officeData.name, Validators.required],
'openingDate': [this.officeData.openingDate && new Date(this.officeData.openingDate), Validators.required],
'externalId': [this.officeData.externalId],
});
if (this.officeData.allowedParents.length) {
this.officeForm.addControl('parentId', this.formBuilder.control(this.officeData.parentId, Validators.required));
}
}
/**
* Submits the edit office form.
*/
submit() {
const openedOn: Date = this.officeForm.value.openingDate;
const dateFormat = 'yyyy-MM-dd';
this.officeForm.patchValue({
openingDate: this.datepipe.transform(openedOn, dateFormat)
});
const office = this.officeForm.value;
office.locale = 'en';
office.dateFormat = dateFormat;
this.organizationService.updateOffice(this.officeData.id, office).subscribe((response: any) => {
this.router.navigate(['../'], { relativeTo: this.route });
});
}
}
@@ -0,0 +1,31 @@
/** Angular Imports */
import { Injectable } from '@angular/core';
import { Resolve, ActivatedRouteSnapshot } from '@angular/router';
/** rxjs Imports */
import { Observable } from 'rxjs';
/** Custom Services */
import { OrganizationService } from '../../organization.service';
/**
* Office and template data resolver.
*/
@Injectable()
export class EditOfficeResolver implements Resolve<Object> {
/**
* @param {OrganizationService} organizationService Organization service.
*/
constructor(private organizationService: OrganizationService) {}
/**
* Returns the office and template data.
* @returns {Observable<any>}
*/
resolve(route: ActivatedRouteSnapshot): Observable<any> {
const officeId = route.paramMap.get('id');
return this.organizationService.getOffice(officeId, true);
}
}
@@ -51,7 +51,7 @@
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;" class="select-row"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;" class="select-row" [routerLink]="[row.id]"></tr>
</table>
@@ -47,6 +47,8 @@ import { PaymentTypeResolver } from './payment-types/payment-type.resolver';
import { PasswordPreferencesTemplateResolver } from './password-preferences/password-preferences-template.resolver';
import { EntityDataTableChecksResolver } from './entity-data-table-checks/entity-data-table-checks.resolver';
import { WorkingDaysResolver } from './working-days/working-days.resolver';
import { EditOfficeResolver } from './offices/edit-office/edit-office.resolver';
import { EditOfficeComponent } from './offices/edit-office/edit-office.component';
import { AdhocQueryTemplateResolver } from './adhoc-query/adhoc-query-template.resolver';
/** Organization Routes */
@@ -86,6 +88,20 @@ const routes: Routes = [
resolve: {
offices: OfficesResolver,
}
},
{
path: ':id',
data: { title: extract('View Office'), routeParamBreadcrumb: 'id' },
children: [
{
path: 'edit',
component: EditOfficeComponent,
data: { title: extract('Edit Office'), breadcrumb: 'Edit', routeParamBreadcrumb: false },
resolve: {
officeTemplate: EditOfficeResolver
}
}
]
}
]
},
@@ -281,6 +297,7 @@ const routes: Routes = [
PasswordPreferencesTemplateResolver,
EntityDataTableChecksResolver,
WorkingDaysResolver,
EditOfficeResolver,
AdhocQueryTemplateResolver
]
})
+3 -1
View File
@@ -26,6 +26,7 @@ import { CreateOfficeComponent } from './offices/create-office/create-office.com
import { CreateEmployeeComponent } from './employees/create-employee/create-employee.component';
import { CreatePaymentTypeComponent } from './payment-types/create-payment-type/create-payment-type.component';
import { ViewEmployeeComponent } from './employees/view-employee/view-employee.component';
import { EditOfficeComponent } from './offices/edit-office/edit-office.component';
import { CreateAdhocQueryComponent } from './adhoc-query/create-adhoc-query/create-adhoc-query.component';
import { HolidaysComponent } from './holidays/holidays.component';
@@ -61,7 +62,8 @@ import { HolidaysComponent } from './holidays/holidays.component';
CreatePaymentTypeComponent,
ViewEmployeeComponent,
CreateAdhocQueryComponent,
HolidaysComponent
HolidaysComponent,
EditOfficeComponent,
]
})
export class OrganizationModule { }
@@ -32,6 +32,16 @@ export class OrganizationService {
return this.http.get('/offices');
}
/**
* @param {string} officeId Office ID of Office.
* @param {boolean} template
* @returns {Observable<any>} Office.
*/
getOffice(officeId: string, template: boolean = false): Observable<any> {
const httpParams = new HttpParams().set('template', template.toString());
return this.http.get(`/offices/${officeId}`, { params: httpParams });
}
/**
* @param {any} office Office to be created.
* @returns {Observable<any>}
@@ -40,6 +50,15 @@ export class OrganizationService {
return this.http.post('/offices', office);
}
/**
* @param {any} office Office to be updated.
* @param {string} officeId Office Id
* @returns {Observable<any>}
*/
updateOffice(officeId: string, office: any): Observable<any> {
return this.http.put(`/offices/${officeId}`, office);
}
/**
* @returns {Observable<any>} Employees data
*/