From 5f028d27172ad393c5c0bd8bdf104c3b4c52c0b0 Mon Sep 17 00:00:00 2001 From: Bastian Rihm Date: Mon, 9 Sep 2024 10:35:41 +0200 Subject: [PATCH] Fix vote for delegation buttons (#4089) --- .../base-poll-vote.component.html | 14 ++-- .../base-poll-vote.component.ts | 66 +++++++++++-------- 2 files changed, 46 insertions(+), 34 deletions(-) diff --git a/client/src/app/site/pages/meetings/modules/poll/components/base-poll-vote/base-poll-vote.component.html b/client/src/app/site/pages/meetings/modules/poll/components/base-poll-vote/base-poll-vote.component.html index 355baa5f5e..63989a9b33 100644 --- a/client/src/app/site/pages/meetings/modules/poll/components/base-poll-vote/base-poll-vote.component.html +++ b/client/src/app/site/pages/meetings/modules/poll/components/base-poll-vote/base-poll-vote.component.html @@ -3,7 +3,7 @@ @if (isUserPresent && (voteDelegationEnabled | async)) { - @for (delegation of delegations; track delegation) { + @for (delegation of delegations; track delegation.id) {
} @for ( action of voteActions.length ? voteActions : voteOptions; - track action; + track action.vote; let i = $index ) { @if (!poll.isTopicPoll) { @@ -101,7 +101,7 @@

} @if (poll.isMethodN && poll.hasGlobalOptionEnabled && poll.global_yes) { @if (!settings.hideGlobalOptions) { - @for (option of globalVoteActions; track option) { + @for (option of globalVoteActions; track option.vote) { @if (option.vote === 'Y') {
@@ -148,7 +148,7 @@

} } - @for (option of poll.options; track option; let i = $index) { + @for (option of poll.options; track option.id; let i = $index) {
@if (!poll.max_votes_per_option || poll.max_votes_per_option <= 1) { @for ( action of voteActions.length ? voteActions : voteOptions; - track action; + track action.vote; let i = $index ) {
@@ -241,7 +241,7 @@

@for ( action of voteActions.length ? voteActions : voteOptions; - track action; + track action.vote; let i = $index ) {
@@ -309,7 +309,7 @@

@if (!settings.hideGlobalOptions) { - @for (option of globalVoteActions; track option) { + @for (option of globalVoteActions; track option.vote) { @if (!poll.isMethodN || option.vote !== 'Y') {
diff --git a/client/src/app/site/pages/meetings/modules/poll/components/base-poll-vote/base-poll-vote.component.ts b/client/src/app/site/pages/meetings/modules/poll/components/base-poll-vote/base-poll-vote.component.ts index 399d44da9a..d42b7e2afb 100644 --- a/client/src/app/site/pages/meetings/modules/poll/components/base-poll-vote/base-poll-vote.component.ts +++ b/client/src/app/site/pages/meetings/modules/poll/components/base-poll-vote/base-poll-vote.component.ts @@ -1,7 +1,7 @@ import { ChangeDetectorRef, Directive, inject, Input, OnInit } from '@angular/core'; import { UntypedFormControl, Validators } from '@angular/forms'; import { marker as _ } from '@colsen1991/ngx-translate-extract-marker'; -import { BehaviorSubject, combineLatest, debounceTime, distinctUntilChanged, Observable } from 'rxjs'; +import { BehaviorSubject, combineLatest, debounceTime, distinctUntilChanged, Observable, Subscription } from 'rxjs'; import { Id } from 'src/app/domain/definitions/key-types'; import { GlobalVote, @@ -147,6 +147,8 @@ export abstract class BasePollVoteComponent e protected cd = inject(ChangeDetectorRef); private pollRepo = inject(PollControllerService); private operator = inject(OperatorService); + private votedSubscription: Subscription; + private votedSubscriptionPollId: Id; public constructor(private meetingSettingsService: MeetingSettingsService) { super(); @@ -160,21 +162,28 @@ export abstract class BasePollVoteComponent e this.user = user; this.delegations = user.vote_delegations_from(); this.voteRequestData[this.user.id] = { value: {} } as VotingData; - this.alreadyVoted[this.user.id] = this.poll.hasVoted; - if (this.delegations) { - this.setupDelegations(); + if (this.poll.hasVoted !== undefined) { + this.alreadyVoted[this.user.id] = this.poll.hasVoted; } - - for (const key of Object.keys(this._canVoteForSubjectMap)) { - this._canVoteForSubjectMap[+key].next(this.canVote(this._delegationsMap[+key])); + if (this.delegations && this.poll.user_has_voted_for_delegations !== undefined) { + this.setupDelegations(this.poll.user_has_voted_for_delegations); } + this.setupHasVotedSubscription(); this._isReady = true; this.cd.markForCheck(); } }), this.translate.onLangChange.subscribe(() => { this.updatePollOptionTitleWidth(); + }), + combineLatest([ + this.meetingSettingsService.get(`users_enable_vote_delegations`).pipe(distinctUntilChanged()), + this.meetingSettingsService.get(`users_forbid_delegator_to_vote`).pipe(distinctUntilChanged()) + ]).subscribe(_ => { + for (const key of Object.keys(this._canVoteForSubjectMap)) { + this._canVoteForSubjectMap[+key].next(this.canVote(this._delegationsMap[+key])); + } }) ); } @@ -430,19 +439,30 @@ export abstract class BasePollVoteComponent e } } - protected updatePoll(): void { - this.setupHasVotedSubscription(); + private updatePoll(): void { + if (this._isReady) { + this.setupHasVotedSubscription(); + } this.defineVoteOptions(); this.cd.markForCheck(); } private setupHasVotedSubscription(): void { - this.subscriptions.push( - this.voteRepo.subscribeVoted(this.poll).subscribe(() => { + if (!this.votedSubscription || this.votedSubscription.closed || this.votedSubscriptionPollId !== this.poll.id) { + if (this.votedSubscription) { + this.votedSubscription.unsubscribe(); + } + + this.votedSubscription = this.voteRepo.subscribeVoted(this.poll).subscribe(votedFor => { + if (votedFor[this.poll.id] === undefined) { + return; + } + + const votes = votedFor[this.poll.id] || []; if (this.user) { - this.alreadyVoted[this.user.id] = this.poll.hasVoted; + this.alreadyVoted[this.user.id] = votes.includes(this.user.id); if (this.delegations) { - this.setupDelegations(); + this.setupDelegations(votes); } } @@ -451,24 +471,16 @@ export abstract class BasePollVoteComponent e } this.cd.markForCheck(); - }) - ); - this.subscriptions.push( - combineLatest([ - this.meetingSettingsService.get(`users_enable_vote_delegations`).pipe(distinctUntilChanged()), - this.meetingSettingsService.get(`users_forbid_delegator_to_vote`).pipe(distinctUntilChanged()) - ]).subscribe(_ => { - for (const key of Object.keys(this._canVoteForSubjectMap)) { - this._canVoteForSubjectMap[+key].next(this.canVote(this._delegationsMap[+key])); - } - }) - ); + }); + this.votedSubscriptionPollId = this.poll.id; + this.subscriptions.push(this.votedSubscription); + } } - private setupDelegations(): void { + private setupDelegations(votedFor: Id[]): void { for (const delegation of this.delegations) { this._delegationsMap[delegation.id] = delegation; - this.alreadyVoted[delegation.id] = this.poll.hasVotedForDelegations(delegation.id); + this.alreadyVoted[delegation.id] = votedFor.includes(delegation.id); if (!this.voteRequestData[delegation.id]) { this.voteRequestData[delegation.id] = { value: {} } as VotingData; this.deliveringVote[delegation.id] = false;