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 @@
+
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