@headless-adminapp/app 1.1.10 → 1.1.12

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,3 +1,12 @@
1
- import { SchemaAttributes } from '@headless-adminapp/core/schema';
1
+ import { SectionEditableGridControl } from '@headless-adminapp/core/experience/form';
2
+ import { InferredSchemaType, SchemaAttributes } from '@headless-adminapp/core/schema';
3
+ import { ISchemaStore } from '@headless-adminapp/core/store';
4
+ import { IDataService } from '@headless-adminapp/core/transport';
2
5
  import { RetriveRecordFnOptions } from './types';
3
6
  export declare function getRecord<SA extends SchemaAttributes = SchemaAttributes>({ recordId, dataService, form, schema, columns, schemaStore, }: RetriveRecordFnOptions<SA>): Promise<import("@headless-adminapp/core/transport").Data<SA> | null>;
7
+ export declare function getEditableSubgridRecords({ dataService, schemaStore, control, recordId, }: {
8
+ dataService: IDataService;
9
+ schemaStore: ISchemaStore;
10
+ control: SectionEditableGridControl<SchemaAttributes>;
11
+ recordId: string;
12
+ }): Promise<import("@headless-adminapp/core/transport").Data<InferredSchemaType<SchemaAttributes>>[]>;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getRecord = getRecord;
4
+ exports.getEditableSubgridRecords = getEditableSubgridRecords;
4
5
  const transport_1 = require("@headless-adminapp/core/transport");
5
6
  const utils_1 = require("./utils");
