diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 52e5c17e..58d1a803 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,7 +1,9 @@ import {Component, Inject} from '@angular/core'; import {TranslateService} from '@ngx-translate/core'; -import {Router} from '@angular/router'; +import {NavigationStart, Router} from '@angular/router'; import {DOCUMENT} from "@angular/common"; +import {filter} from "rxjs/operators"; +import {ConfigService} from "./services/config.service"; @Component({ selector: 'app-root', @@ -22,6 +24,7 @@ export class AppComponent { step: string; constructor(private translate: TranslateService, + private config : ConfigService, @Inject(DOCUMENT) private document, private route: Router) { this.translate.setDefaultLang(this.currentLang); @@ -31,20 +34,16 @@ export class AppComponent { detectCurrentTabOnRouteChange() { this.route.events.subscribe((event: any) => { - this.scrollGoToTop(); - console.log('event', event) - if (event.url) { - const tab = event.url.split('/'); - if (tab && tab[2]) { - this.step = tab[2]; - } else { - this.step = 'home'; - } - - } }); + this.route.events.pipe(filter(event => event instanceof NavigationStart)).subscribe((event:NavigationStart) => { + this.scrollGoToTop(); + this.updateCurrentTab(event); + // only if there is a poll ID + this.config.fetchPollFromRoute(event); + }) + } switchLanguage(language: string) { @@ -76,4 +75,15 @@ export class AppComponent { scrollGoToTop() { this.document.documentElement.scrollTop = 0; } + + updateCurrentTab(event){ + if (event.url) { + const tab = event.url.split('/'); + if (tab && tab[2]) { + this.step = tab[2]; + } else { + this.step = 'home'; + } + } + } } diff --git a/src/app/config/PollConfig.ts b/src/app/config/PollConfig.ts index 3b3a0269..8dccd035 100644 --- a/src/app/config/PollConfig.ts +++ b/src/app/config/PollConfig.ts @@ -41,6 +41,7 @@ export class PollConfig { creationDate = new Date(); expirationDate = ''; // expiracy date pollId = null; // id of the current poll when created. data given by the backend api + pollSlug = null; // id of the current poll when created. data given by the backend api currentPoll; // current poll selected with createPoll or getPoll of ConfigService passwordAccess = 0; password = ''; diff --git a/src/app/pages/poll-display/poll-display.component.ts b/src/app/pages/poll-display/poll-display.component.ts index 7c27cf9f..bf672ced 100644 --- a/src/app/pages/poll-display/poll-display.component.ts +++ b/src/app/pages/poll-display/poll-display.component.ts @@ -2,7 +2,7 @@ import {Component, OnInit} from '@angular/core'; import {BaseComponent} from "../base-page/base.component"; import {ConfigService} from "../../services/config.service"; import {mockComments} from "../../config/mocks/mock-comments"; -import {ActivatedRoute, Router, RouterEvent} from "@angular/router"; +import {ActivatedRoute, Router} from "@angular/router"; @Component({ selector: 'framadate-poll-display', @@ -17,13 +17,12 @@ export class PollDisplayComponent extends BaseComponent implements OnInit { private router: Router, public activeRoute: ActivatedRoute) { super(config); - - router.events.subscribe((val: RouterEvent) => { - // see also - console.log(this.activeRoute); - if (val.url && !this.config.loading) { + this.activeRoute.paramMap.subscribe(params => { + console.log('params', params) + this.config.pollId = params.get('poll'); + this.config.pollSlug = params.get('pollSlug'); + if (!this.config.loading) { this.fetchPoll(); - } }); } @@ -41,11 +40,12 @@ export class PollDisplayComponent extends BaseComponent implements OnInit { if (id) { this.config.loading = true; + this.config.pollId = id; // store it in the poll property here this.config.getPollById(id).subscribe( (res: any) => { console.log('res', res) - this.config.pollId = id; + this.config.currentPoll = res; this.config.loading = false; }, (e) => { diff --git a/src/app/pages/voting/voting-comment/voting-comment.component.html b/src/app/pages/voting/voting-comment/voting-comment.component.html index b4eb6f5b..ba135be9 100644 --- a/src/app/pages/voting/voting-comment/voting-comment.component.html +++ b/src/app/pages/voting/voting-comment/voting-comment.component.html @@ -3,7 +3,7 @@ {{comment.pseudo}} , le - {{comment.date.date | date:'medium'}} + {{comment.date.date }}

diff --git a/src/app/services/config.service.ts b/src/app/services/config.service.ts index 7d8a3e8c..04867ae7 100644 --- a/src/app/services/config.service.ts +++ b/src/app/services/config.service.ts @@ -244,7 +244,10 @@ export class ConfigService extends PollConfig { return this.http .get(`${this.baseHref}/poll/${id}`, this.makeHeaders({body: password})) + } + fetchPollFromRoute(event) { + console.log('time to fetch poll', event) } /** @@ -273,8 +276,6 @@ export class ConfigService extends PollConfig { createPoll() { this.loading = true; this.createPollFromConfig(this.getPollConfig()) - - } /** @@ -335,6 +336,7 @@ export class ConfigService extends PollConfig { votes: this.convertChoicesAnsweredToSend(this.currentPoll.choices), } } + console.log('voteStack', voteStack) this.http.post( `${this.baseHref}/poll/${this.currentPoll.id}/vote`, voteStack, @@ -383,8 +385,9 @@ export class ConfigService extends PollConfig { text: this.myComment, } } + console.log('comment', comment) this.http.post( - `${this.baseHref}/poll/${this.currentPoll.id}/comment`, + `${this.baseHref}/poll/${this.pollId}/comment`, comment, this.makeHeaders()) .subscribe((res: any) => { diff --git a/src/app/ui/navigation/navigation.component.html b/src/app/ui/navigation/navigation.component.html index 07e8af12..6d7343e8 100644 --- a/src/app/ui/navigation/navigation.component.html +++ b/src/app/ui/navigation/navigation.component.html @@ -70,12 +70,12 @@ Page démo Sondage dessins animés Sondage 4 diff --git a/src/environments/environment.ts b/src/environments/environment.ts index 10d81771..cc5c5ac1 100644 --- a/src/environments/environment.ts +++ b/src/environments/environment.ts @@ -5,7 +5,7 @@ let baseURL = "http://localhost:8000/"; const baseURLProd = "https://framadate.org/"; const baseURLDemo = "https://framadate-api.cipherbliss.com/"; const apiVersion = 1; -const testOnDemo = 1; +const testOnDemo = 0; if (testOnDemo) { baseURL = baseURLDemo; }