funky-framadate-front/src/app/features/consultation/consultation.component.ts

182 lines
5.1 KiB
TypeScript
Raw Normal View History

2020-06-18 16:15:26 +02:00
import { Component, OnDestroy, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { Subscription } from 'rxjs';
2020-05-12 19:16:23 +02:00
import { Poll } from '../../core/models/poll.model';
import { ModalService } from '../../core/services/modal.service';
2021-04-21 12:43:05 +02:00
import { PollService } from '../../core/services/poll.service';
2021-04-26 10:32:42 +02:00
import { DateService } from '../../core/services/date.service';
2021-04-26 11:27:44 +02:00
import { PollUtilities } from '../old-stuff/config/PollUtilities';
import { Comment } from '../../core/models/comment.model';
import { StorageService } from '../../core/services/storage.service';
import { ApiService } from '../../core/services/api.service';
2020-05-12 19:16:23 +02:00
@Component({
selector: 'app-consultation',
templateUrl: './consultation.component.html',
styleUrls: ['./consultation.component.scss'],
})
2020-06-18 16:15:26 +02:00
export class ConsultationComponent implements OnInit, OnDestroy {
2021-04-27 12:38:12 +02:00
public isCompactMode = true;
2020-06-18 16:15:26 +02:00
public poll: Poll;
2021-04-21 12:43:05 +02:00
public pollSlug: string;
public passHash: string;
2021-04-26 10:32:42 +02:00
public fetching = true;
2020-06-18 16:15:26 +02:00
public isArchived: boolean;
2021-04-26 11:27:44 +02:00
public myVoteStack: any = {
id: '',
};
public myTempVoteStack: any = {
id: '',
};
2020-06-18 16:15:26 +02:00
private routeSubscription: Subscription;
2020-05-12 19:16:23 +02:00
constructor(
2020-06-18 16:15:26 +02:00
private router: Router,
2021-04-26 11:27:44 +02:00
private utils: PollUtilities,
2021-04-21 12:43:05 +02:00
private _Activatedroute: ActivatedRoute,
public storageService: StorageService,
public api: ApiService,
2021-04-21 12:43:05 +02:00
public pollService: PollService,
2021-04-26 10:32:42 +02:00
public dateService: DateService,
2020-05-12 19:16:23 +02:00
private modalService: ModalService
) {}
ngOnInit(): void {
2021-04-21 12:43:05 +02:00
console.log('constultation de poll');
this.pollService.poll.subscribe((newpoll: Poll) => {
this.poll = newpoll;
2021-04-26 10:32:42 +02:00
if (newpoll) {
this.isArchived = new Date(newpoll.expiracy_date) < new Date();
this.poll.is_archived = this.isArchived;
2021-04-26 10:32:42 +02:00
}
2021-04-21 12:43:05 +02:00
});
this._Activatedroute.paramMap.subscribe((params) => {
console.log('params', params);
this.pollSlug = params.get('slug');
this.passHash = params.get('hash');
2021-04-24 12:31:34 +02:00
console.log('hash, slug', this.passHash, this.pollSlug);
2021-04-21 12:43:05 +02:00
if (this.passHash) {
this.pollService.loadPollBySlugWithPasswordHash(this.pollSlug, this.passHash);
2020-06-18 16:15:26 +02:00
} else {
2021-04-21 12:43:05 +02:00
this.pollService.loadPollBySlug(this.pollSlug).then((resp) => {
console.log('resp', resp);
this.fetching = false;
});
2020-06-18 16:15:26 +02:00
}
});
2020-05-12 19:16:23 +02:00
}
2020-06-18 16:15:26 +02:00
ngOnDestroy(): void {
if (this.routeSubscription) {
this.routeSubscription.unsubscribe();
}
2020-05-12 19:16:23 +02:00
}
2021-04-26 11:27:44 +02:00
updateVote() {
2021-04-26 11:27:44 +02:00
alert('TODO');
}
addVoteStack() {
2021-04-28 15:35:08 +02:00
this.api.sendVoteStackOfPoll(this.poll, this.storageService.vote_stack).subscribe((resp) => {
console.log('resp', resp);
this.api.getPollBySlug(this.poll.custom_url);
});
2021-04-26 11:27:44 +02:00
}
/**
* 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');
2021-04-26 17:04:16 +02:00
this.poll.stacks.map((voteStack) => {
2021-04-26 11:27:44 +02:00
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".
}
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);
}
duplicate() {
alert('TODO');
}
print() {
alert('TODO');
}
2020-05-12 19:16:23 +02:00
}