funky-framadate-front/src/app/core/services/storage.service.ts

132 lines
3.5 KiB
TypeScript
Raw Normal View History

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';
import { Stack } from '../models/stack.model';
import { Choice } from '../models/choice.model';
import { Vote } from '../models/vote.model';
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,
defaultTimeOfDay,
2021-04-30 12:37:04 +02:00
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';
2021-05-20 12:51:25 +02:00
import { ToastService } from './toast.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()
public timeSlices: TimeSlices[] = defaultTimeOfDay;
2021-04-30 12:37:04 +02:00
2020-05-05 18:17:12 +02:00
@LocalStorage()
public vote_stack: Stack = new Stack();
2021-05-17 15:25:22 +02:00
@LocalStorage()
public choices: Choice[] = [];
2021-05-17 15:25:22 +02:00
2021-06-07 12:16:56 +02:00
constructor(public dateUtilities: DateUtilitiesService, private toastService: ToastService) {
2021-05-03 10:06:10 +02:00
if (environment.autofill) {
2021-05-20 12:51:25 +02:00
this.toastService.display('autofill des sondages utilisateur');
2021-05-03 10:06:10 +02:00
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
}
setChoicesForVoteStack(choices_list: Choice[]) {
this.vote_stack.votes = [];
// text choices
2021-06-07 12:16:56 +02:00
if (!this.vote_stack.id) {
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);
2021-04-30 12:37:04 +02:00
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) {
2021-04-30 12:37:04 +02:00
for (const vote of this.vote_stack.votes) {
if (vote.choice_id == choice_id) {
if (vote.value == value) {
return true;
}
}
}
return false;
}
2021-06-07 11:30:10 +02:00
/**
* 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;
}
}
});
}
2021-06-07 11:30:10 +02:00
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);
}
2020-04-22 12:56:18 +02:00
}