import { Injectable } from '@angular/core'; import { LocalStorage } from 'ngx-webstorage'; import { Language } from '../enums/language.enum'; import { Theme } from '../enums/theme.enum'; import { Stack } from '../models/stack.model'; import { Choice } from '../models/choice.model'; import { Vote } from '../models/vote.model'; import { environment } from '../../../environments/environment'; import { basicSlicesOfDay, DateChoice, defaultTimeOfDay, moreTimeOfDay, otherDefaultDates, TimeSlices, } from '../../../../mocks/old-stuff/config/defaultConfigs'; import { Poll } from '../models/poll.model'; import { Owner } from '../models/owner.model'; import { DateUtilitiesService } from './date.utilities.service'; import { ToastService } from './toast.service'; @Injectable({ providedIn: 'root', }) export class StorageService { @LocalStorage() public theme: Theme; @LocalStorage() public language: Language; @LocalStorage() public userPolls: Poll[] = []; @LocalStorage() public dateChoices: DateChoice[] = []; @LocalStorage() public timeSlices: TimeSlices[] = defaultTimeOfDay; @LocalStorage() public vote_stack: Stack = new Stack(); @LocalStorage() public choices: Choice[] = []; constructor(public dateUtilities: DateUtilitiesService, private toastService: ToastService) { if (environment.autofill) { this.toastService.display('autofill des sondages utilisateur'); 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') ); } if (!this.dateChoices.length) { this.dateChoices = this.dateUtilities.makeDefaultDateChoices(); } } setChoicesForVoteStack(choices_list: Choice[]) { // text choices if (!this.vote_stack.id) { this.vote_stack.votes = []; for (const choice of choices_list) { if (environment.autofill) { console.log('autofill au hasard des votes à ce sondage'); this.toastService.display('autofill au hasard des votes à ce sondage'); const defaultvalue = Math.random() > 0.75 ? 'yes' : ''; this.vote_stack.votes.push(new Vote(choice.id, defaultvalue)); } else { this.vote_stack.votes.push(new Vote(choice.id)); } } } } toggleAnswer(choice_id: number, value: string) { console.log('this.vote_stack', this.vote_stack); for (const vote of this.vote_stack.votes) { if (vote.choice_id == choice_id) { if (vote.value == value) { vote.value = ''; } else { vote.value = value; } } } } choiceHasAnswerOfValue(choice_id: number, value: any) { for (const vote of this.vote_stack.votes) { if (vote.choice_id == choice_id) { if (vote.value == value) { return true; } } } return false; } /** * set all time slices of a choice to the same answer at once * @param groupe * @param newAnswer */ setAllSubchoicesTo(groupe, newAnswer = 'yes') { groupe.choices.map((choice) => { for (const vote of this.vote_stack.votes) { if (vote.choice_id == choice.id) { vote.value = newAnswer; } } }); } mapVotes(resp) { console.log('data', resp.data); console.log('this.vote_stack', this.vote_stack); this.vote_stack.votes = []; this.vote_stack.owner = resp.data.owner; this.vote_stack.id = resp.data.id; for (const vote of resp.data.votes) { this.vote_stack.votes.push(vote); } console.log('this.vote_stack', this.vote_stack); } }