forked from CodeURJC-DAW-2023-24/webapp07
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit-profile.component.ts
60 lines (55 loc) · 1.53 KB
/
edit-profile.component.ts
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
import { Component } from '@angular/core';
import { AuthService } from '../services/auth.service';
import { UserService } from '../services/user.service';
import { Router } from '@angular/router';
@Component({
selector: 'app-edit-profile',
templateUrl: './edit-profile.component.html',
styleUrl: './edit-profile.component.css'
})
export class EditProfileComponent {
name: string = '';
email: string = '';
profilePhoto: File | null = null;
constructor(private authService: AuthService, private userService: UserService, private router: Router) {
let id = this.authService.getUserId();
this.userService.getUser().subscribe({
next: (data: any) => {
this.name = data.name;
this.email = data.email;
},
error: (error: any) => {
console.error('User retrieval failure');
this.router.navigate(['/login']);
}
});
};
onFileSelected(event: any): void {
const files: FileList = event.target.files;
if (files.length > 0) {
this.profilePhoto = files[0];
} else {
this.profilePhoto = null;
}
}
saveProfile(): void{
this.userService.updateUserEmail(this.email).subscribe({
next: (data: any) => {
if (this.profilePhoto) {
this.userService.updateUserProfilePhoto(this.profilePhoto).subscribe({
error: (error: any) => {
console.error('Profile photo update failure');
}
});
}
},
error: (error: any) => {
console.error('User update failure');
}
});
this.router.navigate(['/']);
setTimeout(() => {
window.location.href = window.location.href;
}, 200);
}
}