Fix poll progress

Fixes a regression in the poll progress.
Poll progress war no longer working for assignments
This commit is contained in:
Sean 2020-06-19 13:04:47 +02:00
parent 2fd4e70b0c
commit 9cf602f0c1
1 changed files with 27 additions and 84 deletions

View File

@ -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 { MatSnackBar } from '@angular/material/snack-bar';
import { Title } from '@angular/platform-browser'; import { Title } from '@angular/platform-browser';
import { TranslateService } from '@ngx-translate/core'; 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 { UserRepositoryService } from 'app/core/repositories/users/user-repository.service';
import { BaseViewComponent } from 'app/site/base/base-view'; import { BaseViewComponent } from 'app/site/base/base-view';
import { ViewBasePoll } from 'app/site/polls/models/view-base-poll'; import { ViewBasePoll } from 'app/site/polls/models/view-base-poll';
import { ViewUser } from 'app/site/users/models/view-user';
@Component({ @Component({
selector: 'os-poll-progress', selector: 'os-poll-progress',
templateUrl: './poll-progress.component.html', templateUrl: './poll-progress.component.html',
styleUrls: ['./poll-progress.component.scss'] styleUrls: ['./poll-progress.component.scss']
}) })
export class PollProgressComponent extends BaseViewComponent implements OnDestroy { export class PollProgressComponent extends BaseViewComponent implements OnInit {
private pollId: number = null;
private pollSubscription: Subscription = null;
@Input() @Input()
public set poll(value: ViewBasePoll) { public poll: 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 max: number; public max: number;
public valueInPercent: number;
public get votescast(): number {
return this.poll?.votescast || 0;
}
public constructor( public constructor(
title: Title, title: Title,
protected translate: TranslateService, protected translate: TranslateService,
snackbar: MatSnackBar, snackbar: MatSnackBar,
private userRepo: UserRepositoryService, private userRepo: UserRepositoryService
private pollRepo: MotionPollRepositoryService
) { ) {
super(title, translate, snackbar); super(title, translate, snackbar);
this.userRepo.getViewModelListObservable().subscribe(users => {
if (users) {
this.calculateMaxUsers(users);
}
});
} }
private updateVotescast(): void { public ngOnInit(): void {
if (this.poll.votescast === 0) { if (this.poll) {
this.votescast = 0; this.subscriptions.push(
return; this.userRepo
} .getViewModelListObservable()
.pipe(
// We may cannot use this.poll.votescast during the voting, since it can map(users =>
// be reported with false values from the server users.filter(
// -> calculate the votes on our own. user => user.is_present && this.poll.groups_id.intersect(user.groups_id).length
const ids = new Set(); )
for (const option of this.poll.options) { )
for (const vote of option.votes) { )
if (vote.user_id) { .subscribe(users => {
ids.add(vote.user_id); this.max = users.length;
} })
} );
}
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;
} }
} }
private calculateMaxUsers(allUsers?: ViewUser[]): void { public get valueInPercent(): number {
if (!this.poll) { return (this.votescast / this.max) * 100;
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;
}
} }
} }