Skip to content

Commit

Permalink
fix(form): don't refetch dynamic options on validation
Browse files Browse the repository at this point in the history
  • Loading branch information
anehx committed Mar 11, 2022
1 parent cf54bf5 commit 27061c6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
12 changes: 1 addition & 11 deletions packages/form/addon/lib/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,12 @@ export default class Field extends Base {
_createQuestion() {
const owner = getOwner(this);

const question =
this.question =
this.calumaStore.find(`Question:${this.raw.question.slug}`) ||
new (owner.factoryFor("caluma-model:question").class)({
raw: this.raw.question,
owner,
});

if (question.isDynamic) {
question.loadDynamicOptions.perform();
}

this.question = question;
}

_createAnswer() {
Expand Down Expand Up @@ -783,8 +777,6 @@ export default class Field extends Base {
* @private
*/
async _validateDynamicChoiceQuestion() {
await this.question.loadDynamicOptions.perform();

return validate("inclusion", this.answer.value, {
in: (this.options || []).map(({ slug }) => slug),
});
Expand All @@ -805,8 +797,6 @@ export default class Field extends Base {
return true;
}

await this.question.loadDynamicOptions.perform();

return value.map((value) => {
return validate("inclusion", value, {
in: (this.options || []).map(({ slug }) => slug),
Expand Down
16 changes: 12 additions & 4 deletions packages/form/addon/lib/question.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { assert } from "@ember/debug";
import { camelize } from "@ember/string";
import { queryManager } from "ember-apollo-client";
import { dropTask, lastValue } from "ember-concurrency";
import { dropTask } from "ember-concurrency";
import { useTask } from "ember-resources";
import { cached } from "tracked-toolbox";

import getDynamicOptions from "@projectcaluma/ember-form/gql/queries/dynamic-options.graphql";
Expand Down Expand Up @@ -71,13 +72,20 @@ export default class Question extends Base {
);

return (
question.node.dynamicChoiceOptions ||
question.node.dynamicChoiceOptions ??
question.node.dynamicMultipleChoiceOptions
);
}

@lastValue("loadDynamicOptions") dynamicChoiceOptions;
@lastValue("loadDynamicOptions") dynamicMultipleChoiceOptions;
dynamicOptions = useTask(this, this.loadDynamicOptions, () => []);

get dynamicChoiceOptions() {
return this.dynamicOptions.value ?? [];
}

get dynamicMultipleChoiceOptions() {
return this.dynamicOptions.value ?? [];
}

/**
* Whether the question is a single choice question
Expand Down

0 comments on commit 27061c6

Please sign in to comment.