From 4b97c4228a5562d458aa150bb6be43575f6959a4 Mon Sep 17 00:00:00 2001 From: Amritanshu Date: Sun, 24 Jul 2022 18:25:02 +0530 Subject: [PATCH] Fix: FormArrays were not getting cleared and were basically fucking the whole thing up. Fix: EmployeeAttendance was borking because the inital employee was null. --- .../src/app/attendance/attendance.component.ts | 2 +- .../closing-stock/closing-stock.component.ts | 1 + .../employee-attendance.component.ts | 18 ++++++++++-------- .../employee-attendance.service.ts | 5 ++++- .../employee-attendance/employee-attendance.ts | 3 +-- .../src/app/incentive/incentive.component.ts | 1 + .../role/role-detail/role-detail.component.ts | 2 +- .../src/app/settings/settings.component.ts | 4 ++-- .../user/user-detail/user-detail.component.ts | 1 + 9 files changed, 22 insertions(+), 15 deletions(-) diff --git a/overlord/src/app/attendance/attendance.component.ts b/overlord/src/app/attendance/attendance.component.ts index 233b30c2..640e9f24 100644 --- a/overlord/src/app/attendance/attendance.component.ts +++ b/overlord/src/app/attendance/attendance.component.ts @@ -57,7 +57,7 @@ export class AttendanceComponent implements OnInit { this.info = data.info; this.attendanceTypes = data.attendanceTypes; this.form.controls.date.setValue(moment(this.info.date, 'DD-MMM-YYYY').toDate()); - this.form.controls.attendances.reset(); + this.form.controls.attendances.clear(); this.info.body.forEach((x) => this.form.controls.attendances.push( new FormGroup({ diff --git a/overlord/src/app/closing-stock/closing-stock.component.ts b/overlord/src/app/closing-stock/closing-stock.component.ts index 2e97d041..bde95cc2 100644 --- a/overlord/src/app/closing-stock/closing-stock.component.ts +++ b/overlord/src/app/closing-stock/closing-stock.component.ts @@ -81,6 +81,7 @@ export class ClosingStockComponent implements OnInit { date: moment(this.info.date, 'DD-MMM-YYYY').toDate(), costCentre: this.info.costCentre.id, }); + this.form.controls.stocks.clear(); this.info.items.forEach((x) => this.form.controls.stocks.push( new FormGroup({ diff --git a/overlord/src/app/employee-attendance/employee-attendance.component.ts b/overlord/src/app/employee-attendance/employee-attendance.component.ts index b5672141..eeff482d 100644 --- a/overlord/src/app/employee-attendance/employee-attendance.component.ts +++ b/overlord/src/app/employee-attendance/employee-attendance.component.ts @@ -79,8 +79,8 @@ export class EmployeeAttendanceComponent implements OnInit, AfterViewInit { this.attendanceTypes = data.attendanceTypes; this.form.controls.startDate.setValue(moment(this.info.startDate, 'DD-MMM-YYYY').toDate()); this.form.controls.finishDate.setValue(moment(this.info.finishDate, 'DD-MMM-YYYY').toDate()); - this.form.controls.employee.setValue(this.info.employee.name); - this.form.controls.attendances.reset(); + this.form.controls.employee.setValue(this.info.employee?.name ?? ''); + this.form.controls.attendances.clear(); this.info.body.forEach((x) => this.form.controls.attendances.push( new FormGroup({ @@ -120,12 +120,14 @@ export class EmployeeAttendanceComponent implements OnInit, AfterViewInit { const formValue = this.form.value; this.info.startDate = moment(formValue.startDate).format('DD-MMM-YYYY'); this.info.finishDate = moment(formValue.finishDate).format('DD-MMM-YYYY'); - this.router.navigate(['/employee-attendance', this.info.employee.id], { - queryParams: { - startDate: this.info.startDate, - finishDate: this.info.finishDate, - }, - }); + if (this.info.employee) { + this.router.navigate(['/employee-attendance', this.info.employee.id], { + queryParams: { + startDate: this.info.startDate, + finishDate: this.info.finishDate, + }, + }); + } } save() { diff --git a/overlord/src/app/employee-attendance/employee-attendance.service.ts b/overlord/src/app/employee-attendance/employee-attendance.service.ts index 46f0b350..c6eaef10 100644 --- a/overlord/src/app/employee-attendance/employee-attendance.service.ts +++ b/overlord/src/app/employee-attendance/employee-attendance.service.ts @@ -1,6 +1,6 @@ import { HttpClient, HttpParams } from '@angular/common/http'; import { Injectable } from '@angular/core'; -import { Observable } from 'rxjs/internal/Observable'; +import { Observable, of as observableOf } from 'rxjs'; import { catchError } from 'rxjs/operators'; import { ErrorLoggerService } from '../core/error-logger.service'; @@ -35,6 +35,9 @@ export class EmployeeAttendanceService { } save(employeeAttendance: EmployeeAttendance): Observable { + if (!employeeAttendance.employee) { + return observableOf(new EmployeeAttendance()); + } const { id } = employeeAttendance.employee; return this.http .post(`${url}/${id}`, employeeAttendance) diff --git a/overlord/src/app/employee-attendance/employee-attendance.ts b/overlord/src/app/employee-attendance/employee-attendance.ts index a874975b..46dd743e 100644 --- a/overlord/src/app/employee-attendance/employee-attendance.ts +++ b/overlord/src/app/employee-attendance/employee-attendance.ts @@ -5,13 +5,12 @@ import { EmployeeAttendanceItem } from './employee-attendance-item'; export class EmployeeAttendance { startDate: string; finishDate: string; - employee: Employee; + employee?: Employee; body: EmployeeAttendanceItem[]; public constructor(init?: Partial) { this.startDate = ''; this.finishDate = ''; - this.employee = new Employee(); this.body = []; Object.assign(this, init); } diff --git a/overlord/src/app/incentive/incentive.component.ts b/overlord/src/app/incentive/incentive.component.ts index 53f4c4af..30e78405 100644 --- a/overlord/src/app/incentive/incentive.component.ts +++ b/overlord/src/app/incentive/incentive.component.ts @@ -77,6 +77,7 @@ export class IncentiveComponent implements OnInit { loadVoucher(voucher: Voucher) { this.voucher = voucher; this.form.controls.date.setValue(moment(this.voucher.date, 'DD-MMM-YYYY').toDate()); + this.form.controls.incentives.clear(); this.voucher.incentives.forEach((x) => this.form.controls.incentives.push( new FormGroup({ diff --git a/overlord/src/app/role/role-detail/role-detail.component.ts b/overlord/src/app/role/role-detail/role-detail.component.ts index a91f34cd..e3677742 100644 --- a/overlord/src/app/role/role-detail/role-detail.component.ts +++ b/overlord/src/app/role/role-detail/role-detail.component.ts @@ -45,7 +45,7 @@ export class RoleDetailComponent implements OnInit, AfterViewInit { this.item = data.item; this.form.controls.name.setValue(this.item.name); - this.form.controls.permissions.reset(); + this.form.controls.permissions.clear(); this.item.permissions.forEach((x) => this.form.controls.permissions.push( new FormGroup({ diff --git a/overlord/src/app/settings/settings.component.ts b/overlord/src/app/settings/settings.component.ts index df22849d..1e6e45b2 100644 --- a/overlord/src/app/settings/settings.component.ts +++ b/overlord/src/app/settings/settings.component.ts @@ -111,7 +111,7 @@ export class SettingsComponent implements OnInit { showLockInformation(info: LockInfo[]) { this.lockInformation = info; - this.lockInfoForm.controls.accountTypes.reset(); + this.lockInfoForm.controls.accountTypes.clear(); this.accountTypes.forEach((x) => this.lockInfoForm.controls.accountTypes.push( new FormGroup({ @@ -119,7 +119,7 @@ export class SettingsComponent implements OnInit { }), ), ); - this.lockInfoForm.controls.voucherTypes.reset(); + this.lockInfoForm.controls.voucherTypes.clear(); this.voucherTypes.forEach((x) => this.lockInfoForm.controls.voucherTypes.push( new FormGroup({ diff --git a/overlord/src/app/user/user-detail/user-detail.component.ts b/overlord/src/app/user/user-detail/user-detail.component.ts index 0b680925..abac3bc2 100644 --- a/overlord/src/app/user/user-detail/user-detail.component.ts +++ b/overlord/src/app/user/user-detail/user-detail.component.ts @@ -58,6 +58,7 @@ export class UserDetailComponent implements OnInit, AfterViewInit { this.form.controls.name.setValue(item.name); this.form.controls.password.setValue(null); this.form.controls.lockedOut.setValue(item.lockedOut); + this.form.controls.roles.clear(); this.item.roles.forEach((x) => this.form.controls.roles.push( new FormGroup({