@projectcaluma/ember-form 14.8.0 → 14.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,11 @@
1
1
  import { action } from "@ember/object";
2
2
  import Component from "@glimmer/component";
3
+ import { queryManager } from "ember-apollo-client";
3
4
  import { task } from "ember-concurrency";
4
5
  import { cached } from "tracked-toolbox";
5
6
 
7
+ import documentValidityQuery from "@projectcaluma/ember-form/gql/queries/document-validity.graphql";
8
+
6
9
  /**
7
10
  * Component to check the validity of a document
8
11
  *
@@ -20,6 +23,8 @@ import { cached } from "tracked-toolbox";
20
23
  * @yield {Function} validate
21
24
  */
22
25
  export default class DocumentValidity extends Component {
26
+ @queryManager apollo;
27
+
23
28
  /**
24
29
  * The document to be validated
25
30
  *
@@ -43,39 +48,61 @@ export default class DocumentValidity extends Component {
43
48
  return this._validate.isRunning;
44
49
  }
45
50
 
46
- _validateField = task(async (field) => {
47
- await field.validate.linked().perform();
51
+ _validate = task({ restartable: true }, async () => {
52
+ try {
53
+ const saveTasks = this.args.document.fields
54
+ .flatMap((field) => [
55
+ ...[...(field._components ?? [])].map((c) => c.save.last),
56
+ field.save?.last,
57
+ ])
58
+ .filter(Boolean);
48
59
 
49
- if (field.question.hasFormatValidators) {
50
- await field.save.linked().perform();
51
- }
52
- });
60
+ // Wait until all currently running save tasks in the UI and in the field
61
+ // itself are finished
62
+ await Promise.all(saveTasks);
53
63
 
54
- _validate = task({ restartable: true }, async () => {
55
- const saveTasks = this.args.document.fields
56
- .flatMap((field) => [
57
- ...[...(field._components ?? [])].map((c) => c.save.last),
58
- field.save?.last,
59
- ])
60
- .filter(Boolean);
64
+ await Promise.all(
65
+ this.args.document.fields.map((field) =>
66
+ field.validate.linked().perform(),
67
+ ),
68
+ );
61
69
 
62
- // Wait until all currently running save tasks in the UI and in the field
63
- // itself are finished
64
- await Promise.all(saveTasks);
70
+ const { isValid, errors } = await this.apollo.query(
71
+ {
72
+ query: documentValidityQuery,
73
+ fetchPolicy: "network-only",
74
+ variables: { id: this.args.document.uuid },
75
+ },
76
+ "documentValidity.edges.0.node",
77
+ );
65
78
 
66
- await Promise.all(
67
- this.args.document.fields.map((field) =>
68
- this._validateField.perform(field),
69
- ),
70
- );
79
+ if (!isValid) {
80
+ errors
81
+ .filter(({ errorCode }) => errorCode === "format_validation_failed")
82
+ .forEach(({ slug, errorMsg }) => {
83
+ const field = this.args.document.findField(slug);
71
84
 
72
- if (this.isValid) {
73
- this.args.onValid?.();
74
- } else {
75
- this.args.onInvalid?.();
76
- }
85
+ field._errors = [
86
+ ...field._errors,
87
+ {
88
+ type: "format",
89
+ context: { errorMsg },
90
+ value: field.value,
91
+ },
92
+ ];
93
+ });
94
+ }
77
95
 
78
- return this.isValid;
96
+ if (this.isValid) {
97
+ this.args.onValid?.();
98
+ } else {
99
+ this.args.onInvalid?.();
100
+ }
101
+
102
+ return this.isValid;
103
+ } catch {
104
+ return false;
105
+ }
79
106
  });
80
107
 
81
108
  @action
@@ -0,0 +1,15 @@
1
+ query DocumentValidity($id: ID!, $dataSourceContext: JSONString) {
2
+ documentValidity(id: $id, dataSourceContext: $dataSourceContext) {
3
+ edges {
4
+ node {
5
+ id
6
+ isValid
7
+ errors {
8
+ slug
9
+ errorMsg
10
+ errorCode
11
+ }
12
+ }
13
+ }
14
+ }
15
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@projectcaluma/ember-form",
3
- "version": "14.8.0",
3
+ "version": "14.8.1",
4
4
  "description": "Ember addon for rendering Caluma forms.",
5
5
  "keywords": [
6
6
  "ember-addon"
@@ -39,7 +39,7 @@
39
39
  "luxon": "^3.5.0",
40
40
  "reactiveweb": "^1.3.0",
41
41
  "tracked-toolbox": "^2.0.0",
42
- "@projectcaluma/ember-core": "^14.8.0"
42
+ "@projectcaluma/ember-core": "^14.8.1"
43
43
  },
44
44
  "devDependencies": {
45
45
  "@ember/optional-features": "2.3.0",
@@ -74,12 +74,12 @@
74
74
  "uikit": "3.25.6",
75
75
  "uuid": "13.0.0",
76
76
  "webpack": "5.104.1",
77
- "@projectcaluma/ember-workflow": "14.8.0",
78
- "@projectcaluma/ember-testing": "14.8.0"
77
+ "@projectcaluma/ember-workflow": "14.8.1",
78
+ "@projectcaluma/ember-testing": "14.8.1"
79
79
  },
80
80
  "peerDependencies": {
81
81
  "ember-source": ">= 4.0.0",
82
- "@projectcaluma/ember-workflow": "^14.8.0"
82
+ "@projectcaluma/ember-workflow": "^14.8.1"
83
83
  },
84
84
  "dependenciesMeta": {
85
85
  "@projectcaluma/ember-core": {