|
7 | 7 | */ |
8 | 8 |
|
9 | 9 | /** Angular Imports */ |
10 | | -import { ChangeDetectionStrategy, Component, OnInit, inject } from '@angular/core'; |
11 | | -import { |
12 | | - UntypedFormGroup, |
13 | | - UntypedFormBuilder, |
14 | | - UntypedFormControl, |
15 | | - Validators, |
16 | | - ReactiveFormsModule |
17 | | -} from '@angular/forms'; |
18 | | -import { Router, ActivatedRoute, RouterLink } from '@angular/router'; |
| 10 | +import { ChangeDetectionStrategy, Component, DestroyRef, OnInit, inject } from '@angular/core'; |
| 11 | +import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; |
| 12 | +import { FormGroup, FormBuilder, FormControl, Validators } from '@angular/forms'; |
| 13 | +import { Router, ActivatedRoute } from '@angular/router'; |
19 | 14 |
|
20 | 15 | /** Custom Services */ |
21 | 16 | import { ProductsService } from '../../products.service'; |
@@ -46,15 +41,16 @@ import { STANDALONE_SHARED_IMPORTS } from 'app/standalone-shared.module'; |
46 | 41 | changeDetection: ChangeDetectionStrategy.OnPush |
47 | 42 | }) |
48 | 43 | export class CreateChargeComponent implements OnInit { |
49 | | - private formBuilder = inject(UntypedFormBuilder); |
| 44 | + private formBuilder = inject(FormBuilder); |
50 | 45 | private productsService = inject(ProductsService); |
51 | 46 | private route = inject(ActivatedRoute); |
52 | 47 | private router = inject(Router); |
53 | 48 | private dateUtils = inject(Dates); |
54 | 49 | private settingsService = inject(SettingsService); |
| 50 | + private destroyRef = inject(DestroyRef); |
55 | 51 |
|
56 | 52 | /** Charge form. */ |
57 | | - chargeForm: UntypedFormGroup; |
| 53 | + chargeForm: FormGroup; |
58 | 54 | /** Charges template data. */ |
59 | 55 | chargesTemplateData: any; |
60 | 56 | /** Charge time type data. */ |
@@ -84,7 +80,7 @@ export class CreateChargeComponent implements OnInit { |
84 | 80 | * @param {SettingsService} settingsService Settings Service |
85 | 81 | */ |
86 | 82 | constructor() { |
87 | | - this.route.data.subscribe((data: { chargesTemplate: any }) => { |
| 83 | + this.route.data.pipe(takeUntilDestroyed(this.destroyRef)).subscribe((data: { chargesTemplate: any }) => { |
88 | 84 | this.chargesTemplateData = data.chargesTemplate; |
89 | 85 | this.chargePaymentModeData = this.chargesTemplateData.chargePaymetModeOptions; |
90 | 86 | const incomeOptions = data.chargesTemplate.incomeOrLiabilityAccountOptions.incomeAccountOptions || []; |
@@ -156,35 +152,42 @@ export class CreateChargeComponent implements OnInit { |
156 | 152 | * Sets the charge calculation type and charge time type data |
157 | 153 | */ |
158 | 154 | setChargeForm() { |
159 | | - this.chargeForm.get('chargeAppliesTo').valueChanges.subscribe((chargeAppliesTo) => { |
160 | | - switch (chargeAppliesTo) { |
161 | | - case 1: |
162 | | - this.chargeCalculationTypeData = this.chargesTemplateData.loanChargeCalculationTypeOptions; |
163 | | - this.chargeTimeTypeData = this.chargesTemplateData.loanChargeTimeTypeOptions; |
164 | | - break; |
165 | | - case 2: |
166 | | - this.chargeCalculationTypeData = this.chargesTemplateData.savingsChargeCalculationTypeOptions; |
167 | | - this.chargeTimeTypeData = this.chargesTemplateData.savingsChargeTimeTypeOptions; |
168 | | - break; |
169 | | - case 3: |
170 | | - this.chargeCalculationTypeData = this.chargesTemplateData.clientChargeCalculationTypeOptions; |
171 | | - this.chargeTimeTypeData = this.chargesTemplateData.clientChargeTimeTypeOptions; |
172 | | - break; |
173 | | - case 4: |
174 | | - this.chargeCalculationTypeData = this.chargesTemplateData.shareChargeCalculationTypeOptions; |
175 | | - this.chargeTimeTypeData = this.chargesTemplateData.shareChargeTimeTypeOptions; |
176 | | - break; |
177 | | - case 5: |
178 | | - this.chargeCalculationTypeData = this.chargesTemplateData.loanChargeCalculationTypeOptions; |
179 | | - this.chargeTimeTypeData = this.chargesTemplateData.loanChargeTimeTypeOptions.filter((chargeTimeType: any) => { |
180 | | - return [2].includes(chargeTimeType.id); // Only Specific Due Date |
181 | | - }); |
182 | | - this.chargePaymentModeData = this.chargePaymentModeData.filter((chargePaymentMode: any) => { |
183 | | - return chargePaymentMode.id === 0; |
184 | | - }); |
185 | | - break; |
186 | | - } |
187 | | - }); |
| 155 | + this.chargeForm |
| 156 | + .get('chargeAppliesTo') |
| 157 | + .valueChanges.pipe(takeUntilDestroyed(this.destroyRef)) |
| 158 | + .subscribe((chargeAppliesTo) => { |
| 159 | + const allPaymentModes = this.chargesTemplateData.chargePaymetModeOptions || []; |
| 160 | + this.chargePaymentModeData = allPaymentModes; |
| 161 | + switch (chargeAppliesTo) { |
| 162 | + case 1: |
| 163 | + this.chargeCalculationTypeData = this.chargesTemplateData.loanChargeCalculationTypeOptions; |
| 164 | + this.chargeTimeTypeData = this.chargesTemplateData.loanChargeTimeTypeOptions; |
| 165 | + break; |
| 166 | + case 2: |
| 167 | + this.chargeCalculationTypeData = this.chargesTemplateData.savingsChargeCalculationTypeOptions; |
| 168 | + this.chargeTimeTypeData = this.chargesTemplateData.savingsChargeTimeTypeOptions; |
| 169 | + break; |
| 170 | + case 3: |
| 171 | + this.chargeCalculationTypeData = this.chargesTemplateData.clientChargeCalculationTypeOptions; |
| 172 | + this.chargeTimeTypeData = this.chargesTemplateData.clientChargeTimeTypeOptions; |
| 173 | + break; |
| 174 | + case 4: |
| 175 | + this.chargeCalculationTypeData = this.chargesTemplateData.shareChargeCalculationTypeOptions; |
| 176 | + this.chargeTimeTypeData = this.chargesTemplateData.shareChargeTimeTypeOptions; |
| 177 | + break; |
| 178 | + case 5: |
| 179 | + this.chargeCalculationTypeData = this.chargesTemplateData.loanChargeCalculationTypeOptions; |
| 180 | + this.chargeTimeTypeData = this.chargesTemplateData.loanChargeTimeTypeOptions.filter( |
| 181 | + (chargeTimeType: any) => { |
| 182 | + return [2].includes(chargeTimeType.id); // Only Specific Due Date |
| 183 | + } |
| 184 | + ); |
| 185 | + this.chargePaymentModeData = allPaymentModes.filter((chargePaymentMode: any) => { |
| 186 | + return chargePaymentMode.id === 0; |
| 187 | + }); |
| 188 | + break; |
| 189 | + } |
| 190 | + }); |
188 | 191 | } |
189 | 192 |
|
190 | 193 | /** |
@@ -245,95 +248,102 @@ export class CreateChargeComponent implements OnInit { |
245 | 248 | * Sets the conditional controls of the user form |
246 | 249 | */ |
247 | 250 | setConditionalControls() { |
248 | | - this.chargeForm.get('chargeAppliesTo').valueChanges.subscribe((chargeAppliesTo) => { |
249 | | - this.chargeForm.get('penalty').enable(); |
250 | | - switch (chargeAppliesTo) { |
251 | | - case 1: // Loan |
252 | | - this.chargeForm.addControl('chargePaymentMode', new UntypedFormControl('', Validators.required)); |
253 | | - this.chargeForm.removeControl('incomeAccountId'); |
254 | | - break; |
255 | | - case 2: // Savings |
256 | | - this.chargeForm.removeControl('chargePaymentMode'); |
257 | | - this.chargeForm.removeControl('incomeAccountId'); |
258 | | - break; |
259 | | - case 3: // Client |
260 | | - this.chargeForm.removeControl('chargePaymentMode'); |
261 | | - this.chargeForm.addControl('incomeAccountId', new UntypedFormControl('')); |
262 | | - break; |
263 | | - case 4: // Shares |
264 | | - this.chargeForm.removeControl('chargePaymentMode'); |
265 | | - this.chargeForm.removeControl('incomeAccountId'); |
266 | | - this.chargeForm.get('penalty').setValue(false); |
267 | | - break; |
268 | | - case 5: // Working Capital Loans |
269 | | - this.chargeForm.addControl('chargePaymentMode', new UntypedFormControl('', Validators.required)); |
270 | | - this.chargeForm.removeControl('incomeAccountId'); |
271 | | - break; |
272 | | - } |
273 | | - this.chargeForm.get('chargeCalculationType').reset(); |
274 | | - this.chargeForm.get('chargeTimeType').reset(); |
275 | | - }); |
276 | | - this.chargeForm.get('chargeTimeType').valueChanges.subscribe((chargeTimeType) => { |
277 | | - this.chargeForm.removeControl('feeFrequency'); |
278 | | - this.chargeForm.removeControl('feeInterval'); |
279 | | - this.chargeForm.removeControl('feeOnMonthDay'); |
280 | | - this.chargeForm.removeControl('addFeeFrequency'); |
281 | | - if (this.chargeForm.get('chargeAppliesTo').value !== 4) { |
| 251 | + this.chargeForm |
| 252 | + .get('chargeAppliesTo') |
| 253 | + .valueChanges.pipe(takeUntilDestroyed(this.destroyRef)) |
| 254 | + .subscribe((chargeAppliesTo) => { |
282 | 255 | this.chargeForm.get('penalty').enable(); |
283 | | - } |
284 | | - switch (chargeTimeType) { |
285 | | - case 6: // Annual Fee |
286 | | - this.chargeForm.addControl('feeOnMonthDay', new UntypedFormControl('', Validators.required)); |
287 | | - break; |
288 | | - case 7: // Monthly Fee |
289 | | - this.chargeForm.addControl('feeOnMonthDay', new UntypedFormControl('')); |
290 | | - this.chargeForm.addControl( |
291 | | - 'feeInterval', |
292 | | - new UntypedFormControl('', [ |
293 | | - Validators.required, |
294 | | - Validators.min(1), |
295 | | - Validators.max(12), |
296 | | - Validators.pattern('^[1-9]\\d*$') |
297 | | - ]) |
298 | | - ); |
299 | | - this.repeatEveryLabel = 'Months'; |
300 | | - break; |
301 | | - case 9: // Overdue Fee |
302 | | - this.chargeForm.get('penalty').setValue(true); |
303 | | - this.chargeForm.addControl('addFeeFrequency', new UntypedFormControl(false)); |
304 | | - this.chargeForm.get('addFeeFrequency').valueChanges.subscribe((addFeeFrequency) => { |
305 | | - if (addFeeFrequency) { |
306 | | - this.chargeForm.addControl('feeFrequency', new UntypedFormControl('', Validators.required)); |
307 | | - this.chargeForm.addControl( |
308 | | - 'feeInterval', |
309 | | - new UntypedFormControl('', [ |
310 | | - Validators.required, |
311 | | - Validators.pattern('^[1-9]\\d*$') |
312 | | - ]) |
313 | | - ); |
314 | | - } else { |
315 | | - this.chargeForm.removeControl('feeFrequency'); |
316 | | - this.chargeForm.removeControl('feeInterval'); |
317 | | - } |
318 | | - }); |
319 | | - break; |
320 | | - case 11: // Weekly Fee |
321 | | - this.chargeForm.addControl( |
322 | | - 'feeInterval', |
323 | | - new UntypedFormControl('', [ |
324 | | - Validators.required, |
325 | | - Validators.pattern('^[1-9]\\d*$') |
326 | | - ]) |
327 | | - ); |
328 | | - this.repeatEveryLabel = 'Weeks'; |
329 | | - break; |
330 | | - } |
331 | | - }); |
332 | | - this.chargeForm.get('currencyCode').valueChanges.subscribe((currencyCode) => { |
333 | | - this.currencyDecimalPlaces = this.chargesTemplateData.currencyOptions.find( |
334 | | - (currency: any) => currency.code === currencyCode |
335 | | - ).decimalPlaces; |
336 | | - }); |
| 256 | + this.chargeForm.removeControl('chargePaymentMode'); |
| 257 | + this.chargeForm.removeControl('incomeAccountId'); |
| 258 | + switch (chargeAppliesTo) { |
| 259 | + case 1: // Loan |
| 260 | + this.chargeForm.addControl('chargePaymentMode', new FormControl('', Validators.required)); |
| 261 | + break; |
| 262 | + case 2: // Savings |
| 263 | + break; |
| 264 | + case 3: // Client |
| 265 | + this.chargeForm.addControl('incomeAccountId', new FormControl('')); |
| 266 | + break; |
| 267 | + case 4: // Shares |
| 268 | + this.chargeForm.get('penalty').setValue(false); |
| 269 | + break; |
| 270 | + case 5: // Working Capital Loans |
| 271 | + this.chargeForm.addControl('chargePaymentMode', new FormControl('', Validators.required)); |
| 272 | + break; |
| 273 | + } |
| 274 | + this.chargeForm.get('chargeCalculationType').reset(); |
| 275 | + this.chargeForm.get('chargeTimeType').reset(); |
| 276 | + }); |
| 277 | + this.chargeForm |
| 278 | + .get('chargeTimeType') |
| 279 | + .valueChanges.pipe(takeUntilDestroyed(this.destroyRef)) |
| 280 | + .subscribe((chargeTimeType) => { |
| 281 | + this.chargeForm.removeControl('feeFrequency'); |
| 282 | + this.chargeForm.removeControl('feeInterval'); |
| 283 | + this.chargeForm.removeControl('feeOnMonthDay'); |
| 284 | + this.chargeForm.removeControl('addFeeFrequency'); |
| 285 | + if (this.chargeForm.get('chargeAppliesTo').value !== 4) { |
| 286 | + this.chargeForm.get('penalty').enable(); |
| 287 | + } |
| 288 | + switch (chargeTimeType) { |
| 289 | + case 6: // Annual Fee |
| 290 | + this.chargeForm.addControl('feeOnMonthDay', new FormControl('', Validators.required)); |
| 291 | + break; |
| 292 | + case 7: // Monthly Fee |
| 293 | + this.chargeForm.addControl('feeOnMonthDay', new FormControl('')); |
| 294 | + this.chargeForm.addControl( |
| 295 | + 'feeInterval', |
| 296 | + new FormControl('', [ |
| 297 | + Validators.required, |
| 298 | + Validators.min(1), |
| 299 | + Validators.max(12), |
| 300 | + Validators.pattern('^[1-9]\\d*$') |
| 301 | + ]) |
| 302 | + ); |
| 303 | + this.repeatEveryLabel = 'Months'; |
| 304 | + break; |
| 305 | + case 9: // Overdue Fee |
| 306 | + this.chargeForm.get('penalty').setValue(true); |
| 307 | + this.chargeForm.addControl('addFeeFrequency', new FormControl(false)); |
| 308 | + this.chargeForm |
| 309 | + .get('addFeeFrequency') |
| 310 | + .valueChanges.pipe(takeUntilDestroyed(this.destroyRef)) |
| 311 | + .subscribe((addFeeFrequency) => { |
| 312 | + if (addFeeFrequency) { |
| 313 | + this.chargeForm.addControl('feeFrequency', new FormControl('', Validators.required)); |
| 314 | + this.chargeForm.addControl( |
| 315 | + 'feeInterval', |
| 316 | + new FormControl('', [ |
| 317 | + Validators.required, |
| 318 | + Validators.pattern('^[1-9]\\d*$') |
| 319 | + ]) |
| 320 | + ); |
| 321 | + } else { |
| 322 | + this.chargeForm.removeControl('feeFrequency'); |
| 323 | + this.chargeForm.removeControl('feeInterval'); |
| 324 | + } |
| 325 | + }); |
| 326 | + break; |
| 327 | + case 11: // Weekly Fee |
| 328 | + this.chargeForm.addControl( |
| 329 | + 'feeInterval', |
| 330 | + new FormControl('', [ |
| 331 | + Validators.required, |
| 332 | + Validators.pattern('^[1-9]\\d*$') |
| 333 | + ]) |
| 334 | + ); |
| 335 | + this.repeatEveryLabel = 'Weeks'; |
| 336 | + break; |
| 337 | + } |
| 338 | + }); |
| 339 | + this.chargeForm |
| 340 | + .get('currencyCode') |
| 341 | + .valueChanges.pipe(takeUntilDestroyed(this.destroyRef)) |
| 342 | + .subscribe((currencyCode) => { |
| 343 | + this.currencyDecimalPlaces = this.chargesTemplateData.currencyOptions.find( |
| 344 | + (currency: any) => currency.code === currencyCode |
| 345 | + ).decimalPlaces; |
| 346 | + }); |
337 | 347 | } |
338 | 348 |
|
339 | 349 | /** |
|
0 commit comments