@projectcaluma/ember-form 14.7.1 → 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
+ }
@@ -627,6 +627,8 @@ export default class Field extends Base {
627
627
  input.dataSourceContext = JSON.stringify(this.document.dataSourceContext);
628
628
  }
629
629
 
630
+ await this.beforeSave();
631
+
630
632
  try {
631
633
  const response = await this.apollo.mutate(
632
634
  {
@@ -652,12 +654,16 @@ export default class Field extends Base {
652
654
  this._errors = this._errors.filter(({ type }) => type !== "format");
653
655
  }
654
656
 
657
+ await this.afterSave(response);
658
+
655
659
  return response;
656
660
  } catch (e) {
657
661
  const validationError = e.errors.find(
658
662
  (err) => err.extensions?.code === "format_validation_failed",
659
663
  );
660
664
 
665
+ await this.onSaveError(e);
666
+
661
667
  if (validationError) {
662
668
  this._errors = [
663
669
  ...this._errors,
@@ -673,6 +679,31 @@ export default class Field extends Base {
673
679
  }
674
680
  });
675
681
 
682
+ /**
683
+ * Hook method that can be overridden to add functionality before saving the field.
684
+ *
685
+ * @method beforeSave
686
+ */
687
+ async beforeSave() {}
688
+
689
+ /**
690
+ * Hook method that can be overridden to add functionality after saving the field.
691
+ *
692
+ * @method afterSave
693
+ * @param {Object} response The response from the server
694
+ */
695
+ // eslint-disable-next-line no-unused-vars
696
+ async afterSave(response) {}
697
+
698
+ /**
699
+ * Hook method that can be overridden to add functionality when saving the field fails.
700
+ *
701
+ * @method onSaveError
702
+ * @param {Object} error The error thrown during saving
703
+ */
704
+ // eslint-disable-next-line no-unused-vars
705
+ async onSaveError(error) {}
706
+
676
707
  /**
677
708
  * The translated error messages
678
709
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@projectcaluma/ember-form",
3
- "version": "14.7.1",
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.7.1"
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-testing": "14.7.1",
78
- "@projectcaluma/ember-workflow": "14.7.1"
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.7.1"
82
+ "@projectcaluma/ember-workflow": "^14.8.1"
83
83
  },
84
84
  "dependenciesMeta": {
85
85
  "@projectcaluma/ember-core": {