From 06cf447529583f05789e17f9d9b9d196f0a56409 Mon Sep 17 00:00:00 2001 From: Baptiste Lemoine Date: Mon, 20 Jan 2020 15:58:35 +0100 Subject: [PATCH] :zap: update vote list after vote creation --- src/app/config/mocks/mock-poll3.ts | 2 +- .../poll-display/poll-display.component.ts | 6 ++++-- src/app/services/config.service.ts | 20 +++++++++++-------- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/app/config/mocks/mock-poll3.ts b/src/app/config/mocks/mock-poll3.ts index 9a917f6e..b518e87d 100644 --- a/src/app/config/mocks/mock-poll3.ts +++ b/src/app/config/mocks/mock-poll3.ts @@ -237,4 +237,4 @@ export const mockPoll3 = { } ], "comments": mockComments -} +}; diff --git a/src/app/pages/poll-display/poll-display.component.ts b/src/app/pages/poll-display/poll-display.component.ts index d1705f95..f1a49e93 100644 --- a/src/app/pages/poll-display/poll-display.component.ts +++ b/src/app/pages/poll-display/poll-display.component.ts @@ -12,7 +12,7 @@ import {ActivatedRoute} from "@angular/router"; }) export class PollDisplayComponent extends BaseComponent implements OnInit { - private pollConfigFetched = mockPoll3; + private pollConfigFetched; private comments = mockComments; constructor(public config: ConfigService, @@ -21,6 +21,8 @@ export class PollDisplayComponent extends BaseComponent implements OnInit { } ngOnInit() { + this.pollConfigFetched = this.config.currentPoll; + // fetch poll with its ID or slug. const id = this.activeRoute.snapshot.params.poll; if (id) { @@ -28,7 +30,7 @@ export class PollDisplayComponent extends BaseComponent implements OnInit { // store it in the poll property here this.config.getPollById(id).subscribe( (res: any) => { - this.pollConfigFetched = res; + this.config.currentPoll = res; }, (e) => { // handle need for a password this.config.handleError(e) diff --git a/src/app/services/config.service.ts b/src/app/services/config.service.ts index 6c7594fe..75b6aa81 100644 --- a/src/app/services/config.service.ts +++ b/src/app/services/config.service.ts @@ -4,6 +4,7 @@ import {HttpClient, HttpHeaders} from "@angular/common/http"; import {environment} from "../../environments/environment"; import {ConfirmationService, MessageService} from 'primeng/api'; import {Router} from "@angular/router"; +import {mockPoll3} from "../config/mocks/mock-poll3"; /** * le service transverse à chaque page qui permet de syncroniser la configuration de sondage souhaitée @@ -13,6 +14,7 @@ import {Router} from "@angular/router"; }) export class ConfigService extends PollConfig { + currentPoll: any = mockPoll3; loading: boolean = false; baseHref: any = environment.baseApiHref; @@ -307,8 +309,7 @@ export class ConfigService extends PollConfig { .subscribe((res: any) => { this.messageService.add({severity: 'success', summary: 'Vote ajouté'}); - alert("succès!"); - this.myPolls = res; + this.currentPoll = res; }, (e) => { this.handleError(e) } @@ -327,7 +328,7 @@ export class ConfigService extends PollConfig { this.makeHeaders()) .subscribe((res: any) => { this.messageService.add({severity: 'success', summary: 'Vote mis à jour'}); - this.myPolls = res; + this.currentPoll = res; }, (e) => { this.handleError(e) } @@ -340,10 +341,11 @@ export class ConfigService extends PollConfig { * @param comment */ addComment(comment?: any) { - if (!comment) { + if (!comment && this.myComment) { comment = { - pseudo: this.myName, - comment: this.myComment, + name: this.myName, + date: new Date(), + text: this.myComment, } } this.http.post( @@ -358,8 +360,10 @@ export class ConfigService extends PollConfig { }); // empty comment after success this.myComment = ''; + this.currentPoll.comments.push(comment); }, (e) => { - this.handleError(e) + this.handleError(e); + this.currentPoll.comments.push(comment); } ); } @@ -463,7 +467,7 @@ export class ConfigService extends PollConfig { severity: 'success', summary: 'Sondage mis à jour', }); - this.myPolls = res; + this.currentPoll = res; }, (e) => { this.handleError(e) }