diff --git a/src/app/form/form.component.ts b/src/app/form/form.component.ts index 3e931bedada2803a234a7ab0b89ca453f05fdc6f..93ab67f337a7341b780192c3fc491270853aeb2e 100644 --- a/src/app/form/form.component.ts +++ b/src/app/form/form.component.ts @@ -253,13 +253,11 @@ export class FormComponent implements OnInit { } public onSubmitClaimWithAccount(): void { - this.structureService - .claimStructureWithAccount(this.structureId, this.profile.email) - .subscribe((structuresLinked) => { - this.profile.pendingStructuresLink = structuresLinked; - this.profileService.setProfile(this.profile); - this.closeEvent.emit(this.structureForm.value); - }); + this.structureService.claimStructureWithAccount(this.structureId, this.profile).subscribe((structuresLinked) => { + this.profile.pendingStructuresLink = structuresLinked; + this.profileService.setProfile(this.profile); + this.closeEvent.emit(this.structureForm.value); + }); } public onSubmit(structureForm: FormGroup): void { if (structureForm.valid) { diff --git a/src/app/profile/services/profile.service.ts b/src/app/profile/services/profile.service.ts index 87c91ca9f988eaec479661528e1a70c3e0d5e96b..79abef7df3942504935f52044c66b875f6ba384c 100644 --- a/src/app/profile/services/profile.service.ts +++ b/src/app/profile/services/profile.service.ts @@ -4,6 +4,7 @@ import { Observable } from 'rxjs'; import { User } from '../../models/user.model'; import decode from 'jwt-decode'; import { UserRole } from '../../shared/enum/userRole.enum'; +import { AuthService } from '../../services/auth.service'; @Injectable({ providedIn: 'root', @@ -11,7 +12,7 @@ import { UserRole } from '../../shared/enum/userRole.enum'; export class ProfileService { private readonly baseUrl = 'api/users'; private currentProfile: User = null; - constructor(private http: HttpClient) { + constructor(private http: HttpClient, private authService: AuthService) { this.getProfile(); } @@ -45,14 +46,15 @@ export class ProfileService { } public isAdmin(): boolean { - const user = JSON.parse(localStorage.getItem('user')); - if (user) { + if (this.authService.isLoggedIn()) { + const user = this.authService.userValue; const token = user.accessToken; // decode the token to get its payload const tokenPayload: User = decode(token); if (tokenPayload.role == UserRole.admin) { return true; } + return false; } return false; } diff --git a/src/app/services/structure.service.ts b/src/app/services/structure.service.ts index e28d78f8f4bacf463e01f4bfdd1ee32a232110fe..5b928a6a5bf877e042e7c336774891d5c0416ce7 100644 --- a/src/app/services/structure.service.ts +++ b/src/app/services/structure.service.ts @@ -35,8 +35,8 @@ export class StructureService { return this.http.get(`${this.baseUrl}/${id}/isClaimed`); } - public claimStructureWithAccount(id: string, email: string): Observable<string[]> { - return this.http.post<any>(`${this.baseUrl}/${id}/claim`, { email }); + public claimStructureWithAccount(id: string, user: User): Observable<string[]> { + return this.http.post<any>(`${this.baseUrl}/${id}/claim`, user); } public getStructure(id: string): Observable<Structure> { diff --git a/src/app/structure-list/components/structure-details/structure-details.component.html b/src/app/structure-list/components/structure-details/structure-details.component.html index 2fe93a904bf62edbc9580e64286d0f6ae5ef39fa..1d401c6c57c590f218a4878767dd8148dfcaaf3b 100644 --- a/src/app/structure-list/components/structure-details/structure-details.component.html +++ b/src/app/structure-list/components/structure-details/structure-details.component.html @@ -12,8 +12,11 @@ <button (click)="claimStructure()">Revendiquer</button> </div> <div fxLayout="row" fxLayoutAlign="center center"> - <button *ngIf="profileService.isLinkedToStructure(structure._id)" (click)="editStructure()"> - Modifier ma structure + <button + *ngIf="profileService.isLinkedToStructure(structure._id) || profileService.isAdmin()" + (click)="editStructure()" + > + Modifier la structure </button> </div> <div fxLayout="row" fxLayoutAlign="end center">