|
|
|
@ -1,6 +1,6 @@
|
|
|
|
|
import {Injectable} from '@angular/core'; |
|
|
|
|
import {PollConfig} from '../config/PollConfig'; |
|
|
|
|
import {HttpClient, HttpHeaders, HttpParams} from "@angular/common/http"; |
|
|
|
|
import {HttpClient, HttpHeaders} from "@angular/common/http"; |
|
|
|
|
import {environment} from "../../environments/environment"; |
|
|
|
|
import {MessageService} from 'primeng/api'; |
|
|
|
|
|
|
|
|
@ -17,7 +17,7 @@ export class ConfigService extends PollConfig {
|
|
|
|
|
myPolls: any;// list of retrieved polls from the backend api
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor(public http: HttpClient, |
|
|
|
|
constructor(private http: HttpClient, |
|
|
|
|
private messageService: MessageService) { |
|
|
|
|
super(); |
|
|
|
|
} |
|
|
|
@ -26,6 +26,10 @@ export class ConfigService extends PollConfig {
|
|
|
|
|
this[key] = val; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
clear() { |
|
|
|
|
this.messageService.clear(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** ================================== |
|
|
|
|
* |
|
|
|
|
* poll public calls to get non authenticated info |
|
|
|
@ -64,6 +68,24 @@ export class ConfigService extends PollConfig {
|
|
|
|
|
return jsonConfig |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
makeHeaders(bodyContent?: any) { |
|
|
|
|
|
|
|
|
|
const headerDict = { |
|
|
|
|
'Charset': 'UTF-8', |
|
|
|
|
'Content-Type': 'application/json', |
|
|
|
|
'Accept': 'application/json', |
|
|
|
|
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE', |
|
|
|
|
'Access-Control-Allow-Origin': '*' |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
const requestOptions = { |
|
|
|
|
headers: new HttpHeaders(headerDict), |
|
|
|
|
body: bodyContent |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
return requestOptions; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* search in localstorage, fallback asking the backend to send an email to the owner if it exists |
|
|
|
|
* @param email |
|
|
|
@ -76,27 +98,28 @@ export class ConfigService extends PollConfig {
|
|
|
|
|
|
|
|
|
|
this.myEmail = email; |
|
|
|
|
|
|
|
|
|
const headerDict = { |
|
|
|
|
'Content-Type': 'application/json', |
|
|
|
|
'Accept': 'application/json', |
|
|
|
|
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE', |
|
|
|
|
'Access-Control-Allow-Origin': environment.baseApiHref |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
const requestOptions = { |
|
|
|
|
headers: new HttpHeaders(headerDict), |
|
|
|
|
email: this.myEmail |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
this.loading = true; |
|
|
|
|
this.http.get(`${this.baseHref}/my-polls`, |
|
|
|
|
requestOptions, |
|
|
|
|
this.makeHeaders({email: this.myEmail}), |
|
|
|
|
) |
|
|
|
|
.subscribe(res => { |
|
|
|
|
// message: 'Trouvé! Allez voir votre boite email',
|
|
|
|
|
this.myPolls = res; |
|
|
|
|
console.log('res', res) |
|
|
|
|
this.loading = false; |
|
|
|
|
}, this.handleError |
|
|
|
|
this.messageService.add({ |
|
|
|
|
severity: 'success', |
|
|
|
|
summary: 'Service Message', |
|
|
|
|
detail: 'Via MessageService' |
|
|
|
|
}); |
|
|
|
|
}, (e) => { |
|
|
|
|
this.messageService.add( |
|
|
|
|
{ |
|
|
|
|
severity: 'warning', |
|
|
|
|
summary: "Erreur lors de l'appel " |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -107,9 +130,9 @@ export class ConfigService extends PollConfig {
|
|
|
|
|
*/ |
|
|
|
|
handleError(err: any) { |
|
|
|
|
// TODO handle a toast message
|
|
|
|
|
console.error('err', err) |
|
|
|
|
console.error('err', err); |
|
|
|
|
this.loading = false; |
|
|
|
|
this.messageService.add({severity: 'success', summary: 'Service Message', detail: 'Via MessageService'}); |
|
|
|
|
this.messageService.add({severity: 'warning', summary: "Erreur lors de l'appel "}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -120,14 +143,16 @@ export class ConfigService extends PollConfig {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* |
|
|
|
|
* get one poll by its slug name |
|
|
|
|
* @param url |
|
|
|
|
*/ |
|
|
|
|
getPollByURL(url: string) { |
|
|
|
|
this.http.get(`${this.baseHref}/poll/${url}`).subscribe( |
|
|
|
|
this.http.get(`${this.baseHref}/poll/${url}`, this.makeHeaders()).subscribe( |
|
|
|
|
(res: any) => { |
|
|
|
|
this.myPolls = res.data; |
|
|
|
|
}, this.handleError |
|
|
|
|
}, (e) => { |
|
|
|
|
this.handleError(e) |
|
|
|
|
} |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -140,11 +165,13 @@ export class ConfigService extends PollConfig {
|
|
|
|
|
|
|
|
|
|
this.http |
|
|
|
|
.get(`${this.baseHref}/poll/${id}`, |
|
|
|
|
{params: new HttpParams().set('body', password)}) |
|
|
|
|
this.makeHeaders({body: password})) |
|
|
|
|
.subscribe( |
|
|
|
|
(res: any) => { |
|
|
|
|
this.myPolls = res.data; |
|
|
|
|
}, this.handleError |
|
|
|
|
}, (e) => { |
|
|
|
|
this.handleError(e) |
|
|
|
|
} |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -156,17 +183,14 @@ export class ConfigService extends PollConfig {
|
|
|
|
|
getMyPolls(ownerEmail: string) { |
|
|
|
|
this.http |
|
|
|
|
.get(`${this.baseHref}/my-polls`, |
|
|
|
|
{ |
|
|
|
|
headers: new HttpHeaders() |
|
|
|
|
.append('Content-Type', 'application/json') |
|
|
|
|
.append('Charset', 'UTF-8') |
|
|
|
|
, |
|
|
|
|
params: new HttpParams().set('ownerEmail', ownerEmail) |
|
|
|
|
}) |
|
|
|
|
this.makeHeaders({ownerEmail: ownerEmail}) |
|
|
|
|
) |
|
|
|
|
.subscribe( |
|
|
|
|
(res: any) => { |
|
|
|
|
this.myPolls = res.data; |
|
|
|
|
}, this.handleError |
|
|
|
|
}, (e) => { |
|
|
|
|
this.handleError(e) |
|
|
|
|
} |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -177,7 +201,7 @@ export class ConfigService extends PollConfig {
|
|
|
|
|
createPoll() { |
|
|
|
|
console.log('sends the form'); |
|
|
|
|
// alert('envoi de formulaire pour création de sondage en XHR à faire');
|
|
|
|
|
this.http.get(`${this.baseHref}/`) |
|
|
|
|
this.http.get(`${this.baseHref}/`, this.makeHeaders()) |
|
|
|
|
.subscribe((res) => { |
|
|
|
|
console.log('res', res); |
|
|
|
|
this.createPollFromConfig(this.getPollConfig()) |
|
|
|
@ -194,13 +218,15 @@ export class ConfigService extends PollConfig {
|
|
|
|
|
* @param config |
|
|
|
|
*/ |
|
|
|
|
createPollFromConfig(config: any) { |
|
|
|
|
this.http.post(`${this.baseHref}/poll`, config) |
|
|
|
|
this.http.post(`${this.baseHref}/poll`, this.makeHeaders({config: config})) |
|
|
|
|
.subscribe((res: any) => { |
|
|
|
|
// redirect to the page to administrate the new poll
|
|
|
|
|
alert("succès!"); |
|
|
|
|
this.messageService.add({severity: 'success', summary: 'Sondage Créé',}); |
|
|
|
|
this.selectedPoll = res; |
|
|
|
|
this.pollId = res.pollId; |
|
|
|
|
}, this.handleError |
|
|
|
|
}, (e) => { |
|
|
|
|
this.handleError(e) |
|
|
|
|
} |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -210,12 +236,20 @@ export class ConfigService extends PollConfig {
|
|
|
|
|
* @param voteStack |
|
|
|
|
*/ |
|
|
|
|
updatePoll(voteStack: any) { |
|
|
|
|
this.http.put(`${this.baseHref}/poll/${this.pollId}`, voteStack) |
|
|
|
|
this.http.put( |
|
|
|
|
`${this.baseHref}/poll/${this.pollId}`, |
|
|
|
|
voteStack, |
|
|
|
|
this.makeHeaders() |
|
|
|
|
) |
|
|
|
|
.subscribe((res: any) => { |
|
|
|
|
|
|
|
|
|
alert("succès!"); |
|
|
|
|
this.messageService.add({ |
|
|
|
|
severity: 'success', |
|
|
|
|
summary: 'Sondage mis à jour', |
|
|
|
|
}); |
|
|
|
|
this.myPolls = res; |
|
|
|
|
}, this.handleError |
|
|
|
|
}, (e) => { |
|
|
|
|
this.handleError(e) |
|
|
|
|
} |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -225,12 +259,18 @@ export class ConfigService extends PollConfig {
|
|
|
|
|
* @param voteStack |
|
|
|
|
*/ |
|
|
|
|
addVote(voteStack: any) { |
|
|
|
|
this.http.post(`${this.baseHref}/poll/${this.pollId}/vote`, voteStack) |
|
|
|
|
this.http.post( |
|
|
|
|
`${this.baseHref}/poll/${this.pollId}/vote`, |
|
|
|
|
voteStack, |
|
|
|
|
this.makeHeaders()) |
|
|
|
|
.subscribe((res: any) => { |
|
|
|
|
|
|
|
|
|
this.messageService.add({severity: 'success', summary: 'Vote ajouté'}); |
|
|
|
|
alert("succès!"); |
|
|
|
|
this.myPolls = res; |
|
|
|
|
}, this.handleError |
|
|
|
|
}, (e) => { |
|
|
|
|
this.handleError(e) |
|
|
|
|
} |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -240,12 +280,16 @@ export class ConfigService extends PollConfig {
|
|
|
|
|
* @param voteStack |
|
|
|
|
*/ |
|
|
|
|
updateVote(voteStack: any) { |
|
|
|
|
this.http.put(`${this.baseHref}/poll/${this.pollId}/vote`, voteStack) |
|
|
|
|
this.http.put( |
|
|
|
|
`${this.baseHref}/poll/${this.pollId}/vote`, |
|
|
|
|
voteStack, |
|
|
|
|
this.makeHeaders()) |
|
|
|
|
.subscribe((res: any) => { |
|
|
|
|
|
|
|
|
|
alert("succès!"); |
|
|
|
|
this.messageService.add({severity: 'success', summary: 'Vote mis à jour'}); |
|
|
|
|
this.myPolls = res; |
|
|
|
|
}, this.handleError |
|
|
|
|
}, (e) => { |
|
|
|
|
this.handleError(e) |
|
|
|
|
} |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -255,10 +299,19 @@ export class ConfigService extends PollConfig {
|
|
|
|
|
* @param comment |
|
|
|
|
*/ |
|
|
|
|
addComment(comment: any) { |
|
|
|
|
this.http.post(`${this.baseHref}/poll/${this.pollId}/comment`, comment) |
|
|
|
|
this.http.post( |
|
|
|
|
`${this.baseHref}/poll/${this.pollId}/comment`, |
|
|
|
|
comment, |
|
|
|
|
this.makeHeaders()) |
|
|
|
|
.subscribe((res: any) => { |
|
|
|
|
alert("succès!"); |
|
|
|
|
}, this.handleError |
|
|
|
|
this.messageService.add({ |
|
|
|
|
severity: 'success', |
|
|
|
|
summary: 'Commentaire Créé', |
|
|
|
|
detail: 'Via MessageService' |
|
|
|
|
}); |
|
|
|
|
}, (e) => { |
|
|
|
|
this.handleError(e) |
|
|
|
|
} |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|