🐛 fix fetch of old params

This commit is contained in:
Baptiste Lemoine 2020-01-23 14:23:07 +01:00
parent 7e4e80f1ed
commit 01bddc842d
7 changed files with 41 additions and 27 deletions

View File

@ -1,7 +1,9 @@
import {Component, Inject} from '@angular/core'; import {Component, Inject} from '@angular/core';
import {TranslateService} from '@ngx-translate/core'; import {TranslateService} from '@ngx-translate/core';
import {Router} from '@angular/router'; import {NavigationStart, Router} from '@angular/router';
import {DOCUMENT} from "@angular/common"; import {DOCUMENT} from "@angular/common";
import {filter} from "rxjs/operators";
import {ConfigService} from "./services/config.service";
@Component({ @Component({
selector: 'app-root', selector: 'app-root',
@ -22,6 +24,7 @@ export class AppComponent {
step: string; step: string;
constructor(private translate: TranslateService, constructor(private translate: TranslateService,
private config : ConfigService,
@Inject(DOCUMENT) private document, @Inject(DOCUMENT) private document,
private route: Router) { private route: Router) {
this.translate.setDefaultLang(this.currentLang); this.translate.setDefaultLang(this.currentLang);
@ -31,20 +34,16 @@ export class AppComponent {
detectCurrentTabOnRouteChange() { detectCurrentTabOnRouteChange() {
this.route.events.subscribe((event: any) => { 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) { switchLanguage(language: string) {
@ -76,4 +75,15 @@ export class AppComponent {
scrollGoToTop() { scrollGoToTop() {
this.document.documentElement.scrollTop = 0; 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';
}
}
}
} }

View File

@ -41,6 +41,7 @@ export class PollConfig {
creationDate = new Date(); creationDate = new Date();
expirationDate = ''; // expiracy date expirationDate = ''; // expiracy date
pollId = null; // id of the current poll when created. data given by the backend api 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 currentPoll; // current poll selected with createPoll or getPoll of ConfigService
passwordAccess = 0; passwordAccess = 0;
password = ''; password = '';

View File

@ -2,7 +2,7 @@ import {Component, OnInit} from '@angular/core';
import {BaseComponent} from "../base-page/base.component"; import {BaseComponent} from "../base-page/base.component";
import {ConfigService} from "../../services/config.service"; import {ConfigService} from "../../services/config.service";
import {mockComments} from "../../config/mocks/mock-comments"; import {mockComments} from "../../config/mocks/mock-comments";
import {ActivatedRoute, Router, RouterEvent} from "@angular/router"; import {ActivatedRoute, Router} from "@angular/router";
@Component({ @Component({
selector: 'framadate-poll-display', selector: 'framadate-poll-display',
@ -17,13 +17,12 @@ export class PollDisplayComponent extends BaseComponent implements OnInit {
private router: Router, private router: Router,
public activeRoute: ActivatedRoute) { public activeRoute: ActivatedRoute) {
super(config); super(config);
this.activeRoute.paramMap.subscribe(params => {
router.events.subscribe((val: RouterEvent) => { console.log('params', params)
// see also this.config.pollId = params.get('poll');
console.log(this.activeRoute); this.config.pollSlug = params.get('pollSlug');
if (val.url && !this.config.loading) { if (!this.config.loading) {
this.fetchPoll(); this.fetchPoll();
} }
}); });
} }
@ -41,11 +40,12 @@ export class PollDisplayComponent extends BaseComponent implements OnInit {
if (id) { if (id) {
this.config.loading = true; this.config.loading = true;
this.config.pollId = id;
// store it in the poll property here // store it in the poll property here
this.config.getPollById(id).subscribe( this.config.getPollById(id).subscribe(
(res: any) => { (res: any) => {
console.log('res', res) console.log('res', res)
this.config.pollId = id;
this.config.currentPoll = res; this.config.currentPoll = res;
this.config.loading = false; this.config.loading = false;
}, (e) => { }, (e) => {

View File

@ -3,7 +3,7 @@
{{comment.pseudo}} {{comment.pseudo}}
</span >, le </span >, le
<span class="date padding-btm-x1" > <span class="date padding-btm-x1" >
{{comment.date.date | date:'medium'}} {{comment.date.date }}
</span > </span >
<blockquote > <blockquote >
<p class="text" > <p class="text" >

View File

@ -244,7 +244,10 @@ export class ConfigService extends PollConfig {
return this.http return this.http
.get(`${this.baseHref}/poll/${id}`, .get(`${this.baseHref}/poll/${id}`,
this.makeHeaders({body: password})) this.makeHeaders({body: password}))
}
fetchPollFromRoute(event) {
console.log('time to fetch poll', event)
} }
/** /**
@ -273,8 +276,6 @@ export class ConfigService extends PollConfig {
createPoll() { createPoll() {
this.loading = true; this.loading = true;
this.createPollFromConfig(this.getPollConfig()) this.createPollFromConfig(this.getPollConfig())
} }
/** /**
@ -335,6 +336,7 @@ export class ConfigService extends PollConfig {
votes: this.convertChoicesAnsweredToSend(this.currentPoll.choices), votes: this.convertChoicesAnsweredToSend(this.currentPoll.choices),
} }
} }
console.log('voteStack', voteStack)
this.http.post( this.http.post(
`${this.baseHref}/poll/${this.currentPoll.id}/vote`, `${this.baseHref}/poll/${this.currentPoll.id}/vote`,
voteStack, voteStack,
@ -383,8 +385,9 @@ export class ConfigService extends PollConfig {
text: this.myComment, text: this.myComment,
} }
} }
console.log('comment', comment)
this.http.post( this.http.post(
`${this.baseHref}/poll/${this.currentPoll.id}/comment`, `${this.baseHref}/poll/${this.pollId}/comment`,
comment, comment,
this.makeHeaders()) this.makeHeaders())
.subscribe((res: any) => { .subscribe((res: any) => {

View File

@ -70,12 +70,12 @@
Page démo Page démo
</a > </a >
<a <a
[routerLink]="'/vote/poll/id/3'" [routerLink]="['vote/poll/id/',3]"
i18n > i18n >
Sondage dessins animés Sondage dessins animés
</a > </a >
<a <a
[routerLink]="'/vote/poll/id/4'" [routerLink]="['vote/poll/id/',4]"
i18n > i18n >
Sondage 4 Sondage 4
</a > </a >

View File

@ -5,7 +5,7 @@ let baseURL = "http://localhost:8000/";
const baseURLProd = "https://framadate.org/"; const baseURLProd = "https://framadate.org/";
const baseURLDemo = "https://framadate-api.cipherbliss.com/"; const baseURLDemo = "https://framadate-api.cipherbliss.com/";
const apiVersion = 1; const apiVersion = 1;
const testOnDemo = 1; const testOnDemo = 0;
if (testOnDemo) { if (testOnDemo) {
baseURL = baseURLDemo; baseURL = baseURLDemo;
} }