diff --git a/src/app/core/models/choice.model.ts b/src/app/core/models/choice.model.ts index 445fd15d..54ba74f4 100644 --- a/src/app/core/models/choice.model.ts +++ b/src/app/core/models/choice.model.ts @@ -3,7 +3,8 @@ import { User } from './user.model'; export class Choice { constructor( - public label: string, + public id: number, + public name: string, public imageUrl?: string, public participants: Map> = new Map>([ [Answer.YES, new Set()], diff --git a/src/app/core/models/comment.model.ts b/src/app/core/models/comment.model.ts index 4b9fd984..ff47d739 100644 --- a/src/app/core/models/comment.model.ts +++ b/src/app/core/models/comment.model.ts @@ -1,11 +1,16 @@ -export class Comment { - constructor(public author: string, public content: string, public dateCreated: Date) {} +import { User } from './user.model'; - public static sortChronologically(a: Comment, b: Comment): number { - if (a.dateCreated < b.dateCreated) { +export class Comment { + constructor(public owner: User, public text: string, public pseudo: string, public created_at: string) {} + + public static sortChronologically(first: Comment, second: Comment): number { + const a = new Date(first.created_at); + const b = new Date(second.created_at); + + if (a < b) { return -1; } - if (a.dateCreated > b.dateCreated) { + if (a > b) { return 1; } return 0; diff --git a/src/app/core/models/poll.model.ts b/src/app/core/models/poll.model.ts index 61f75ca2..c764df20 100644 --- a/src/app/core/models/poll.model.ts +++ b/src/app/core/models/poll.model.ts @@ -8,9 +8,14 @@ import { User } from './user.model'; export class Poll { constructor( public owner: User = new User(), + public urlPublic: string = '', public slug: string = '', + public id: number = 0, + public default_expiracy_days_from_now: number = 60, public title: string = 'mon titre', + public kind: string, public description?: string, + public custom_url?: string, public expiracy_date?: string, public creation_date?: string, public creatorPseudo?: string, @@ -21,18 +26,13 @@ export class Poll { public comments: Comment[] = [], public choices: Choice[] = [], public votes = [], + public stacks_of_votes = [], + public allowed_answers = [], + public modification_policy = [], public dateChoices: Choice[] = [], // sets of days as strings, config to set identical time for days in a special days poll public timeChoices: Choice[] = [] // ranges of time expressed as strings ) {} - public getAdministrationUrl(): string { - return `${environment.api.baseHref}/administration/polls/${this.slug}`; - } - - public getParticipationUrl(): string { - return `${environment.api.baseHref}/participation/polls/${this.slug}`; - } - public static adaptFromLocalJsonServer( item: Pick ): Poll { @@ -58,4 +58,12 @@ export class Poll { // }) ); } + + public getAdministrationUrl(): string { + return `${environment.api.baseHref}/administration/polls/${this.slug}`; + } + + public getParticipationUrl(): string { + return `${environment.api.baseHref}/participation/polls/${this.slug}`; + } } diff --git a/src/app/features/consultation/consultation.component.html b/src/app/features/consultation/consultation.component.html index 87b7cd45..4351fe6a 100644 --- a/src/app/features/consultation/consultation.component.html +++ b/src/app/features/consultation/consultation.component.html @@ -12,6 +12,40 @@ + + +
+ +
@@ -39,21 +73,39 @@ Detailed
- - - + +
+
+
+ +
+
+ +
+ + +
+
TODO links
- -
-
- -
-
diff --git a/src/app/features/consultation/consultation.component.ts b/src/app/features/consultation/consultation.component.ts index fcb116f6..ac1bce4d 100644 --- a/src/app/features/consultation/consultation.component.ts +++ b/src/app/features/consultation/consultation.component.ts @@ -5,6 +5,8 @@ import { Poll } from '../../core/models/poll.model'; import { ModalService } from '../../core/services/modal.service'; import { PollService } from '../../core/services/poll.service'; import { DateService } from '../../core/services/date.service'; +import { PollUtilities } from '../old-stuff/config/PollUtilities'; +import { Comment } from '../../core/models/comment.model'; @Component({ selector: 'app-consultation', @@ -18,10 +20,18 @@ export class ConsultationComponent implements OnInit, OnDestroy { public passHash: string; public fetching = true; public isArchived: boolean; + + public myVoteStack: any = { + id: '', + }; + public myTempVoteStack: any = { + id: '', + }; private routeSubscription: Subscription; constructor( private router: Router, + private utils: PollUtilities, private _Activatedroute: ActivatedRoute, public pollService: PollService, public dateService: DateService, @@ -58,4 +68,97 @@ export class ConsultationComponent implements OnInit, OnDestroy { this.routeSubscription.unsubscribe(); } } + + updateVote(myVoteStack: boolean) { + alert('TODO'); + } + + addVoteStack() { + alert('TODO'); + } + + /** + * 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_of_votes.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". + } + + 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); + } } diff --git a/src/app/features/old-stuff/config/PollUtilities.ts b/src/app/features/old-stuff/config/PollUtilities.ts index 1a4508a0..30db26cf 100644 --- a/src/app/features/old-stuff/config/PollUtilities.ts +++ b/src/app/features/old-stuff/config/PollUtilities.ts @@ -1,6 +1,7 @@ import { HttpHeaders } from '@angular/common/http'; import { PollConfig } from './PollConfig'; import { Injectable } from '@angular/core'; +import { Poll } from '../../../core/models/poll.model'; @Injectable({ providedIn: 'root', @@ -22,16 +23,18 @@ export class PollUtilities { * make a uniq slug for the current poll creation * @param str */ - makeSlug(config: PollConfig) { + makeSlug(config: Poll) { let str = ''; + const creation = new Date(config.creation_date); + str = - config.creationDate.getFullYear() + + creation.getFullYear() + '_' + - (config.creationDate.getMonth() + 1) + + (creation.getMonth() + 1) + '_' + - config.creationDate.getDate() + + creation.getDate() + '_' + - config.myName + + config.creatorPseudo + '_' + config.title; str = str.replace(/^\s+|\s+$/g, ''); // trim diff --git a/src/app/features/old-stuff/pages/poll/poll-display/poll-display.component.html b/src/app/features/old-stuff/pages/poll/poll-display/poll-display.component.html index e5f4869e..e12211a9 100644 --- a/src/app/features/old-stuff/pages/poll/poll-display/poll-display.component.html +++ b/src/app/features/old-stuff/pages/poll/poll-display/poll-display.component.html @@ -4,7 +4,7 @@