funky-framadate-front/src/app/core/models/choice.model.ts

54 lines
1.4 KiB
TypeScript
Raw Normal View History

2020-06-12 19:17:39 +02:00
import { Answer } from '../enums/answer.enum';
import { Owner } from './owner.model';
2020-05-05 18:17:12 +02:00
2021-05-20 11:04:29 +02:00
export class ChoiceGroup {
date_string: string;
subSetToYes = false; // to know if all the choices are set to YES, to toggle them all at once without checking them individually
2021-05-20 11:04:29 +02:00
choices: Choice[];
}
2020-05-05 18:17:12 +02:00
export class Choice {
2021-04-27 12:14:57 +02:00
public id: number;
public name: string;
public created_at: string;
public score: number;
public enabled: boolean;
public url?: string;
2021-04-27 13:04:32 +02:00
public yes?: any;
public no?: any;
public maybe?: any;
2021-04-27 12:14:57 +02:00
2020-05-12 19:16:23 +02:00
constructor(
public participants: Map<Answer, Set<Owner>> = new Map<Answer, Set<Owner>>([
[Answer.YES, new Set<Owner>()],
[Answer.NO, new Set<Owner>()],
[Answer.MAYBE, new Set<Owner>()],
2020-05-12 19:16:23 +02:00
]),
2020-06-12 19:17:39 +02:00
public counts: Map<Answer, number> = new Map<Answer, number>([
[Answer.YES, 0],
[Answer.NO, 0],
[Answer.MAYBE, 0],
2020-05-12 19:16:23 +02:00
])
) {}
public updateParticipation(user: Owner, responseType: Answer): void {
2020-06-12 19:17:39 +02:00
this.removeParticipant(user);
this.participants.get(responseType).add(user);
2020-05-12 19:16:23 +02:00
this.updateCounts();
}
public removeParticipant(user: Owner): void {
2020-06-12 19:17:39 +02:00
for (const responseType of Object.values(Answer)) {
if (this.participants.get(responseType).has(user)) {
this.participants.get(responseType).delete(user);
2020-05-12 19:16:23 +02:00
}
}
}
public updateCounts(): void {
2020-06-12 19:17:39 +02:00
for (const responseType of Object.values(Answer)) {
2020-05-12 19:16:23 +02:00
this.counts.set(responseType, this.participants.get(responseType).size);
}
}
2020-05-05 18:17:12 +02:00
}