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

142 lines
3.6 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 { Theme } from '../enums/theme.enum';
import { Stack } from '../models/stack.model';
2022-02-09 12:25:08 +01:00
import { Choice, ChoiceText } from '../models/choice.model';
import { Vote } from '../models/vote.model';
import { environment } from '../../../environments/environment';
2022-02-09 12:25:08 +01:00
import { DateChoice, defaultTimeOfDay, 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()
2022-02-08 13:44:17 +01:00
public language: string;
2020-04-22 12:56:18 +02:00
@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();
2022-02-09 12:25:08 +01:00
/**
* choix de dates
*/
2021-05-17 15:25:22 +02:00
@LocalStorage()
public choices: Choice[] = [];
2021-05-17 15:25:22 +02:00
2022-02-09 12:25:08 +01:00
/**
* choix textes dans un sondage classique
*/
@LocalStorage()
public choicesText: ChoiceText[] = [
{
name: '',
url_display: '',
url_href: '',
},
];
constructor(public dateUtilities: DateUtilitiesService) {
2021-11-12 12:09:48 +01:00
if (environment.autofill_participation) {
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-06-08 14:30:05 +02:00
/**
* set default choices for the vote stack from the poll choices objects
* @param choices_list
*/
setChoicesForVoteStack(choices_list: Choice[]) {
// change only if the poll custom_url changed or if there is no stack id for this poll
2021-06-07 12:16:56 +02:00
if (!this.vote_stack.id) {
2021-06-08 14:30:05 +02:00
this.vote_stack = new Stack();
2021-06-07 12:16:56 +02:00
for (const choice of choices_list) {
// if (environment.autofill_participation) {
// 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(timeSlice.id, defaultvalue));
// } else {
this.vote_stack.votes.push(new Vote(choice.id));
// }
}
}
}
2021-06-08 14:30:05 +02:00
/**
* look for a timeSlice in the stored vote stack and change it answer
2021-06-08 14:30:05 +02:00
* @param choice_id
* @param value
*/
toggleAnswer(choice_id: number, value: string) {
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;
}
}
}
}
2021-06-08 14:30:05 +02:00
/**
* check for the value of a timeSlice in the stored vote stack
2021-06-08 14:30:05 +02:00
* @param choice_id
* @param 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 timeSlice to the same answer at once
2021-06-07 11:30:10 +02:00
* @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
2021-06-08 14:30:05 +02:00
/**
* update vote stack from the backend
* @param voteStack
2021-06-08 14:30:05 +02:00
*/
mapVotes(voteStack: Stack) {
console.log('mapVotes voteStack', voteStack);
this.vote_stack = voteStack;
2021-06-07 11:30:10 +02:00
}
2020-04-22 12:56:18 +02:00
}