Skip to content

Commit

Permalink
feat(form): add method to refresh the answer of a field
Browse files Browse the repository at this point in the history
  • Loading branch information
anehx committed Mar 6, 2024
1 parent 3341c20 commit 78fd9b5
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/form/addon/components/cf-field.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
}}
<FieldComponent
@field={{@field}}
@disabled={{@disabled}}
@disabled={{or @disabled @field.refreshAnswer.isRunning}}
@context={{@context}}
@onSave={{perform this.save}}
/>
Expand All @@ -38,7 +38,7 @@
<div
class="cf-field__icon uk-padding-remove-vertical uk-flex uk-flex-middle uk-flex-center"
>
{{#if this.save.isRunning}}
{{#if (or this.save.isRunning @field.refreshAnswer.isRunning)}}
<UkSpinner class="uk-animation-fade" />
{{else if (or this.save.last.isError @field.isInvalid)}}
<div class="uk-flex-inline">
Expand Down
18 changes: 18 additions & 0 deletions packages/form/addon/gql/queries/refresh-answer.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#import * from '../fragments/field.graphql'

query RefreshAnswer($document: ID!, $question: ID!) {
allDocuments(filter: [{ id: $document }]) {
edges {
node {
id
answers(filter: [{ questions: [$question] }]) {
edges {
node {
...FieldAnswer
}
}
}
}
}
}
}
24 changes: 24 additions & 0 deletions packages/form/addon/lib/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import saveDocumentListAnswerMutation from "@projectcaluma/ember-form/gql/mutati
import saveDocumentStringAnswerMutation from "@projectcaluma/ember-form/gql/mutations/save-document-string-answer.graphql";
import saveDocumentTableAnswerMutation from "@projectcaluma/ember-form/gql/mutations/save-document-table-answer.graphql";
import getDocumentUsedDynamicOptionsQuery from "@projectcaluma/ember-form/gql/queries/document-used-dynamic-options.graphql";
import refreshAnswerQuery from "@projectcaluma/ember-form/gql/queries/refresh-answer.graphql";
import Base from "@projectcaluma/ember-form/lib/base";
import dependencies from "@projectcaluma/ember-form/lib/dependencies";

Expand Down Expand Up @@ -634,6 +635,29 @@ export default class Field extends Base {
this._errors = errors;
}

@dropTask
*refreshAnswer() {
const response = yield this.apollo.query(
{
query: refreshAnswerQuery,
fetchPolicy: "network-only",
variables: {
document: this.document.uuid,
question: this.question.slug,
},
},
"allDocuments.edges",
);

const rawAnswer = response[0].node.answers.edges[0]?.node;

if (rawAnswer) {
Object.entries(rawAnswer).forEach(([key, value]) => {
this.answer.raw[key] = value;
});
}
}

/**
* Validate the value against the regexes of the given format validators.
*
Expand Down
44 changes: 44 additions & 0 deletions packages/form/tests/unit/lib/field-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { settled } from "@ember/test-helpers";
import { setupMirage } from "ember-cli-mirage/test-support";
import { setupIntl } from "ember-intl/test-support";
import { module, test } from "qunit";

Expand All @@ -11,6 +12,7 @@ import { setupTest } from "dummy/tests/helpers";
module("Unit | Library | field", function (hooks) {
setupTest(hooks);
setupIntl(hooks);
setupMirage(hooks);

hooks.beforeEach(async function () {
this.set(
Expand Down Expand Up @@ -539,6 +541,48 @@ module("Unit | Library | field", function (hooks) {
}, /(Error while evaluating `isRequired` expression).*(Field for question `nonexistent` could not be found)/);
});

test("can refresh the answer", async function (assert) {
assert.expect(2);

const field = this.document.findField("question-1");

assert.strictEqual(field.answer.value, "test answer");

this.server.post("/graphql/", {
data: {
allDocuments: {
edges: [
{
node: {
id: this.document.raw.id,
answers: {
edges: [
{
node: {
id: field.answer.raw.id,
stringValue: "new answer",
__typename: "StringAnswer",
},
__typename: "AnswerEdge",
},
],
__typename: "AnswerConnection",
},
__typename: "Document",
},
__typename: "DocumentEdge",
},
],
__typename: "DocumentConnection",
},
},
});

await field.refreshAnswer.perform();

assert.strictEqual(field.answer.value, "new answer");
});

module("dependencies", function () {
test("calculates mapby dependencies correctly", async function (assert) {
this.field = this.document.findField("json-dependency");
Expand Down

0 comments on commit 78fd9b5

Please sign in to comment.