2020-04-19 14:22:10 +02:00
|
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
|
import { BehaviorSubject, Observable } from 'rxjs';
|
|
|
|
|
|
2020-05-01 19:10:17 +02:00
|
|
|
|
import { MessageSeverity } from '../enums/message-severity.enum';
|
2020-05-12 19:16:23 +02:00
|
|
|
|
import { ResponseType } from '../enums/response-type.enum';
|
|
|
|
|
import { Choice } from '../models/choice.model';
|
2020-04-19 14:22:10 +02:00
|
|
|
|
import { Poll } from '../models/poll.model';
|
2020-05-12 19:16:23 +02:00
|
|
|
|
import { User } from '../models/user.model';
|
2020-04-19 14:22:10 +02:00
|
|
|
|
import { ApiService } from './api.service';
|
2020-05-01 19:10:17 +02:00
|
|
|
|
import { MessageDisplayerService } from './message-displayer.service';
|
2020-04-19 14:22:10 +02:00
|
|
|
|
|
|
|
|
|
@Injectable({
|
2020-04-21 17:26:25 +02:00
|
|
|
|
providedIn: 'root',
|
2020-04-19 14:22:10 +02:00
|
|
|
|
})
|
|
|
|
|
export class PollService {
|
2020-04-21 17:26:25 +02:00
|
|
|
|
private _poll: BehaviorSubject<Poll | undefined> = new BehaviorSubject<Poll | undefined>(undefined);
|
2020-04-22 12:56:18 +02:00
|
|
|
|
public readonly poll: Observable<Poll | undefined> = this._poll.asObservable();
|
2020-04-19 14:22:10 +02:00
|
|
|
|
|
2020-05-12 19:16:23 +02:00
|
|
|
|
constructor(private apiService: ApiService, private messageDisplayerService: MessageDisplayerService) {}
|
2020-04-19 14:22:10 +02:00
|
|
|
|
|
2020-04-21 17:26:25 +02:00
|
|
|
|
public updateCurrentPoll(poll: Poll): void {
|
|
|
|
|
this._poll.next(poll);
|
|
|
|
|
}
|
2020-04-19 14:22:10 +02:00
|
|
|
|
|
2020-05-12 19:16:23 +02:00
|
|
|
|
public async getPollBySlug(slug: string): Promise<void> {
|
|
|
|
|
const poll: Poll | undefined = await this.apiService.getPollBySlug(slug);
|
|
|
|
|
this.updateCurrentPoll(poll);
|
2020-04-21 17:26:25 +02:00
|
|
|
|
}
|
2020-04-19 14:22:10 +02:00
|
|
|
|
|
2020-05-12 19:16:23 +02:00
|
|
|
|
public async saveCurrentPoll(): Promise<void> {
|
|
|
|
|
const pollUrl: string = await this.apiService.createPoll(this._poll.getValue());
|
|
|
|
|
// TODO: Maybe handle the url to update currentPoll according to backend response
|
|
|
|
|
if (pollUrl) {
|
|
|
|
|
this.messageDisplayerService.display(MessageSeverity.SUCCESS, 'Le sondage a été enregistré.');
|
|
|
|
|
} else {
|
|
|
|
|
this.messageDisplayerService.display(
|
|
|
|
|
MessageSeverity.ERROR,
|
|
|
|
|
'Le sondage n’a été correctement enregistré, veuillez ré-essayer.'
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-04-21 17:26:25 +02:00
|
|
|
|
}
|
2020-04-19 14:22:10 +02:00
|
|
|
|
|
2020-05-12 19:16:23 +02:00
|
|
|
|
public saveParticipation(choice: Choice, user: User, response: ResponseType): void {
|
|
|
|
|
const currentPoll = this._poll.getValue();
|
|
|
|
|
currentPoll.choices.find((c) => c.label === choice.label)?.updateParticipation(user.pseudo, response);
|
|
|
|
|
this.updateCurrentPoll(currentPoll);
|
|
|
|
|
this.apiService.createParticipation(currentPoll.slug, choice.label, user.pseudo, response);
|
|
|
|
|
this.messageDisplayerService.display(
|
|
|
|
|
MessageSeverity.SUCCESS,
|
|
|
|
|
'Votre participation au sondage a été enregistrée.'
|
|
|
|
|
);
|
2020-04-21 17:26:25 +02:00
|
|
|
|
}
|
2020-04-19 14:22:10 +02:00
|
|
|
|
|
2020-05-12 19:16:23 +02:00
|
|
|
|
public async deleteAllAnswers(): Promise<void> {
|
|
|
|
|
await this.apiService.deletePollAnswers(this._poll.getValue().slug);
|
|
|
|
|
this.messageDisplayerService.display(
|
2020-05-01 19:10:17 +02:00
|
|
|
|
MessageSeverity.SUCCESS,
|
|
|
|
|
'Les participations des votants à ce sondage ont été supprimées.'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-12 19:16:23 +02:00
|
|
|
|
public async addComment(comment: string): Promise<void> {
|
|
|
|
|
await this.apiService.createComment(this._poll.getValue().slug, comment);
|
|
|
|
|
this.messageDisplayerService.display(MessageSeverity.SUCCESS, 'Votre commentaire a été enregistré.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async deleteComments(): Promise<void> {
|
|
|
|
|
await this.apiService.deletePollComments(this._poll.getValue().slug);
|
|
|
|
|
this.messageDisplayerService.display(
|
|
|
|
|
MessageSeverity.SUCCESS,
|
|
|
|
|
'Les commentaires de ce sondage ont été supprimés.'
|
|
|
|
|
);
|
2020-04-21 17:26:25 +02:00
|
|
|
|
}
|
2020-04-19 14:22:10 +02:00
|
|
|
|
}
|