send email to owner, show if nothing is found

This commit is contained in:
Tykayn 2021-05-21 12:31:42 +02:00 committed by tykayn
parent 2c4be3e87a
commit d05c77cb08
6 changed files with 50 additions and 48 deletions

View File

@ -9,11 +9,6 @@
<app-header [appTitle]="appTitle" [appLogo]="appLogo"></app-header> <app-header [appTitle]="appTitle" [appLogo]="appLogo"></app-header>
<main [@routeAnimations]="prepareRoute(outlet)"> <main [@routeAnimations]="prepareRoute(outlet)">
<router-outlet #outlet></router-outlet> <router-outlet #outlet></router-outlet>
<div class="padded" *ngIf="devModeEnabled">
<br />
<mat-slide-toggle (change)="sidenav.toggle()" label="dev menu"> </mat-slide-toggle> menu
développeur
</div>
</main> </main>
<app-footer></app-footer> <app-footer></app-footer>
<app-feedback></app-feedback> <app-feedback></app-feedback>

View File

@ -39,21 +39,27 @@
<form (ngSubmit)="searchMyPolls()"> <form (ngSubmit)="searchMyPolls()">
<div class="search-others"> <div class="search-others">
<label for="search_others"> <label for="search_email">
Je cherche d'autres sondages, qui correspondent à mon mail : Je cherche d'autres sondages, qui correspondent à mon mail :
</label> </label>
<input <input
type="email" type="email"
name="search_email" name="search_email"
id="search_others" id="search_email"
placeholder="moi@example.com" placeholder="moi@example.com"
[ngModel]="storageService.vote_stack.owner.email" [ngClass]="{
'ng-invalid': nonexistent_email === storageService.vote_stack.owner.email
}"
[(ngModel)]="storageService.vote_stack.owner.email"
/> />
<button <button
type="submit" type="submit"
role="button" role="button"
class="button is-outlined is-fullwidth is-info is-size-3" class="button is-outlined is-fullwidth is-info is-size-3"
[disabled]="!storageService.vote_stack.owner.email.length" [disabled]="
!storageService.vote_stack.owner.email.length ||
nonexistent_email === storageService.vote_stack.owner.email
"
routerLink="user/polls" routerLink="user/polls"
> >
<i class="fa fa-search"></i> <i class="fa fa-search"></i>

View File

@ -1,7 +1,9 @@
import { Component } from '@angular/core'; import { Component, Inject } from '@angular/core';
import { environment } from '../../../../environments/environment'; import { environment } from '../../../../environments/environment';
import { StorageService } from '../../services/storage.service'; import { StorageService } from '../../services/storage.service';
import { ApiService } from '../../services/api.service'; import { ApiService } from '../../services/api.service';
import { ToastService } from '../../services/toast.service';
import { DOCUMENT } from '@angular/common';
@Component({ @Component({
selector: 'app-home', selector: 'app-home',
@ -10,9 +12,34 @@ import { ApiService } from '../../services/api.service';
}) })
export class HomeComponent { export class HomeComponent {
public environment = environment; public environment = environment;
constructor(public storageService: StorageService, private api: ApiService) {} public nonexistent_email = '';
constructor(
@Inject(DOCUMENT) private document: any,
public storageService: StorageService,
public toastService: ToastService,
private api: ApiService
) {}
searchMyPolls() { searchMyPolls() {
this.api.sendEmailToUserOfItsPollsList(this.storageService.vote_stack.owner.email); const email = this.storageService.vote_stack.owner.email;
this.api.findMyPollsByEmail(email).then(
(resp) => {
console.log('resp', resp);
if (resp) {
if (resp.data && resp.data.mail_sent == '1') {
this.toastService.display("C'est bon, vérifiez votre boite mail");
}
}
},
(error) => {
if (error.response.status == '404') {
this.toastService.display('Aucun sondage géré par cet email : ' + email);
this.nonexistent_email = email;
this.document.querySelector('#search_email').select();
}
console.log('error', error);
}
);
} }
} }

View File

@ -4,7 +4,7 @@ import { Poll } from './poll.model';
export class Owner { export class Owner {
constructor( constructor(
public pseudo: string = 'pseudo', public pseudo: string = 'pseudo',
public email: string = 'example@example.com', public email: string = '_nonexistent_contact@cipherbliss.com',
public polls: Poll[] = [], public polls: Poll[] = [],
public role?: UserRole, public role?: UserRole,
public token?: string public token?: string

View File

@ -31,7 +31,7 @@ export class ApiService {
private baseHref: string; private baseHref: string;
private static loaderService: LoaderService; private static loaderService: LoaderService;
constructor(private http: HttpClient, private loaderService: LoaderService) { constructor(private http: HttpClient, private toastService: ToastService, private loaderService: LoaderService) {
this.baseHref = apiBaseHref; this.baseHref = apiBaseHref;
this.axiosInstance = axios.create({ baseURL: apiBaseHref }); this.axiosInstance = axios.create({ baseURL: apiBaseHref });
@ -232,28 +232,12 @@ export class ApiService {
ApiService.handleError(error); ApiService.handleError(error);
} }
} }
public findMyPollsByEmail(email: string): Promise<any> {
//////////// return this.axiosInstance.get<any>(`${this.baseHref}/poll/owner/${email}`);
// DELETE //
public async getPollsUrlsByUserEmail(email: string): Promise<Poll[]> {
// If user is authenticated : retrieve polls & display directly in frontend.
// TODO: Backend should handle this case. Actually the endpoint doesn't exist in backend.
// Here, only the list of slugs is usefull. Maybe just handle the list of slugs.
try {
const response: AxiosResponse<Poll[]> = await this.axiosInstance.get<Poll[]>(
`${this.usersEndpoint}/${email}${this.usersPollsEndpoint}`
);
return response?.data;
} catch (error) {
ApiService.handleError(error);
}
} }
////////////
public async updateAnswer(slug: string, choiceLabel: string, pseudo: string, answer: Answer): Promise<string> { public async updateAnswer(slug: string, choiceLabel: string, pseudo: string, answer: Answer): Promise<string> {
try { try {
return await this.axiosInstance.patch(`${this.pollsEndpoint}/${slug}${this.answersEndpoint}`, { return await this.axiosInstance.patch(`${this.baseHref}/${slug}${this.answersEndpoint}`, {
choiceLabel, choiceLabel,
pseudo, pseudo,
answer, answer,
@ -262,6 +246,10 @@ export class ApiService {
ApiService.handleError(error); ApiService.handleError(error);
} }
} }
////////////
// DELETE //
////////////
//////////// ////////////
public async deletePoll(slug: string): Promise<boolean> { public async deletePoll(slug: string): Promise<boolean> {
@ -298,19 +286,4 @@ export class ApiService {
///////////////////// /////////////////////
// PRIVATE METHODS // // PRIVATE METHODS //
///////////////////// /////////////////////
public async sendEmailToUserOfItsPollsList(email: string): Promise<void> {
// if (this.loaderService.isLoading) {
// return;
// }
// If user is not authenticated: the list of polls is send to user's email by the backend.
try {
// this.loaderService.setStatus(false);
await this.axiosInstance.get<Poll[]>(
`${this.usersEndpoint}/${email}${this.usersPollsEndpoint}${this.usersPollsSendEmailEndpoint}`
);
} catch (error) {
ApiService.handleError(error);
}
}
} }

View File

@ -36,6 +36,7 @@ export const endpoints = {
}, },
}, },
users: { users: {
find_my_polls: '/api/v1/poll/owner/:email/',
name: '/users', name: '/users',
polls: { polls: {
name: '/polls', name: '/polls',