Skip to content

Commit 8baf358

Browse files
committed
connect profile info to update user api
1 parent 03b1e10 commit 8baf358

File tree

5 files changed

+116
-31
lines changed

5 files changed

+116
-31
lines changed

src/@seed/api/user/user.service.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import type { HttpErrorResponse } from '@angular/common/http'
12
import { HttpClient } from '@angular/common/http'
23
import { inject, Injectable } from '@angular/core'
34
import type { Observable } from 'rxjs'
4-
import { distinctUntilChanged, ReplaySubject, switchMap, take, tap } from 'rxjs'
5-
import type { CurrentUser, SetDefaultOrganizationResponse } from '@seed/api/user'
5+
import { catchError, distinctUntilChanged, ReplaySubject, switchMap, take, tap } from 'rxjs'
6+
import type { CurrentUser, SetDefaultOrganizationResponse, UserUpdateRequest } from '@seed/api/user'
67

78
@Injectable({ providedIn: 'root' })
89
export class UserService {
@@ -42,4 +43,19 @@ export class UserService {
4243
}),
4344
)
4445
}
46+
47+
/**
48+
* Update user
49+
*/
50+
updateUser(userId: number, params: UserUpdateRequest): Observable<CurrentUser> {
51+
return this._httpClient.put<CurrentUser>(`api/v3/users/${userId}/`, params).pipe(
52+
tap((user) => {
53+
this._currentUser.next(user)
54+
}),
55+
catchError((error: HttpErrorResponse) => {
56+
console.error('Error occurred while updating user:', error.error)
57+
return this._currentUser
58+
}),
59+
)
60+
}
4561
}

src/@seed/api/user/user.types.ts

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ export type CurrentUser = {
1818
api_key: string;
1919
}
2020

21+
export type UserUpdateRequest = {
22+
first_name: string;
23+
last_name: string;
24+
email: string;
25+
}
26+
2127
export type SetDefaultOrganizationResponse = {
2228
status: string;
2329
user: {

src/app/layout/common/user/user.component.ts

-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ export class UserComponent implements OnInit, OnDestroy {
5555
}
5656

5757
goToProfile() {
58-
console.log('hi!')
5958
void this._router.navigate(['/profile'])
6059
}
6160
}

src/app/modules/profile/info/info.component.html

+13-17
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,39 @@ <h2 class="mb-6 flex items-center text-center text-2xl">
44
<mat-icon class="mr-2" svgIcon="fa-solid:circle-user"></mat-icon>Profile Information
55
</h2>
66

7-
<form class="bg-card mt-8 flex flex-col overflow-hidden rounded-2xl p-8 pb-4 shadow">
7+
<form [formGroup]="profileForm" (ngSubmit)="onSubmit()" class="bg-card mt-8 flex flex-col overflow-hidden rounded-2xl p-8 pb-4 shadow">
88
<div class="gt-xs:flex-row flex flex-col">
99
<mat-form-field class="gt-xs:pr-3 flex-auto">
1010
<mat-label>{{ t('First Name') }}</mat-label>
11-
<input matInput />
11+
<input matInput [formControlName]="'firstName'" />
1212
<mat-icon class="icon-size-5" matPrefix [svgIcon]="'fa-solid:circle-user'"></mat-icon>
1313
</mat-form-field>
1414
<mat-form-field class="gt-xs:pl-3 flex-auto">
1515
<mat-label>{{ t('Last Name') }}</mat-label>
16-
<input matInput />
16+
<input matInput [formControlName]="'lastName'" />
1717
<mat-icon class="icon-size-5" matPrefix [svgIcon]="'fa-solid:circle-user'"></mat-icon>
1818
</mat-form-field>
1919
</div>
2020
<div class="flex">
21-
<!-- <mat-form-field
22-
class="flex-auto"
23-
>
24-
<mat-label>{{ t('Email') }}</mat-label>
25-
<input matInput/>
26-
<mat-icon
27-
class="icon-size-5"
28-
matSuffix
29-
[svgIcon]="'fa-solid:envelope'"
30-
></mat-icon>
31-
</mat-form-field> -->
32-
3321
<mat-form-field class="w-full">
3422
<mat-label>Email</mat-label>
3523
<mat-icon class="icon-size-5" [svgIcon]="'fa-solid:envelope'" matPrefix></mat-icon>
3624
<input [formControlName]="'email'" matInput />
3725
</mat-form-field>
3826
</div>
3927
<div class="flex justify-center">
40-
<a mat-flat-button color="primary">
28+
<button mat-flat-button color="primary">
4129
<span class="ml-2">{{ t('Update') }}</span>
42-
</a>
30+
</button>
4331
</div>
32+
<!-- Alert -->
33+
@if (showAlert) {
34+
<div class="mb-4 mt-0">
35+
<seed-alert class="-mb-4 mt-8" appearance="outline" [showIcon]="false" [type]="alert.type">
36+
{{ alert.message }}
37+
</seed-alert>
38+
</div>
39+
}
4440
</form>
4541
</div>
4642
</div>
+79-11
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,94 @@
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'
34
import { MatButtonModule } from '@angular/material/button'
45
import { MatFormFieldModule } from '@angular/material/form-field'
56
import { MatIconModule } from '@angular/material/icon'
67
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'
713
import { SharedImports } from '@seed/directives'
8-
914
@Component({
1015
selector: 'seed-profile-info',
1116
templateUrl: './info.component.html',
1217
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+
],
1429
})
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
1750

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+
}
1965

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
2174

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+
}
2593
}
2694
}

0 commit comments

Comments
 (0)