diff --git a/client/src/app/site/polls/components/poll-progress/poll-progress.component.ts b/client/src/app/site/polls/components/poll-progress/poll-progress.component.ts index 46e891bea..289708a72 100644 --- a/client/src/app/site/polls/components/poll-progress/poll-progress.component.ts +++ b/client/src/app/site/polls/components/poll-progress/poll-progress.component.ts @@ -1,114 +1,57 @@ -import { Component, Input, OnDestroy } from '@angular/core'; +import { Component, Input, OnInit } from '@angular/core'; import { MatSnackBar } from '@angular/material/snack-bar'; import { Title } from '@angular/platform-browser'; import { TranslateService } from '@ngx-translate/core'; -import { Subscription } from 'rxjs'; +import { map } from 'rxjs/operators'; -import { MotionPollRepositoryService } from 'app/core/repositories/motions/motion-poll-repository.service'; import { UserRepositoryService } from 'app/core/repositories/users/user-repository.service'; import { BaseViewComponent } from 'app/site/base/base-view'; import { ViewBasePoll } from 'app/site/polls/models/view-base-poll'; -import { ViewUser } from 'app/site/users/models/view-user'; @Component({ selector: 'os-poll-progress', templateUrl: './poll-progress.component.html', styleUrls: ['./poll-progress.component.scss'] }) -export class PollProgressComponent extends BaseViewComponent implements OnDestroy { - private pollId: number = null; - private pollSubscription: Subscription = null; - +export class PollProgressComponent extends BaseViewComponent implements OnInit { @Input() - public set poll(value: ViewBasePoll) { - if (value.id !== this.pollId) { - this.pollId = value.id; - - this.unsubscribePoll(); - this.pollSubscription = this.pollRepo.getViewModelObservable(this.pollId).subscribe(poll => { - if (poll) { - this._poll = poll; - this.updateVotescast(); - this.calculateMaxUsers(); - } - }); - } - } - public get poll(): ViewBasePoll { - return this._poll; - } - private _poll: ViewBasePoll; - - public votescast = 0; + public poll: ViewBasePoll; public max: number; - public valueInPercent: number; + + public get votescast(): number { + return this.poll?.votescast || 0; + } public constructor( title: Title, protected translate: TranslateService, snackbar: MatSnackBar, - private userRepo: UserRepositoryService, - private pollRepo: MotionPollRepositoryService + private userRepo: UserRepositoryService ) { super(title, translate, snackbar); - this.userRepo.getViewModelListObservable().subscribe(users => { - if (users) { - this.calculateMaxUsers(users); - } - }); } - private updateVotescast(): void { - if (this.poll.votescast === 0) { - this.votescast = 0; - return; - } - - // We may cannot use this.poll.votescast during the voting, since it can - // be reported with false values from the server - // -> calculate the votes on our own. - const ids = new Set(); - for (const option of this.poll.options) { - for (const vote of option.votes) { - if (vote.user_id) { - ids.add(vote.user_id); - } - } - } - if (ids.size > this.votescast) { - this.votescast = ids.size; - } - - // But sometimes there are not enough votes (poll.votescast is higher). - // If this happens, take the value from the poll - if (this.poll.votescast > this.votescast) { - this.votescast = this.poll.votescast; + public ngOnInit(): void { + if (this.poll) { + this.subscriptions.push( + this.userRepo + .getViewModelListObservable() + .pipe( + map(users => + users.filter( + user => user.is_present && this.poll.groups_id.intersect(user.groups_id).length + ) + ) + ) + .subscribe(users => { + this.max = users.length; + }) + ); } } - private calculateMaxUsers(allUsers?: ViewUser[]): void { - if (!this.poll) { - return; - } - if (!allUsers) { - allUsers = this.userRepo.getViewModelList(); - } - - allUsers = allUsers.filter(user => user.is_present && this.poll.groups_id.intersect(user.groups_id).length); - - this.max = allUsers.length; - this.valueInPercent = this.poll ? (this.votescast / this.max) * 100 : 0; - } - - public ngOnDestroy(): void { - this.unsubscribePoll(); - } - - private unsubscribePoll(): void { - if (this.pollSubscription !== null) { - this.pollSubscription.unsubscribe(); - this.pollSubscription = null; - } + public get valueInPercent(): number { + return (this.votescast / this.max) * 100; } }