drf-react-by-schema 0.5.2 → 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* () {
@@ -377,7 +378,7 @@ const getDataGridColumns = (schema, columnFields = [], hiddenFields = [], creata
377
378
  field,
378
379
  headerName: schema[field].label,
379
380
  hide: (hiddenFields.includes(field)),
380
- creatable: (creatableFields.includes(field)),
381
+ creatable: (creatableFields.includes(field) || schema[field].creatable),
381
382
  width: 100
382
383
  };
383
384
  return column;
@@ -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 });
@@ -74,7 +75,8 @@ function SelectEditInputCell({ field, id, value, column, type, optionsAC, isInde
74
75
  ? 'id'
75
76
  : 'value';
76
77
  let creatableProps = {};
77
- if (column.creatable || isIndexField) {
78
+ // if (column.creatable || isIndexField) {
79
+ if (column.creatable) {
78
80
  creatableProps = {
79
81
  freesolo: 'true',
80
82
  filterOptions: (options, params) => {
@@ -117,7 +119,8 @@ function SelectEditInputCell({ field, id, value, column, type, optionsAC, isInde
117
119
  }, getOptionLabel: (option) => {
118
120
  return option[labelKey];
119
121
  }, onChange: (e, value) => {
120
- if (!column.creatable && !isIndexField) {
122
+ // if (!column.creatable && !isIndexField) {
123
+ if (!column.creatable) {
121
124
  handleChange(value);
122
125
  return;
123
126
  }
@@ -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,
@@ -72,11 +72,14 @@ function APIWrapper({ setLoading, handleLoading, setSnackBar, setDialog, childre
72
72
  const [pageForm, setPageForm] = (0, react_1.useReducer)(utils_1.reducer, initialPageForm);
73
73
  const editModel = (0, react_1.useRef)({});
74
74
  const jsonSchemaFormRef = (0, react_1.useRef)(null);
75
- (0, react_1.useEffect)(() => {
75
+ const updateUsuaria = () => {
76
76
  setUsuaria(null);
77
77
  (0, api_1.isLoggedIn)(serverEndPoint).then(usuaria => {
78
78
  setUsuaria(usuaria || { erro: 'token inválido' });
79
79
  });
80
+ };
81
+ (0, react_1.useEffect)(() => {
82
+ updateUsuaria();
80
83
  }, []);
81
84
  (0, react_1.useEffect)(() => {
82
85
  setPageForm(initialPageForm);
@@ -88,7 +91,7 @@ function APIWrapper({ setLoading, handleLoading, setSnackBar, setDialog, childre
88
91
  severity
89
92
  });
90
93
  };
91
- 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* () {
92
95
  setLoading(true);
93
96
  if (!objId || objId === 'novo') {
94
97
  objId = (0, utils_1.getTmpId)();
@@ -104,7 +107,9 @@ function APIWrapper({ setLoading, handleLoading, setSnackBar, setDialog, childre
104
107
  return false;
105
108
  }
106
109
  setLoading(false);
107
- populateOptionsAC(optionsACModels);
110
+ if (optionsACModels) {
111
+ populateOptionsAC(optionsACModels);
112
+ }
108
113
  for (const [field, value] of Object.entries(defaults)) {
109
114
  if (!object.data[field]) {
110
115
  object.data[field] = value;
@@ -114,6 +119,10 @@ function APIWrapper({ setLoading, handleLoading, setSnackBar, setDialog, childre
114
119
  return values;
115
120
  });
116
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
+ }
117
126
  setLoading(true);
118
127
  const response = yield (0, api_1.updateDataBySchema)({
119
128
  model,
@@ -331,6 +340,7 @@ function APIWrapper({ setLoading, handleLoading, setSnackBar, setDialog, childre
331
340
  });
332
341
  return (react_1.default.createElement(APIWrapperContext_1.APIWrapperContext.Provider, { value: {
333
342
  usuaria,
343
+ updateUsuaria,
334
344
  onSubmit,
335
345
  loadSinglePageData,
336
346
  handleLoading,
@@ -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;
@@ -58,15 +58,12 @@ export interface DialogType {
58
58
  export type DRFReactBySchemaSubmitHandler = (...args: [...[model: string, id: Id], ...Parameters<SubmitHandler<FieldValues>>]) => ReturnType<SubmitHandler<FieldValues>>;
59
59
  export interface APIWrapperContextType {
60
60
  usuaria: Item | null;
61
+ updateUsuaria: () => void;
61
62
  onSubmit: DRFReactBySchemaSubmitHandler;
62
63
  loadSinglePageData: (p: LoadSinglePageDataProps) => Promise<boolean | FieldValues>;
63
64
  handleLoading: (p: boolean) => void;
64
- optionsACState: [{
65
- [x: string]: any;
66
- }, React.Dispatch<OptionsACType>];
67
- pageFormState: [{
68
- [x: string]: any;
69
- }, React.Dispatch<PageFormType>];
65
+ optionsACState: [OptionsACType | null, React.Dispatch<OptionsACType>];
66
+ pageFormState: [Record<string, any> | null, React.Dispatch<PageFormType>];
70
67
  onEditModel: (p: OnEditModelType) => void;
71
68
  onDeleteModel: (model: string, id: Id, onSuccess?: (e: React.BaseSyntheticEvent) => any) => void;
72
69
  onEditRelatedModelSave: (p: OnEditRelatedModelType) => Promise<boolean | Id | ItemSchemaColumnsType>;
package/dist/utils.d.ts CHANGED
@@ -23,6 +23,9 @@ export interface Field {
23
23
  is_currency?: boolean;
24
24
  prefix?: string;
25
25
  suffix?: string;
26
+ creatable?: boolean;
27
+ related_editable?: boolean;
28
+ validators_regex?: Item[];
26
29
  }
27
30
  interface GridBySchemaColDef extends GridColDef {
28
31
  isIndexField?: boolean;
@@ -74,9 +77,7 @@ export declare const errorProps: ({ type, errors, fieldKey, fieldKeyProp, index
74
77
  };
75
78
  export declare const getTmpId: () => string;
76
79
  export declare const isTmpId: (id: string | number | undefined | null) => boolean;
77
- export declare const reducer: (state: Record<string, any>, newState: Record<string, any>) => {
78
- [x: string]: any;
79
- };
80
+ export declare const reducer: (state: Record<string, any> | null, newState: Record<string, any>) => Record<string, any> | null;
80
81
  export declare const getPatternFormat: (type: string) => string;
81
82
  export type AddParametersToEnd<TFunction extends (...args: any) => any, TParameters extends [...args: any]> = (...args: [...Parameters<TFunction>, ...TParameters]) => ReturnType<TFunction>;
82
83
  export {};
package/dist/utils.js CHANGED
@@ -51,6 +51,8 @@ const emptyByType = (field, forDatabase = false) => {
51
51
  return 0;
52
52
  case 'array':
53
53
  return [];
54
+ case 'boolean':
55
+ return false;
54
56
  default:
55
57
  return null;
56
58
  }
@@ -215,6 +217,11 @@ const buildGenericYupValidationSchema = ({ data, schema, many = false, skipField
215
217
  return val.toString().length <= maxDigits;
216
218
  });
217
219
  }
220
+ if (!field.read_only && field.validators_regex) {
221
+ for (const validator of field.validators_regex) {
222
+ yupValidator[key] = yupValidator[key].matches(validator.regex, validator.message);
223
+ }
224
+ }
218
225
  }
219
226
  // console.log({ yupValidator });
220
227
  return (many)
@@ -249,6 +256,12 @@ const isTmpId = (id) => {
249
256
  };
250
257
  exports.isTmpId = isTmpId;
251
258
  const reducer = (state, newState) => {
259
+ if (newState === null) {
260
+ return null;
261
+ }
262
+ if (state === null) {
263
+ return newState;
264
+ }
252
265
  return Object.assign(Object.assign({}, state), newState);
253
266
  };
254
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.2",
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",