Skip to content
Permalink
Browse files

fix(navigation): fix document states in navigation

  • Loading branch information...
anehx committed May 20, 2019
1 parent f6cf5a5 commit 480b67bd544523a43bcfc07bdf2bd6affecd6ad3
Showing with 89 additions and 37 deletions.
  1. +47 −29 addon/lib/document.js
  2. +42 −8 addon/lib/field.js
@@ -7,7 +7,17 @@ import { atob } from "ember-caluma/helpers/atob";
import { inject as service } from "@ember/service";
import { intersects } from "ember-caluma/utils/jexl";

const STATE_PRECEDENCE = ["invalid", "unfinished", "untouched", "valid"];
const getParentState = childStates => {
if (childStates.every(state => state === "untouched")) {
return "untouched";
}

if (childStates.some(state => state === "invalid")) {
return "invalid";
}

return childStates.every(state => state === "valid") ? "valid" : "unfinished";
};

/**
* Object which represents a document
@@ -188,52 +198,60 @@ export default EmberObject.extend({

fields: computed(() => []).readOnly(),

childDocuments: computed("fields.[]", function() {
return this.get("fields")
.map(field => field.childDocument)
.filter(Boolean);
}),

childState: computed(
"fields.@each.{isNew,isValid,_errors,question}",
"childDocuments.@each.state",
childDocuments: computed(
"fields.{[],@each.hidden,childDocument}",
function() {
const childDocumentStates = this.get("childDocuments")
.filter(Boolean)
.map(c => c.state);

return STATE_PRECEDENCE.find(state =>
childDocumentStates.includes(state)
);
return this.fields
.filter(field => !field.hidden)
.map(field => field.childDocument)
.filter(Boolean);
}
),

childState: computed("childDocuments.{[],@each.state}", function() {
const childDocumentStates = this.childDocuments
.filter(Boolean)
.map(c => c.state);

if (!childDocumentStates.length) {
return null;
}

return getParentState(childDocumentStates);
}),

ownState: computed(
"fields.@each.{isNew,isValid,_errors,hidden,question,childDocument}",
"fields.@each.{isNew,isValid,hidden,optional,childDocument}",
function() {
if (this.fields.every(f => f.isNew)) {
const visibleFields = this.fields
.filter(f => !f.hidden)
.filter(f => !f.childDocument);

if (!visibleFields.length) {
return null;
}

if (visibleFields.every(f => f.isNew)) {
return "untouched";
}

const visibleFields = this.fields.filter(f => !f.hidden);
const requiredFields = visibleFields.filter(f => !f.question.optional);
if (visibleFields.some(f => !f.isValid && !f.isNew)) {
return "invalid";
}

if (
visibleFields.every(f => f.isValid) &&
requiredFields.every(f => !f.isNew)
visibleFields
.filter(f => !f.question.optional)
.every(f => f.isValid && !f.isNew)
) {
return "valid";
}

return visibleFields.some(f => f._errors.some(e => e.type !== "blank"))
? "invalid"
: "unfinished";
return "unfinished";
}
),

state: computed("childState", "ownState", function() {
return STATE_PRECEDENCE.find(state =>
[this.get("childState"), this.get("ownState")].includes(state)
);
return getParentState([this.childState, this.ownState].filter(Boolean));
})
});
@@ -5,7 +5,7 @@ import { assert } from "@ember/debug";
import { getOwner } from "@ember/application";
import { camelize } from "@ember/string";
import { task } from "ember-concurrency";
import { all } from "rsvp";
import { all, resolve } from "rsvp";
import { validate } from "ember-validators";
import Evented, { on } from "@ember/object/evented";

@@ -161,7 +161,21 @@ export default EmberObject.extend(Evented, {
*/
isInvalid: not("isValid"),

isNew: empty("answer.value"),
/**
* Whether the field is new (never saved to the backend service)
*
* @property {Boolean} isNew
* @accessor
*/
isNew: empty("answer.id"),

/**
* Whether the field is optional
*
* @property {Boolean} optional
* @accessor
*/
optional: reads("question.optional"),

/**
* Whether or not the question is hidden.
@@ -197,8 +211,10 @@ export default EmberObject.extend(Evented, {
const type = this.get("answer.__typename");
const value = this.get("answer.value");

let response;

if (value === null || value.length === 0) {
return yield this.apollo.mutate(
response = yield this.apollo.mutate(
{
mutation: removeAnswerMutation,
variables: {
@@ -209,8 +225,10 @@ export default EmberObject.extend(Evented, {
},
`removeAnswer.answer`
);

this.answer.id = undefined;
} else {
const response = yield this.apollo.mutate(
response = yield this.apollo.mutate(
{
mutation: this.get(`saveDocument${type}Mutation`),
variables: {
@@ -223,9 +241,11 @@ export default EmberObject.extend(Evented, {
},
`saveDocument${type}.answer`
);

this.answer.setProperties(response);
return response;
}

return response;
}).restartable(),

/**
@@ -419,7 +439,7 @@ export default EmberObject.extend(Evented, {
* @internal
*/
_validateFileQuestion() {
return Promise.resolve(true);
return resolve(true);
},

/**
@@ -435,11 +455,25 @@ export default EmberObject.extend(Evented, {
});
},

/**
* Dummy method for the validation of table fields
*
* @method _validateTableQuestion
* @return {RSVP.Promise}
* @internal
*/
_validateTableQuestion() {
return true;
return resolve(true);
},

/**
* Dummy method for the validation of static fields
*
* @method _validateStaticQuestion
* @return {RSVP.Promise}
* @internal
*/
_validateStaticQuestion() {
return true;
return resolve(true);
}
});

0 comments on commit 480b67b

Please sign in to comment.
You can’t perform that action at this time.