Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with voting overview page where the number of votings are limited to 20 #353

Merged
merged 3 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 46 additions & 15 deletions app/components/treatment/voting/modal.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
import { tracked } from '@glimmer/tracking';
import { tracked } from 'tracked-built-ins';
import Component from '@glimmer/component';
import { task, restartableTask } from 'ember-concurrency';
/** @typedef {import("../../../models/behandeling-van-agendapunt").default} Behandeling*/
Expand All @@ -17,7 +17,7 @@ import { task, restartableTask } from 'ember-concurrency';

/** @extends {Component<Args>} */
export default class TreatmentVotingModalComponent extends Component {
@tracked stemmingen;
@tracked stemmingen = tracked([]);
@tracked create = false;
@tracked edit = false;
@tracked editMode = false;
Expand All @@ -33,9 +33,33 @@ export default class TreatmentVotingModalComponent extends Component {
@restartableTask
/** @type {import("ember-concurrency").Task} */
*fetchStemmingen() {
this.stemmingen = (yield this.args.behandeling.stemmingen).sortBy(
'position'
);
const stemmingen = [];
const pageSize = 20;

const firstPage = yield this.store.query('stemming', {
'filter[behandeling-van-agendapunt][:id:]': this.args.behandeling.id,
sort: 'position',
page: {
size: pageSize,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't be easier to fetch with an absurd pageSize? like 999

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I'm still hesitating what the best approach is.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be interesting to abstract out this logic, seems useful to have a generic "get all" for paginated endpoints
We could probably get away with increasing the pagesize here since even 30 votings is already exceptional, but it's good to be diligent.
Actually we just need a serious solution for pagination, which can handle all cases

},
});
const count = firstPage.meta.count;
firstPage.forEach((result) => stemmingen.push(result));
let pageNumber = 1;

while (pageNumber * pageSize < count) {
const pageResults = yield this.store.query('stemming', {
'filter[behandeling-van-agendapunt][:id:]': this.args.behandeling.id,
sort: 'position',
page: {
size: pageSize,
number: pageNumber,
},
});
pageResults.forEach((result) => stemmingen.push(result));
pageNumber++;
}
this.stemmingen = tracked(stemmingen);
}

@task
Expand All @@ -44,17 +68,12 @@ export default class TreatmentVotingModalComponent extends Component {
const isNew = this.editStemming.stemming.isNew;

if (isNew) {
this.editStemming.stemming.position =
this.args.behandeling.stemmingen.length;
this.editStemming.stemming.position = this.stemmingen.length;
this.editStemming.stemming.behandelingVanAgendapunt =
this.args.behandeling;
this.stemmingen.push(this.editStemming.stemming);
}
yield this.editStemming.saveTask.perform();

if (isNew) {
this.args.behandeling.stemmingen.pushObject(this.editStemming.stemming);
this.args.behandeling.save();
}
yield this.fetchStemmingen.perform();

this.onCancelEdit();
}

Expand All @@ -81,6 +100,16 @@ export default class TreatmentVotingModalComponent extends Component {
this.editStemming.stemming = stemmingToEdit;
}

@task
*fixPositions() {
for (const [i, stemming] of this.stemmingen.entries()) {
if (i !== stemming.position) {
stemming.position = i;
yield stemming.save();
}
}
}

@action
toggleEditStemming(stemming) {
this.editStemming.stemming = stemming;
Expand All @@ -90,7 +119,9 @@ export default class TreatmentVotingModalComponent extends Component {
@task
*removeStemming(stemming) {
yield stemming.destroyRecord();
this.stemmingen = this.stemmingen.reject((x) => x === stemming);
const index = this.stemmingen.indexOf(stemming);
this.stemmingen.splice(index, 1);
yield this.fixPositions.perform();
}

@action
Expand Down
2 changes: 1 addition & 1 deletion app/models/behandeling-van-agendapunt.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default class BehandelingVanAgendapunt extends Model {
besluiten;
@hasMany('mandataris', { inverse: null }) aanwezigen;
@hasMany('mandataris', { inverse: null }) afwezigen;
@hasMany('stemming', { inverse: null }) stemmingen;
@hasMany('stemming', { inverse: 'behandelingVanAgendapunt' }) stemmingen;
@belongsTo('document-container') documentContainer;
@belongsTo('versioned-behandeling') versionedBehandeling;

Expand Down
5 changes: 4 additions & 1 deletion app/models/stemming.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Model, { attr, hasMany } from '@ember-data/model';
import Model, { attr, hasMany, belongsTo } from '@ember-data/model';

export default class StemmingModel extends Model {
@attr('number') position;
Expand All @@ -15,4 +15,7 @@ export default class StemmingModel extends Model {
@hasMany('mandataris', { inverse: null }) stemmers;
@hasMany('mandataris', { inverse: null }) tegenstanders;
@hasMany('mandataris', { inverse: null }) voorstanders;

@belongsTo('behandeling-van-agendapunt', { inverse: 'stemmingen' })
behandelingVanAgendapunt;
}