drf-react-by-schema 0.5.3 → 0.5.4

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.
package/dist/api.js CHANGED
@@ -330,6 +330,7 @@ const prepareDataBySchema = ({ data, schema, parentIsField = false }) => {
330
330
  // Default:
331
331
  dbData[key] = data[key];
332
332
  }
333
+ // console.log({dbData});
333
334
  return dbData;
334
335
  };
335
336
  const updateDataBySchema = ({ model, modelObjectId, serverEndPoint, data, schema, path = null }) => __awaiter(void 0, void 0, void 0, function* () {
@@ -63,6 +63,7 @@ const filter = (0, Autocomplete_1.createFilterOptions)();
63
63
  function SelectEditInputCell({ field, id, value, column, type, optionsAC, isIndexField, multiple = false, onEditModel, sx = {} }) {
64
64
  // TODO: allow edit option label, as in formautocomplete!
65
65
  const apiRef = (0, x_data_grid_1.useGridApiContext)();
66
+ console.log(column);
66
67
  const handleChange = (newValue) => __awaiter(this, void 0, void 0, function* () {
67
68
  yield apiRef.current.setEditCellValue({ id, field, value: newValue });
68
69
  apiRef.current.stopCellEditMode({ id, field });
@@ -160,6 +160,9 @@ const DataGridBySchemaEditable = (0, react_1.forwardRef)((_a, ref) => {
160
160
  col.isIndexField = true;
161
161
  }
162
162
  column.editable = (isEditable) && (!schema[col.field].read_only || ['field', 'nested object'].includes(schema[col.field].type));
163
+ if (['field', 'nested object'].includes(schema[col.field].type)) {
164
+ column.creatable = 'related_editable' in schema[col.field] ? schema[col.field].related_editable : column.creatable;
165
+ }
163
166
  switch (schema[col.field].type) {
164
167
  case 'date':
165
168
  column.type = 'date';
@@ -62,7 +62,7 @@ function APIWrapper({ setLoading, handleLoading, setSnackBar, setDialog, childre
62
62
  return (react_1.default.createElement(react_1.default.Fragment, null, children));
63
63
  }
64
64
  const [usuaria, setUsuaria] = (0, react_1.useState)(null);
65
- const [optionsAC, setOptionsAC] = (0, react_1.useReducer)(utils_1.reducer, {});
65
+ const [optionsAC, setOptionsAC] = (0, react_1.useReducer)(utils_1.reducer, null);
66
66
  const initialPageForm = {
67
67
  id: '',
68
68
  schema: null,
@@ -91,7 +91,7 @@ function APIWrapper({ setLoading, handleLoading, setSnackBar, setDialog, childre
91
91
  severity
92
92
  });
93
93
  };
94
- const loadSinglePageData = ({ model, objId, optionsACModels, defaults = {}, extraValidators = {} }) => __awaiter(this, void 0, void 0, function* () {
94
+ const loadSinglePageData = ({ model, objId, optionsACModels = null, defaults = {}, extraValidators = {} }) => __awaiter(this, void 0, void 0, function* () {
95
95
  setLoading(true);
96
96
  if (!objId || objId === 'novo') {
97
97
  objId = (0, utils_1.getTmpId)();
@@ -107,7 +107,9 @@ function APIWrapper({ setLoading, handleLoading, setSnackBar, setDialog, childre
107
107
  return false;
108
108
  }
109
109
  setLoading(false);
110
- populateOptionsAC(optionsACModels);
110
+ if (optionsACModels) {
111
+ populateOptionsAC(optionsACModels);
112
+ }
111
113
  for (const [field, value] of Object.entries(defaults)) {
112
114
  if (!object.data[field]) {
113
115
  object.data[field] = value;
@@ -117,6 +119,10 @@ function APIWrapper({ setLoading, handleLoading, setSnackBar, setDialog, childre
117
119
  return values;
118
120
  });
119
121
  const onSubmit = (model, id, data) => __awaiter(this, void 0, void 0, function* () {
122
+ if (!pageForm) {
123
+ console.log('there must be a pageForm!');
124
+ return false;
125
+ }
120
126
  setLoading(true);
121
127
  const response = yield (0, api_1.updateDataBySchema)({
122
128
  model,
@@ -6,7 +6,7 @@ export interface LoadSinglePageDataProps {
6
6
  model: string;
7
7
  objId?: Id;
8
8
  objTitleField?: string;
9
- optionsACModels: string[];
9
+ optionsACModels: string[] | null;
10
10
  defaults?: Item;
11
11
  basePath?: string;
12
12
  formPath?: string | null;
@@ -62,12 +62,8 @@ export interface APIWrapperContextType {
62
62
  onSubmit: DRFReactBySchemaSubmitHandler;
63
63
  loadSinglePageData: (p: LoadSinglePageDataProps) => Promise<boolean | FieldValues>;
64
64
  handleLoading: (p: boolean) => void;
65
- optionsACState: [{
66
- [x: string]: any;
67
- }, React.Dispatch<OptionsACType>];
68
- pageFormState: [{
69
- [x: string]: any;
70
- }, React.Dispatch<PageFormType>];
65
+ optionsACState: [OptionsACType | null, React.Dispatch<OptionsACType>];
66
+ pageFormState: [Record<string, any> | null, React.Dispatch<PageFormType>];
71
67
  onEditModel: (p: OnEditModelType) => void;
72
68
  onDeleteModel: (model: string, id: Id, onSuccess?: (e: React.BaseSyntheticEvent) => any) => void;
73
69
  onEditRelatedModelSave: (p: OnEditRelatedModelType) => Promise<boolean | Id | ItemSchemaColumnsType>;
package/dist/utils.d.ts CHANGED
@@ -24,6 +24,7 @@ export interface Field {
24
24
  prefix?: string;
25
25
  suffix?: string;
26
26
  creatable?: boolean;
27
+ related_editable?: boolean;
27
28
  validators_regex?: Item[];
28
29
  }
29
30
  interface GridBySchemaColDef extends GridColDef {
@@ -76,9 +77,7 @@ export declare const errorProps: ({ type, errors, fieldKey, fieldKeyProp, index
76
77
  };
77
78
  export declare const getTmpId: () => string;
78
79
  export declare const isTmpId: (id: string | number | undefined | null) => boolean;
79
- export declare const reducer: (state: Record<string, any>, newState: Record<string, any>) => {
80
- [x: string]: any;
81
- };
80
+ export declare const reducer: (state: Record<string, any> | null, newState: Record<string, any>) => Record<string, any> | null;
82
81
  export declare const getPatternFormat: (type: string) => string;
83
82
  export type AddParametersToEnd<TFunction extends (...args: any) => any, TParameters extends [...args: any]> = (...args: [...Parameters<TFunction>, ...TParameters]) => ReturnType<TFunction>;
84
83
  export {};
package/dist/utils.js CHANGED
@@ -217,7 +217,7 @@ const buildGenericYupValidationSchema = ({ data, schema, many = false, skipField
217
217
  return val.toString().length <= maxDigits;
218
218
  });
219
219
  }
220
- if (field.validators_regex) {
220
+ if (!field.read_only && field.validators_regex) {
221
221
  for (const validator of field.validators_regex) {
222
222
  yupValidator[key] = yupValidator[key].matches(validator.regex, validator.message);
223
223
  }
@@ -256,6 +256,12 @@ const isTmpId = (id) => {
256
256
  };
257
257
  exports.isTmpId = isTmpId;
258
258
  const reducer = (state, newState) => {
259
+ if (newState === null) {
260
+ return null;
261
+ }
262
+ if (state === null) {
263
+ return newState;
264
+ }
259
265
  return Object.assign(Object.assign({}, state), newState);
260
266
  };
261
267
  exports.reducer = reducer;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drf-react-by-schema",
3
- "version": "0.5.3",
3
+ "version": "0.5.4",
4
4
  "description": "Components and Tools for building a React App having Django Rest Framework (DRF) as server",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",