clean lifecycle of setting base votes choices on load of a poll

This commit is contained in:
Tykayn 2021-06-10 11:43:17 +02:00 committed by tykayn
parent 5a75685b0a
commit 534ef03f6a
9 changed files with 56 additions and 27 deletions

View File

@ -98,7 +98,7 @@
</a> </a>
<a <a
class="navbar-item" class="navbar-item"
[routerLink]="['/poll/citron/consultation/secure/9199bdd9e0d4b29deafbf3463c0727fc']" [routerLink]="['/poll/citron/consultation/secure/1c01ed9c94fc640a1be864f197ff808c']"
routerLinkActive="is-primary" routerLinkActive="is-primary"
> >
<i class="fa fa-key-modern"></i> <i class="fa fa-key-modern"></i>

View File

@ -4,6 +4,7 @@ import { Owner } from './owner.model';
export class Stack { export class Stack {
public id: number; public id: number;
public poll_custom_url: string; public poll_custom_url: string;
public pass_hash: string;
public pseudo = 'Choque Nourrice'; public pseudo = 'Choque Nourrice';
public comment = 'Le beau commentaire de Choque Nourrice'; public comment = 'Le beau commentaire de Choque Nourrice';
public owner: Owner = new Owner(); public owner: Owner = new Owner();

View File

@ -93,20 +93,15 @@ export class ApiService {
* @param vote_stack * @param vote_stack
*/ */
public sendNewVoteStackOfPoll(vote_stack: Stack): Promise<void> { public sendNewVoteStackOfPoll(vote_stack: Stack): Promise<void> {
// api_new_vote_stack POST ANY ANY /api/v1/poll/{id}/answer // const headers = ApiService.makeHeaders(vote_stack);
console.log('vote_stack', vote_stack);
console.log('this.baseHref', this.baseHref);
const headers = ApiService.makeHeaders(vote_stack);
console.log('headers', headers);
const url = `${this.baseHref}/vote-stack/`; const url = `${this.baseHref}/vote-stack/`;
const axiosconf = { // const axiosconf = {
url, // url,
method: 'POST', // method: 'POST',
body: vote_stack, // body: vote_stack,
headers, // headers,
}; // };
return this.axiosInstance.post(url, vote_stack); return this.axiosInstance.post(url, vote_stack);
} }

View File

@ -24,6 +24,7 @@ import { Vote } from '../models/vote.model';
export class PollService implements Resolve<Poll> { export class PollService implements Resolve<Poll> {
_poll: BehaviorSubject<Poll | undefined> = new BehaviorSubject<Poll | undefined>(undefined); _poll: BehaviorSubject<Poll | undefined> = new BehaviorSubject<Poll | undefined>(undefined);
public readonly poll: Observable<Poll | undefined> = this._poll.asObservable(); public readonly poll: Observable<Poll | undefined> = this._poll.asObservable();
public pass_hash: string;
constructor( constructor(
private http: HttpClient, private http: HttpClient,
@ -54,10 +55,17 @@ export class PollService implements Resolve<Poll> {
!this._poll.getValue().custom_url || !this._poll.getValue().custom_url ||
this._poll.getValue().custom_url !== wantedcustom_url this._poll.getValue().custom_url !== wantedcustom_url
) { ) {
await this.loadPollBycustom_url(wantedcustom_url); if (this.pass_hash) {
this.storageService.vote_stack.pass_hash = this.pass_hash;
await this.loadPollBycustom_urlWithPasswordHash(wantedcustom_url, this.pass_hash);
} else {
await this.loadPollBycustom_url(wantedcustom_url);
}
} }
if (this._poll.getValue()) { const loadedPoll = this._poll.getValue();
return this._poll.getValue(); if (loadedPoll) {
this.storageService.vote_stack.poll_custom_url = loadedPoll.custom_url;
return loadedPoll;
} else { } else {
this.router.navigate(['page-not-found']); this.router.navigate(['page-not-found']);
return; return;
@ -104,7 +112,7 @@ export class PollService implements Resolve<Poll> {
this.titleService.setTitle(`☑️ ${poll.title} - ${environment.appTitle}`); this.titleService.setTitle(`☑️ ${poll.title} - ${environment.appTitle}`);
} else { } else {
this.toastService.display(`sondage ${custom_url} non trouvé`); this.toastService.display(`sondage ${custom_url} non trouvé`);
// this.router.navigate(['page-not-found']); this.router.navigate(['page-not-found']);
} }
} else { } else {
this.toastService.display(`sondage sans custom url : ${custom_url}`); this.toastService.display(`sondage sans custom url : ${custom_url}`);
@ -120,8 +128,12 @@ export class PollService implements Resolve<Poll> {
if (!this.storageService.vote_stack.id || this.storageService.vote_stack.poll_custom_url !== poll.custom_url) { if (!this.storageService.vote_stack.id || this.storageService.vote_stack.poll_custom_url !== poll.custom_url) {
console.log('set base choices', poll.choices); console.log('set base choices', poll.choices);
// set the choices only the first time the poll loads // set the choices only the first time the poll loads, or if we changed the poll
this.storageService.setChoicesForVoteStack(poll.choices); console.log(
'this.storageService.vote_stack.poll_custom_url',
this.storageService.vote_stack.poll_custom_url
);
// this.storageService.setChoicesForVoteStack(poll.choices);
} }
this.toastService.display('sondage bien mis à jour', 'success'); this.toastService.display('sondage bien mis à jour', 'success');

View File

@ -65,7 +65,8 @@ export class StorageService {
* @param choices_list * @param choices_list
*/ */
setChoicesForVoteStack(choices_list: Choice[]) { setChoicesForVoteStack(choices_list: Choice[]) {
// text choices // change only if the poll custom_url changed or if there is no stack id for this poll
if (!this.vote_stack.id) { if (!this.vote_stack.id) {
this.vote_stack = new Stack(); this.vote_stack = new Stack();

View File

@ -6,7 +6,6 @@ import { WipTodoComponent } from '../../shared/components/ui/wip-todo/wip-todo.c
const routes: Routes = [ const routes: Routes = [
{ path: 'secure/:pass_hash', component: ConsultationComponent }, { path: 'secure/:pass_hash', component: ConsultationComponent },
{ {
path: '', path: '',
component: ConsultationComponent, component: ConsultationComponent,

View File

@ -1,4 +1,3 @@
<section class="loading_poll" *ngIf="fetching"></section>
<section class="poll_loaded padded" *ngIf="!fetching && poll"> <section class="poll_loaded padded" *ngIf="!fetching && poll">
<!-- messages--> <!-- messages-->
@ -116,6 +115,9 @@
aucun vote pour le moment aucun vote pour le moment
</div> </div>
</div> </div>
<section class="loading_poll" *ngIf="fetching">
<i class="fa fa-spinner fa-4x"></i>
</section>
<button <button
class="btn btn-block submit-votestack is-primary" class="btn btn-block submit-votestack is-primary"
(click)="addVoteStack()" (click)="addVoteStack()"

View File

@ -0,0 +1,6 @@
.loading_poll {
position: fixed;
bottom: 5em;
left: 1em;
z-index: 10;
}

View File

@ -18,7 +18,7 @@ export class ConsultationComponent implements OnInit, OnDestroy {
public isCompactMode = true; public isCompactMode = true;
public poll: Poll; public poll: Poll;
public pollSlug: string; public pollSlug: string;
public passHash: string; public pass_hash: string;
public fetching = true; public fetching = true;
public isArchived: boolean; public isArchived: boolean;
public isAdmin: boolean; public isAdmin: boolean;
@ -55,14 +55,22 @@ export class ConsultationComponent implements OnInit, OnDestroy {
this._Activatedroute.paramMap.subscribe((params: ParamMap) => { this._Activatedroute.paramMap.subscribe((params: ParamMap) => {
console.log('params _Activatedroute', params); console.log('params _Activatedroute', params);
this.pollSlug = params.get('custom_url'); this.pollSlug = params.get('custom_url');
this.passHash = params.get('pass_hash'); this.pass_hash = params.get('pass_hash');
if (this.passHash) { console.log('this.pass_hash ', this.pass_hash);
this.pollService.loadPollBycustom_urlWithPasswordHash(this.pollSlug, this.passHash); if (this.pass_hash) {
this.pollService.loadPollBycustom_urlWithPasswordHash(this.pollSlug, this.pass_hash).then((resp) => {
console.log('resp', resp);
this.fetching = false;
this.storageService.vote_stack.id = null;
this.storageService.setChoicesForVoteStack(this.pollService._poll.getValue().choices);
});
} else { } else {
this.pollService.loadPollBycustom_url(this.pollSlug).then((resp) => { this.pollService.loadPollBycustom_url(this.pollSlug).then((resp) => {
console.log('resp', resp); console.log('resp', resp);
this.fetching = false; this.fetching = false;
this.storageService.vote_stack.id = null;
this.storageService.setChoicesForVoteStack(this.pollService._poll.getValue().choices);
}); });
} }
}); });
@ -100,6 +108,7 @@ export class ConsultationComponent implements OnInit, OnDestroy {
*/ */
addVoteStack(): void { addVoteStack(): void {
this.storageService.vote_stack.poll_custom_url = this.poll.custom_url; this.storageService.vote_stack.poll_custom_url = this.poll.custom_url;
this.pollService.pass_hash = this.pass_hash;
this.toastService.display('envoi du vote ....'); this.toastService.display('envoi du vote ....');
this.api this.api
@ -120,7 +129,11 @@ export class ConsultationComponent implements OnInit, OnDestroy {
if (voteStack.status == 200) { if (voteStack.status == 200) {
this.storageService.mapVotes(voteStack.data); this.storageService.mapVotes(voteStack.data);
this.pollService.enrichVoteStackWithCurrentPollChoicesDefaultVotes(this.storageService.vote_stack); this.pollService.enrichVoteStackWithCurrentPollChoicesDefaultVotes(this.storageService.vote_stack);
this.pollService.loadPollBycustom_url(this.poll.custom_url); if (this.pass_hash) {
this.pollService.loadPollBycustom_urlWithPasswordHash(this.poll.custom_url, this.pass_hash);
} else {
this.pollService.loadPollBycustom_url(this.poll.custom_url);
}
} else { } else {
this.toastService.display('erreur à l enregistrement'); this.toastService.display('erreur à l enregistrement');
} }