Repository navigation
Expand file tree
/
Copy pathsettings.component.ts
More file actions
67 lines (57 loc) · 1.62 KB
/
Copy pathsettings.component.ts
File metadata and controls
67 lines (57 loc) · 1.62 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
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { Router } from '@angular/router';
import { User, UserService } from '../shared';
@Component({
selector: 'settings-page',
templateUrl: './settings.component.html'
})
export class SettingsComponent implements OnInit {
user: User = new User();
settingsForm: FormGroup;
errors: Object = {};
isSubmitting: boolean = false;
constructor(
private router: Router,
private userService: UserService,
private fb: FormBuilder
) {
// create form group using the form builder
this.settingsForm = this.fb.group({
image: '',
username: '',
bio: '',
email: '',
password: ''
});
// Optional: subscribe to changes on the form
// this.settingsForm.valueChanges.subscribe(values => this.updateUser(values));
}
ngOnInit() {
// Make a fresh copy of the current user's object to place in editable form fields
(<any>Object).assign(this.user, this.userService.getCurrentUser());
// Fill the form
this.settingsForm.patchValue(this.user);
}
logout() {
this.userService.purgeAuth();
this.router.navigateByUrl('/');
}
submitForm() {
this.isSubmitting = true;
// update the model
this.updateUser(this.settingsForm.value);
this.userService
.update(this.user)
.subscribe(
updatedUser => this.router.navigateByUrl('/profile/' + updatedUser.username),
err => {
this.errors = err;
this.isSubmitting = false;
}
);
}
updateUser(values: Object) {
(<any>Object).assign(this.user, values);
}
}