2019-12-03 17:20:57 +01:00
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
import {PollConfig} from '../config/PollConfig';
|
2020-02-05 11:13:36 +01:00
|
|
|
import {HttpClient} from "@angular/common/http";
|
2019-12-03 17:20:57 +01:00
|
|
|
import {environment} from "../../environments/environment";
|
2020-01-16 14:37:11 +01:00
|
|
|
import {ConfirmationService, MessageService} from 'primeng/api';
|
|
|
|
import {Router} from "@angular/router";
|
2020-01-22 11:18:55 +01:00
|
|
|
import {mockMyPolls} from "../config/mocks/mockmypolls";
|
2020-01-29 16:30:07 +01:00
|
|
|
import {mockPoll3} from "../config/mocks/mock-poll3";
|
2020-01-30 11:19:17 +01:00
|
|
|
import {mockSuccessVote} from "../config/mocks/mock-success-vote";
|
2020-02-05 11:13:36 +01:00
|
|
|
import {PollUtilities} from "../config/PollUtilities";
|
2019-12-03 17:20:57 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* le service transverse à chaque page qui permet de syncroniser la configuration de sondage souhaitée
|
|
|
|
*/
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class ConfigService extends PollConfig {
|
2020-01-16 11:46:31 +01:00
|
|
|
|
2020-01-15 11:40:39 +01:00
|
|
|
loading: boolean = false;
|
2019-12-03 17:20:57 +01:00
|
|
|
baseHref: any = environment.baseApiHref;
|
|
|
|
|
|
|
|
|
2020-01-15 15:55:15 +01:00
|
|
|
constructor(private http: HttpClient,
|
2020-01-16 14:37:11 +01:00
|
|
|
private messageService: MessageService,
|
|
|
|
private router: Router,
|
2020-02-05 11:13:36 +01:00
|
|
|
private utils: PollUtilities,
|
2020-01-16 14:37:11 +01:00
|
|
|
private confirmationService: ConfirmationService,
|
|
|
|
) {
|
2019-12-03 17:20:57 +01:00
|
|
|
super();
|
2020-02-05 11:13:36 +01:00
|
|
|
this.fillValuesOnDevEnv();
|
2019-12-03 17:20:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
set(key, val) {
|
|
|
|
this[key] = val;
|
|
|
|
}
|
|
|
|
|
2020-02-05 11:13:36 +01:00
|
|
|
// fill in mock values if we are not in production environment
|
|
|
|
fillValuesOnDevEnv() {
|
2020-01-15 15:55:15 +01:00
|
|
|
|
2020-02-05 11:13:36 +01:00
|
|
|
if (!environment.production) {
|
|
|
|
console.info(' ######### framadate ######### we are not in production env, filling with mock values');
|
|
|
|
this.currentPoll = mockPoll3;
|
|
|
|
this.myPolls = mockMyPolls;
|
2020-01-16 11:33:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* add some days to a date, to compute intervals
|
|
|
|
* @param days
|
|
|
|
* @param date
|
|
|
|
*/
|
|
|
|
addDaysToDate(days: number, date: Date) {
|
|
|
|
date = new Date(date.valueOf());
|
|
|
|
date.setDate(date.getDate() + days);
|
|
|
|
return date;
|
|
|
|
};
|
|
|
|
|
2019-12-04 12:45:50 +01:00
|
|
|
/** ==================================
|
|
|
|
*
|
|
|
|
* poll public calls to get non authenticated info
|
|
|
|
*
|
|
|
|
* ==================================/
|
|
|
|
/**
|
|
|
|
* convert current poll config to a payload to send to the backend API
|
|
|
|
*/
|
2019-12-03 17:20:57 +01:00
|
|
|
getPollConfig() {
|
|
|
|
const jsonConfig = {
|
2020-01-22 14:42:46 +01:00
|
|
|
owner: {
|
|
|
|
email: this.myEmail,
|
|
|
|
pseudo: this.myName,
|
|
|
|
},
|
|
|
|
title: this.title,
|
|
|
|
description: this.description,
|
2020-01-23 17:36:56 +01:00
|
|
|
pollType: this.pollType,
|
2020-01-22 14:42:46 +01:00
|
|
|
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,
|
2020-01-22 16:49:15 +01:00
|
|
|
expiracyDateDefaultInDays: this.expiracyDateDefaultInDays,
|
|
|
|
deletionDateAfterLastModification: this.deletionDateAfterLastModification,
|
2019-12-03 17:20:57 +01:00
|
|
|
};
|
|
|
|
return jsonConfig
|
|
|
|
}
|
|
|
|
|
2020-01-15 15:55:15 +01:00
|
|
|
|
2020-01-22 11:18:55 +01:00
|
|
|
checkIfSlugIsUniqueInDatabase(slug: string = '') {
|
2020-01-16 11:33:13 +01:00
|
|
|
this.customUrlIsUnique = null;
|
2020-01-22 11:18:55 +01:00
|
|
|
if (!slug) {
|
2020-02-05 11:13:36 +01:00
|
|
|
slug = this.utils.makeSlug(this);
|
2020-01-22 11:18:55 +01:00
|
|
|
}
|
2020-01-16 11:33:13 +01:00
|
|
|
|
|
|
|
this.loading = true;
|
2020-01-21 12:04:14 +01:00
|
|
|
// TODO
|
|
|
|
this.todo('check slug is unique');
|
2020-01-22 11:18:55 +01:00
|
|
|
this.http.get(`${this.baseHref}/check-slug-is-uniq/${slug}`,
|
2020-02-05 11:13:36 +01:00
|
|
|
this.utils.makeHeaders({slug: this.customUrl}),
|
2020-01-16 11:33:13 +01:00
|
|
|
)
|
|
|
|
.subscribe((res: any) => {
|
|
|
|
|
2020-01-24 12:11:28 +01:00
|
|
|
this.customUrlIsUnique = res.poll.isUnique;
|
2020-01-16 11:33:13 +01:00
|
|
|
this.loading = false;
|
|
|
|
},
|
|
|
|
(e) => this.handleError(e))
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2019-12-04 12:45:50 +01:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
|
2020-01-22 16:49:15 +01:00
|
|
|
this.todo('send email for real : TODO');
|
2020-01-15 11:40:39 +01:00
|
|
|
this.loading = true;
|
2020-01-22 16:05:37 +01:00
|
|
|
this.http.get(`${this.baseHref}/send-polls-to-user/${this.myEmail}`,
|
2020-02-05 11:13:36 +01:00
|
|
|
this.utils.makeHeaders(),
|
2020-01-15 11:40:39 +01:00
|
|
|
)
|
2019-12-04 12:45:50 +01:00
|
|
|
.subscribe(res => {
|
|
|
|
// message: 'Trouvé! Allez voir votre boite email',
|
2020-01-16 12:05:54 +01:00
|
|
|
this.myPolls = res;
|
|
|
|
this.loading = false;
|
2020-01-15 15:55:15 +01:00
|
|
|
this.messageService.add({
|
|
|
|
severity: 'success',
|
2020-01-22 16:05:37 +01:00
|
|
|
summary: 'Succès',
|
|
|
|
detail: `Vos infos de sondages vous ont été transmises. Allez voir votre boite email ${this.myEmail}`
|
2020-01-15 15:55:15 +01:00
|
|
|
});
|
|
|
|
}, (e) => {
|
2020-01-15 17:55:22 +01:00
|
|
|
this.handleError(e)
|
2020-01-15 15:55:15 +01:00
|
|
|
}
|
2019-12-04 12:45:50 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* display error message depending on the response of the backend
|
|
|
|
* @param err
|
|
|
|
*/
|
2019-12-03 17:20:57 +01:00
|
|
|
handleError(err: any) {
|
2020-01-15 15:55:15 +01:00
|
|
|
console.error('err', err);
|
2020-01-15 11:40:39 +01:00
|
|
|
this.loading = false;
|
2020-01-16 12:05:54 +01:00
|
|
|
this.messageService.add({severity: 'warning', summary: "Erreur lors de l'appel ", detail: err.message});
|
2019-12-03 17:20:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
findLocalStorageData() {
|
|
|
|
// TODO check if the person has a key to retrieve her polls
|
2020-01-20 16:03:12 +01:00
|
|
|
console.log('localStorage', localStorage);
|
2020-01-16 14:37:11 +01:00
|
|
|
if (localStorage) {
|
|
|
|
console.log('localStorage', localStorage)
|
|
|
|
}
|
2019-12-03 17:20:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-01-15 15:55:15 +01:00
|
|
|
* get one poll by its slug name
|
2019-12-03 17:20:57 +01:00
|
|
|
* @param url
|
|
|
|
*/
|
|
|
|
getPollByURL(url: string) {
|
2020-01-21 12:04:14 +01:00
|
|
|
|
|
|
|
this.todo();
|
2020-02-05 11:13:36 +01:00
|
|
|
return this.http.get(`${this.baseHref}/poll/slug/${url}`, this.utils.makeHeaders())
|
2019-12-03 17:20:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-12-04 12:45:50 +01:00
|
|
|
* GET
|
|
|
|
* api/v1/poll/{id}
|
2019-12-03 17:20:57 +01:00
|
|
|
* @param id
|
|
|
|
*/
|
2020-01-20 15:50:09 +01:00
|
|
|
getPollById(id: string, password?: string) {
|
2020-01-15 11:40:39 +01:00
|
|
|
|
2020-01-20 15:50:09 +01:00
|
|
|
return this.http
|
2019-12-04 12:45:50 +01:00
|
|
|
.get(`${this.baseHref}/poll/${id}`,
|
2020-02-05 11:13:36 +01:00
|
|
|
this.utils.makeHeaders({body: password}))
|
2020-01-23 14:23:07 +01:00
|
|
|
}
|
2020-01-20 15:50:09 +01:00
|
|
|
|
2020-01-23 14:23:07 +01:00
|
|
|
fetchPollFromRoute(event) {
|
|
|
|
console.log('time to fetch poll', event)
|
2019-12-04 12:45:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET
|
|
|
|
* api/v1/my-polls
|
|
|
|
* @param ownerEmail
|
|
|
|
*/
|
|
|
|
getMyPolls(ownerEmail: string) {
|
|
|
|
this.http
|
|
|
|
.get(`${this.baseHref}/my-polls`,
|
2020-02-05 11:13:36 +01:00
|
|
|
this.utils.makeHeaders({ownerEmail: ownerEmail})
|
2020-01-15 15:55:15 +01:00
|
|
|
)
|
2019-12-04 12:45:50 +01:00
|
|
|
.subscribe(
|
|
|
|
(res: any) => {
|
2020-01-24 12:11:28 +01:00
|
|
|
// this.myPolls = res.poll;
|
2020-01-15 15:55:15 +01:00
|
|
|
}, (e) => {
|
|
|
|
this.handleError(e)
|
|
|
|
}
|
2019-12-04 12:45:50 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-01-20 15:23:26 +01:00
|
|
|
* launch creation call to the api
|
2019-12-04 12:45:50 +01:00
|
|
|
*/
|
|
|
|
createPoll() {
|
2020-01-20 15:23:26 +01:00
|
|
|
this.loading = true;
|
2020-01-22 16:05:37 +01:00
|
|
|
this.createPollFromConfig(this.getPollConfig())
|
2019-12-04 12:45:50 +01:00
|
|
|
}
|
|
|
|
|
2020-01-24 11:10:50 +01:00
|
|
|
updateCurrentPollFromResponse(res: any) {
|
2020-01-29 16:30:07 +01:00
|
|
|
console.log('update res', res);
|
|
|
|
this.currentPoll = res;
|
2020-01-24 12:11:28 +01:00
|
|
|
this.pollId = res.poll.id;
|
2020-02-04 12:51:18 +01:00
|
|
|
this.owner_modifier_token = res.owner_modifier_token;
|
2020-01-24 12:11:28 +01:00
|
|
|
this.urlPublic = this.baseHref + '#/vote/poll/id/' + res.poll.id;
|
|
|
|
this.urlSlugPublic = this.baseHref + '#/vote/poll/slug/' + res.poll.id;
|
|
|
|
if (res.poll.customUrl) {
|
|
|
|
this.urlSlugPublic = this.baseHref + '#/vote/poll/id/' + res.poll.customUrl;
|
2020-01-24 11:10:50 +01:00
|
|
|
}
|
2020-02-04 12:51:18 +01:00
|
|
|
if (res.voteStack) {
|
|
|
|
this.loadVoteStack(res.voteStack);
|
|
|
|
}
|
2020-02-04 11:35:29 +01:00
|
|
|
this.adminKey = res.admin_key;
|
2020-01-24 11:10:50 +01:00
|
|
|
this.urlAdmin = this.baseHref + '#/admin/' + res.admin_key;
|
|
|
|
}
|
|
|
|
|
2020-02-04 14:28:43 +01:00
|
|
|
resetCurrentChoicesAnswers() {
|
|
|
|
this.currentPoll.choices.map(choice => {
|
|
|
|
choice.answer = null;
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-30 10:07:03 +01:00
|
|
|
/**
|
|
|
|
* update current answers with a previous vote
|
|
|
|
* @param voteStack
|
|
|
|
*/
|
|
|
|
loadVoteStack(voteStack: any) {
|
|
|
|
|
|
|
|
// load the pseudo and email
|
|
|
|
this.myName = voteStack.pseudo;
|
2020-02-04 12:51:18 +01:00
|
|
|
this.myEmail = voteStack.email;
|
2020-01-30 10:07:03 +01:00
|
|
|
this.voteStackId = voteStack.id;
|
|
|
|
this.myVoteStack = voteStack;
|
2020-02-05 11:13:36 +01:00
|
|
|
let keys = Object.keys(voteStack.votes);
|
2020-02-04 12:51:18 +01:00
|
|
|
console.log('voteStack', voteStack);
|
2020-02-04 14:28:43 +01:00
|
|
|
this.resetCurrentChoicesAnswers();
|
2020-02-04 12:51:18 +01:00
|
|
|
keys.forEach((id: any) => {
|
2020-01-30 10:07:03 +01:00
|
|
|
let voteItem = voteStack.votes[id];
|
|
|
|
|
2020-02-04 12:51:18 +01:00
|
|
|
/**
|
|
|
|
* the display of the poll uses the choices data, so we update the choices answers of the current poll to reflect the vote stack we have taken
|
|
|
|
*/
|
2020-01-30 10:07:03 +01:00
|
|
|
if (voteItem.choice_id && voteItem.value) {
|
|
|
|
let foundChoiceToModify = this.currentPoll.choices.find(choicesItem => {
|
|
|
|
return voteItem.choice_id == choicesItem.id
|
|
|
|
});
|
2020-02-05 11:13:36 +01:00
|
|
|
console.log('foundChoiceToModify', foundChoiceToModify);
|
2020-01-30 10:07:03 +01:00
|
|
|
if (foundChoiceToModify) {
|
|
|
|
foundChoiceToModify.answer = voteItem.value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-04 12:45:50 +01:00
|
|
|
/**
|
|
|
|
* POST
|
|
|
|
* /api/v1/poll/{id}/poll
|
|
|
|
* @param config
|
|
|
|
*/
|
|
|
|
createPollFromConfig(config: any) {
|
2020-01-20 15:50:09 +01:00
|
|
|
this.loading = true;
|
2020-01-30 10:07:03 +01:00
|
|
|
console.log('config', config);
|
2020-01-20 15:23:26 +01:00
|
|
|
return this.http.post(`${this.baseHref}/poll`,
|
2020-01-22 16:05:37 +01:00
|
|
|
config,
|
2020-02-05 11:13:36 +01:00
|
|
|
this.utils.makeHeaders())
|
2019-12-04 12:45:50 +01:00
|
|
|
.subscribe((res: any) => {
|
|
|
|
// redirect to the page to administrate the new poll
|
2020-01-15 15:55:15 +01:00
|
|
|
this.messageService.add({severity: 'success', summary: 'Sondage Créé',});
|
2020-01-24 11:10:50 +01:00
|
|
|
|
|
|
|
this.updateCurrentPollFromResponse(res);
|
|
|
|
|
2020-01-20 15:23:26 +01:00
|
|
|
this.loading = false;
|
2020-01-22 16:49:15 +01:00
|
|
|
if (!this.myPolls) {
|
|
|
|
this.myPolls = [];
|
|
|
|
}
|
2020-01-23 15:08:52 +01:00
|
|
|
this.myPolls.push(res);
|
2020-01-20 15:23:26 +01:00
|
|
|
this.router.navigate(['step/end']);
|
2020-01-22 11:18:55 +01:00
|
|
|
// TODO save new poll to localstorage
|
|
|
|
// reset all fields in current config
|
|
|
|
this.resetConfig();
|
2020-01-15 15:55:15 +01:00
|
|
|
}, (e) => {
|
|
|
|
this.handleError(e)
|
|
|
|
}
|
2019-12-04 12:45:50 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-01-23 11:16:55 +01:00
|
|
|
/**
|
|
|
|
* conversion to send to back
|
|
|
|
* @param choiceList
|
|
|
|
*/
|
|
|
|
convertChoicesAnsweredToSend(choiceList) {
|
2020-01-30 11:19:17 +01:00
|
|
|
choiceList = choiceList.filter(c => c.answer ? c : null); // remove choices where we did not answer
|
2020-01-23 11:16:55 +01:00
|
|
|
const converted = choiceList.map(elem => {
|
2020-01-30 11:19:17 +01:00
|
|
|
if (elem.answer) {
|
|
|
|
return {
|
|
|
|
choice_id: elem.id,
|
|
|
|
value: elem.answer
|
|
|
|
}
|
2020-01-23 11:16:55 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
console.log('converted', converted);
|
|
|
|
return converted;
|
|
|
|
}
|
2019-12-04 12:45:50 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* POST
|
|
|
|
* /api/v1/poll/{id}/vote
|
|
|
|
* @param voteStack
|
|
|
|
*/
|
2020-01-16 15:35:11 +01:00
|
|
|
addVote(voteStack?: any) {
|
|
|
|
if (!voteStack) {
|
|
|
|
voteStack = {
|
|
|
|
pseudo: this.myName,
|
2020-01-22 16:49:15 +01:00
|
|
|
email: this.myEmail,
|
2020-01-23 11:16:55 +01:00
|
|
|
votes: this.convertChoicesAnsweredToSend(this.currentPoll.choices),
|
2020-01-16 15:35:11 +01:00
|
|
|
}
|
|
|
|
}
|
2020-01-23 15:08:52 +01:00
|
|
|
this.myVoteStack = voteStack;
|
|
|
|
|
2020-01-30 11:19:17 +01:00
|
|
|
if (!environment.production) {
|
|
|
|
this.handleVoteAdded(mockSuccessVote);
|
|
|
|
return;
|
|
|
|
}
|
2020-01-15 15:55:15 +01:00
|
|
|
this.http.post(
|
2020-01-23 15:08:52 +01:00
|
|
|
`${this.baseHref}/poll/${this.pollId}/vote`,
|
2020-01-15 15:55:15 +01:00
|
|
|
voteStack,
|
2020-02-05 11:13:36 +01:00
|
|
|
this.utils.makeHeaders())
|
2019-12-04 12:45:50 +01:00
|
|
|
.subscribe((res: any) => {
|
|
|
|
|
2020-01-30 11:19:17 +01:00
|
|
|
this.handleVoteAdded(res);
|
2020-01-15 15:55:15 +01:00
|
|
|
}, (e) => {
|
|
|
|
this.handleError(e)
|
|
|
|
}
|
2019-12-04 12:45:50 +01:00
|
|
|
);
|
2019-12-03 17:20:57 +01:00
|
|
|
}
|
|
|
|
|
2020-01-30 11:19:17 +01:00
|
|
|
handleVoteAdded(res) {
|
|
|
|
this.messageService.add({severity: 'success', summary: 'Vote ajouté'});
|
|
|
|
// save modifier token
|
|
|
|
this.myVoteStack['modifier_token'] = res.modifier_token;
|
|
|
|
this.myVoteStack['id'] = res.vote_stack.id;
|
|
|
|
this.updateCurrentPollFromResponse(res);
|
|
|
|
}
|
|
|
|
|
2019-12-04 12:45:50 +01:00
|
|
|
/**
|
|
|
|
* UPDATE
|
|
|
|
* /api/v1/poll/{id}/vote
|
|
|
|
* @param voteStack
|
|
|
|
*/
|
2020-01-23 15:08:52 +01:00
|
|
|
updateVote(voteStack?: any) {
|
|
|
|
if (!this.myVoteStack) {
|
|
|
|
return;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
voteStack = this.myVoteStack;
|
|
|
|
}
|
2020-02-04 12:51:18 +01:00
|
|
|
this.http.patch(
|
|
|
|
`${this.baseHref}/vote-stack/${voteStack.id}/token/${this.owner_modifier_token}`,
|
2020-01-15 15:55:15 +01:00
|
|
|
voteStack,
|
2020-02-05 11:13:36 +01:00
|
|
|
this.utils.makeHeaders())
|
2019-12-04 12:45:50 +01:00
|
|
|
.subscribe((res: any) => {
|
2020-01-15 15:55:15 +01:00
|
|
|
this.messageService.add({severity: 'success', summary: 'Vote mis à jour'});
|
2020-01-24 11:10:50 +01:00
|
|
|
this.updateCurrentPollFromResponse(res);
|
2020-01-15 15:55:15 +01:00
|
|
|
}, (e) => {
|
|
|
|
this.handleError(e)
|
|
|
|
}
|
2019-12-04 12:45:50 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* POST
|
|
|
|
* /api/v1/poll/{id}/comment
|
|
|
|
* @param comment
|
|
|
|
*/
|
2020-01-16 15:35:11 +01:00
|
|
|
addComment(comment?: any) {
|
2020-01-20 15:58:35 +01:00
|
|
|
if (!comment && this.myComment) {
|
2020-01-16 15:35:11 +01:00
|
|
|
comment = {
|
2020-01-20 15:58:35 +01:00
|
|
|
name: this.myName,
|
2020-01-22 16:58:38 +01:00
|
|
|
pseudo: this.myName,
|
2020-01-22 16:05:37 +01:00
|
|
|
email: this.myEmail,
|
2020-01-20 15:58:35 +01:00
|
|
|
date: new Date(),
|
|
|
|
text: this.myComment,
|
2020-01-16 15:35:11 +01:00
|
|
|
}
|
|
|
|
}
|
2020-01-30 10:07:03 +01:00
|
|
|
console.log('comment', comment);
|
2020-01-15 15:55:15 +01:00
|
|
|
this.http.post(
|
2020-01-23 14:23:07 +01:00
|
|
|
`${this.baseHref}/poll/${this.pollId}/comment`,
|
2020-01-15 15:55:15 +01:00
|
|
|
comment,
|
2020-02-05 11:13:36 +01:00
|
|
|
this.utils.makeHeaders())
|
2019-12-04 12:45:50 +01:00
|
|
|
.subscribe((res: any) => {
|
2020-01-30 11:19:17 +01:00
|
|
|
this.messageService.add({
|
|
|
|
severity: 'success',
|
|
|
|
summary: 'Commentaire Créé',
|
|
|
|
detail: comment.text
|
|
|
|
});
|
2020-01-16 15:35:11 +01:00
|
|
|
// empty comment after success
|
|
|
|
this.myComment = '';
|
2020-01-22 16:58:38 +01:00
|
|
|
comment.date = {
|
|
|
|
date: comment.date
|
|
|
|
};
|
2020-01-20 15:58:35 +01:00
|
|
|
this.currentPoll.comments.push(comment);
|
2020-01-15 15:55:15 +01:00
|
|
|
}, (e) => {
|
2020-01-20 15:58:35 +01:00
|
|
|
this.handleError(e);
|
2020-01-15 15:55:15 +01:00
|
|
|
}
|
2019-12-04 12:45:50 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* administrator calls
|
|
|
|
*/
|
|
|
|
|
2020-01-16 14:37:11 +01:00
|
|
|
deleteComments() {
|
|
|
|
// prompt for confirmation
|
|
|
|
this.confirmationService.confirm({
|
|
|
|
message: 'Are you sure that you want to completely delete the comments of this poll (' + this.title + ') permanentely?',
|
|
|
|
accept: () => {
|
|
|
|
this.http.delete(
|
|
|
|
`${this.baseHref}/poll/${this.pollId}/comments`,
|
2020-02-05 11:13:36 +01:00
|
|
|
this.utils.makeHeaders())
|
2020-01-16 14:37:11 +01:00
|
|
|
.subscribe((res: any) => {
|
|
|
|
this.messageService.add({
|
|
|
|
severity: 'success',
|
|
|
|
summary: 'Commentaires bien supprimés',
|
|
|
|
detail: 'Commentaires du sondage "' + this.title + '" supprimé'
|
|
|
|
});
|
|
|
|
|
|
|
|
}, (e) => {
|
|
|
|
this.handleError(e)
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteVotes() {
|
2020-01-22 11:18:55 +01:00
|
|
|
// prompt for confirmation
|
2020-01-16 14:37:11 +01:00
|
|
|
this.confirmationService.confirm({
|
|
|
|
message: 'Are you sure that you want to completely delete the votes of this poll (' + this.title + ') permanentely?',
|
|
|
|
accept: () => {
|
|
|
|
this.http.delete(
|
|
|
|
`${this.baseHref}/poll/${this.pollId}/votes`,
|
2020-02-05 11:13:36 +01:00
|
|
|
this.utils.makeHeaders())
|
2020-01-16 14:37:11 +01:00
|
|
|
.subscribe((res: any) => {
|
|
|
|
this.messageService.add({
|
|
|
|
severity: 'success',
|
|
|
|
summary: 'Votes bien supprimés',
|
|
|
|
detail: 'Votes du sondage "' + this.title + '" supprimé'
|
|
|
|
});
|
|
|
|
|
|
|
|
}, (e) => {
|
|
|
|
this.handleError(e)
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
deletePoll() {
|
|
|
|
if (!this.pollId) {
|
|
|
|
this.messageService.add({
|
|
|
|
summary: 'this poll is not administrable, it has no ID',
|
|
|
|
severity: 'warning'
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let self = this;
|
|
|
|
// prompt for confirmation
|
|
|
|
this.confirmationService.confirm({
|
|
|
|
message: 'Are you sure that you want to completely delete this poll (' + self.title + ') and all is data permanentely?',
|
|
|
|
accept: () => {
|
|
|
|
this.http.delete(
|
|
|
|
`${this.baseHref}/poll/${this.pollId}`,
|
2020-02-05 11:13:36 +01:00
|
|
|
this.utils.makeHeaders())
|
2020-01-16 14:37:11 +01:00
|
|
|
.subscribe((res: any) => {
|
|
|
|
this.messageService.add({
|
|
|
|
severity: 'success',
|
|
|
|
summary: 'Sondage bien supprimé',
|
|
|
|
detail: 'sondage "' + this.title + '" supprimé'
|
|
|
|
});
|
|
|
|
|
|
|
|
this.router.navigate(['home']);
|
|
|
|
}, (e) => {
|
|
|
|
this.handleError(e)
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* UPDATE
|
|
|
|
* /api/v1/poll/{id}/vote
|
|
|
|
* @param voteStack
|
2020-01-29 16:30:07 +01:00
|
|
|
* TODO
|
2020-01-16 14:37:11 +01:00
|
|
|
*/
|
|
|
|
updatePoll(voteStack: any) {
|
|
|
|
this.http.put(
|
|
|
|
`${this.baseHref}/poll/${this.pollId}`,
|
|
|
|
voteStack,
|
2020-02-05 11:13:36 +01:00
|
|
|
this.utils.makeHeaders()
|
2020-01-16 14:37:11 +01:00
|
|
|
)
|
|
|
|
.subscribe((res: any) => {
|
|
|
|
this.messageService.add({
|
|
|
|
severity: 'success',
|
|
|
|
summary: 'Sondage mis à jour',
|
|
|
|
});
|
2020-01-24 12:11:28 +01:00
|
|
|
this.updateCurrentPollFromResponse(res);
|
2020-01-16 14:37:11 +01:00
|
|
|
}, (e) => {
|
|
|
|
this.handleError(e)
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2020-01-16 15:35:11 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-29 16:30:07 +01:00
|
|
|
* TODO
|
2020-01-16 15:35:11 +01:00
|
|
|
* export all the poll data available to the public as a CSV single file
|
|
|
|
*/
|
|
|
|
exportCSV() {
|
|
|
|
|
2020-01-29 16:30:07 +01:00
|
|
|
|
2020-02-04 15:23:46 +01:00
|
|
|
let rows = [];
|
|
|
|
let now = new Date();
|
|
|
|
const headers = [
|
|
|
|
['export de sondage Framadate ', this.customUrl],
|
|
|
|
['le', now.toISOString()],
|
|
|
|
[this.currentPoll.pollId, this.currentPoll.title, this.customUrl, this.creationDate],
|
|
|
|
['pseudo']];
|
|
|
|
|
|
|
|
|
|
|
|
let listOfChoices = ['choices : '];
|
|
|
|
this.currentPoll.choices.map(choice => {
|
|
|
|
listOfChoices.push(choice.text)
|
|
|
|
});
|
|
|
|
listOfChoices.push('pseudo');
|
|
|
|
|
|
|
|
this.currentPoll.stacks.map(voteStack => {
|
|
|
|
let voteStackInArray = [voteStack.pseudo];
|
|
|
|
let keysVotes = Object.keys(voteStack.votes);
|
|
|
|
|
|
|
|
keysVotes.map(id => {
|
|
|
|
voteStackInArray.push(voteStack.votes[id].value ? voteStack.votes[id].value : "")
|
|
|
|
});
|
|
|
|
rows.push(
|
|
|
|
voteStackInArray
|
|
|
|
);
|
|
|
|
});
|
|
|
|
const headersComments = [
|
|
|
|
['comments'],
|
|
|
|
['pseudo', 'text', 'creation_date'],
|
2020-01-16 15:35:11 +01:00
|
|
|
];
|
2020-02-04 15:23:46 +01:00
|
|
|
const comments = [];
|
|
|
|
this.currentPoll.comments.map(item => {
|
|
|
|
comments.push(
|
|
|
|
[item.pseudo,
|
|
|
|
item.text,
|
|
|
|
item.date.date,
|
|
|
|
'\n']
|
|
|
|
)
|
|
|
|
});
|
|
|
|
headers.push(listOfChoices);
|
|
|
|
rows = [headers, listOfChoices, rows, headersComments, comments];
|
|
|
|
|
|
|
|
let convertedCsv = rows.map(elem => {
|
2020-02-05 11:13:36 +01:00
|
|
|
console.log('elem', elem);
|
2020-02-04 15:23:46 +01:00
|
|
|
return elem.map(item => {
|
|
|
|
console.log('item', item);
|
|
|
|
if (typeof item === typeof Array) {
|
|
|
|
return item.join('\n')
|
|
|
|
}
|
|
|
|
return item;
|
|
|
|
}).join('\n')
|
|
|
|
}).join('\n');
|
|
|
|
console.log('rows', rows);
|
|
|
|
console.log('convertedCsv', convertedCsv);
|
2020-01-16 15:35:11 +01:00
|
|
|
|
|
|
|
let csvContent = "data:text/csv;charset=utf-8,"
|
2020-02-04 15:23:46 +01:00
|
|
|
+ convertedCsv;
|
2020-02-05 11:13:36 +01:00
|
|
|
console.log('csvContent', csvContent);
|
2020-01-16 15:35:11 +01:00
|
|
|
var encodedUri = encodeURI(csvContent);
|
|
|
|
var link = document.createElement("a");
|
|
|
|
link.setAttribute("href", encodedUri);
|
2020-02-05 11:13:36 +01:00
|
|
|
let exportFileName = (this.urlPublic ? this.urlPublic : this.utils.makeSlug(this)) + "_export_" + new Date() + ".csv";
|
2020-02-04 15:23:46 +01:00
|
|
|
link.setAttribute("download", exportFileName);
|
2020-01-16 15:35:11 +01:00
|
|
|
document.body.appendChild(link); // Required for FF
|
|
|
|
link.click(); // This will download the data file named "my_data.csv".
|
|
|
|
}
|
|
|
|
|
|
|
|
print() {
|
|
|
|
alert('TODO');
|
|
|
|
}
|
|
|
|
|
2020-01-21 12:04:14 +01:00
|
|
|
todo(message = '') {
|
2020-01-16 15:35:11 +01:00
|
|
|
this.messageService.add({
|
2020-01-21 12:04:14 +01:00
|
|
|
severity: 'info' + message,
|
2020-01-16 15:35:11 +01:00
|
|
|
detail: "cette fonctionnalité n'est pas encore disponible. Venez en discuter sur framateam.org / Ux et design libre / Framasoft",
|
|
|
|
summary: "Work in progress",
|
|
|
|
});
|
|
|
|
}
|
2019-12-03 17:20:57 +01:00
|
|
|
}
|