From bbebaa4aaef32d0f9f1d79059baf21a117525640 Mon Sep 17 00:00:00 2001 From: Tykayn Date: Mon, 14 Feb 2022 14:37:42 +0100 Subject: [PATCH] fix add anonymous comment --- src/app/core/models/comment.model.ts | 4 +++ src/app/core/models/stack.model.ts | 2 +- src/app/core/services/api.service.ts | 9 +++--- src/app/core/services/poll.service.ts | 29 ++++++++++--------- .../components/comments/comments.component.ts | 7 ++++- 5 files changed, 30 insertions(+), 21 deletions(-) diff --git a/src/app/core/models/comment.model.ts b/src/app/core/models/comment.model.ts index 8b9f939b..82af5aa8 100644 --- a/src/app/core/models/comment.model.ts +++ b/src/app/core/models/comment.model.ts @@ -1,3 +1,7 @@ +export interface CommentDTO { + pseudo: string; + text: string; +} export class Comment { constructor(public id: string, public text: string, public pseudo: string, public created_at: string) {} diff --git a/src/app/core/models/stack.model.ts b/src/app/core/models/stack.model.ts index ad9492ac..fc0498de 100644 --- a/src/app/core/models/stack.model.ts +++ b/src/app/core/models/stack.model.ts @@ -6,7 +6,7 @@ export class Stack { public custom_url: string; public pass_hash: string; public pseudo = 'Choque Nourrice'; - public comment = 'Le beau commentaire de Choque Nourrice'; + public comment = 'Le beau commentaire tout neuf'; public owner: Owner = new Owner(); public votes: Vote[] = []; } diff --git a/src/app/core/services/api.service.ts b/src/app/core/services/api.service.ts index d29aec26..04389258 100644 --- a/src/app/core/services/api.service.ts +++ b/src/app/core/services/api.service.ts @@ -9,6 +9,7 @@ import { Subscription } from 'rxjs'; import { ToastService } from './toast.service'; import { LoaderService } from './loader.service'; import { Stack } from '../models/stack.model'; +import { Comment, CommentDTO } from '../models/comment.model'; const apiVersion = environment.api.versionToUse; const currentApiRoutes = environment.api.version[apiVersion]; @@ -140,7 +141,7 @@ export class ApiService { } } - public async createComment(slug: string, comment: string): Promise { + public async createComment(slug: string, comment: Comment | CommentDTO): Promise { try { return await this.axiosInstance.post(`${this.baseHref}${this.commentsEndpoint}/${slug}`, comment); } catch (error) { @@ -293,11 +294,9 @@ export class ApiService { } } - public async deletePollComments(slug: string): Promise { + public async deletePollComments(admin_key: string): Promise { try { - const response: AxiosResponse = await this.axiosInstance.delete( - `${this.pollsEndpoint}/${slug}${this.commentsEndpoint}` - ); + const response: AxiosResponse = await this.axiosInstance.delete(`${this.pollsEndpoint}/${admin_key}`); return response?.status === 204; } catch (error) { ApiService.handleError(error); diff --git a/src/app/core/services/poll.service.ts b/src/app/core/services/poll.service.ts index 704090e1..5cc43515 100644 --- a/src/app/core/services/poll.service.ts +++ b/src/app/core/services/poll.service.ts @@ -23,6 +23,7 @@ import { Stack } from '../models/stack.model'; import { Vote } from '../models/vote.model'; import { ClipboardService } from 'ngx-clipboard'; import { TranslateService } from '@ngx-translate/core'; +import { CommentDTO } from '../models/comment.model'; @Injectable({ providedIn: 'root', @@ -283,6 +284,10 @@ export class PollService implements Resolve { } } + /** + * load a poll data and update the current poll of PollService + * @param custom_url + */ public async loadPollByCustomUrl(custom_url: string): Promise { if (custom_url) { const poll: Poll | undefined = await this.apiService.getPollByCustomUrl(custom_url); @@ -321,17 +326,6 @@ export class PollService implements Resolve { */ public updateCurrentPoll(poll: Poll, displayToast = false): Poll { console.log('this.storageService.vote_stack.id', this.storageService.vote_stack.id); - - // if (!this.storageService.vote_stack.id || this.storageService.vote_stack.custom_url !== poll.custom_url) { - // console.log('set base choices', poll.choices); - // // set the choices only the first time the poll loads, or if we changed the poll - // console.log( - // 'this.storageService.vote_stack.custom_url', - // this.storageService.vote_stack.custom_url - // ); - // this.storageService.setChoicesForVoteStack(poll.choices); - // } - this._poll.next(poll); console.log('next poll', poll); @@ -602,9 +596,16 @@ export class PollService implements Resolve { this.toastService.display('Les participations des votants à ce sondage ont été supprimées.'); } - public async addComment(comment: string): Promise { - await this.apiService.createComment(this._poll.getValue().custom_url, comment); - this.toastService.display('Votre commentaire a été enregistré.'); + public async addComment(comment: CommentDTO): Promise { + await this.apiService.createComment(this._poll.getValue().custom_url, comment).then( + (resp) => { + console.log('resp', resp); + this.loadPollByCustomUrl(this._poll.getValue().custom_url); + console.log('resp', resp); + this.toastService.display('Votre commentaire a été enregistré.'); + }, + (err) => this.apiService.ousideHandleError(err) + ); } public async deleteComments(): Promise { diff --git a/src/app/shared/components/comments/comments.component.ts b/src/app/shared/components/comments/comments.component.ts index 19cd0016..55cb90c7 100644 --- a/src/app/shared/components/comments/comments.component.ts +++ b/src/app/shared/components/comments/comments.component.ts @@ -41,8 +41,13 @@ export class CommentsComponent { addComment() { this.api - .createComment(this.pollService._poll.getValue().custom_url, this.storageService.vote_stack.comment) + .createComment(this.pollService._poll.getValue().custom_url, { + pseudo: this.storageService.vote_stack.pseudo, + text: this.storageService.vote_stack.comment, + }) .then((resp) => { + console.log('resp', resp); + // this.poll.comments.push() this.toastService.display('commentaire ajouté'); }) .catch((error) => {