now filters percent bases correctly

This commit is contained in:
Joshua Sangmeister 2020-02-27 12:22:31 +01:00 committed by FinnStutzenstein
parent 72678770bb
commit 9d7028ea5f
6 changed files with 85 additions and 34 deletions

View File

@ -1,7 +1,7 @@
<os-poll-form [data]="pollData" [pollMethods]="AssignmentPollMethodVerbose" [percentBases]="AssignmentPollPercentBaseVerbose" #pollForm></os-poll-form>
<!-- Analog voting -->
<ng-container *ngIf="pollForm.contentForm.get('type').value === 'analog'">
<ng-container *ngIf="isAnalogPoll">
<form [formGroup]="dialogVoteForm">
<!-- Candidates -->
<div formGroupName="options">

View File

@ -7,6 +7,7 @@ import { Title } from '@angular/platform-browser';
import { TranslateService } from '@ngx-translate/core';
import { AssignmentPollMethod } from 'app/shared/models/assignments/assignment-poll';
import { PollType } from 'app/shared/models/poll/base-poll';
import { GeneralValueVerbose, VoteValue, VoteValueVerbose } from 'app/shared/models/poll/base-vote';
import {
AssignmentPollMethodVerbose,
@ -58,6 +59,14 @@ export class AssignmentPollDialogComponent extends BasePollDialogComponent<ViewA
public options: OptionsObject;
public get isAnalogPoll(): boolean {
return (
this.pollForm &&
this.pollForm.contentForm &&
this.pollForm.contentForm.get('type').value === PollType.Analog
);
}
/**
* Constructor. Retrieves necessary metadata from the pollService,
* injects the poll itself
@ -95,13 +104,14 @@ export class AssignmentPollDialogComponent extends BasePollDialogComponent<ViewA
private setAnalogPollValues(): void {
const pollmethod = this.pollForm.contentForm.get('pollmethod').value;
this.analogPollValues = ['Y'];
const analogPollValues: VoteValue[] = ['Y'];
if (pollmethod !== AssignmentPollMethod.Votes) {
this.analogPollValues.push('N');
analogPollValues.push('N');
}
if (pollmethod === AssignmentPollMethod.YNA) {
this.analogPollValues.push('A');
analogPollValues.push('A');
}
this.analogPollValues = analogPollValues;
}
private updateDialogVoteForm(data: Partial<ViewAssignmentPoll>): void {
@ -125,7 +135,7 @@ export class AssignmentPollDialogComponent extends BasePollDialogComponent<ViewA
if (this.dialogVoteForm) {
const result = this.undoReplaceEmptyValues(update);
this.dialogVoteForm.setValue(result);
this.dialogVoteForm.patchValue(result);
}
}
@ -152,7 +162,7 @@ export class AssignmentPollDialogComponent extends BasePollDialogComponent<ViewA
[sumValue]: ['', [Validators.min(-2)]]
}))
});
if (this.pollData.poll) {
if (this.isAnalogPoll && this.pollData.poll) {
this.updateDialogVoteForm(this.pollData);
}
}

View File

@ -27,7 +27,7 @@ export const AssignmentPollPercentBaseVerbose = {
YNA: 'Yes/No/Abstain per candidate',
votes: 'Sum of votes inclusive global ones',
valid: 'All valid ballots',
cast: 'All casted ballots',
cast: 'All cast ballots',
disabled: 'Disabled (no percents)'
};

View File

@ -50,7 +50,7 @@
formControlName="pollmethod"
required
>
<mat-option *ngFor="let option of pollMethods | keyvalue" [value]="option.key">
<mat-option *ngFor="let option of pollMethods | keyvalue: keepEntryOrder" [value]="option.key">
{{ option.value }}
</mat-option>
</mat-select>
@ -65,7 +65,7 @@
formControlName="onehundred_percent_base"
required
>
<ng-container *ngFor="let option of percentBases | keyvalue: keepEntryOrder">
<ng-container *ngFor="let option of validPercentBases | keyvalue: keepEntryOrder">
<mat-option [value]="option.key">{{ option.value | translate }}</mat-option>
</ng-container>
</mat-select>

View File

@ -4,7 +4,7 @@ import { E2EImportsModule } from 'e2e-imports.module';
import { PollFormComponent } from './poll-form.component';
describe('PollFormComponent', () => {
fdescribe('PollFormComponent', () => {
let component: PollFormComponent<any>;
let fixture: ComponentFixture<PollFormComponent<any>>;

View File

@ -8,6 +8,7 @@ import { Observable } from 'rxjs';
import { GroupRepositoryService } from 'app/core/repositories/users/group-repository.service';
import { ConfigService } from 'app/core/ui-services/config.service';
import { AssignmentPollMethod, AssignmentPollPercentBase } from 'app/shared/models/assignments/assignment-poll';
import { PercentBase } from 'app/shared/models/poll/base-poll';
import { PollType } from 'app/shared/models/poll/base-poll';
import { ViewAssignmentPoll } from 'app/site/assignments/models/view-assignment-poll';
@ -15,7 +16,6 @@ import { BaseViewComponent } from 'app/site/base/base-view';
import { ViewMotionPoll } from 'app/site/motions/models/view-motion-poll';
import {
MajorityMethodVerbose,
PercentBaseVerbose,
PollClassType,
PollPropertyVerbose,
PollTypeVerbose,
@ -63,6 +63,11 @@ export class PollFormComponent<T extends ViewBasePoll> extends BaseViewComponent
*/
public majorityMethods = MajorityMethodVerbose;
/**
* the filtered `percentBases`.
*/
public validPercentBases: { [key: string]: string };
/**
* Reference to the observable of the groups. Used by the `search-value-component`.
*/
@ -124,36 +129,21 @@ export class PollFormComponent<T extends ViewBasePoll> extends BaseViewComponent
});
}
this.updatePollValues(this.contentForm.value);
this.updatePercentBases(this.contentForm.get('pollmethod').value);
this.subscriptions.push(
// changes to whole form
this.contentForm.valueChanges.subscribe(values => {
this.updatePollValues(values);
if (values) {
this.updatePollValues(values);
}
}),
// poll method changes
this.contentForm.get('pollmethod').valueChanges.subscribe(method => {
let forbiddenBases: string[];
if (method === 'YN') {
forbiddenBases = [PercentBase.YNA, PercentBase.Cast];
} else if (method === 'YNA') {
forbiddenBases = [PercentBase.Cast];
} else if (method === 'votes') {
forbiddenBases = [PercentBase.YN, PercentBase.YNA];
if (this.contentForm.get('type').value === PollType.Pseudoanonymous) {
this.setVotesAmountCtrl();
}
if (method) {
this.updatePercentBases(method);
this.setVotesAmountCtrl();
}
const percentBases = {};
for (const [key, value] of Object.entries(PercentBaseVerbose)) {
if (!forbiddenBases.includes(key)) {
percentBases[key] = value;
}
}
this.percentBases = percentBases;
// TODO: update selected base
this.setVotesAmountCtrl();
}),
// poll type changes
this.contentForm.get('type').valueChanges.subscribe(() => {
@ -162,6 +152,55 @@ export class PollFormComponent<T extends ViewBasePoll> extends BaseViewComponent
);
}
/**
* updates the available percent bases according to the pollmethod
* @param method the currently chosen pollmethod
*/
private updatePercentBases(method: AssignmentPollMethod): void {
if (method) {
let forbiddenBases = [];
if (method === AssignmentPollMethod.YN) {
forbiddenBases = [PercentBase.YNA, AssignmentPollPercentBase.Votes];
} else if (method === AssignmentPollMethod.YNA) {
forbiddenBases = [AssignmentPollPercentBase.Votes];
} else if (method === AssignmentPollMethod.Votes) {
forbiddenBases = [PercentBase.YN, PercentBase.YNA];
}
const bases = {};
for (const [key, value] of Object.entries(this.percentBases)) {
if (!forbiddenBases.includes(key)) {
bases[key] = value;
}
}
// update value in case that its no longer valid
const percentBaseControl = this.contentForm.get('onehundred_percent_base');
percentBaseControl.setValue(this.getNormedPercentBase(percentBaseControl.value, method));
this.validPercentBases = bases;
}
}
private getNormedPercentBase(
base: AssignmentPollPercentBase,
method: AssignmentPollMethod
): AssignmentPollPercentBase {
if (
method === AssignmentPollMethod.YN &&
(base === AssignmentPollPercentBase.YNA || base === AssignmentPollPercentBase.Votes)
) {
return AssignmentPollPercentBase.YN;
} else if (method === AssignmentPollMethod.YNA && base === AssignmentPollPercentBase.Votes) {
return AssignmentPollPercentBase.YNA;
} else if (
method === AssignmentPollMethod.Votes &&
(base === AssignmentPollPercentBase.YN || base === AssignmentPollPercentBase.YNA)
) {
return AssignmentPollPercentBase.Votes;
}
return base;
}
/**
* Disable votes_amount form control if the poll type is anonymous
* and the poll method is votes.
@ -210,7 +249,9 @@ export class PollFormComponent<T extends ViewBasePoll> extends BaseViewComponent
if (data.type !== 'analog') {
this.pollValues.push([
this.pollService.getVerboseNameForKey('groups'),
this.groupRepo.getNameForIds(...([] || (data && data.groups_id)))
data && data.groups_id && data.groups_id.length
? this.groupRepo.getNameForIds(...data.groups_id)
: '---'
]);
}
if (data.pollmethod === 'votes') {