Skip to content

Commit

Permalink
Fix vote for delegation buttons (#4089)
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianjoel authored and openslides-automation committed Sep 9, 2024
1 parent 597b49d commit 5f028d2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<ng-container [ngTemplateOutlet]="votingArea"></ng-container>
<!-- Delegations -->
@if (isUserPresent && (voteDelegationEnabled | async)) {
@for (delegation of delegations; track delegation) {
@for (delegation of delegations; track delegation.id) {
<div class="poll-vote-delegation">
<mat-divider></mat-divider>
<ng-container
Expand Down Expand Up @@ -88,7 +88,7 @@ <h4 class="poll-delegation-title">
}
@for (
action of voteActions.length ? voteActions : voteOptions;
track action;
track action.vote;
let i = $index
) {
@if (!poll.isTopicPoll) {
Expand All @@ -101,7 +101,7 @@ <h4 class="poll-delegation-title">
}
@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') {
<div class="single-vote-grid">
<div class="vote-option-title">
Expand Down Expand Up @@ -148,7 +148,7 @@ <h4 class="poll-delegation-title">
}
}

@for (option of poll.options; track option; let i = $index) {
@for (option of poll.options; track option.id; let i = $index) {
<div
[ngClass]="{
'split-grid': settings.isSplitSingleOption && !displayed_in_autopilot,
Expand Down Expand Up @@ -205,7 +205,7 @@ <h4 class="poll-delegation-title">
@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
) {
<div class="vote-button-area" [ngClass]="'option' + i">
Expand Down Expand Up @@ -241,7 +241,7 @@ <h4 class="poll-delegation-title">
<ng-container>
@for (
action of voteActions.length ? voteActions : voteOptions;
track action;
track action.vote;
let i = $index
) {
<div class="vote-button-area" [ngClass]="'option' + i">
Expand Down Expand Up @@ -309,7 +309,7 @@ <h4 class="poll-delegation-title">

<!-- global yes/no/abstain -->
@if (!settings.hideGlobalOptions) {
@for (option of globalVoteActions; track option) {
@for (option of globalVoteActions; track option.vote) {
@if (!poll.isMethodN || option.vote !== 'Y') {
<div class="single-vote-grid">
<div class="vote-option-title">
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -147,6 +147,8 @@ export abstract class BasePollVoteComponent<C extends PollContentObject = any> 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();
Expand All @@ -160,21 +162,28 @@ export abstract class BasePollVoteComponent<C extends PollContentObject = any> 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]));
}
})
);
}
Expand Down Expand Up @@ -430,19 +439,30 @@ export abstract class BasePollVoteComponent<C extends PollContentObject = any> 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);
}
}

Expand All @@ -451,24 +471,16 @@ export abstract class BasePollVoteComponent<C extends PollContentObject = any> 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;
Expand Down

0 comments on commit 5f028d2

Please sign in to comment.