269 lines
7.0 KiB
TypeScript
269 lines
7.0 KiB
TypeScript
import {Injectable} from '@angular/core';
|
|
import {PollConfig} from '../config/PollConfig';
|
|
import {HttpClient, HttpHeaders, HttpParams} from "@angular/common/http";
|
|
import {environment} from "../../environments/environment";
|
|
|
|
|
|
/**
|
|
* le service transverse à chaque page qui permet de syncroniser la configuration de sondage souhaitée
|
|
*/
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class ConfigService extends PollConfig {
|
|
myEmail: string;
|
|
loading: boolean = false;
|
|
baseHref: any = environment.baseApiHref;
|
|
myPolls: any;// list of retrieved polls from the backend api
|
|
|
|
|
|
constructor(public http: HttpClient) {
|
|
super();
|
|
}
|
|
|
|
set(key, val) {
|
|
this[key] = val;
|
|
}
|
|
|
|
/** ==================================
|
|
*
|
|
* poll public calls to get non authenticated info
|
|
*
|
|
* ==================================/
|
|
/**
|
|
* convert current poll config to a payload to send to the backend API
|
|
*/
|
|
getPollConfig() {
|
|
const jsonConfig = {
|
|
method: 'POST',
|
|
data: {
|
|
|
|
owner: {
|
|
email: this.myEmail,
|
|
pseudo: this.myName,
|
|
},
|
|
title: this.title,
|
|
description: this.description,
|
|
type: this.pollType,
|
|
visibility: this.visibility,
|
|
voteChoices: this.voteChoices,
|
|
allowSeveralHours: this.allowSeveralHours,
|
|
expirationDate: this.expirationDate,
|
|
passwordAccess: this.passwordAccess,
|
|
password: this.password,
|
|
customUrl: this.customUrl,
|
|
canModifyAnswers: this.canModifyAnswers,
|
|
whoModifiesAnswers: this.whoModifiesAnswers,
|
|
dateList: this.dateList,
|
|
timeList: this.timeList,
|
|
answers: this.answers,
|
|
}
|
|
};
|
|
console.log('jsonConfig', jsonConfig);
|
|
return jsonConfig
|
|
}
|
|
|
|
/**
|
|
* search in localstorage, fallback asking the backend to send an email to the owner if it exists
|
|
* @param email
|
|
*/
|
|
findPollsByEmail(email: string) {
|
|
|
|
|
|
this.findLocalStorageData();
|
|
// If no key is found in the localstorage, ask the backend to send an email to the user
|
|
|
|
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,
|
|
)
|
|
.subscribe(res => {
|
|
// message: 'Trouvé! Allez voir votre boite email',
|
|
this.myPolls = res;
|
|
this.loading = false;
|
|
}, this.handleError
|
|
)
|
|
}
|
|
|
|
|
|
/**
|
|
* display error message depending on the response of the backend
|
|
* @param err
|
|
*/
|
|
handleError(err: any) {
|
|
// TODO handle a toast message
|
|
console.error('err', err)
|
|
this.loading = false;
|
|
}
|
|
|
|
|
|
findLocalStorageData() {
|
|
// TODO check if the person has a key to retrieve her polls
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
*
|
|
* @param url
|
|
*/
|
|
getPollByURL(url: string) {
|
|
this.http.get(`${this.baseHref}/poll/${url}`).subscribe(
|
|
(res: any) => {
|
|
this.myPolls = res.data;
|
|
}, this.handleError
|
|
);
|
|
}
|
|
|
|
/**
|
|
* GET
|
|
* api/v1/poll/{id}
|
|
* @param id
|
|
*/
|
|
getPollById(id: string, password: string) {
|
|
|
|
this.http
|
|
.get(`${this.baseHref}/poll/${id}`,
|
|
{params: new HttpParams().set('body', password)})
|
|
.subscribe(
|
|
(res: any) => {
|
|
this.myPolls = res.data;
|
|
}, this.handleError
|
|
);
|
|
}
|
|
|
|
/**
|
|
* GET
|
|
* api/v1/my-polls
|
|
* @param ownerEmail
|
|
*/
|
|
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)
|
|
})
|
|
.subscribe(
|
|
(res: any) => {
|
|
this.myPolls = res.data;
|
|
}, this.handleError
|
|
);
|
|
}
|
|
|
|
|
|
/**
|
|
* action of the form
|
|
*/
|
|
createPoll() {
|
|
console.log('sends the form');
|
|
// alert('envoi de formulaire pour création de sondage en XHR à faire');
|
|
this.http.get(`${this.baseHref}/`)
|
|
.subscribe((res) => {
|
|
console.log('res', res);
|
|
this.createPollFromConfig(this.getPollConfig())
|
|
},
|
|
this.handleError
|
|
)
|
|
;
|
|
|
|
}
|
|
|
|
/**
|
|
* POST
|
|
* /api/v1/poll/{id}/poll
|
|
* @param config
|
|
*/
|
|
createPollFromConfig(config: any) {
|
|
this.http.post(`${this.baseHref}/poll`, config)
|
|
.subscribe((res: any) => {
|
|
// redirect to the page to administrate the new poll
|
|
alert("succès!");
|
|
this.selectedPoll = res;
|
|
this.pollId = res.pollId;
|
|
}, this.handleError
|
|
);
|
|
}
|
|
|
|
/**
|
|
* UPDATE
|
|
* /api/v1/poll/{id}/vote
|
|
* @param voteStack
|
|
*/
|
|
updatePoll(voteStack: any) {
|
|
this.http.put(`${this.baseHref}/poll/${this.pollId}`, voteStack)
|
|
.subscribe((res: any) => {
|
|
|
|
alert("succès!");
|
|
this.myPolls = res;
|
|
}, this.handleError
|
|
);
|
|
}
|
|
|
|
/**
|
|
* POST
|
|
* /api/v1/poll/{id}/vote
|
|
* @param voteStack
|
|
*/
|
|
addVote(voteStack: any) {
|
|
this.http.post(`${this.baseHref}/poll/${this.pollId}/vote`, voteStack)
|
|
.subscribe((res: any) => {
|
|
|
|
alert("succès!");
|
|
this.myPolls = res;
|
|
}, this.handleError
|
|
);
|
|
}
|
|
|
|
/**
|
|
* UPDATE
|
|
* /api/v1/poll/{id}/vote
|
|
* @param voteStack
|
|
*/
|
|
updateVote(voteStack: any) {
|
|
this.http.put(`${this.baseHref}/poll/${this.pollId}/vote`, voteStack)
|
|
.subscribe((res: any) => {
|
|
|
|
alert("succès!");
|
|
this.myPolls = res;
|
|
}, this.handleError
|
|
);
|
|
}
|
|
|
|
/**
|
|
* POST
|
|
* /api/v1/poll/{id}/comment
|
|
* @param comment
|
|
*/
|
|
addComment(comment: any) {
|
|
this.http.post(`${this.baseHref}/poll/${this.pollId}/comment`, comment)
|
|
.subscribe((res: any) => {
|
|
alert("succès!");
|
|
}, this.handleError
|
|
);
|
|
}
|
|
|
|
|
|
/**
|
|
* administrator calls
|
|
*/
|
|
|
|
}
|