drf-react-by-schema 0.4.0 → 0.5.0

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.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { serverEndPointType } from './context/DRFReactBySchemaContext';
2
- import { Item, SchemaType, DataSchemaColumnsType, ItemSchemaColumnsType } from './utils';
2
+ import { Item, SchemaType, modelOptionsType, DataSchemaColumnsType, ItemSchemaColumnsType } from './utils';
3
3
  type Id = string | number | null;
4
4
  /**
5
5
  * Interface for CRUD on API
@@ -71,8 +71,9 @@ export interface getGenericModelListProps {
71
71
  creatableFields?: string[];
72
72
  isInBatches?: boolean;
73
73
  loadedSchema?: SchemaType | boolean;
74
+ loadedModelOptions?: modelOptionsType | boolean;
74
75
  }
75
- export declare const getGenericModelList: ({ model, serverEndPoint, id, relatedModel, relatedModelId, columnFields, hiddenFields, creatableFields, isInBatches, loadedSchema }: getGenericModelListProps) => Promise<false | DataSchemaColumnsType>;
76
+ export declare const getGenericModelList: ({ model, serverEndPoint, id, relatedModel, relatedModelId, columnFields, hiddenFields, creatableFields, isInBatches, loadedSchema, loadedModelOptions, }: getGenericModelListProps) => Promise<false | DataSchemaColumnsType>;
76
77
  export declare const getGenericModel: ({ model, serverEndPoint, id, relatedModel, relatedModelId }: {
77
78
  model: string;
78
79
  serverEndPoint: serverEndPointType | null;
package/dist/api.js CHANGED
@@ -73,7 +73,11 @@ const getSchema = (path, serverEndPoint) => __awaiter(void 0, void 0, void 0, fu
73
73
  }
74
74
  }
75
75
  }
76
- return options.actions.POST;
76
+ const modelOptions = Object.assign({}, options);
77
+ delete modelOptions.renders;
78
+ delete modelOptions.parses;
79
+ delete modelOptions.actions;
80
+ return { schema: options.actions.POST, modelOptions };
77
81
  });
78
82
  const getData = (path, serverEndPoint, route = 'api') => __awaiter(void 0, void 0, void 0, function* () {
79
83
  var _b;
@@ -532,10 +536,11 @@ const isLoggedIn = (serverEndPoint) => __awaiter(void 0, void 0, void 0, functio
532
536
  });
533
537
  exports.isLoggedIn = isLoggedIn;
534
538
  ;
535
- const getGenericModelList = ({ model, serverEndPoint, id = '', relatedModel = '', relatedModelId = '', columnFields, hiddenFields = ['id'], creatableFields = [], isInBatches = false, loadedSchema }) => __awaiter(void 0, void 0, void 0, function* () {
539
+ const getGenericModelList = ({ model, serverEndPoint, id = '', relatedModel = '', relatedModelId = '', columnFields, hiddenFields = ['id'], creatableFields = [], isInBatches = false, loadedSchema, loadedModelOptions, }) => __awaiter(void 0, void 0, void 0, function* () {
536
540
  let path = `${model}/${id}`;
537
541
  let schemaPath = model;
538
542
  let schema = loadedSchema;
543
+ let modelOptions = loadedModelOptions;
539
544
  let columns;
540
545
  if (!(0, utils_1.isTmpId)(id) && relatedModel) {
541
546
  path += `/${relatedModel}/${relatedModelId}`;
@@ -543,10 +548,12 @@ const getGenericModelList = ({ model, serverEndPoint, id = '', relatedModel = ''
543
548
  }
544
549
  // Only get schema and columns if not in batches or in first batch:
545
550
  if (!schema) {
546
- schema = yield getSchema(schemaPath, serverEndPoint);
547
- if (!schema) {
551
+ const options = yield getSchema(schemaPath, serverEndPoint);
552
+ if (!options) {
548
553
  return false;
549
554
  }
555
+ schema = options.schema;
556
+ modelOptions = options.modelOptions;
550
557
  columns = getDataGridColumns(schema, columnFields, hiddenFields, creatableFields);
551
558
  if (!columns) {
552
559
  return false;
@@ -590,7 +597,7 @@ const getGenericModelList = ({ model, serverEndPoint, id = '', relatedModel = ''
590
597
  return { data };
591
598
  }
592
599
  // DEBUG console.log({ path, data, columns, schema });
593
- return { data, columns, schema };
600
+ return { data, columns, schema, modelOptions };
594
601
  });
595
602
  exports.getGenericModelList = getGenericModelList;
596
603
  const getGenericModel = ({ model, serverEndPoint, id = '', relatedModel = '', relatedModelId = '' }) => __awaiter(void 0, void 0, void 0, function* () {
@@ -600,14 +607,15 @@ const getGenericModel = ({ model, serverEndPoint, id = '', relatedModel = '', re
600
607
  path += `/${relatedModel}/${relatedModelId}`;
601
608
  schemaPath += `/${id}/${relatedModel}`;
602
609
  }
603
- const schema = yield getSchema(schemaPath, serverEndPoint);
604
- if (!schema) {
610
+ const options = yield getSchema(schemaPath, serverEndPoint);
611
+ if (!options) {
605
612
  return false;
606
613
  }
614
+ const { schema, modelOptions } = options;
607
615
  const data = (!id || (id && relatedModel && !relatedModelId))
608
616
  ? {}
609
617
  : yield getData(path, serverEndPoint);
610
618
  // console.log({ schema, data }); // DEBUG
611
- return { schema, data };
619
+ return { schema, modelOptions, data };
612
620
  });
613
621
  exports.getGenericModel = getGenericModel;
@@ -415,9 +415,7 @@ const DataGridBySchemaEditable = (0, react_1.forwardRef)((_a, ref) => {
415
415
  setDataGridLoading(true);
416
416
  const indexCol = preparedColumns.find(col => col.field === indexField);
417
417
  processingRow.current = newRow.id;
418
- console.log('antes');
419
418
  yield yupValidationSchema.current.validate(newRow);
420
- console.log('depois');
421
419
  const onlyAddExisting = (indexField &&
422
420
  (0, utils_1.isTmpId)(newRow.id) &&
423
421
  (newRow[indexField] && !(0, utils_1.isTmpId)(newRow[indexField].id)) &&
@@ -443,7 +441,6 @@ const DataGridBySchemaEditable = (0, react_1.forwardRef)((_a, ref) => {
443
441
  newRow[indexField] = newRow[indexField].label;
444
442
  }
445
443
  }
446
- console.log({ modelParent, modelParentId, apiContext });
447
444
  if (modelParent && modelParentId && apiContext) {
448
445
  const response = yield apiContext.onEditRelatedModelSave({
449
446
  model: modelParent,
@@ -1,4 +1,4 @@
1
- import React from 'react';
1
+ /// <reference types="react" />
2
2
  import { SumRowsType } from './DataTotals';
3
3
  import { GridEnrichedBySchemaColDef } from '../utils';
4
4
  interface GenericModelListProps {
@@ -13,7 +13,8 @@ interface GenericModelListProps {
13
13
  isAutoHeight?: boolean;
14
14
  model: string;
15
15
  forceReload: boolean;
16
- LinkComponent?: React.ReactNode;
16
+ LinkComponent?: JSX.Element | null;
17
+ hasHeader: boolean;
17
18
  }
18
- declare const GenericModelList: ({ columnFields, hiddenFields, minWidthFields, indexFieldBasePath, indexField, customColumnOperations, customLinkDestination, sumRows, isAutoHeight, model, forceReload, LinkComponent }: GenericModelListProps) => JSX.Element;
19
+ declare const GenericModelList: ({ columnFields, hiddenFields, minWidthFields, indexFieldBasePath, indexField, customColumnOperations, customLinkDestination, sumRows, isAutoHeight, model, forceReload, LinkComponent, hasHeader, }: GenericModelListProps) => JSX.Element;
19
20
  export default GenericModelList;
@@ -37,13 +37,16 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
37
37
  Object.defineProperty(exports, "__esModule", { value: true });
38
38
  const react_1 = __importStar(require("react"));
39
39
  const Box_1 = __importDefault(require("@mui/material/Box"));
40
+ const Typography_1 = __importDefault(require("@mui/material/Typography"));
41
+ const Button_1 = __importDefault(require("@mui/material/Button"));
42
+ const AddCircleOutline_1 = __importDefault(require("@mui/icons-material/AddCircleOutline"));
40
43
  const DataGridBySchemaEditable_1 = __importDefault(require("./DataGridBySchemaEditable"));
41
44
  const DataTotals_1 = __importDefault(require("./DataTotals"));
42
45
  const styles_1 = require("../styles");
43
46
  const api_1 = require("../api");
44
47
  const DRFReactBySchemaContext_1 = require("../context/DRFReactBySchemaContext");
45
48
  const APIWrapperContext_1 = require("../context/APIWrapperContext");
46
- const GenericModelList = ({ columnFields, hiddenFields = [], minWidthFields, indexFieldBasePath, indexField, customColumnOperations, customLinkDestination, sumRows, isAutoHeight = true, model, forceReload = false, LinkComponent = null }) => {
49
+ const GenericModelList = ({ columnFields, hiddenFields = [], minWidthFields, indexFieldBasePath, indexField, customColumnOperations, customLinkDestination, sumRows, isAutoHeight = true, model, forceReload = false, LinkComponent = null, hasHeader = false, }) => {
47
50
  const context = react_1.default.useContext(DRFReactBySchemaContext_1.DRFReactBySchemaContext);
48
51
  const apiContext = react_1.default.useContext(APIWrapperContext_1.APIWrapperContext);
49
52
  if (!context.serverEndPoint || !apiContext) {
@@ -82,7 +85,7 @@ const GenericModelList = ({ columnFields, hiddenFields = [], minWidthFields, ind
82
85
  handleLoading(false);
83
86
  if (isInBatches && loadedData.data.length === firstBatchLength) {
84
87
  setHideFooterPagination(true);
85
- (0, api_1.getGenericModelList)(Object.assign(Object.assign({}, loadParams), { loadedSchema: loadedData.schema })).then(lastBatchData => {
88
+ (0, api_1.getGenericModelList)(Object.assign(Object.assign({}, loadParams), { loadedSchema: loadedData.schema, loadedModelOptions: loadedData.modelOptions })).then(lastBatchData => {
86
89
  if (lastBatchData && typeof lastBatchData !== 'boolean') {
87
90
  setData(Object.assign(Object.assign({}, loadedData), { data: [
88
91
  ...loadedData.data,
@@ -101,9 +104,16 @@ const GenericModelList = ({ columnFields, hiddenFields = [], minWidthFields, ind
101
104
  }
102
105
  (0, react_1.useEffect)(() => {
103
106
  loadObjectList();
104
- }, []);
107
+ }, [model]);
105
108
  return (react_1.default.createElement(react_1.default.Fragment, null, typeof data !== 'boolean' && data.columns &&
106
109
  react_1.default.createElement(react_1.default.Fragment, null,
110
+ hasHeader &&
111
+ react_1.default.createElement(Box_1.default, { sx: Object.assign(Object.assign({}, styles_1.Layout.flexRowGrow), { mb: 2 }) },
112
+ react_1.default.createElement(Typography_1.default, { variant: "h5" }, data.modelOptions.verbose_name_plural || data.modelOptions.name),
113
+ LinkComponent &&
114
+ react_1.default.createElement(Box_1.default, { sx: styles_1.Layout.flexRow },
115
+ react_1.default.createElement(LinkComponent, { to: `novo` },
116
+ react_1.default.createElement(Button_1.default, { variant: "contained", size: "medium", sx: { alignSelf: 'stretch' }, startIcon: react_1.default.createElement(AddCircleOutline_1.default, null) }, "Adicionar")))),
107
117
  react_1.default.createElement(Box_1.default, { sx: styles_1.Layout.dataGridWithTabs },
108
118
  react_1.default.createElement(DataGridBySchemaEditable_1.default, { data: data.data, columns: data.columns, schema: data.schema || {}, model: model, indexField: indexField, indexFieldBasePath: indexFieldBasePath, isEditable: false, isAutoHeight: isAutoHeight, hideFooterPagination: hideFooterPagination, customColumnOperations: finalCustomColumnOperations, customLinkDestination: customLinkDestination, setVisibleRows: setVisibleRows, onDataChange: newData => {
109
119
  setData(Object.assign(Object.assign({}, data), { data: newData }));
@@ -108,7 +108,7 @@ function APIWrapper({ setLoading, handleLoading, setSnackBar, setDialog, childre
108
108
  const values = populateInitialValues(Object.assign({ model, id: objId, extraValidators }, object));
109
109
  return values;
110
110
  });
111
- const onSubmit = (model, id, data, event) => __awaiter(this, void 0, void 0, function* () {
111
+ const onSubmit = (model, id, data) => __awaiter(this, void 0, void 0, function* () {
112
112
  setLoading(true);
113
113
  const response = yield (0, api_1.updateDataBySchema)({
114
114
  model,
@@ -153,6 +153,7 @@ function APIWrapper({ setLoading, handleLoading, setSnackBar, setDialog, childre
153
153
  model,
154
154
  id,
155
155
  schema: object.schema,
156
+ modelOptions: object.modelOptions,
156
157
  initialValues: values,
157
158
  validationSchema: yupSchema
158
159
  });
@@ -335,7 +336,11 @@ function APIWrapper({ setLoading, handleLoading, setSnackBar, setDialog, childre
335
336
  onEditRelatedModelSave,
336
337
  onDeleteRelatedModel,
337
338
  onTriggerSnackBar,
338
- setDialog
339
+ setDialog,
340
+ // Remove after integrating new "onEditModel" to package:
341
+ serverEndPoint,
342
+ editModel,
343
+ populateOptionsAC,
339
344
  } }, children));
340
345
  }
341
346
  exports.default = react_1.default.memo(APIWrapper);
@@ -1,6 +1,7 @@
1
1
  import React from 'react';
2
2
  import { FieldValues, SubmitHandler } from 'react-hook-form';
3
3
  import { ItemSchemaColumnsType, Id, Item, SchemaType } from '../utils';
4
+ import { serverEndPointType } from './DRFReactBySchemaContext';
4
5
  export interface LoadSinglePageDataProps {
5
6
  model: string;
6
7
  objId?: Id;
@@ -71,5 +72,8 @@ export interface APIWrapperContextType {
71
72
  onDeleteRelatedModel: (p: OnDeleteRelatedModelType) => Promise<boolean>;
72
73
  onTriggerSnackBar: (p: SnackBarType) => void;
73
74
  setDialog: React.Dispatch<React.SetStateAction<DialogType>>;
75
+ serverEndPoint: serverEndPointType;
76
+ editModel: React.MutableRefObject<Item>;
77
+ populateOptionsAC: (optionsACModels: string[]) => void;
74
78
  }
75
79
  export declare const APIWrapperContext: React.Context<APIWrapperContextType | null>;
package/dist/utils.d.ts CHANGED
@@ -2,6 +2,7 @@ import { GridActionsColDef, GridColDef } from '@mui/x-data-grid';
2
2
  export type Id = string | number;
3
3
  export type Item = Record<string, any>;
4
4
  export type SchemaType = Record<string, Field>;
5
+ export type modelOptionsType = Record<string, string>;
5
6
  export interface Choice {
6
7
  value: string | number;
7
8
  display_name: string;
@@ -34,11 +35,13 @@ export type GridEnrichedBySchemaColDef = GridBySchemaColDef | GridActionsBySchem
34
35
  export interface DataSchemaColumnsType {
35
36
  data: Item[];
36
37
  schema: SchemaType;
38
+ modelOptions: modelOptionsType;
37
39
  columns?: GridEnrichedBySchemaColDef[];
38
40
  }
39
41
  export interface ItemSchemaColumnsType {
40
42
  data: Item;
41
43
  schema: SchemaType;
44
+ modelOptions: modelOptionsType;
42
45
  columns?: GridEnrichedBySchemaColDef[];
43
46
  }
44
47
  export declare const emptyByType: any;
package/dist/utils.js CHANGED
@@ -236,7 +236,7 @@ const isTmpId = (id) => {
236
236
  if (!id) {
237
237
  return true;
238
238
  }
239
- return id.toString().substr(0, 3) === 'tmp';
239
+ return id.toString().slice(0, 3) === 'tmp';
240
240
  };
241
241
  exports.isTmpId = isTmpId;
242
242
  const reducer = (state, newState) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drf-react-by-schema",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
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",