forked from realworld-apps/angular-realworld-example-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.component.ts
More file actions
75 lines (68 loc) · 2.09 KB
/
settings.component.ts
File metadata and controls
75 lines (68 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { Component, DestroyRef, inject, OnInit } from "@angular/core";
import {
FormControl,
FormGroup,
ReactiveFormsModule,
Validators,
} from "@angular/forms";
import { Router } from "@angular/router";
import { User } from "../../core/auth/user.model";
import { UserService } from "../../core/auth/services/user.service";
import { ListErrorsComponent } from "../../shared/components/list-errors.component";
import { Errors } from "../../core/models/errors.model";
import { takeUntilDestroyed } from "@angular/core/rxjs-interop";
interface SettingsForm {
image: FormControl<string>;
username: FormControl<string>;
bio: FormControl<string>;
email: FormControl<string>;
password: FormControl<string>;
}
@Component({
selector: "app-settings-page",
templateUrl: "./settings.component.html",
imports: [ListErrorsComponent, ReactiveFormsModule],
})
export default class SettingsComponent implements OnInit {
user!: User;
settingsForm = new FormGroup<SettingsForm>({
image: new FormControl("", { nonNullable: true }),
username: new FormControl("", { nonNullable: true }),
bio: new FormControl("", { nonNullable: true }),
email: new FormControl("", { nonNullable: true }),
password: new FormControl("", {
validators: [Validators.required],
nonNullable: true,
}),
});
errors: Errors | null = null;
isSubmitting = false;
destroyRef = inject(DestroyRef);
constructor(
private readonly router: Router,
private readonly formGroup: FormGroup,
private readonly userService: UserService,
) {}
ngOnInit(): void {
this.settingsForm.patchValue(
this.userService.getCurrentUser() as Partial<User>,
);
}
logout(): void {
this.userService.logout();
}
submitForm() {
this.isSubmitting = true;
this.userService
.update(this.settingsForm.value)
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe({
next: ({ user }) =>
void this.router.navigate(["/profile/", user.username]),
error: (err) => {
this.errors = err;
// this.isSubmitting = false;
},
});
}
}