connexion avec back des pages 'advertiser/manageAds' et 'admin/adList'

This commit is contained in:
MiharyR 2021-12-12 19:04:40 +01:00
parent c91fc523aa
commit 0dd39af7ad
8 changed files with 183 additions and 97 deletions

View file

@ -97,9 +97,9 @@
<ng-container matColumnDef="interests"> <ng-container matColumnDef="interests">
<th mat-header-cell *matHeaderCellDef> Sujets </th> <th mat-header-cell *matHeaderCellDef> Sujets </th>
<td mat-cell *matCellDef="let advert"> <td mat-cell *matCellDef="let advert">
<span *ngFor="let interest of advert.interests; let isLast = last;"> <span *ngFor="let objectInterest of advert.interests; let isLast = last;">
<span *ngIf="!isLast"> {{interest}}, </span> <span *ngIf="!isLast"> {{objectInterest.interest}}, </span>
<span *ngIf="isLast"> {{interest}} </span> <span *ngIf="isLast"> {{objectInterest.interest}} </span>
</span> </span>
</td> </td>
</ng-container> </ng-container>

View file

@ -10,6 +10,7 @@ import {PopupDeleteAdAdminComponent} from "../popup-delete-ad-admin/popup-delete
import {PopupVisualizeImagesAdminComponent} from "../popup-visualize-images-admin/popup-visualize-images-admin.component"; import {PopupVisualizeImagesAdminComponent} from "../popup-visualize-images-admin/popup-visualize-images-admin.component";
import {FormControl} from "@angular/forms"; import {FormControl} from "@angular/forms";
import {MessageService} from "../../../utils/services/message/message.service"; import {MessageService} from "../../../utils/services/message/message.service";
import {HttpParams} from "@angular/common/http";
@ -66,8 +67,10 @@ export class PageAdListAdminComponent implements AfterViewInit
ngAfterViewInit(): void ngAfterViewInit(): void
{ {
// Ask for ads and then for advertiser // Ask for ads and then for advertiser
let params = new HttpParams();
params = params.append("isActive", true);
this.messageService this.messageService
.get("ad/findAll") .get("ad/findAll", params)
.subscribe(ret => this.afterReceivingAds(ret), err => this.afterReceivingAds(err) ); .subscribe(ret => this.afterReceivingAds(ret), err => this.afterReceivingAds(err) );
// Ask for interest // Ask for interest
@ -79,11 +82,8 @@ export class PageAdListAdminComponent implements AfterViewInit
afterReceivingAds(retour: any): void afterReceivingAds(retour: any): void
{ {
console.log("afterReceivingAds");
console.log(retour);
if(retour.status !== "success") { if(retour.status !== "success") {
//console.log(retour); console.log(retour);
} }
else { else {
const tabAdvert = retour.data; const tabAdvert = retour.data;
@ -96,11 +96,8 @@ export class PageAdListAdminComponent implements AfterViewInit
afterReceivingAdvertiser(retour: any, tabAdvert): void afterReceivingAdvertiser(retour: any, tabAdvert): void
{ {
console.log("afterReceivingAdvertiser");
console.log(retour);
if(retour.status !== "success") { if(retour.status !== "success") {
//console.log(retour); console.log(retour);
} }
else { else {
this.tabAdvertiser = retour.data.filter(x => x.role.name === "advertiser"); this.tabAdvertiser = retour.data.filter(x => x.role.name === "advertiser");
@ -113,14 +110,10 @@ export class PageAdListAdminComponent implements AfterViewInit
afterReceivingInterests(retour: any): void afterReceivingInterests(retour: any): void
{ {
console.log("afterReceivingInterests");
console.log(retour);
if(retour.status !== "success") { if(retour.status !== "success") {
console.log(retour); console.log(retour);
} }
else { else {
this.allInterests = [];
this.allInterests = retour.data.map(x => x.interest); this.allInterests = retour.data.map(x => x.interest);
this.allInterests.sort(); this.allInterests.sort();
} }
@ -136,19 +129,27 @@ export class PageAdListAdminComponent implements AfterViewInit
onVisualizeImages(advert: AdvertWithCountViewsAndCompany) onVisualizeImages(advert: AdvertWithCountViewsAndCompany)
{ {
const config = { if(advert.images.length !== 0)
width: '30%', {
height: '90%', const config = {
data: { width: '30%',
images: advert.images, height: '90%',
width: 300, data: {
height: 800, images: advert.images,
} width: 300,
}; height: 800,
this.dialog }
.open(PopupVisualizeImagesAdminComponent, config) };
.afterClosed() this.dialog
.subscribe(retour => {}); .open(PopupVisualizeImagesAdminComponent, config)
.afterClosed()
.subscribe(retour => {});
}
else {
const config = { duration: 2000, panelClass: "custom-class" };
const message = "Cette annonce ne contient aucune image" ;
this.snackBar.open( message, "", config);
}
} }

View file

@ -29,7 +29,7 @@ export class PopupDeleteAdAdminComponent implements OnInit
{ {
this.messageService this.messageService
.delete("ad/delete/"+this.advert.id) .delete("ad/delete/"+this.advert.id)
.subscribe(ret => this.onValidateCallback(ret), err => this.onValidateCallback(err) ); .subscribe(ret => this.onValidateCallback(ret), err => this.onValidateCallback(err));
} }
@ -40,6 +40,8 @@ export class PopupDeleteAdAdminComponent implements OnInit
this.dialogRef.close(); this.dialogRef.close();
} }
else { else {
console.log("suppr");
console.log(retour);
this.dialogRef.close(true); this.dialogRef.close(true);
} }
} }

View file

@ -38,9 +38,18 @@ export class InputInterestsAdComponent implements OnInit
startWith(null), startWith(null),
map((fruit: string | null) => fruit ? this._filter(fruit) : this.allTags.slice())); map((fruit: string | null) => fruit ? this._filter(fruit) : this.allTags.slice()));
// --- FAUX CODE --- this.messageService
this.allTags = this.fictitiousUtilsService.getTags(); .get("misc/getInterests")
this.allTags.sort(); .subscribe( retour => {
if(retour.status !== "success") {
console.log(retour);
}
else {
this.allTags = retour.data.map(x => x.interest)
this.allTags.sort();
}
});
} }

View file

@ -12,6 +12,8 @@ import {PopupVisualizeImagesAdvertiserComponent} from "../popup-visualize-images
import {FictitiousAdvertsService} from "../../../utils/services/fictitiousDatas/fictitiousAdverts/fictitious-adverts.service"; import {FictitiousAdvertsService} from "../../../utils/services/fictitiousDatas/fictitiousAdverts/fictitious-adverts.service";
import {FormControl} from "@angular/forms"; import {FormControl} from "@angular/forms";
import {FictitiousUtilsService} from "../../../utils/services/fictitiousDatas/fictitiousUtils/fictitious-utils.service"; import {FictitiousUtilsService} from "../../../utils/services/fictitiousDatas/fictitiousUtils/fictitious-utils.service";
import {MessageService} from "../../../utils/services/message/message.service";
import {HttpParams} from "@angular/common/http";
@ -24,7 +26,7 @@ export class PageAdListAdvertiserComponent implements AfterViewInit
{ {
displayedColumns: string[] = [ 'isVisible', 'title', 'interests', 'createdAt', 'updatedAt', 'countViews', 'actions' ]; displayedColumns: string[] = [ 'isVisible', 'title', 'interests', 'createdAt', 'updatedAt', 'countViews', 'actions' ];
tabAdvertWithCountViews: AdvertWithCountViews[] = []; tabAdvertWithCountViews: AdvertWithCountViews[] = [];
dataSource ; dataSource;
@ViewChild(MatSort) sort: MatSort; @ViewChild(MatSort) sort: MatSort;
@ViewChild(MatPaginator) paginator: MatPaginator; @ViewChild(MatPaginator) paginator: MatPaginator;
@ -33,6 +35,8 @@ export class PageAdListAdvertiserComponent implements AfterViewInit
startDate: Date = null; startDate: Date = null;
endDate: Date = null; endDate: Date = null;
formControlInterests = new FormControl(); formControlInterests = new FormControl();
allVideoCategorie = [];
allInterests: string[] = []; allInterests: string[] = [];
@ -40,18 +44,50 @@ export class PageAdListAdvertiserComponent implements AfterViewInit
private fictitiousAdvertsService: FictitiousAdvertsService, private fictitiousAdvertsService: FictitiousAdvertsService,
private fictitiousUtilsService: FictitiousUtilsService, private fictitiousUtilsService: FictitiousUtilsService,
public dialog: MatDialog, public dialog: MatDialog,
private snackBar: MatSnackBar ) { } private snackBar: MatSnackBar,
private messageService: MessageService ) { }
ngAfterViewInit(): void ngAfterViewInit(): void
{ {
// --- FAUX CODE --- // Ask interests
const tabAdvert = this.fictitiousAdvertsService.getTabAdvert(8); this.messageService
this.allInterests = this.fictitiousUtilsService.getTags(); .get("misc/getInterests")
.subscribe(ret => this.afterReceivingInterests(ret), err => this.afterReceivingInterests(err) );
for(let advert of tabAdvert) this.tabAdvertWithCountViews.push(this.advertToAdvertWithCountViews(advert)); // Ask ads
this.dataSource = new MatTableDataSource<Advert>(); let params = new HttpParams();
this.onFilter(); params = params.append("isActive", true);
this.messageService
.get("ad/findAll", params)
.subscribe(ret => this.afterReceivingAds(ret), err => this.afterReceivingAds(err));
}
afterReceivingInterests(retour: any): void
{
if(retour.status !== "success") {
console.log("afterReceivingInterests");
console.log(retour);
}
else {
this.allVideoCategorie = retour.data;
this.allInterests = retour.data.map(x => x.interest);
this.allInterests.sort();
}
}
afterReceivingAds(retour: any): void
{
if(retour.status !== "success") {
console.log(retour);
}
else {
for(let advert of retour.data) this.tabAdvertWithCountViews.push(this.advertToAdvertWithCountViews(advert));
this.dataSource = new MatTableDataSource<AdvertWithCountViews>();
this.onFilter();
}
} }
@ -86,7 +122,7 @@ export class PageAdListAdvertiserComponent implements AfterViewInit
width: '75%', width: '75%',
height: '80%', height: '80%',
panelClass: 'custom-dialog-container', panelClass: 'custom-dialog-container',
data: { action: "add", advert: null } data: { action: "add", advert: null, allVideoCategorie: this.allVideoCategorie }
}; };
this.dialog this.dialog
.open(PopupAddOrUpdateAdComponent, config) .open(PopupAddOrUpdateAdComponent, config)
@ -99,10 +135,9 @@ export class PageAdListAdvertiserComponent implements AfterViewInit
message = "Opération annulée" ; message = "Opération annulée" ;
} }
else { else {
this.dataSource.data.push(advertAdded); this.tabAdvertWithCountViews.push(this.advertToAdvertWithCountViews(advertAdded));
this.dataSource.data = this.dataSource.data; this.onFilter();
this.dataSource = this.dataSource; message = "L'annoonce a bien été ajoutée ✔" ;
message = "L'annoonce a bien été ajoutée ✔"
} }
this.snackBar.open( message, "", config); this.snackBar.open( message, "", config);
}); });
@ -115,7 +150,7 @@ export class PageAdListAdvertiserComponent implements AfterViewInit
width: '75%', width: '75%',
height: '80%', height: '80%',
panelClass: 'custom-dialog-container', panelClass: 'custom-dialog-container',
data: { action: "update", advert: advertToUpdate } data: { action: "update", advert: advertToUpdate, allVideoCategorie: this.allVideoCategorie }
}; };
this.dialog this.dialog
.open(PopupAddOrUpdateAdComponent, config) .open(PopupAddOrUpdateAdComponent, config)
@ -128,11 +163,10 @@ export class PageAdListAdvertiserComponent implements AfterViewInit
message = "Opération annulée" ; message = "Opération annulée" ;
} }
else { else {
const index = this.dataSource.data.findIndex( elt => (elt._id === advertToUpdate._id)); const index = this.tabAdvertWithCountViews.findIndex(elt => (elt.id === advertToUpdate.id));
this.dataSource.data.splice(index, 1, advertUpdated); this.tabAdvertWithCountViews.splice(index, 1, this.advertToAdvertWithCountViews(advertUpdated));
this.dataSource.data = this.dataSource.data; this.onFilter();
this.dataSource = this.dataSource; message = "L'annonce a bien été modifiée ✔" ;
message = "L'annonce a bien été modifiée ✔"
} }
this.snackBar.open( message, "", config); this.snackBar.open( message, "", config);
}); });
@ -155,7 +189,7 @@ export class PageAdListAdvertiserComponent implements AfterViewInit
message = "Opération annulée" ; message = "Opération annulée" ;
} }
else { else {
const index = this.dataSource.data.findIndex( elt => (elt._id === advert._id)); const index = this.dataSource.data.findIndex( elt => (elt.id === advert.id));
this.dataSource.data.splice(index, 1); this.dataSource.data.splice(index, 1);
this.dataSource.data = this.dataSource.data; this.dataSource.data = this.dataSource.data;
this.dataSource = this.dataSource; this.dataSource = this.dataSource;
@ -168,6 +202,7 @@ export class PageAdListAdvertiserComponent implements AfterViewInit
onFilter(): void onFilter(): void
{ {
if(this.dataSource === null || this.dataSource === undefined) this.dataSource = new MatTableDataSource();
this.dataSource.data = []; this.dataSource.data = [];
for(let advert of this.tabAdvertWithCountViews) for(let advert of this.tabAdvertWithCountViews)
{ {
@ -220,21 +255,30 @@ export class PageAdListAdvertiserComponent implements AfterViewInit
} }
onSliderIsVisible(advert: Advert): void onSliderIsVisible(advert: any): void
{ {
// il faut envoyer la négation de user.isActive // il faut envoyer la négation de user.isActive
this.messageService
.put("ad/update/"+advert.id, { isVisible: !advert.isVisible })
.subscribe(
ret => {},
err => {
console.log("onSliderIsVisible");
console.log(err);
}
);
} }
advertToAdvertWithCountViews(advert: Advert): AdvertWithCountViews advertToAdvertWithCountViews(advert): AdvertWithCountViews
{ {
return { return {
_id: advert._id, id: advert.id,
userId: advert.userId, userId: advert.userId,
title: advert.title, title: advert.title,
url: advert.url, url: advert.url,
images: advert.images, images: advert.images,
interests: advert.interests, interests: advert.interests.map(x => x.interest),
comment: advert.comment, comment: advert.comment,
views: advert.views, views: advert.views,
countViews: advert.views.length, countViews: advert.views.length,

View file

@ -28,9 +28,9 @@ const ADVERT_VIDE: Advert = {
}) })
export class PopupAddOrUpdateAdComponent implements OnInit export class PopupAddOrUpdateAdComponent implements OnInit
{ {
advert: Advert; advert: any;
urlBackend: string = "" ;
title: string = "" ; title: string = "" ;
allVideoCategorie = [];
tabWaitingFile: File[] = []; // fichiers selectionnés mais pas encore "validés" tabWaitingFile: File[] = []; // fichiers selectionnés mais pas encore "validés"
tabSelectedFile: File[] = []; // fichier "validés" tabSelectedFile: File[] = []; // fichier "validés"
_event; _event;
@ -44,18 +44,17 @@ export class PopupAddOrUpdateAdComponent implements OnInit
ngOnInit(): void ngOnInit(): void
{ {
this.allVideoCategorie = this.data.allVideoCategorie
if(this.data.action === "add") if(this.data.action === "add")
{ {
this.advert = Object.assign({}, ADVERT_VIDE); this.advert = Object.assign({}, ADVERT_VIDE);
this.advert.interests = []; this.advert.interests = [];
this.urlBackend = "url/add/ad" ;
this.title = "Ajouter annonce" ; this.title = "Ajouter annonce" ;
} }
else else
{ {
this.advert = Object.assign({}, this.data.advert); this.advert = Object.assign({}, this.data.advert);
this.advert.interests = this.data.advert.interests.slice(); this.advert.interests = this.data.advert.interests.slice();
this.urlBackend = "url/update/ad" ;
this.title = "Modifier annonce" ; this.title = "Modifier annonce" ;
} }
} }
@ -63,24 +62,59 @@ export class PopupAddOrUpdateAdComponent implements OnInit
onValidate(): void onValidate(): void
{ {
// --- FAUX CODE --- // On transforme 'this.user.interests' en tableau de 'videoCategorie'
this.dialogRef.close(this.advert); let interests = []; // tableau de videoCategorie
for(let interest of this.advert.interests)
// --- VRAI CODE --- {
/* for(let videoCategorie of this.allVideoCategorie)
this.messageService {
.sendMessage(this.urlBackend, this.advert) if(videoCategorie.interest === interest) {
.subscribe( retour => { interests.push(videoCategorie);
break;
if(retour.status === "error") {
console.log(retour);
this.dialogRef.close(this.advert);
} }
else { }
this.dialogRef.close(retour.data.advert); }
} this.advert.interests = interests;
});
*/ if(this.data.action === "add")
{
this.messageService
.post("ad/create", this.advert)
.subscribe(ret => this.onCreateCallback(ret), err => this.onCreateCallback(err));
}
else {
const id = this.advert.id;
Reflect.deleteProperty(this.advert, "id");
Reflect.deleteProperty(this.advert, "_id");
this.messageService
.put("ad/update/"+id, this.advert)
.subscribe(ret => this.onUpdateCallback(ret,id), err => this.onUpdateCallback(err,id));
}
}
onCreateCallback(retour: any): void
{
if(retour.status !== "success") {
console.log(retour);
this.dialogRef.close();
}
else {
this.dialogRef.close(retour.data);
}
}
onUpdateCallback(retour: any, id: string): void
{
if(retour.status !== "success") {
console.log(retour);
this.dialogRef.close();
}
else {
this.advert.id = id;
this.dialogRef.close(this.advert);
}
} }

View file

@ -1,7 +1,6 @@
import {Component, Inject, OnInit} from '@angular/core'; import {Component, Inject, OnInit} from '@angular/core';
import {MAT_DIALOG_DATA, MatDialogRef} from "@angular/material/dialog"; import {MAT_DIALOG_DATA, MatDialogRef} from "@angular/material/dialog";
import {MessageService} from "../../../utils/services/message/message.service"; import {MessageService} from "../../../utils/services/message/message.service";
import {Advert} from "../../../utils/interfaces/advert";
@ -12,7 +11,7 @@ import {Advert} from "../../../utils/interfaces/advert";
}) })
export class PopupDeleteAdAdvertiserComponent implements OnInit export class PopupDeleteAdAdvertiserComponent implements OnInit
{ {
advert: Advert; advert: any;
constructor( public dialogRef: MatDialogRef<PopupDeleteAdAdvertiserComponent>, constructor( public dialogRef: MatDialogRef<PopupDeleteAdAdvertiserComponent>,
@ -28,24 +27,21 @@ export class PopupDeleteAdAdvertiserComponent implements OnInit
onValidate(): void onValidate(): void
{ {
// --- FAUX CODE --- this.messageService
this.dialogRef.close(true); .delete("ad/delete/"+this.advert.id)
.subscribe(ret => this.onValidateCallback(ret), err => this.onValidateCallback(err));
}
// --- VRAI CODE ---
/*
this.messageService
.sendMessage("url/delete/ad", {"advert": this.advert})
.subscribe( retour => {
if(retour.status === "error") { onValidateCallback(retour: any): void
console.log(retour); {
this.dialogRef.close(); if(retour.status !== "success") {
} console.log(retour);
else { this.dialogRef.close();
this.dialogRef.close(true); }
} else {
}); this.dialogRef.close(true);
*/ }
} }
} }

View file

@ -20,7 +20,7 @@ export interface Advert
export interface AdvertWithCountViews { export interface AdvertWithCountViews {
_id: string, id: string,
userId: string, userId: string,
title: string, title: string,
url: string, url: string,