|
1 |
| -import { Component, ViewEncapsulation } from '@angular/core' |
2 |
| -import { FormControl, FormsModule, ReactiveFormsModule, Validators } from '@angular/forms' |
| 1 | +import type { OnDestroy, OnInit } from '@angular/core' |
| 2 | +import { ChangeDetectionStrategy, ChangeDetectorRef, Component, inject, ViewEncapsulation } from '@angular/core' |
| 3 | +import { FormControl, FormGroup, FormsModule, ReactiveFormsModule, Validators } from '@angular/forms' |
3 | 4 | import { MatButtonModule } from '@angular/material/button'
|
4 | 5 | import { MatFormFieldModule } from '@angular/material/form-field'
|
5 | 6 | import { MatIconModule } from '@angular/material/icon'
|
6 | 7 | import { MatInputModule } from '@angular/material/input'
|
| 8 | +import { Subject, takeUntil } from 'rxjs' |
| 9 | +import type { CurrentUser, UserUpdateRequest } from '@seed/api/user' |
| 10 | +import { UserService } from '@seed/api/user' |
| 11 | +import type { Alert } from '@seed/components' |
| 12 | +import { AlertComponent } from '@seed/components' |
7 | 13 | import { SharedImports } from '@seed/directives'
|
8 |
| - |
9 | 14 | @Component({
|
10 | 15 | selector: 'seed-profile-info',
|
11 | 16 | templateUrl: './info.component.html',
|
12 | 17 | encapsulation: ViewEncapsulation.None,
|
13 |
| - imports: [MatIconModule, FormsModule, MatFormFieldModule, MatInputModule, ReactiveFormsModule, MatButtonModule, SharedImports], |
| 18 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 19 | + imports: [ |
| 20 | + AlertComponent, |
| 21 | + MatIconModule, |
| 22 | + FormsModule, |
| 23 | + MatFormFieldModule, |
| 24 | + MatInputModule, |
| 25 | + ReactiveFormsModule, |
| 26 | + MatButtonModule, |
| 27 | + SharedImports, |
| 28 | + ], |
14 | 29 | })
|
15 |
| -export class ProfileInfoComponent { |
16 |
| - firstName: FormControl = new FormControl('', [Validators.required]) |
| 30 | +export class ProfileInfoComponent implements OnInit, OnDestroy { |
| 31 | + private _userService = inject(UserService) |
| 32 | + private _changeDetectorRef = inject(ChangeDetectorRef) |
| 33 | + |
| 34 | + alert: Alert |
| 35 | + showAlert = false |
| 36 | + user: CurrentUser |
| 37 | + |
| 38 | + profileForm = new FormGroup({ |
| 39 | + firstName: new FormControl('', [Validators.required]), |
| 40 | + lastName: new FormControl('', [Validators.required]), |
| 41 | + email: new FormControl('', [Validators.required]), |
| 42 | + }) |
| 43 | + |
| 44 | + private readonly _unsubscribeAll$ = new Subject<void>() |
| 45 | + |
| 46 | + ngOnInit(): void { |
| 47 | + // Subscribe to user changes |
| 48 | + this._userService.currentUser$.pipe(takeUntil(this._unsubscribeAll$)).subscribe((currentUser) => { |
| 49 | + this.user = currentUser |
17 | 50 |
|
18 |
| - lastName: FormControl = new FormControl('', [Validators.required]) |
| 51 | + // pre-populate form (here?) |
| 52 | + if (this.user.first_name) this.profileForm.get('firstName')?.setValue(this.user.first_name) |
| 53 | + if (this.user.last_name) this.profileForm.get('lastName')?.setValue(this.user.last_name) |
| 54 | + if (this.user.email) this.profileForm.get('email')?.setValue(this.user.email) |
| 55 | + |
| 56 | + // Mark for check |
| 57 | + this._changeDetectorRef.markForCheck() |
| 58 | + }) |
| 59 | + } |
| 60 | + |
| 61 | + ngOnDestroy(): void { |
| 62 | + this._unsubscribeAll$.next() |
| 63 | + this._unsubscribeAll$.complete() |
| 64 | + } |
19 | 65 |
|
20 |
| - email: FormControl = new FormControl('', [Validators.required]) |
| 66 | + onSubmit(): void { |
| 67 | + // Handle form submission |
| 68 | + if (this.profileForm.valid) { |
| 69 | + const userData = { |
| 70 | + first_name: this.profileForm.value.firstName, |
| 71 | + last_name: this.profileForm.value.lastName, |
| 72 | + email: this.profileForm.value.email, |
| 73 | + } as UserUpdateRequest |
21 | 74 |
|
22 |
| - onSubmit() { |
23 |
| - // Handle form submission (e.g., send the form data to an API) |
24 |
| - console.log('Form Submitted', { firstName: this.firstName, lastName: this.lastName, email: this.email }) |
| 75 | + this._userService.updateUser(this.user.id, userData).subscribe({ |
| 76 | + error: (error) => { |
| 77 | + console.error('Error:', error) |
| 78 | + this.alert = { |
| 79 | + type: 'error', |
| 80 | + message: 'Update User Unsuccessful...', |
| 81 | + } |
| 82 | + this.showAlert = true |
| 83 | + }, |
| 84 | + complete: () => { |
| 85 | + this.alert = { |
| 86 | + type: 'success', |
| 87 | + message: 'Changes saved!', |
| 88 | + } |
| 89 | + this.showAlert = true |
| 90 | + }, |
| 91 | + }) |
| 92 | + } |
25 | 93 | }
|
26 | 94 | }
|
0 commit comments