2020-04-22 12:56:18 +02:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { LocalStorage } from 'ngx-webstorage';
|
|
|
|
|
|
|
|
import { Language } from '../enums/language.enum';
|
|
|
|
import { Theme } from '../enums/theme.enum';
|
2021-04-28 12:01:09 +02:00
|
|
|
import { Stack } from '../models/stack.model';
|
2021-04-28 12:15:49 +02:00
|
|
|
import { Choice } from '../models/choice.model';
|
|
|
|
import { Vote } from '../models/vote.model';
|
2021-04-28 14:33:41 +02:00
|
|
|
import { environment } from '../../../environments/environment';
|
2021-04-30 12:37:04 +02:00
|
|
|
import {
|
2021-05-03 15:34:26 +02:00
|
|
|
basicSlicesOfDay,
|
2021-04-30 12:37:04 +02:00
|
|
|
DateChoice,
|
|
|
|
moreTimeOfDay,
|
|
|
|
otherDefaultDates,
|
|
|
|
TimeSlices,
|
|
|
|
} from '../../../../mocks/old-stuff/config/defaultConfigs';
|
2021-05-03 10:06:10 +02:00
|
|
|
import { Poll } from '../models/poll.model';
|
|
|
|
import { Owner } from '../models/owner.model';
|
2021-05-17 15:25:22 +02:00
|
|
|
import { DateUtilitiesService } from './date.utilities.service';
|
2020-04-22 12:56:18 +02:00
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root',
|
|
|
|
})
|
|
|
|
export class StorageService {
|
|
|
|
@LocalStorage()
|
|
|
|
public theme: Theme;
|
|
|
|
|
|
|
|
@LocalStorage()
|
|
|
|
public language: Language;
|
|
|
|
|
|
|
|
@LocalStorage()
|
2021-05-03 10:06:10 +02:00
|
|
|
public userPolls: Poll[] = [];
|
2020-05-05 18:17:12 +02:00
|
|
|
|
2021-04-30 12:37:04 +02:00
|
|
|
@LocalStorage()
|
2021-05-18 12:12:08 +02:00
|
|
|
public dateChoices: DateChoice[] = [];
|
2021-04-30 12:37:04 +02:00
|
|
|
|
|
|
|
@LocalStorage()
|
2021-05-03 15:47:27 +02:00
|
|
|
public timeSlices: TimeSlices[];
|
2021-04-30 12:37:04 +02:00
|
|
|
|
2020-05-05 18:17:12 +02:00
|
|
|
@LocalStorage()
|
2021-04-28 12:01:09 +02:00
|
|
|
public vote_stack: Stack = new Stack();
|
2021-04-28 12:15:49 +02:00
|
|
|
|
2021-05-17 15:25:22 +02:00
|
|
|
@LocalStorage()
|
|
|
|
public choices: Choice[];
|
|
|
|
|
|
|
|
constructor(public dateUtilities: DateUtilitiesService) {
|
2021-05-03 10:06:10 +02:00
|
|
|
if (environment.autofill) {
|
|
|
|
this.userPolls.push(new Poll(new Owner(), 'Démo: Anniversaire de tonton Patrick', 'aujourdhui-ou-demain'));
|
|
|
|
this.userPolls.push(new Poll(new Owner(), 'Démo: Atelier cuisine du quartier', 'aujourdhui-ou-demain'));
|
|
|
|
this.userPolls.push(
|
|
|
|
new Poll(new Owner(), 'Démo: Réunion du département des chatons', 'aujourdhui-ou-demain')
|
|
|
|
);
|
|
|
|
}
|
2021-05-17 15:25:22 +02:00
|
|
|
|
2021-05-18 12:12:08 +02:00
|
|
|
if (!this.dateChoices.length) {
|
|
|
|
this.dateChoices = this.dateUtilities.makeDefaultDateChoices();
|
2021-05-17 16:15:54 +02:00
|
|
|
}
|
2021-05-03 10:06:10 +02:00
|
|
|
}
|
|
|
|
|
2021-04-28 12:15:49 +02:00
|
|
|
setChoicesForVoteStack(choices_list: Choice[]) {
|
|
|
|
this.vote_stack.votes = [];
|
|
|
|
|
2021-04-30 14:15:21 +02:00
|
|
|
// text choices
|
2021-04-30 12:37:04 +02:00
|
|
|
for (const choice of choices_list) {
|
2021-04-28 14:33:41 +02:00
|
|
|
if (environment.autofill) {
|
|
|
|
this.vote_stack.votes.push(new Vote(choice.id, 'yes'));
|
|
|
|
} else {
|
|
|
|
this.vote_stack.votes.push(new Vote(choice.id));
|
|
|
|
}
|
2021-04-28 12:15:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleAnswer(choice_id: number, value: string) {
|
2021-04-30 15:49:43 +02:00
|
|
|
console.log('this.vote_stack', this.vote_stack);
|
2021-04-30 12:37:04 +02:00
|
|
|
for (const vote of this.vote_stack.votes) {
|
2021-04-28 12:15:49 +02:00
|
|
|
if (vote.choice_id == choice_id) {
|
2021-04-28 12:22:40 +02:00
|
|
|
if (vote.value == value) {
|
2021-04-28 12:15:49 +02:00
|
|
|
vote.value = '';
|
|
|
|
} else {
|
|
|
|
vote.value = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-28 12:22:40 +02:00
|
|
|
|
|
|
|
choiceHasAnswerOfValue(choice_id: number, value: any) {
|
2021-04-30 12:37:04 +02:00
|
|
|
for (const vote of this.vote_stack.votes) {
|
2021-04-28 12:22:40 +02:00
|
|
|
if (vote.choice_id == choice_id) {
|
|
|
|
if (vote.value == value) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-22 12:56:18 +02:00
|
|
|
}
|