auto send vote in dev mode

This commit is contained in:
Tykayn 2021-04-29 10:56:36 +02:00 committed by tykayn
parent b9181b5b3f
commit 3a89645e66
2 changed files with 87 additions and 80 deletions

View File

@ -1,6 +1,7 @@
import { HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Poll } from '../models/poll.model';
import { Comment } from '../models/comment.model';
@Injectable({
providedIn: 'root',
@ -67,11 +68,84 @@ export class PollUtilitiesService {
'Access-Control-Allow-Origin': '*',
};
const requestOptions = {
headers: new HttpHeaders(headerDict),
return {
headers: headerDict,
body: bodyContent,
};
}
return requestOptions;
exportCSV(poll: Poll): void {
let rows = [];
const now = new Date();
const headers = [
['export de sondage Framadate ', poll.custom_url],
['le', now.toISOString()],
[poll.id, poll.title, poll.custom_url, poll.creation_date],
['pseudo'],
];
const listOfChoices = ['choices : '];
poll.choices.map((choice) => {
listOfChoices.push(choice.name);
});
listOfChoices.push('pseudo');
poll.stacks.map((voteStack) => {
const voteStackInArray = [voteStack.pseudo];
const 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']];
const comments = [];
poll.comments.map((item: Comment) => {
comments.push([item.pseudo, item.text, new Date(item.created_at), '\n']);
});
headers.push(listOfChoices);
rows = [headers, listOfChoices, rows, headersComments, comments];
const convertedCsv = rows
.map((elem) => {
console.log('elem', elem);
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);
const csvContent = 'data:text/csv;charset=utf-8,' + convertedCsv;
console.log('csvContent', csvContent);
const encodedUri = encodeURI(csvContent);
const link = document.createElement('a');
link.setAttribute('href', encodedUri);
const exportFileName =
(poll.urlPublic ? poll.urlPublic : this.makeSlug(poll)) + '_export_' + new Date() + '.csv';
link.setAttribute('download', exportFileName);
document.body.appendChild(link); // Required for FF
link.click(); // This will download the data file named "my_data.csv".
}
download(filename: string, text: string): void {
const element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
}

View File

@ -10,6 +10,7 @@ import { Comment } from '../../core/models/comment.model';
import { StorageService } from '../../core/services/storage.service';
import { ApiService } from '../../core/services/api.service';
import { Stack } from '../../core/models/stack.model';
import { environment } from '../../../environments/environment';
@Component({
selector: 'app-consultation',
@ -50,6 +51,10 @@ export class ConsultationComponent implements OnInit, OnDestroy {
if (newpoll) {
this.isArchived = new Date(newpoll.expiracy_date) < new Date();
this.poll.is_archived = this.isArchived;
if (!environment.production) {
this.addVoteStack();
}
}
});
@ -83,7 +88,9 @@ export class ConsultationComponent implements OnInit, OnDestroy {
this.api.sendVoteStackOfPoll(this.poll, this.storageService.vote_stack).subscribe((resp) => {
console.log('resp', resp);
this.api.getPollBySlug(this.poll.custom_url);
if (resp) {
this.api.getPollBySlug(this.poll.custom_url);
}
});
}
@ -91,85 +98,11 @@ export class ConsultationComponent implements OnInit, OnDestroy {
* export all the poll data available to the public as a CSV single file
*/
exportCSV() {
let rows = [];
const now = new Date();
const headers = [
['export de sondage Framadate ', this.poll.custom_url],
['le', now.toISOString()],
[this.poll.id, this.poll.title, this.poll.custom_url, this.poll.creation_date],
['pseudo'],
];
const listOfChoices = ['choices : '];
this.poll.choices.map((choice) => {
listOfChoices.push(choice.name);
});
listOfChoices.push('pseudo');
this.poll.stacks.map((voteStack) => {
const voteStackInArray = [voteStack.pseudo];
const 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']];
const comments = [];
this.poll.comments.map((item: Comment) => {
comments.push([item.pseudo, item.text, new Date(item.created_at), '\n']);
});
headers.push(listOfChoices);
rows = [headers, listOfChoices, rows, headersComments, comments];
const convertedCsv = rows
.map((elem) => {
console.log('elem', elem);
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);
const csvContent = 'data:text/csv;charset=utf-8,' + convertedCsv;
console.log('csvContent', csvContent);
const encodedUri = encodeURI(csvContent);
const link = document.createElement('a');
link.setAttribute('href', encodedUri);
const exportFileName =
(this.poll.urlPublic ? this.poll.urlPublic : this.utils.makeSlug(this.poll)) +
'_export_' +
new Date() +
'.csv';
link.setAttribute('download', exportFileName);
document.body.appendChild(link); // Required for FF
link.click(); // This will download the data file named "my_data.csv".
this.utils.exportCSV(this.poll);
}
exportJson() {
this.download('export_poll_' + this.pollSlug + '.json', JSON.stringify(this.poll));
}
download(filename, text) {
const element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
this.utils.download('export_poll_' + this.pollSlug + '.json', JSON.stringify(this.poll));
}
duplicate() {