connexion avec back de la page 'admin/userList'
This commit is contained in:
parent
fd722f613d
commit
c91fc523aa
4 changed files with 87 additions and 61 deletions
|
|
@ -8,11 +8,7 @@ import {MatTableDataSource} from "@angular/material/table";
|
|||
import {Advert} from "../../../utils/interfaces/advert";
|
||||
import {PopupDeleteAdAdminComponent} from "../popup-delete-ad-admin/popup-delete-ad-admin.component";
|
||||
import {PopupVisualizeImagesAdminComponent} from "../popup-visualize-images-admin/popup-visualize-images-admin.component";
|
||||
import {FictitiousAdvertsService} from "../../../utils/services/fictitiousDatas/fictitiousAdverts/fictitious-adverts.service";
|
||||
import {FormControl} from "@angular/forms";
|
||||
import {FictitiousUtilsService} from "../../../utils/services/fictitiousDatas/fictitiousUtils/fictitious-utils.service";
|
||||
import {User} from "../../../utils/interfaces/user";
|
||||
import {FictitiousUsersService} from "../../../utils/services/fictitiousDatas/fictitiousUsers/fictitious-users.service";
|
||||
import {MessageService} from "../../../utils/services/message/message.service";
|
||||
|
||||
|
||||
|
|
@ -62,9 +58,6 @@ export class PageAdListAdminComponent implements AfterViewInit
|
|||
|
||||
|
||||
constructor( public themeService: ThemeService,
|
||||
private fictitiousAdvertsService: FictitiousAdvertsService,
|
||||
private fictitiousUtilsService: FictitiousUtilsService,
|
||||
private fictitiousUsersService: FictitiousUsersService,
|
||||
public dialog: MatDialog,
|
||||
private snackBar: MatSnackBar,
|
||||
private messageService: MessageService) { }
|
||||
|
|
@ -72,25 +65,15 @@ export class PageAdListAdminComponent implements AfterViewInit
|
|||
|
||||
ngAfterViewInit(): void
|
||||
{
|
||||
// --- FAUX CODE ---
|
||||
/*
|
||||
const tabAdvert = this.fictitiousAdvertsService.getTabAdvert(8);
|
||||
this.allInterests = this.fictitiousUtilsService.getTags();
|
||||
this.tabAdvertiser = this.fictitiousUsersService.getTabAdvertiser(3);
|
||||
for(let advert of tabAdvert) this.tabAdvertWithCountViews.push(this.advertToAdvertWithCountViewsAndCompany(advert));
|
||||
this.dataSource = new MatTableDataSource<Advert>();
|
||||
this.onFilter();
|
||||
*/
|
||||
// Ask for ads and then for advertiser
|
||||
this.messageService
|
||||
.get("ad/findAll")
|
||||
.subscribe(ret => this.afterReceivingAds(ret), err => this.afterReceivingAds(err) );
|
||||
|
||||
// Ask for interest
|
||||
this.messageService
|
||||
.get("misc/getInterests")
|
||||
.subscribe(ret => this.afterReceivingInterests(ret), err => this.afterReceivingInterests(err) );
|
||||
|
||||
// Ask for ads
|
||||
this.messageService
|
||||
.get("ad/findAll")
|
||||
.subscribe(ret => this.afterReceivingAds(ret), err => this.afterReceivingAds(err) );
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -93,7 +93,8 @@
|
|||
<mat-icon>power_settings_new</mat-icon>
|
||||
</th>
|
||||
<td mat-cell *matCellDef="let user">
|
||||
<mat-slide-toggle [(ngModel)]="user.isActive" (click)="onSliderIsActive(user)"></mat-slide-toggle>
|
||||
<mat-slide-toggle *ngIf="user.role.name !== 'superAdmin'" [(ngModel)]="user.isActive" (click)="onSliderIsActive(user)"></mat-slide-toggle>
|
||||
<mat-slide-toggle *ngIf="user.role.name === 'superAdmin'" [(ngModel)]="user.isActive" disabled></mat-slide-toggle>
|
||||
</td>
|
||||
</ng-container>
|
||||
|
||||
|
|
|
|||
|
|
@ -5,10 +5,9 @@ import {ThemeService} from "../../../utils/services/theme/theme.service";
|
|||
import {MatDialog} from "@angular/material/dialog";
|
||||
import {MatSnackBar} from "@angular/material/snack-bar";
|
||||
import {MatTableDataSource} from "@angular/material/table";
|
||||
import {User} from "../../../utils/interfaces/user";
|
||||
import {PopupDeleteUserComponent} from "../popup-delete-user/popup-delete-user.component";
|
||||
import {PopupCreateUserComponent} from "../popup-create-user/popup-create-user.component";
|
||||
import {FictitiousUsersService} from "../../../utils/services/fictitiousDatas/fictitiousUsers/fictitious-users.service";
|
||||
import {MessageService} from "../../../utils/services/message/message.service";
|
||||
|
||||
|
||||
|
||||
|
|
@ -25,8 +24,8 @@ export class PageUserListComponent implements AfterViewInit
|
|||
displayedColumnsAdmin: string[] = [ 'isActive', 'login', 'email', 'createdAt', 'lastConnexion' ];
|
||||
|
||||
tabUser: any[] = [];
|
||||
tabAdvertiser: User[] = [];
|
||||
tabAdmin: User[] = [];
|
||||
tabAdvertiser: any[] = [];
|
||||
tabAdmin: any[] = [];
|
||||
|
||||
roleName: string = "user" ;
|
||||
dataSource ;
|
||||
|
|
@ -40,22 +39,37 @@ export class PageUserListComponent implements AfterViewInit
|
|||
|
||||
|
||||
constructor( public themeService: ThemeService,
|
||||
private fictitiousUsersService: FictitiousUsersService,
|
||||
public dialog: MatDialog,
|
||||
private snackBar: MatSnackBar ) { }
|
||||
private snackBar: MatSnackBar,
|
||||
private messageService: MessageService ) { }
|
||||
|
||||
|
||||
ngAfterViewInit(): void
|
||||
{
|
||||
// --- FAUX CODE ---
|
||||
this.tabUser = this.fictitiousUsersService.getTabUser(32);
|
||||
this.tabAdvertiser = this.fictitiousUsersService.getTabAdvertiser(8);
|
||||
this.tabAdmin = this.fictitiousUsersService.getTabAdmin(4);
|
||||
this.messageService
|
||||
.get("user/findAll")
|
||||
.subscribe(ret => this.ngAfterViewInitCallback(ret), err => this.ngAfterViewInitCallback(err));
|
||||
}
|
||||
|
||||
for(const user of this.tabUser) user.age = this.getAge(user.dateOfBirth);
|
||||
|
||||
ngAfterViewInitCallback(retour: any): void
|
||||
{
|
||||
if(retour.status !== "success") {
|
||||
console.log(retour);
|
||||
}
|
||||
else {
|
||||
for(let person of retour.data)
|
||||
{
|
||||
if(person.role.name === "user") {
|
||||
person["age"] = this.getAge(person.dateOfBirth);
|
||||
this.tabUser.push(person);
|
||||
}
|
||||
else if(person.role.name === "advertiser") this.tabAdvertiser.push(person);
|
||||
else this.tabAdmin.push(person);
|
||||
}
|
||||
this.onFilter();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
applyFilter(event: Event): void
|
||||
|
|
@ -65,7 +79,7 @@ export class PageUserListComponent implements AfterViewInit
|
|||
}
|
||||
|
||||
|
||||
onDelete(user: User): void
|
||||
onDelete(user: any): void
|
||||
{
|
||||
const config = {
|
||||
data: { user: user }
|
||||
|
|
@ -81,7 +95,7 @@ export class PageUserListComponent implements AfterViewInit
|
|||
message = "Opération annulée" ;
|
||||
}
|
||||
else {
|
||||
const index = this.dataSource.data.findIndex( elt => (elt._id === user._id));
|
||||
const index = this.dataSource.data.findIndex( elt => (elt.id === user.id));
|
||||
this.dataSource.data.splice(index, 1);
|
||||
this.dataSource.data = this.dataSource.data;
|
||||
this.dataSource = this.dataSource;
|
||||
|
|
@ -106,20 +120,47 @@ export class PageUserListComponent implements AfterViewInit
|
|||
}
|
||||
else {
|
||||
this.snackBar.open( "L'utilisateur a bien été créé", "", config);
|
||||
if(retour.role.name === "user") this.tabUser.push(retour);
|
||||
else if(retour.role.name === "advertiser") this.tabAdvertiser.push(retour);
|
||||
else if(retour.role.name === "admin") this.tabAdmin.push(retour);
|
||||
this.onFilter();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
onSliderIsActive(user: User): void
|
||||
onSliderIsActive(user: any): void
|
||||
{
|
||||
// il faut envoyer la négation de user.isActive
|
||||
this.messageService
|
||||
.put("user/update/"+user.id, { isActive: !user.isActive })
|
||||
.subscribe(
|
||||
ret => {},
|
||||
err => {
|
||||
console.log("onSliderIsActive");
|
||||
console.log(err);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
onSlideIsAccepted(user: User): void
|
||||
onSlideIsAccepted(user: any): void
|
||||
{
|
||||
// il faut envoyer la négation de user.isActive
|
||||
// il faut envoyer la négation de user.role.isAccepted
|
||||
const role0 = {
|
||||
name: user.role.name,
|
||||
permission: user.role.permission,
|
||||
isAccepted: !user.role.isAccepted,
|
||||
};
|
||||
this.messageService
|
||||
.put("user/update/"+user.id, {role: role0})
|
||||
.subscribe(
|
||||
ret => {},
|
||||
err => {
|
||||
console.log("onSlideIsAccepted");
|
||||
console.log(err);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -127,7 +168,7 @@ export class PageUserListComponent implements AfterViewInit
|
|||
{
|
||||
if((date === null) || (date === undefined)) return -1;
|
||||
else {
|
||||
const diff = Date.now() - date.getTime();
|
||||
const diff = Date.now() - (new Date(date)).getTime();
|
||||
const age = new Date(diff);
|
||||
return Math.abs(age.getUTCFullYear() - 1970);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import {Component, Inject, OnInit} from '@angular/core';
|
||||
import {MAT_DIALOG_DATA, MatDialogRef} from "@angular/material/dialog";
|
||||
import {User} from "../../../utils/interfaces/user";
|
||||
import {MessageService} from "../../../utils/services/message/message.service";
|
||||
|
||||
|
||||
|
||||
|
|
@ -11,7 +11,7 @@ import {User} from "../../../utils/interfaces/user";
|
|||
})
|
||||
export class PopupCreateUserComponent implements OnInit
|
||||
{
|
||||
user: User;
|
||||
user: any;
|
||||
hasError: boolean = false;
|
||||
errorMessage: string = "";
|
||||
password: string = "";
|
||||
|
|
@ -19,7 +19,8 @@ export class PopupCreateUserComponent implements OnInit
|
|||
|
||||
|
||||
constructor( public dialogRef: MatDialogRef<PopupCreateUserComponent>,
|
||||
@Inject(MAT_DIALOG_DATA) public data ) { }
|
||||
@Inject(MAT_DIALOG_DATA) public data,
|
||||
private messageService: MessageService ) { }
|
||||
|
||||
|
||||
// Initialise l'utilisateur qui va être créé
|
||||
|
|
@ -52,12 +53,25 @@ export class PopupCreateUserComponent implements OnInit
|
|||
onEnregistrer(): void
|
||||
{
|
||||
this.checkField();
|
||||
|
||||
if(!this.hasError)
|
||||
{
|
||||
this.user.hashPass = this.hashage(this.password);
|
||||
this.dialogRef.close(this.user);
|
||||
// VRAI CODE ...
|
||||
this.user.hashPass = this.password;
|
||||
this.user.role = this.user.role.name;
|
||||
this.messageService
|
||||
.post("user/create", this.user)
|
||||
.subscribe(ret => this.onEnregistrerCallback(ret), err => this.onEnregistrerCallback(err));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Callback de 'onEnregistrer'
|
||||
onEnregistrerCallback(retour: any): void
|
||||
{
|
||||
if(retour.status !== "success") {
|
||||
console.log(retour);
|
||||
}
|
||||
else {
|
||||
this.dialogRef.close(retour.data);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -110,17 +124,4 @@ export class PopupCreateUserComponent implements OnInit
|
|||
this.user.interests = myInterets;
|
||||
}
|
||||
|
||||
|
||||
// Fonction de hashage (faible)
|
||||
hashage(input: string): string
|
||||
{
|
||||
let hash = 0;
|
||||
for (let i = 0; i < input.length; i++) {
|
||||
let ch = input.charCodeAt(i);
|
||||
hash = ((hash << 5) - hash) + ch;
|
||||
hash = hash & hash;
|
||||
}
|
||||
return hash.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue