From 7dbb24623b935ddb03634d9602be74ac6b73c669 Mon Sep 17 00:00:00 2001 From: Benura Abeywardena <43112139+BLasan@users.noreply.github.com> Date: Wed, 13 May 2020 01:20:15 +0530 Subject: [PATCH] feat: add edit office component (#664) (#664) Fixes: #662 --- .../edit-office/edit-office.component.html | 59 +++++++++++++ .../edit-office/edit-office.component.scss | 3 + .../edit-office/edit-office.component.spec.ts | 25 ++++++ .../edit-office/edit-office.component.ts | 83 +++++++++++++++++++ .../edit-office/edit-office.resolver.ts | 31 +++++++ .../offices/offices.component.html | 2 +- .../organization-routing.module.ts | 17 ++++ src/app/organization/organization.module.ts | 4 +- src/app/organization/organization.service.ts | 19 +++++ 9 files changed, 241 insertions(+), 2 deletions(-) create mode 100644 src/app/organization/offices/edit-office/edit-office.component.html create mode 100644 src/app/organization/offices/edit-office/edit-office.component.scss create mode 100644 src/app/organization/offices/edit-office/edit-office.component.spec.ts create mode 100644 src/app/organization/offices/edit-office/edit-office.component.ts create mode 100644 src/app/organization/offices/edit-office/edit-office.resolver.ts diff --git a/src/app/organization/offices/edit-office/edit-office.component.html b/src/app/organization/offices/edit-office/edit-office.component.html new file mode 100644 index 000000000..fd5da0cc1 --- /dev/null +++ b/src/app/organization/offices/edit-office/edit-office.component.html @@ -0,0 +1,59 @@ +
+ + + +
+ + + +
+ + + Offices + + + Office is required + + + + + Parent Office + + + {{ office.name }} + + + + Parent Office is required + + + + + Opened On + + + + + Date is required + + + + + External ID + + + +
+ +
+ + + + + + +
+ +
+ +
diff --git a/src/app/organization/offices/edit-office/edit-office.component.scss b/src/app/organization/offices/edit-office/edit-office.component.scss new file mode 100644 index 000000000..0f0bec918 --- /dev/null +++ b/src/app/organization/offices/edit-office/edit-office.component.scss @@ -0,0 +1,3 @@ +.container{ + max-width: 37rem; +} diff --git a/src/app/organization/offices/edit-office/edit-office.component.spec.ts b/src/app/organization/offices/edit-office/edit-office.component.spec.ts new file mode 100644 index 000000000..5dafec9d6 --- /dev/null +++ b/src/app/organization/offices/edit-office/edit-office.component.spec.ts @@ -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; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [ EditOfficeComponent ] + }) + .compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(EditOfficeComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/app/organization/offices/edit-office/edit-office.component.ts b/src/app/organization/offices/edit-office/edit-office.component.ts new file mode 100644 index 000000000..aebc1ebed --- /dev/null +++ b/src/app/organization/offices/edit-office/edit-office.component.ts @@ -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 }); + }); + } + +} diff --git a/src/app/organization/offices/edit-office/edit-office.resolver.ts b/src/app/organization/offices/edit-office/edit-office.resolver.ts new file mode 100644 index 000000000..ec90b70f2 --- /dev/null +++ b/src/app/organization/offices/edit-office/edit-office.resolver.ts @@ -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 { + + /** + * @param {OrganizationService} organizationService Organization service. + */ + constructor(private organizationService: OrganizationService) {} + + /** + * Returns the office and template data. + * @returns {Observable} + */ + resolve(route: ActivatedRouteSnapshot): Observable { + const officeId = route.paramMap.get('id'); + return this.organizationService.getOffice(officeId, true); + } + +} diff --git a/src/app/organization/offices/offices.component.html b/src/app/organization/offices/offices.component.html index 50e48c9c8..fb5b3cdfa 100644 --- a/src/app/organization/offices/offices.component.html +++ b/src/app/organization/offices/offices.component.html @@ -51,7 +51,7 @@ - + diff --git a/src/app/organization/organization-routing.module.ts b/src/app/organization/organization-routing.module.ts index 17a266f55..cdd509af4 100644 --- a/src/app/organization/organization-routing.module.ts +++ b/src/app/organization/organization-routing.module.ts @@ -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 ] }) diff --git a/src/app/organization/organization.module.ts b/src/app/organization/organization.module.ts index 4f9ffc581..afe37c808 100644 --- a/src/app/organization/organization.module.ts +++ b/src/app/organization/organization.module.ts @@ -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 { } diff --git a/src/app/organization/organization.service.ts b/src/app/organization/organization.service.ts index 607c5f54a..f1423aa32 100644 --- a/src/app/organization/organization.service.ts +++ b/src/app/organization/organization.service.ts @@ -32,6 +32,16 @@ export class OrganizationService { return this.http.get('/offices'); } + /** + * @param {string} officeId Office ID of Office. + * @param {boolean} template + * @returns {Observable} Office. + */ + getOffice(officeId: string, template: boolean = false): Observable { + 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} @@ -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} + */ + updateOffice(officeId: string, office: any): Observable { + return this.http.put(`/offices/${officeId}`, office); + } + /** * @returns {Observable} Employees data */