@headless-adminapp/app 0.0.17-alpha.47 → 0.0.17-alpha.48

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.
@@ -2,31 +2,12 @@ import type { Attribute } from '@headless-adminapp/core/attributes';
2
2
  import { Form } from '@headless-adminapp/core/experience/form';
3
3
  import { InferredSchemaType, Schema, SchemaAttributes } from '@headless-adminapp/core/schema';
4
4
  import { ISchemaStore } from '@headless-adminapp/core/store';
5
- import { IDataService } from '@headless-adminapp/core/transport';
6
5
  import { Nullable } from '@headless-adminapp/core/types';
7
6
  import { MemoizedFunction } from 'lodash';
8
7
  import { ResolverResult } from 'react-hook-form';
9
8
  import * as yup from 'yup';
10
9
  import { FormValidationStringSet } from '../../form/FormValidationStringContext';
11
- export declare function getModifiedValues(initialValues: any, values: any, exclude?: string[]): Record<string, any>;
12
- type SaveRecordResult = {
13
- success: true;
14
- recordId: string;
15
- } | {
16
- success: false;
17
- title?: string;
18
- message: string;
19
- isError: boolean;
20
- };
21
- export declare function saveRecord({ values, form, schema, dataService, initialValues, record, schemaStore, }: {
22
- values: any;
23
- form: Form<SchemaAttributes>;
24
- record: InferredSchemaType<SchemaAttributes> | undefined;
25
- initialValues: Nullable<InferredSchemaType<SchemaAttributes>>;
26
- schema: Schema<SchemaAttributes>;
27
- dataService: IDataService;
28
- schemaStore: ISchemaStore;
29
- }): Promise<SaveRecordResult>;
10
+ export { saveRecord } from './saveRecord';
30
11
  export declare function getInitialValues({ cloneRecord, form, record, recordId, defaultParameters, }: {
31
12
  cloneRecord: InferredSchemaType<SchemaAttributes> | undefined;
32
13
  form: Form<SchemaAttributes>;
@@ -64,4 +45,3 @@ export declare const generateValidationSchema: (<A extends SchemaAttributes = Sc
64
45
  [x: string]: any;
65
46
  }, "">) & MemoizedFunction;
66
47
  export declare const generateAttributeValidationSchema: ((attribute: Attribute, language: string, strings: FormValidationStringSet) => yup.Schema<any, any, any, "">) & MemoizedFunction;
67
- export {};
@@ -32,126 +32,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
32
32
  });
33
33
  };
34
34
  Object.defineProperty(exports, "__esModule", { value: true });
35
- exports.generateAttributeValidationSchema = exports.generateValidationSchema = exports.formValidator = void 0;
36
- exports.getModifiedValues = getModifiedValues;
37
- exports.saveRecord = saveRecord;
35
+ exports.generateAttributeValidationSchema = exports.generateValidationSchema = exports.formValidator = exports.saveRecord = void 0;
38
36
  exports.getInitialValues = getInitialValues;
39
37
  const yup_1 = require("@hookform/resolvers/yup");
40
38
  const lodash_1 = require("lodash");
41
39
  const yup = __importStar(require("yup"));
42
40
  const DataResolver_1 = require("../../dataform/DataFormProvider/DataResolver");
43
41
  const utils_1 = require("../../locale/utils");
