|
| 1 | +/* eslint-disable @typescript-eslint/no-non-null-assertion */ |
| 2 | +import {Component, OnInit} from '@angular/core'; |
| 3 | +import {CommonModule} from '@angular/common'; |
| 4 | +import {FormControl, FormGroup, ReactiveFormsModule, Validators} from '@angular/forms'; |
| 5 | +import {ActivatedRoute, Router, RouterModule} from '@angular/router'; |
| 6 | +import {CardModule} from 'primeng/card'; |
| 7 | +import {ButtonModule} from 'primeng/button'; |
| 8 | +import {ProgressSpinnerModule} from 'primeng/progressspinner'; |
| 9 | +import {ApiService, ToastService} from '@core/services'; |
| 10 | +import {UpdateCustomer} from '@core/models'; |
| 11 | + |
| 12 | + |
| 13 | +@Component({ |
| 14 | + selector: 'app-edit-customer', |
| 15 | + standalone: true, |
| 16 | + templateUrl: './edit-customer.component.html', |
| 17 | + styleUrls: [], |
| 18 | + imports: [ |
| 19 | + CommonModule, |
| 20 | + CardModule, |
| 21 | + ButtonModule, |
| 22 | + ProgressSpinnerModule, |
| 23 | + ReactiveFormsModule, |
| 24 | + RouterModule, |
| 25 | + ], |
| 26 | +}) |
| 27 | +export class EditCustomerComponent implements OnInit { |
| 28 | + public title: string; |
| 29 | + public id: string | null; |
| 30 | + public form: FormGroup<CustomerForm>; |
| 31 | + public isLoading: boolean; |
| 32 | + |
| 33 | + constructor( |
| 34 | + private apiService: ApiService, |
| 35 | + private toastService: ToastService, |
| 36 | + private route: ActivatedRoute, |
| 37 | + private router: Router) |
| 38 | + { |
| 39 | + this.title = 'Edit customer'; |
| 40 | + this.id = null; |
| 41 | + this.isLoading = false; |
| 42 | + |
| 43 | + this.form = new FormGroup<CustomerForm>({ |
| 44 | + name: new FormControl<string>('', {validators: Validators.required, nonNullable: true}) |
| 45 | + }); |
| 46 | + } |
| 47 | + |
| 48 | + ngOnInit(): void { |
| 49 | + this.route.params.subscribe((params) => { |
| 50 | + this.id = params['id']; |
| 51 | + }); |
| 52 | + |
| 53 | + if (this.id) { |
| 54 | + this.title = 'Edit customer'; |
| 55 | + this.fetchCustomer(); |
| 56 | + } |
| 57 | + else { |
| 58 | + this.title = 'Add a new customer'; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + submit() { |
| 63 | + if (this.id) { |
| 64 | + this.updateCustomer(); |
| 65 | + } |
| 66 | + else { |
| 67 | + this.addCustomer(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private fetchCustomer() { |
| 72 | + this.isLoading = true; |
| 73 | + |
| 74 | + this.apiService.getCustomer(this.id!).subscribe((result) => { |
| 75 | + if (result.isSuccess) { |
| 76 | + this.form.patchValue({name: result.data?.name}); |
| 77 | + } |
| 78 | + |
| 79 | + this.isLoading = false; |
| 80 | + }) |
| 81 | + } |
| 82 | + |
| 83 | + private addCustomer() { |
| 84 | + this.isLoading = true; |
| 85 | + |
| 86 | + this.apiService.createCustomer({name: this.form.value.name!}).subscribe((result) => { |
| 87 | + if (result.isSuccess) { |
| 88 | + this.toastService.showSuccess('A new customer has been added successfully'); |
| 89 | + this.router.navigateByUrl('/customers'); |
| 90 | + } |
| 91 | + |
| 92 | + this.isLoading = false; |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + private updateCustomer() { |
| 97 | + this.isLoading = true; |
| 98 | + |
| 99 | + const commad: UpdateCustomer = { |
| 100 | + id: this.id!, |
| 101 | + name: this.form.value.name!, |
| 102 | + } |
| 103 | + |
| 104 | + this.apiService.updateCustomer(commad).subscribe((result) => { |
| 105 | + if (result.isSuccess) { |
| 106 | + this.toastService.showSuccess('A new customer has been added successfully'); |
| 107 | + this.router.navigateByUrl('/customers'); |
| 108 | + } |
| 109 | + |
| 110 | + this.isLoading = false; |
| 111 | + }); |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +interface CustomerForm { |
| 116 | + name: FormControl<string>; |
| 117 | +} |
0 commit comments