Skip to content

Commit 350fe49

Browse files
committed
✨ update user info added
1 parent 80e07e0 commit 350fe49

File tree

11 files changed

+141
-31
lines changed

11 files changed

+141
-31
lines changed

src/app/core/components/sidebar/sidebar.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
src="https://images.unsplash.com/photo-1487412720507-e7ab37603c6f?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1171&q=80"
4141
alt="img-user">
4242
</figure>
43-
<span class="username"> {{userActive.email }} </span>
43+
<span class="username"> {{ getName }} </span>
4444
</div>
4545
</div>
4646

src/app/core/components/sidebar/sidebar.component.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,9 @@ export class SidebarComponent implements OnInit, AfterViewInit, OnDestroy {
4747
this.userActive = this.authService.getUserActive;
4848
}
4949

50+
get getName(): string {
51+
const { name, lastName, email } = this.userActive;
52+
return (name && name?.length > 0) ? `${name} ${lastName || ''}` : email;
53+
}
54+
5055
}

src/app/core/interfaces/response.interface.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ export interface IResponseWebsite {
1515
}
1616

1717
export interface IResponseLogin {
18-
ok: true,
18+
ok: boolean,
1919
token: string,
2020
user: User,
2121
};
22+
23+
export interface IResponseUser {
24+
ok: boolean,
25+
msg: string;
26+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface IUser {
2+
readonly name: string;
3+
readonly lastName: string;
4+
}

src/app/core/services/auth.service.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { EventEmitter, Injectable } from '@angular/core';
2-
import { catchError, map, Observable, of, Subject, tap } from 'rxjs';
2+
import { catchError, map, Observable, of } from 'rxjs';
33
import { environment } from 'environments/environment';
44
import { HttpClient } from '@angular/common/http';
55

@@ -80,4 +80,9 @@ export class AuthService {
8080
}))
8181
}
8282

83+
setNewUserInfo(name: string, lastName: string): void {
84+
this.userActive.name = name;
85+
this.userActive.lastName = lastName
86+
}
87+
8388
}

src/app/core/services/user.service.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { Injectable } from '@angular/core';
33
import { environment } from 'environments/environment';
44

55
import Storage from '@utils/storage.util';
6+
import { IUser } from '@interfaces/user.interface';
7+
import { map, Observable } from 'rxjs';
8+
import { IResponseUser } from '@interfaces/response.interface';
9+
import { AuthService } from './auth.service';
610

711
const base_url = environment.base_url;
812

@@ -20,7 +24,8 @@ export class UserService {
2024
}
2125

2226
constructor(
23-
private http: HttpClient
27+
private http: HttpClient,
28+
private authService: AuthService,
2429
) { }
2530

2631
modifyPreferences(resourceID: string, filter: 'subscription' | 'read' | 'saved') {
@@ -30,7 +35,15 @@ export class UserService {
3035

3136
setTheme(darkMode: boolean): void {
3237
const url = `${base_url}/user/theme`;
33-
this.http.patch(url, { darkMode }, this.headers).subscribe(resp => console.log({resp}));
38+
this.http.patch(url, { darkMode }, this.headers).subscribe();
39+
}
40+
41+
updateUserInfo(data: IUser): Observable<boolean> {
42+
const url = `${base_url}/user`;
43+
return this.http.patch<IResponseUser>(url, data, this.headers).pipe(map(resp => {
44+
this.authService.setNewUserInfo(data.name, data.lastName);
45+
return resp.ok;
46+
}));
3447
}
3548

3649
}

src/app/core/utils/regex.util.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export class RegexExpressions {
22
public static readonly EMAIL = /^((?!\.)[\w\-_.]*[^.])(@\w+)(\.\w+(\.\w+)?[^.\W])$/;
33
public static readonly PASSWORD = /^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=.\-_*])([a-zA-Z0-9@#$%^&+=*.\-_]){5,40}$/;
4+
public static readonly TEXT = /^([a-zA-ZÀ-ú]+(\s?[a-zA-ZÀ-ú])){2,20}$/;
45
}

src/app/features/settings/components/components.module.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ import { NgModule } from '@angular/core';
22
import { CommonModule } from '@angular/common';
33

44
import { UserFormComponent } from './user-form/user-form.component';
5+
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
56

67
@NgModule({
78
declarations: [
89
UserFormComponent
910
],
1011
imports: [
11-
CommonModule
12+
CommonModule,
13+
FormsModule,
14+
ReactiveFormsModule,
1215
],
1316
exports: [
1417
UserFormComponent
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
/* label.floating span {
2-
top: -24px;
3-
transform: translateY(17px);
4-
}
5-
6-
label.floating:focus-within > span,
7-
input:not(:placeholder-shown) + span {
8-
transform: translateY(7px);
9-
} */
10-
111
form {
122
margin-top: 10px;
133
display: flex;
144
flex-direction: column;
155
gap: 10px;
166
}
7+
8+
.message {
9+
color: var(--primary-color);
10+
margin-left: 5px;
11+
}
12+
13+
.input-group {
14+
display: flex;
15+
flex-direction: column;
16+
}
Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
1-
<form>
2-
<label for="email-user" class="floating">
3-
<input type="text" id="email-user" placeholder="email">
4-
<span>Email</span>
5-
</label>
6-
<label for="name" class="floating">
7-
<input type="text" id="name" placeholder="email">
8-
<span>Name</span>
9-
</label>
10-
<label for="lastName" class="floating">
11-
<input type="text" id="lastName" placeholder="email">
12-
<span>Lastname</span>
13-
</label>
14-
<button class="btn btn-primary btn-lg">Save</button>
1+
<form [formGroup]="userForm" (submit)="updateUserInfo()">
2+
<div class="input-group">
3+
<label for="email" class="floating">
4+
<input formControlName="email" type="text" id="email" placeholder="email">
5+
<span>Email</span>
6+
</label>
7+
<div *ngIf="validateForm('email')" class="text-error">
8+
<span *ngIf="validateField('email', 'required')" class="error">must provide an email</span>
9+
<span *ngIf="validateField('email', 'pattern')" class="error">email format wrong</span>
10+
</div>
11+
</div>
12+
<div class="input-group">
13+
<label for="name" class="floating">
14+
<input formControlName="name" type="text" id="name" placeholder="email">
15+
<span>Name</span>
16+
</label>
17+
<div *ngIf="validateForm('name')" class="text-error">
18+
<span *ngIf="validateField('name', 'required')" class="error">must provide a name</span>
19+
<span *ngIf="validateField('name', 'pattern')" class="error">name format wrong</span>
20+
</div>
21+
</div>
22+
<div class="input-group">
23+
<label for="lastName" class="floating">
24+
<input formControlName="lastName" type="text" id="lastName" placeholder="email">
25+
<span>Lastname</span>
26+
</label>
27+
<div *ngIf="validateForm('lastName')" class="text-error">
28+
<span *ngIf="validateField('lastName', 'pattern')" class="error">lastName format wrong</span>
29+
</div>
30+
</div>
31+
<span class="message" *ngIf="message">{{message}}</span>
32+
<button class="btn btn-primary btn-lg" type="submit">Save</button>
1533
</form>

0 commit comments

Comments
 (0)