forked from tykayn/funky-framadate-front
143 lines
3.8 KiB
TypeScript
143 lines
3.8 KiB
TypeScript
|
import {Injectable} from '@angular/core';
|
||
|
import {PollConfig} from '../config/PollConfig';
|
||
|
import {HttpClient, HttpHeaders} 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;
|
||
|
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;
|
||
|
}
|
||
|
|
||
|
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
|
||
|
)
|
||
|
;
|
||
|
|
||
|
}
|
||
|
|
||
|
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.myPolls = res;
|
||
|
}, this.handleError
|
||
|
);
|
||
|
}
|
||
|
|
||
|
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 headers = new HttpHeaders('Content-Type:application/json');
|
||
|
const headerDict = {
|
||
|
'Content-Type': 'application/json',
|
||
|
'Accept': 'application/json',
|
||
|
};
|
||
|
|
||
|
const requestOptions = {
|
||
|
headers: new HttpHeaders(headerDict),
|
||
|
};
|
||
|
|
||
|
this.http.get(`${this.baseHref}/my-polls`)
|
||
|
.subscribe(res => {
|
||
|
// message: 'Trouvé! Allez voir votre boite email',
|
||
|
this.myPolls = res;
|
||
|
}, this.handleError
|
||
|
)
|
||
|
}
|
||
|
|
||
|
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
|
||
|
}
|
||
|
|
||
|
handleError(err: any) {
|
||
|
console.error('err', err)
|
||
|
}
|
||
|
|
||
|
|
||
|
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
|
||
|
);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
* @param id
|
||
|
*/
|
||
|
getPollById(id: string) {
|
||
|
// http://127.0.0.1:8000/api/v1/poll/1
|
||
|
this.http.get(`${this.baseHref}/poll/${id}`).subscribe(
|
||
|
(res: any) => {
|
||
|
this.myPolls = res.data;
|
||
|
}, this.handleError
|
||
|
);
|
||
|
}
|
||
|
|
||
|
}
|