connexion avec le back (non testé)

This commit is contained in:
MiharyR 2022-01-13 14:46:01 +01:00
parent 22f6d316b4
commit 6b9a6d7c73
14 changed files with 215 additions and 130 deletions

View file

@ -66,7 +66,7 @@
</div>
<!-- Deconnexion -->
<button mat-button class="btnDeconnexion" (click)="onDeconnexion()" routerLink="/">
<button mat-button class="btnDeconnexion" (click)="onDeconnexion()" routerLink="/login">
Deconnexion
</button>
@ -100,7 +100,7 @@
</div>
<!-- Deconnexion -->
<button mat-button class="btnDeconnexion" (click)="onDeconnexion()" routerLink="/">
<button mat-button class="btnDeconnexion" (click)="onDeconnexion()" routerLink="/login">
Deconnexion
</button>

View file

@ -1,4 +1,5 @@
import {Component, Input, OnInit} from '@angular/core';
import {ProfilService} from "../../services/profil/profil.service";
@Component({
selector: 'app-navbar',
@ -9,9 +10,14 @@ export class NavbarComponent implements OnInit
{
@Input() pour = "login";
constructor() { }
constructor(private profilService: ProfilService) { }
ngOnInit(): void {}
onDeconnexion(): void {}
onDeconnexion(): void
{
this.profilService.setId(-1);
this.profilService.setIsAdmin(false);
}
}

View file

@ -23,8 +23,8 @@
<div class="row myRow">
<div class="col-6 myLabel">Rôle:</div>
<div class="col-6 myValue">
<span *ngIf="!person.is_admin">utilisateur</span>
<span *ngIf="person.is_admin">admin</span>
<span *ngIf="this.from === 'user'">utilisateur</span>
<span *ngIf="this.from === 'admin'">admin</span>
</div>
<!-- boutons -->

View file

@ -2,9 +2,11 @@ import { Component, OnInit } from '@angular/core';
import {MatDialog} from "@angular/material/dialog";
import {MatSnackBar} from "@angular/material/snack-bar";
import {PopupUpdateProfilComponent} from "../popup-update-profil/popup-update-profil.component";
import {FictitiousDatasService} from "../../services/fictitiousDatas/fictitious-datas.service";
import {Router} from "@angular/router";
import {PopupDeleteProfilComponent} from "../popup-delete-profil/popup-delete-profil.component";
import {MessageService} from "../../services/message/message.service";
import {HttpParams} from "@angular/common/http";
import {ProfilService} from "../../services/profil/profil.service";
@ -19,31 +21,42 @@ export class PageProfilComponent implements OnInit
id: "",
nickname: "",
email: "",
hash_pass: "",
is_admin: false,
};
from: string = "" ;
configSnackbar = { duration: 3000, panelClass: "custom-class" };
constructor( public dialog: MatDialog,
constructor( private messageService: MessageService,
private profilService: ProfilService,
public dialog: MatDialog,
private snackBar: MatSnackBar,
private fictitiousDatasService: FictitiousDatasService,
private router: Router ) { }
ngOnInit(): void
{
// faux code
if(this.router.url.startsWith("/user")) {
this.person = this.fictitiousDatasService.getUser();
this.from = "user" ;
}
else if(this.router.url.startsWith("/admin")){
this.person = this.fictitiousDatasService.getAdmin();
this.from = "admin" ;
}
if(this.router.url.startsWith("/user")) this.from = "user" ;
else if(this.router.url.startsWith("/admin")) this.from = "admin" ;
// Vrai code ...
let params = new HttpParams()
params = params.set("order", "");
params = params.set("id", this.profilService.getId());
this.messageService
.get("user", params)
.subscribe(ret => this.ngOnInitCallback(ret), err => this.ngOnInitCallback(err));
}
// Callback de ngOnInit
ngOnInitCallback(retour: any): void
{
if(retour.status !== "success") {
console.log(retour);
}
else {
this.person = retour.data[0];
}
}
@ -64,15 +77,8 @@ export class PageProfilComponent implements OnInit
// Callback de onModifier
onModifierCallback(retour: any): void
{
if((retour === null) || (retour === undefined))
{
const config = { duration: 1000, panelClass: "custom-class" };
this.snackBar.open( "Opération annulé", "", config);
}
else
{
this.person = retour;
}
if((retour === null) || (retour === undefined)) this.snackBar.open( "Opération annulé", "", this.configSnackbar);
else if(retour.status === "success") this.person = retour.data;
}
@ -96,15 +102,9 @@ export class PageProfilComponent implements OnInit
// Callback de onSupprimer
onSupprimerCallback(retour: any): void
{
if((retour === null) || (retour === undefined))
{
const config = { duration: 1000, panelClass: "custom-class" };
this.snackBar.open( "Opération annulé", "", config);
}
else
{
this.router.navigateByUrl("/login");
}
if((retour === null) || (retour === undefined)) this.snackBar.open( "Opération annulé", "", this.configSnackbar);
else if(retour.status === "error") this.snackBar.open(retour.message, "", this.configSnackbar);
else if(retour.status === "success") this.router.navigateByUrl("/login");
}
}

View file

@ -1,5 +1,7 @@
import {Component, Inject, OnInit} from '@angular/core';
import {MAT_DIALOG_DATA, MatDialogRef} from "@angular/material/dialog";
import {MessageService} from "../../services/message/message.service";
import {HttpParams} from "@angular/common/http";
@ -10,19 +12,58 @@ import {MAT_DIALOG_DATA, MatDialogRef} from "@angular/material/dialog";
})
export class PopupDeleteProfilComponent implements OnInit
{
id: number;
me: boolean = false; // on se supprime soi-même
email: string = "";
constructor( public dialogRef: MatDialogRef<PopupDeleteProfilComponent>,
constructor( private messageService: MessageService,
public dialogRef: MatDialogRef<PopupDeleteProfilComponent>,
@Inject(MAT_DIALOG_DATA) public data: any ) { }
ngOnInit(): void {
this.id = this.data.id;
this.me = this.data.me;
this.email = this.data.email;
}
onValider(): void {
this.dialogRef.close(true);
// Appuie sur 'valider'
onValider(): void
{
if(this.me)
{
this.messageService
.delete("user/delete")
.subscribe(ret => this.onValiderCallback(ret), err => this.onValiderCallback(err));
}
else {
let params = new HttpParams();
params = params.set("id", this.id);
this.messageService
.delete("admin/delete", params)
.subscribe(ret => this.onValiderCallback(ret), err => this.onValiderCallback(err));
}
}
// Callback de onValider
onValiderCallback(retour: any): void
{
if(retour.status === "success")
{
this.dialogRef.close(retour);
}
else if(retour.status === "error")
{
console.log(retour);
this.dialogRef.close(retour);
}
else {
console.log(retour);
this.dialogRef.close(null);
}
}
}

View file

@ -1,7 +1,7 @@
import {Component, Inject, OnInit} from '@angular/core';
import {MAT_DIALOG_DATA, MatDialogRef} from "@angular/material/dialog";
import {CheckEmailService} from "../../services/checkEmail/check-email.service";
import {HashageService} from "../../services/hashage/hashage.service";
import {MessageService} from "../../services/message/message.service";
@ -20,10 +20,10 @@ export class PopupUpdateProfilComponent implements OnInit
errorMessage: string = "" ;
constructor( public dialogRef: MatDialogRef<PopupUpdateProfilComponent>,
@Inject(MAT_DIALOG_DATA) public data: any,
private checkEmailService: CheckEmailService,
private hashageService: HashageService ) { }
constructor( private checkEmailService: CheckEmailService,
private messageService: MessageService,
public dialogRef: MatDialogRef<PopupUpdateProfilComponent>,
@Inject(MAT_DIALOG_DATA) public data: any ) { }
ngOnInit(): void
@ -33,7 +33,6 @@ export class PopupUpdateProfilComponent implements OnInit
id: person.id,
nickname: person.nickname,
email: person.email,
hash_pass: person.hash_pass,
is_admin: person.is_admin
};
}
@ -45,13 +44,14 @@ export class PopupUpdateProfilComponent implements OnInit
this.checkField();
if(!this.hasError)
{
if(this.changePassword) this.personCopy.hash_pass = this.hashageService.run(this.newPassword);
const data = { user: this.personCopy };
// ...
// Faux code
this.onValiderCallback({ status: "success"});
let data: any = {nickname: this.personCopy.nickname};
if(this.changePassword) data = {
nickname: this.personCopy.nickname,
password: this.newPassword
};
this.messageService
.put("user/update", data)
.subscribe(ret => this.onValiderCallback(ret), err => this.onValiderCallback(err));
}
}
@ -59,14 +59,19 @@ export class PopupUpdateProfilComponent implements OnInit
// Callback de 'onValider'
onValiderCallback(retour: any)
{
if(retour.status === 'error')
if(retour.status === "success")
{
this.dialogRef.close(retour);
}
else if(retour.status === "error")
{
console.log(retour);
this.dialogRef.close(null);
this.errorMessage = retour.message;
this.hasError = true;
}
else
{
this.dialogRef.close(this.personCopy);
else {
console.log(retour);
this.dialogRef.close(null);
}
}

View file

@ -29,7 +29,7 @@ export class MessageService
return this.http.put<any>(urlComplete, data, {withCredentials: true});
}
delete(url: string): Observable<any>
delete(url: string, params:HttpParams = new HttpParams()): Observable<any>
{
const urlComplete = environment.debutUrl + url ;
return this.http.delete<any>(urlComplete,{withCredentials: true});

View file

@ -6,10 +6,16 @@ import { Injectable } from '@angular/core';
export class ProfilService
{
getId(): number | null
constructor()
{
this.setId(-1);
this.setIsAdmin(false);
}
getId(): number
{
let idString = localStorage.getItem('id');
if(idString === null) return null;
if(idString === null) return -1;
else return parseInt(idString);
}