44
- function getModifiedValues(initialValues, values, exclude) {
45
- const keys = Object.keys(values);
46
- return keys.reduce((p, c) => {
47
- if (c === '_id') {
48
- return p;
49
- }
50
- if (exclude && exclude.includes(c)) {
51
- return p;
52
- }
53
- if (JSON.stringify(values[c]) !== JSON.stringify(initialValues[c])) {
54
- p[c] = values[c];
55
- }
56
- return p;
57
- }, {});
58
- }
59
- function saveRecord(_a) {
60
- return __awaiter(this, arguments, void 0, function* ({ values, form, schema, dataService, initialValues, record, schemaStore, }) {
61
- const controls = (0, DataResolver_1.getControls)(form);
62
- const editableGridControls = controls.filter((control) => control.type === 'editablegrid');
63
- const modifiedValues = getModifiedValues(initialValues, values, editableGridControls.map((x) => x.attributeName));
64
- let recordId;
65
- if (record) {
66
- recordId = record[schema.idAttribute];
67
- const operations = [];
68
- if (Object.keys(modifiedValues).length) {
69
- operations.push({
70
- type: 'update',
71
- logicalName: schema.logicalName,
72
- data: modifiedValues,
73
- id: recordId,
74
- });
75
- }
76
- for (const control of editableGridControls) {
77
- const gridSchema = schemaStore.getSchema(control.logicalName);
78
- const gridRows = values[control.attributeName];
79
- const initialGridRows = initialValues[control.attributeName];
80
- const newRows = gridRows.filter((x) => !x[gridSchema.idAttribute]);
81
- const updatedRows = gridRows.filter((x) => x[gridSchema.idAttribute]);
82
- const deletedIds = initialGridRows === null || initialGridRows === void 0 ? void 0 : initialGridRows.map((x) => x[gridSchema.idAttribute]).filter((id) => !gridRows.find((x) => x[gridSchema.idAttribute] === id));
83
- for (const row of newRows) {
84
- operations.push({
85
- type: 'create',
86
- logicalName: control.logicalName,
87
- data: Object.assign(Object.assign({}, row), { [control.referenceAttribute]: {
88
- id: recordId,
89
- } }),
90
- });
91
- }
92
- for (const row of updatedRows) {
93
- const initialRow = initialGridRows.find((x) => x[gridSchema.idAttribute] === row[gridSchema.idAttribute]);
94
- if (!initialRow) {
95
- throw new Error('Initial row not found');
96
- }
97
- const modifiedRow = getModifiedValues(initialRow, row);
98
- if (!Object.keys(modifiedRow).length) {
99
- continue;
100
- }
101
- operations.push({
102
- type: 'update',
103
- logicalName: control.logicalName,
104
- data: modifiedRow,
105
- id: row[gridSchema.idAttribute],
106
- });
107
- }
108
- for (const id of deletedIds) {
109
- operations.push({
110
- type: 'delete',
111
- logicalName: control.logicalName,
112
- id,
113
- });
114
- }
115
- }
116
- if (!operations.length) {
117
- return {
118
- success: false,
119
- title: 'No changes',
120
- message: 'No changes made to the record',
121
- isError: false,
122
- };
123
- }
124
- for (const operation of operations) {
125
- switch (operation.type) {
126
- case 'create':
127
- yield dataService.createRecord(operation.logicalName, operation.data);
128
- break;
129
- case 'update':
130
- yield dataService.updateRecord(operation.logicalName, operation.id, operation.data);
131
- break;
132
- case 'delete':
133
- yield dataService.deleteRecord(operation.logicalName, operation.id);
134
- }
135
- }
136
- }
137
- else {
138
- const result = yield dataService.createRecord(schema.logicalName, values);
139
- recordId = result[schema.idAttribute];
140
- for (const control of editableGridControls) {
141
- const gridRows = values[control.attributeName];
142
- for (const row of gridRows) {
143
- yield dataService.createRecord(control.logicalName, Object.assign(Object.assign({}, row), { [control.referenceAttribute]: {
144
- id: recordId,
145
- } }));
146
- }
147
- }
148
- }
149
- return {
150
- success: true,
151
- recordId,
152
- };
153
- });
154
- }
42
+ var saveRecord_1 = require("./saveRecord");
43
+ Object.defineProperty(exports, "saveRecord", { enumerable: true, get: function () { return saveRecord_1.saveRecord; } });
155
44
  function getInitialValues({ cloneRecord, form, record, recordId, defaultParameters, }) {
156
45
  const formColumns = (0, DataResolver_1.getColumns)(form);
157
46
  const editableGridControls = (0, DataResolver_1.getControls)(form).filter((control) => control.type === 'editablegrid');
@@ -0,0 +1,25 @@
1
+ import { Form } from '@headless-adminapp/core/experience/form';
2
+ import { InferredSchemaType, Schema, SchemaAttributes } from '@headless-adminapp/core/schema';
3
+ import { ISchemaStore } from '@headless-adminapp/core/store';
4
+ import { IDataService } from '@headless-adminapp/core/transport';
5
+ import { Nullable } from '@headless-adminapp/core/types';
6
+ export declare function getModifiedValues(initialValues: any, values: any, exclude?: string[]): Record<string, any>;
7
+ type SaveRecordResult = {
8
+ success: true;
9
+ recordId: string;
10
+ } | {
11
+ success: false;
12
+ title?: string;
13
+ message: string;
14
+ isError: boolean;
15
+ };
16
+ export declare function saveRecord({ values, form, schema, dataService, initialValues, record, schemaStore, }: {
17
+ values: any;
18
+ form: Form<SchemaAttributes>;
19
+ record: InferredSchemaType<SchemaAttributes> | undefined;
20
+ initialValues: Nullable<InferredSchemaType<SchemaAttributes>>;
21
+ schema: Schema<SchemaAttributes>;
22
+ dataService: IDataService;
23
+ schemaStore: ISchemaStore;
24
+ }): Promise<SaveRecordResult>;
25
+ export {};
@@ -0,0 +1,167 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.getModifiedValues = getModifiedValues;
13
+ exports.saveRecord = saveRecord;
14
+ const DataResolver_1 = require("../../dataform/DataFormProvider/DataResolver");
15
+ function getModifiedValues(initialValues, values, exclude) {
16
+ const keys = Object.keys(values);
17
+ return keys.reduce((p, c) => {
18
+ if (c === '_id') {
19
+ return p;
20
+ }
21
+ if (exclude === null || exclude === void 0 ? void 0 : exclude.includes(c)) {
22
+ return p;
23
+ }
24
+ if (JSON.stringify(values[c]) !== JSON.stringify(initialValues[c])) {
25
+ p[c] = values[c];
26
+ }
27
+ return p;
28
+ }, {});
29
+ }
30
+ function executeOperation(operation, dataService) {
31
+ return __awaiter(this, void 0, void 0, function* () {
32
+ switch (operation.type) {
33
+ case 'create':
34
+ yield dataService.createRecord(operation.logicalName, operation.data);
35
+ break;
36
+ case 'update':
37
+ yield dataService.updateRecord(operation.logicalName, operation.id, operation.data);
38
+ break;
39
+ case 'delete':
40
+ yield dataService.deleteRecord(operation.logicalName, operation.id);
41
+ }
42
+ });
43
+ }
44
+ function generateSubgridUpdateOperation({ recordId, control, schemaStore, values, initialValues, }) {
45
+ const operations = [];
46
+ const gridSchema = schemaStore.getSchema(control.logicalName);
47
+ const gridRows = values[control.attributeName];
48
+ const initialGridRows = initialValues[control.attributeName];
49
+ const newRows = gridRows.filter((x) => !x[gridSchema.idAttribute]);
50
+ const updatedRows = gridRows.filter((x) => x[gridSchema.idAttribute]);
51
+ const deletedIds = initialGridRows === null || initialGridRows === void 0 ? void 0 : initialGridRows.map((x) => x[gridSchema.idAttribute]).filter((id) => !gridRows.find((x) => x[gridSchema.idAttribute] === id));
52
+ for (const row of newRows) {
53
+ operations.push({
54
+ type: 'create',
55
+ logicalName: control.logicalName,
56
+ data: Object.assign(Object.assign({}, row), { [control.referenceAttribute]: {
57
+ id: recordId,
58
+ } }),
59
+ });
60
+ }
61
+ for (const row of updatedRows) {
62
+ const initialRow = initialGridRows.find((x) => x[gridSchema.idAttribute] === row[gridSchema.idAttribute]);
63
+ if (!initialRow) {
64
+ throw new Error('Initial row not found');
65
+ }
66
+ const modifiedRow = getModifiedValues(initialRow, row);
67
+ if (!Object.keys(modifiedRow).length) {
68
+ continue;
69
+ }
70
+ operations.push({
71
+ type: 'update',
72
+ logicalName: control.logicalName,
73
+ data: modifiedRow,
74
+ id: row[gridSchema.idAttribute],
75
+ });
76
+ }
77
+ for (const id of deletedIds) {
78
+ operations.push({
79
+ type: 'delete',
80
+ logicalName: control.logicalName,
81
+ id,
82
+ });
83
+ }
84
+ return operations;
85
+ }
86
+ function createRecord(_a) {
87
+ return __awaiter(this, arguments, void 0, function* ({ values, form, schema, dataService, }) {
88
+ const controls = (0, DataResolver_1.getControls)(form);
89
+ const editableGridControls = controls.filter((control) => control.type === 'editablegrid');
90
+ const result = yield dataService.createRecord(schema.logicalName, values);
91
+ const recordId = result[schema.idAttribute];
92
+ for (const control of editableGridControls) {
93
+ const gridRows = values[control.attributeName];
94
+ for (const row of gridRows) {
95
+ yield dataService.createRecord(control.logicalName, Object.assign(Object.assign({}, row), { [control.referenceAttribute]: {
96
+ id: recordId,
97
+ } }));
98
+ }
99
+ }
100
+ return recordId;
101
+ });
102
+ }
103
+ function updateRecord(_a) {
104
+ return __awaiter(this, arguments, void 0, function* ({ recordId, values, form, schema, dataService, initialValues, schemaStore, }) {
105
+ const controls = (0, DataResolver_1.getControls)(form);
106
+ const editableGridControls = controls.filter((control) => control.type === 'editablegrid');
107
+ const modifiedValues = getModifiedValues(initialValues, values, editableGridControls.map((x) => x.attributeName));
108
+ const operations = [];
109
+ if (Object.keys(modifiedValues).length) {
110
+ operations.push({
111
+ type: 'update',
112
+ logicalName: schema.logicalName,
113
+ data: modifiedValues,
114
+ id: recordId,
115
+ });
116
+ }
117
+ for (const control of editableGridControls) {
118
+ operations.push(...generateSubgridUpdateOperation({
119
+ recordId,
120
+ control,
121
+ schemaStore,
122
+ initialValues,
123
+ values,
124
+ }));
125
+ }
126
+ if (!operations.length) {
127
+ return {
128
+ success: false,
129
+ title: 'No changes',
130
+ message: 'No changes made to the record',
131
+ isError: false,
132
+ };
133
+ }
134
+ for (const operation of operations) {
135
+ yield executeOperation(operation, dataService);
136
+ }
137
+ });
138
+ }
139
+ function saveRecord(_a) {
140
+ return __awaiter(this, arguments, void 0, function* ({ values, form, schema, dataService, initialValues, record, schemaStore, }) {
141
+ let recordId;
142
+ if (record) {
143
+ recordId = record[schema.idAttribute];
144
+ yield updateRecord({
145
+ recordId,
146
+ values,
147
+ form,
148
+ schema,
149
+ dataService,
150
+ initialValues,
151
+ schemaStore,
152
+ });
153
+ }
154
+ else {
155
+ recordId = yield createRecord({
156
+ dataService,
157
+ form,
158
+ schema,
159
+ values,
160
+ });
161
+ }
162
+ return {
163
+ success: true,
164
+ recordId,
165
+ };
166
+ });
167
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@headless-adminapp/app",
3
- "version": "0.0.17-alpha.47",
3
+ "version": "0.0.17-alpha.48",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -39,5 +39,5 @@
39
39
  "react-hook-form": "7.52.2",
40
40
  "yup": "^1.4.0"
41
41
  },
42
- "gitHead": "749a69f512d51c82f33fe27f8bbe5046fdeab4c5"
42
+ "gitHead": "e8887232802a525648c316b2b8526b2571bc8b8c"
43
43
  }