6
7
  async function getRecord({ recordId, dataService, form, schema, columns, schemaStore, }) {
@@ -20,37 +21,46 @@ async function getRecord({ recordId, dataService, form, schema, columns, schemaS
20
21
  const controls = (0, utils_1.getControls)(form);
21
22
  const editableGridControls = controls.filter((control) => control.type === 'editablegrid');
22
23
  for (const control of editableGridControls) {
23
- if (control.type !== 'editablegrid') {
24
+ if (!control.alias) {
24
25
  continue;
25
26
  }
26
- const controlSchema = schemaStore.getSchema(control.logicalName);
27
- const records = await dataService.retriveRecords({
28
- logicalName: controlSchema.logicalName,
29
- filter: {
30
- type: 'and',
31
- conditions: [
32
- {
33
- field: control.referenceAttribute,
34
- operator: 'eq',
35
- value: recordId,
36
- },
37
- ],
38
- },
39
- sort: [
40
- {
41
- field: controlSchema.createdAtAttribute ?? controlSchema.idAttribute,
42
- order: 'asc',
43
- },
44
- ],
45
- limit: 5000,
46
- search: '',
47
- columns: [
48
- controlSchema.idAttribute,
49
- control.referenceAttribute,
50
- ...control.attributes,
51
- ],
27
+ const records = await getEditableSubgridRecords({
28
+ dataService,
29
+ control,
30
+ schemaStore,
31
+ recordId,
52
32
  });
53
- record[control.attributeName] = records.records;
33
+ record[control.alias] = records;
54
34
  }
55
35
  return record;
56
36
  }
37
+ async function getEditableSubgridRecords({ dataService, schemaStore, control, recordId, }) {
38
+ const controlSchema = schemaStore.getSchema(control.logicalName);
39
+ const result = await dataService.retriveRecords({
40
+ logicalName: controlSchema.logicalName,
41
+ filter: {
42
+ type: 'and',
43
+ conditions: [
44
+ {
45
+ field: control.associatedAttribute,
46
+ operator: 'eq',
47
+ value: recordId,
48
+ },
49
+ ],
50
+ },
51
+ sort: control.sort ?? [
52
+ {
53
+ field: controlSchema.createdAtAttribute ?? controlSchema.idAttribute,
54
+ order: 'asc',
55
+ },
56
+ ],
57
+ limit: 5000,
58
+ search: '',
59
+ columns: [
60
+ controlSchema.idAttribute,
61
+ control.associatedAttribute,
62
+ ...control.controls.map((x) => typeof x === 'string' ? x : x.attributeName),
63
+ ],
64
+ });
65
+ return result.records;
66
+ }
@@ -7,7 +7,7 @@ export declare function getControls<SA extends SchemaAttributes>(form: Form<SA>)
7
7
  export declare function getColumns<SA extends SchemaAttributes>(form: Form<SA>, schema: Schema<SA>): (keyof SA)[];
8
8
  export declare function transformFormInternal<SA extends SchemaAttributes>(form: Form<SA>): DataFormContextState<SA>['formInternal'];
9
9
  export declare function getIsFieldDisabled<S extends SchemaAttributes = SchemaAttributes>({ attribute, isFormReadonly, disabledFields, control, }: {
10
- attribute: AttributeBase;
10
+ attribute: AttributeBase | null;
11
11
  isFormReadonly: boolean | undefined;
12
12
  disabledFields: Record<string, boolean>;
13
13
  control: Section<S>['controls'][0];
@@ -76,7 +76,7 @@ function getIsFieldDisabled({ attribute, isFormReadonly, disabledFields, control
76
76
  else if ('disabled' in control && control.disabled !== undefined) {
77
77
  disabled = control.disabled;
78
78
  }
79
- else if (attribute.readonly !== undefined) {
79
+ else if (attribute && attribute.readonly !== undefined) {
80
80
  disabled = attribute.readonly;
81
81
  }
82
82
  }
@@ -1,5 +1,5 @@
1
1
  import type { Attribute } from '@headless-adminapp/core/attributes';
2
- import { Form } from '@headless-adminapp/core/experience/form';
2
+ import { Form, SectionEditableGridControl } 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
5
  import { Nullable } from '@headless-adminapp/core/types';
@@ -28,6 +28,19 @@ interface FormValidatorOptions<A extends SchemaAttributes = SchemaAttributes> {
28
28
  }
29
29
  type FormValidator = (<A extends SchemaAttributes = SchemaAttributes>(options: FormValidatorOptions<A>) => (values: Record<string, any>, context: any, options: any) => Promise<ResolverResult<{}>>) & MemoizedFunction;
30
30
  export declare const formValidator: FormValidator;
31
+ interface EditableSubgridFormValidatorOptions<A extends SchemaAttributes = SchemaAttributes> {
32
+ schema: Schema<A>;
33
+ control: SectionEditableGridControl<A>;
34
+ language: string;
35
+ formReadOnly?: boolean;
36
+ readonlyAttributes?: string[];
37
+ strings: FormValidationStringSet;
38
+ schemaStore: ISchemaStore;
39
+ region: string;
40
+ alias: string;
41
+ }
42
+ type EditableSubgridFormValidator = (<A extends SchemaAttributes = SchemaAttributes>(options: EditableSubgridFormValidatorOptions<A>) => (values: Record<string, any>, context: any, options: any) => Promise<ResolverResult<{}>>) & MemoizedFunction;
43
+ export declare const editableSubgridFormValidator: EditableSubgridFormValidator;
31
44
  export declare const generateValidationSchema: (<A extends SchemaAttributes = SchemaAttributes>({ columns, editableGrids, language, schema, strings, readonlyAttributes, region, }: {
32
45
  schema: Schema<A>;
33
46
  columns: string[];
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.generateAttributeValidationSchema = exports.generateValidationSchema = exports.formValidator = exports.saveRecord = void 0;
26
+ exports.generateAttributeValidationSchema = exports.generateValidationSchema = exports.editableSubgridFormValidator = exports.formValidator = exports.saveRecord = void 0;
27
27
  exports.getInitialValues = getInitialValues;
28
28
  const phone_1 = require("@headless-adminapp/app/utils/phone");
29
29
  const yup_1 = require("@hookform/resolvers/yup");
@@ -35,10 +35,10 @@ var saveRecord_1 = require("./saveRecord");
35
35
  Object.defineProperty(exports, "saveRecord", { enumerable: true, get: function () { return saveRecord_1.saveRecord; } });
36
36
  function getInitialValues({ cloneRecord, form, record, recordId, schema, defaultParameters, }) {
37
37
  const formColumns = (0, utils_1.getColumns)(form, schema);
38
- const editableGridControls = (0, utils_1.getControls)(form).filter((control) => control.type === 'editablegrid');
38
+ const editableGridControls = (0, utils_1.getControls)(form).filter((control) => control.type === 'editablegrid' && control.alias !== false);
39
39
  const allColumns = [
40
40
  ...formColumns,
41
- ...editableGridControls.map((x) => x.attributeName),
41
+ ...editableGridControls.map((x) => x.alias),
42
42
  ];
43
43
  if (!recordId && !record && form.experience.cloneAttributes && cloneRecord) {
44
44
  const cloneAttributesObj = form.experience.cloneAttributes.reduce((acc, item) => {
@@ -116,7 +116,7 @@ exports.formValidator = (0, lodash_1.memoize)(function formValidator({ form, sch
116
116
  }
117
117
  return true;
118
118
  });
119
- const editableGridControls = activeControls.filter((control) => control.type === 'editablegrid');
119
+ const editableGridControls = activeControls.filter((control) => control.type === 'editablegrid' && control.alias !== false);
120
120
  const columns = Array.from(new Set([
121
121
  schema.primaryAttribute,
122
122
  ...activeControls
@@ -125,14 +125,14 @@ exports.formValidator = (0, lodash_1.memoize)(function formValidator({ form, sch
125
125
  ]));
126
126
  validator = (0, exports.generateValidationSchema)({
127
127
  editableGrids: editableGridControls.map((control) => {
128
- if (control.type !== 'editablegrid') {
128
+ if (control.type !== 'editablegrid' || control.alias === false) {
129
129
  throw new Error('Invalid control type');
130
130
  }
131
131
  const schema = schemaStore.getSchema(control.logicalName);
132
132
  return {
133
- columns: control.attributes,
133
+ columns: control.controls.map((x) => typeof x === 'string' ? x : x.attributeName),
134
134
  schema: schema,
135
- attributeName: control.attributeName,
135
+ attributeName: control.alias,
136
136
  required: control.required,
137
137
  };
138
138
  }),
@@ -156,6 +156,44 @@ exports.formValidator = (0, lodash_1.memoize)(function formValidator({ form, sch
156
156
  readonlyAttributes,
157
157
  formReadOnly,
158
158
  }));
159
+ exports.editableSubgridFormValidator = (0, lodash_1.memoize)(function formValidator({ schema, readonlyAttributes, formReadOnly, schemaStore, language, strings, region, control, alias, }) {
160
+ return async (values, context, options) => {
161
+ let validator = yup.object().shape({});
162
+ if (!formReadOnly) {
163
+ const controlSchema = schemaStore.getSchema(control.logicalName);
164
+ const columns = [];
165
+ validator = (0, exports.generateValidationSchema)({
166
+ editableGrids: [
167
+ {
168
+ columns: control.controls.map((x) => typeof x === 'string'
169
+ ? x
170
+ : x.attributeName),
171
+ schema: controlSchema,
172
+ attributeName: alias,
173
+ required: control.required,
174
+ },
175
+ ],
176
+ schema,
177
+ columns,
178
+ language,
179
+ strings,
180
+ readonlyAttributes,
181
+ region,
182
+ });
183
+ }
184
+ const resolver = (0, yup_1.yupResolver)(validator);
185
+ const result = await resolver(values, context, options);
186
+ return result;
187
+ };
188
+ }, ({ language, schema, strings, readonlyAttributes, formReadOnly, alias, control, }) => JSON.stringify({
189
+ schema,
190
+ language,
191
+ strings,
192
+ readonlyAttributes,
193
+ formReadOnly,
194
+ alias,
195
+ control,
196
+ }));
159
197
  exports.generateValidationSchema = (0, lodash_1.memoize)(function generateValidationSchema({ columns, editableGrids, language, schema, strings, readonlyAttributes, region, }) {
160
198
  return yup.object().shape({
161
199
  ...columns.reduce((acc, column) => {
@@ -1,5 +1,5 @@
1
1
  import { Id } from '@headless-adminapp/core';
2
- import { Form } from '@headless-adminapp/core/experience/form';
2
+ import { Form, SectionEditableGridControl } 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
5
  import { IDataService } from '@headless-adminapp/core/transport';
@@ -25,3 +25,38 @@ export interface SaveRecordFnOptions<S extends SchemaAttributes = SchemaAttribut
25
25
  }
26
26
  export type SaveRecordFn = (options: SaveRecordFnOptions) => Promise<SaveRecordResult>;
27
27
  export declare function saveRecord({ values, form, schema, dataService, initialValues, record, schemaStore, }: SaveRecordFnOptions): Promise<SaveRecordResult>;
28
+ interface SaveEditableGridControlOptions {
29
+ recordId: string;
30
+ control: SectionEditableGridControl;
31
+ schemaStore: ISchemaStore;
32
+ values: any;
33
+ initialValues: any;
34
+ dataService: IDataService;
35
+ alias: string;
36
+ }
37
+ export type SaveEditableGridResult = {
38
+ success: true;
39
+ updated: number;
40
+ created: number;
41
+ } | {
42
+ success: false;
43
+ title?: string;
44
+ message: string;
45
+ isError: boolean;
46
+ };
47
+ export declare function saveEditableGridControl({ recordId, control, schemaStore, values, initialValues, dataService, alias, }: SaveEditableGridControlOptions): Promise<{
48
+ success: boolean;
49
+ title: string;
50
+ message: string;
51
+ isError: boolean;
52
+ updated?: undefined;
53
+ created?: undefined;
54
+ } | {
55
+ success: boolean;
56
+ updated: number;
57
+ created: number;
58
+ title?: undefined;
59
+ message?: undefined;
60
+ isError?: undefined;
61
+ }>;
62
+ export {};
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getModifiedValues = getModifiedValues;
4
4
  exports.saveRecord = saveRecord;
5
+ exports.saveEditableGridControl = saveEditableGridControl;
5
6
  const utils_1 = require("../../dataform/DataFormProvider/utils");
6
7
  function getModifiedValues(initialValues, values, exclude) {
7
8
  const keys = Object.keys(values);
@@ -30,11 +31,11 @@ async function executeOperation(operation, dataService) {
30
31
  await dataService.deleteRecord(operation.logicalName, operation.id);
31
32
  }
32
33
  }
33
- function generateSubgridUpdateOperation({ recordId, control, schemaStore, values, initialValues, }) {
34
+ function generateSubgridUpdateOperation({ recordId, control, schemaStore, values, initialValues, alias, }) {
34
35
  const operations = [];
35
36
  const gridSchema = schemaStore.getSchema(control.logicalName);
36
- const gridRows = values[control.attributeName];
37
- const initialGridRows = initialValues[control.attributeName];
37
+ const gridRows = values[alias];
38
+ const initialGridRows = initialValues[alias];
38
39
  const newRows = gridRows.filter((x) => !x[gridSchema.idAttribute]);
39
40
  const updatedRows = gridRows.filter((x) => x[gridSchema.idAttribute]);
40
41
  const deletedIds = initialGridRows
@@ -46,9 +47,10 @@ function generateSubgridUpdateOperation({ recordId, control, schemaStore, values
46
47
  logicalName: control.logicalName,
47
48
  data: {
48
49
  ...row,
49
- [control.referenceAttribute]: {
50
+ [control.associatedAttribute]: {
50
51
  id: recordId,
51
52
  },
53
+ [gridSchema.idAttribute]: undefined,
52
54
  },
53
55
  });
54
56
  }
@@ -79,15 +81,15 @@ function generateSubgridUpdateOperation({ recordId, control, schemaStore, values
79
81
  }
80
82
  async function createRecord({ values, form, schema, dataService, }) {
81
83
  const controls = (0, utils_1.getControls)(form);
82
- const editableGridControls = controls.filter((control) => control.type === 'editablegrid');
84
+ const editableGridControls = controls.filter((control) => control.type === 'editablegrid' && control.alias);
83
85
  const result = await dataService.createRecord(schema.logicalName, values);
84
86
  const recordId = result.id;
85
87
  for (const control of editableGridControls) {
86
- const gridRows = values[control.attributeName];
88
+ const gridRows = values[control.alias];
87
89
  for (const row of gridRows) {
88
90
  await dataService.createRecord(control.logicalName, {
89
91
  ...row,
90
- [control.referenceAttribute]: {
92
+ [control.associatedAttribute]: {
91
93
  id: recordId,
92
94
  },
93
95
  });
@@ -97,8 +99,8 @@ async function createRecord({ values, form, schema, dataService, }) {
97
99
  }
98
100
  async function updateRecord({ recordId, values, form, schema, dataService, initialValues, schemaStore, }) {
99
101
  const controls = (0, utils_1.getControls)(form);
100
- const editableGridControls = controls.filter((control) => control.type === 'editablegrid');
101
- const modifiedValues = getModifiedValues(initialValues, values, editableGridControls.map((x) => x.attributeName));
102
+ const editableGridControls = controls.filter((control) => control.type === 'editablegrid' && control.alias);
103
+ const modifiedValues = getModifiedValues(initialValues, values, editableGridControls.map((x) => x.alias));
102
104
  const operations = [];
103
105
  if (Object.keys(modifiedValues).length) {
104
106
  operations.push({
@@ -115,6 +117,7 @@ async function updateRecord({ recordId, values, form, schema, dataService, initi
115
117
  schemaStore,
116
118
  initialValues,
117
119
  values,
120
+ alias: control.alias,
118
121
  }));
119
122
  }
120
123
  if (!operations.length) {
@@ -161,3 +164,29 @@ async function saveRecord({ values, form, schema, dataService, initialValues, re
161
164
  recordId,
162
165
  };
163
166
  }
167
+ async function saveEditableGridControl({ recordId, control, schemaStore, values, initialValues, dataService, alias, }) {
168
+ const operations = generateSubgridUpdateOperation({
169
+ recordId,
170
+ control,
171
+ schemaStore,
172
+ initialValues,
173
+ values,
174
+ alias,
175
+ });
176
+ if (!operations.length) {
177
+ return {
178
+ success: false,
179
+ title: 'No changes',
180
+ message: 'No changes made to the record',
181
+ isError: false,
182
+ };
183
+ }
184
+ for (const operation of operations) {
185
+ await executeOperation(operation, dataService);
186
+ }
187
+ return {
188
+ success: true,
189
+ updated: operations.filter((x) => x.type === 'update').length,
190
+ created: operations.filter((x) => x.type === 'create').length,
191
+ };
192
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@headless-adminapp/app",
3
- "version": "1.1.10",
3
+ "version": "1.1.12",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -36,5 +36,5 @@
36
36
  "uuid": "11.0.3",
37
37
  "yup": "^1.4.0"
38
38
  },
39
- "gitHead": "3920cedff7216fb37b07dff95c50499c0f56f57c"
39
+ "gitHead": "ead918f1f609c2a260ceae498368ddc2c4980725"
40
40
  }