drf-react-by-schema 0.2.2 → 0.3.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/index.js +14 -27
- package/package.json +5 -1
- package/src/api.ts +21 -27
- package/src/components/DataGridBySchemaEditable/SelectEditInputCell.tsx +12 -11
- package/src/components/DataGridBySchemaEditable.md +2 -2
- package/src/components/DataGridBySchemaEditable.tsx +38 -27
- package/src/components/DataTotals.tsx +4 -3
- package/src/components/DialogActions.tsx +33 -0
- package/src/components/DialogJSONSchemaForm.tsx +40 -0
- package/src/components/GenericModelList.tsx +24 -14
- package/src/components/GenericRelatedModelList.tsx +168 -0
- package/src/context/APIWrapper.tsx +447 -0
- package/src/context/APIWrapperContext.tsx +84 -0
- package/src/context/DRFReactBySchemaContext.tsx +33 -0
- package/src/context/DRFReactBySchemaProvider.md +30 -23
- package/src/context/DRFReactBySchemaProvider.tsx +27 -33
- package/src/context/Overlays.tsx +94 -0
- package/src/index.ts +15 -2
- package/src/styles/theme.ts +2 -2
- package/src/utils.ts +11 -5
package/dist/index.js
CHANGED
|
@@ -1,32 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
4
|
};
|
|
28
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.getPatternFormat = exports.isTmpId = exports.getTmpId = exports.errorProps = exports.buildGenericYupValidationSchema = exports.populateValues = exports.getChoiceByValue = exports.emptyByType = exports.getGenericModel = exports.getGenericModelList = exports.isLoggedIn = exports.setAuthToken = exports.loginByPayload = exports.createOrUpdateJSONSchema = exports.getJSONSchema = exports.getAutoComplete = exports.addExistingRelatedModel = exports.updateDataBySchema = exports.createOrUpdateData = exports.deleteData = exports.createData = exports.partialUpdateData = exports.updateData = exports.DataTotals = exports.GenericModelList = exports.DataGridBySchemaEditable = exports.DRFReactBySchemaContext = exports.DRFReactBySchemaProvider = void 0;
|
|
6
|
+
exports.getPatternFormat = exports.isTmpId = exports.getTmpId = exports.errorProps = exports.buildGenericYupValidationSchema = exports.populateValues = exports.getChoiceByValue = exports.emptyByType = exports.getGenericModel = exports.getGenericModelList = exports.isLoggedIn = exports.setAuthToken = exports.loginByPayload = exports.createOrUpdateJSONSchema = exports.getJSONSchema = exports.getAutoComplete = exports.addExistingRelatedModel = exports.updateDataBySchema = exports.createOrUpdateData = exports.deleteData = exports.createData = exports.partialUpdateData = exports.updateData = exports.DialogActions = exports.DataTotals = exports.GenericRelatedModelList = exports.GenericModelList = exports.DataGridBySchemaEditable = exports.useTheme = exports.APIWrapperContext = exports.DRFReactBySchemaContext = exports.DRFReactBySchemaProvider = void 0;
|
|
7
|
+
// Methods:
|
|
30
8
|
const api_1 = require("./api");
|
|
31
9
|
Object.defineProperty(exports, "updateData", { enumerable: true, get: function () { return api_1.updateData; } });
|
|
32
10
|
Object.defineProperty(exports, "partialUpdateData", { enumerable: true, get: function () { return api_1.partialUpdateData; } });
|
|
@@ -52,13 +30,22 @@ Object.defineProperty(exports, "errorProps", { enumerable: true, get: function (
|
|
|
52
30
|
Object.defineProperty(exports, "getTmpId", { enumerable: true, get: function () { return utils_1.getTmpId; } });
|
|
53
31
|
Object.defineProperty(exports, "isTmpId", { enumerable: true, get: function () { return utils_1.isTmpId; } });
|
|
54
32
|
Object.defineProperty(exports, "getPatternFormat", { enumerable: true, get: function () { return utils_1.getPatternFormat; } });
|
|
55
|
-
//
|
|
56
|
-
const DRFReactBySchemaProvider_1 =
|
|
33
|
+
// Wrappers/Providers:
|
|
34
|
+
const DRFReactBySchemaProvider_1 = __importDefault(require("./context/DRFReactBySchemaProvider"));
|
|
57
35
|
exports.DRFReactBySchemaProvider = DRFReactBySchemaProvider_1.default;
|
|
58
|
-
|
|
36
|
+
const DRFReactBySchemaContext_1 = require("./context/DRFReactBySchemaContext");
|
|
37
|
+
Object.defineProperty(exports, "DRFReactBySchemaContext", { enumerable: true, get: function () { return DRFReactBySchemaContext_1.DRFReactBySchemaContext; } });
|
|
38
|
+
Object.defineProperty(exports, "useTheme", { enumerable: true, get: function () { return DRFReactBySchemaContext_1.useTheme; } });
|
|
39
|
+
const APIWrapperContext_1 = require("./context/APIWrapperContext");
|
|
40
|
+
Object.defineProperty(exports, "APIWrapperContext", { enumerable: true, get: function () { return APIWrapperContext_1.APIWrapperContext; } });
|
|
41
|
+
// Components
|
|
59
42
|
const DataGridBySchemaEditable_1 = __importDefault(require("./components/DataGridBySchemaEditable"));
|
|
60
43
|
exports.DataGridBySchemaEditable = DataGridBySchemaEditable_1.default;
|
|
61
44
|
const GenericModelList_1 = __importDefault(require("./components/GenericModelList"));
|
|
62
45
|
exports.GenericModelList = GenericModelList_1.default;
|
|
46
|
+
const GenericRelatedModelList_1 = __importDefault(require("./components/GenericRelatedModelList"));
|
|
47
|
+
exports.GenericRelatedModelList = GenericRelatedModelList_1.default;
|
|
63
48
|
const DataTotals_1 = __importDefault(require("./components/DataTotals"));
|
|
64
49
|
exports.DataTotals = DataTotals_1.default;
|
|
50
|
+
const DialogActions_1 = __importDefault(require("./components/DialogActions"));
|
|
51
|
+
exports.DialogActions = DialogActions_1.default;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "drf-react-by-schema",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.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",
|
|
@@ -60,6 +60,10 @@
|
|
|
60
60
|
"@mui/material": "^5.11.0",
|
|
61
61
|
"@mui/x-data-grid": "^5.17.14",
|
|
62
62
|
"@mui/x-date-pickers": "^5.0.11",
|
|
63
|
+
"@rjsf/core": "^5.0.0-beta.15",
|
|
64
|
+
"@rjsf/mui": "^5.0.0-beta.15",
|
|
65
|
+
"@rjsf/utils": "^5.0.0-beta.15",
|
|
66
|
+
"@rjsf/validator-ajv8": "^5.0.0-beta.15",
|
|
63
67
|
"@types/jest": "^29.2.4",
|
|
64
68
|
"@types/node": "^18.11.15",
|
|
65
69
|
"@types/react-dom": "^18.0.9",
|
package/src/api.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import axios, { AxiosError, AxiosResponse } from 'axios';
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import { serverEndPointType } from './context/DRFReactBySchemaProvider';
|
|
2
|
+
import moment from 'moment';
|
|
3
|
+
import { serverEndPointType } from './context/DRFReactBySchemaContext';
|
|
5
4
|
import {
|
|
6
5
|
isTmpId,
|
|
7
6
|
emptyByType,
|
|
@@ -9,11 +8,10 @@ import {
|
|
|
9
8
|
Field,
|
|
10
9
|
Item,
|
|
11
10
|
SchemaType,
|
|
12
|
-
|
|
11
|
+
DataSchemaColumnsType,
|
|
12
|
+
ItemSchemaColumnsType
|
|
13
13
|
} from './utils';
|
|
14
14
|
|
|
15
|
-
const moment = require('moment');
|
|
16
|
-
|
|
17
15
|
type Id = string | number | null;
|
|
18
16
|
|
|
19
17
|
/**
|
|
@@ -138,7 +136,6 @@ export const updateData = async ({ path, serverEndPoint, data, id }: TargetApiPa
|
|
|
138
136
|
const url = `${serverEndPoint.api}/${path}/${id}/`;
|
|
139
137
|
try {
|
|
140
138
|
await axios.put(url, data);
|
|
141
|
-
return id;
|
|
142
139
|
} catch (e) {
|
|
143
140
|
if ((<any>e).isAxiosError) {
|
|
144
141
|
const err = <AxiosError>e;
|
|
@@ -150,16 +147,17 @@ export const updateData = async ({ path, serverEndPoint, data, id }: TargetApiPa
|
|
|
150
147
|
}
|
|
151
148
|
try {
|
|
152
149
|
await axios.put(url, data);
|
|
153
|
-
return true;
|
|
154
150
|
} catch (e) {
|
|
155
151
|
console.log(`Error updating data at ${url}`, data, e);
|
|
156
152
|
return false;
|
|
157
153
|
}
|
|
154
|
+
return id;
|
|
158
155
|
}
|
|
159
156
|
console.log(`Error updating data at ${url}`, data, err.response?.data);
|
|
160
157
|
return ({ errors: err.response?.data } as unknown) as AxiosResponse;
|
|
161
158
|
}
|
|
162
159
|
}
|
|
160
|
+
return id;
|
|
163
161
|
};
|
|
164
162
|
|
|
165
163
|
export const partialUpdateData = async ({ path, serverEndPoint, data, id }: TargetApiParams) => {
|
|
@@ -170,7 +168,6 @@ export const partialUpdateData = async ({ path, serverEndPoint, data, id }: Targ
|
|
|
170
168
|
try {
|
|
171
169
|
await axios.patch(url, data);
|
|
172
170
|
// DEBUG console.log({ path, data, id });
|
|
173
|
-
return id;
|
|
174
171
|
} catch (e) {
|
|
175
172
|
if ((<any>e).isAxiosError) {
|
|
176
173
|
const err = <AxiosError>e;
|
|
@@ -178,20 +175,21 @@ export const partialUpdateData = async ({ path, serverEndPoint, data, id }: Targ
|
|
|
178
175
|
const isRefreshed = await refreshToken(serverEndPoint);
|
|
179
176
|
if (!isRefreshed) {
|
|
180
177
|
console.log('Token expirou! Deve-se fazer login de novo');
|
|
181
|
-
return
|
|
178
|
+
return ({ errors: err.response?.data } as unknown) as AxiosResponse;
|
|
182
179
|
}
|
|
183
180
|
try {
|
|
184
181
|
await axios.patch(url, data);
|
|
185
|
-
return true;
|
|
186
182
|
} catch (e) {
|
|
187
183
|
console.log(`Error partial updating data at ${url}`, data, e);
|
|
188
|
-
return
|
|
184
|
+
return ({ errors: err.response?.data } as unknown) as AxiosResponse;
|
|
189
185
|
}
|
|
186
|
+
return id;
|
|
190
187
|
}
|
|
191
188
|
console.log(`Error partial updating data at ${url}`, data, err.response?.data);
|
|
192
189
|
return ({ errors: err.response?.data } as unknown) as AxiosResponse;
|
|
193
190
|
}
|
|
194
191
|
}
|
|
192
|
+
return id;
|
|
195
193
|
};
|
|
196
194
|
|
|
197
195
|
export const createData = async ({ path, serverEndPoint, data }: Omit<TargetApiParams, 'id'>) => {
|
|
@@ -199,9 +197,9 @@ export const createData = async ({ path, serverEndPoint, data }: Omit<TargetApiP
|
|
|
199
197
|
return ({ errors: 'Não há definição de API (serverEndPoint!' } as unknown) as AxiosResponse;
|
|
200
198
|
}
|
|
201
199
|
const url = `${serverEndPoint.api}/${path}/`;
|
|
200
|
+
let ret = <AxiosResponse>{};
|
|
202
201
|
try {
|
|
203
|
-
|
|
204
|
-
return ret;
|
|
202
|
+
ret = await axios.post(url, data);
|
|
205
203
|
} catch (e) {
|
|
206
204
|
if ((<any>e).isAxiosError) {
|
|
207
205
|
const err = <AxiosError>e;
|
|
@@ -212,17 +210,18 @@ export const createData = async ({ path, serverEndPoint, data }: Omit<TargetApiP
|
|
|
212
210
|
return false;
|
|
213
211
|
}
|
|
214
212
|
try {
|
|
215
|
-
|
|
216
|
-
return ret;
|
|
213
|
+
ret = await axios.post(url, data);
|
|
217
214
|
} catch (e) {
|
|
218
215
|
console.log(`Error creating data at ${url}`, data, e);
|
|
219
216
|
return false;
|
|
220
217
|
}
|
|
218
|
+
return ret;
|
|
221
219
|
}
|
|
222
220
|
console.log(`Error creating data at ${url}`, data, err.response?.data);
|
|
223
221
|
return ({ errors: err.response?.data } as unknown) as AxiosResponse;
|
|
224
222
|
}
|
|
225
223
|
}
|
|
224
|
+
return ret;
|
|
226
225
|
};
|
|
227
226
|
|
|
228
227
|
export const deleteData = async (path:string, serverEndPoint:serverEndPointType | null, id:Id) => {
|
|
@@ -233,7 +232,6 @@ export const deleteData = async (path:string, serverEndPoint:serverEndPointType
|
|
|
233
232
|
const url = `${serverEndPoint.api}/${path}/${id}`;
|
|
234
233
|
try {
|
|
235
234
|
await axios.delete(url);
|
|
236
|
-
return true;
|
|
237
235
|
} catch (e) {
|
|
238
236
|
if ((<any>e).isAxiosError) {
|
|
239
237
|
const err = <AxiosError>e;
|
|
@@ -245,16 +243,17 @@ export const deleteData = async (path:string, serverEndPoint:serverEndPointType
|
|
|
245
243
|
}
|
|
246
244
|
try {
|
|
247
245
|
await axios.delete(url);
|
|
248
|
-
return true;
|
|
249
246
|
} catch (e) {
|
|
250
247
|
console.log(`Error deleting data from ${url}`, e);
|
|
251
248
|
return false;
|
|
252
249
|
}
|
|
250
|
+
return true;
|
|
253
251
|
}
|
|
254
252
|
console.log(`Error deleting data from ${url}`, e);
|
|
255
253
|
return false;
|
|
256
254
|
}
|
|
257
255
|
}
|
|
256
|
+
return true;
|
|
258
257
|
};
|
|
259
258
|
|
|
260
259
|
export const createOrUpdateData = async ({ path, serverEndPoint, data, id }: TargetApiParamsOptionalId) => {
|
|
@@ -362,7 +361,7 @@ export const updateDataBySchema = async ({
|
|
|
362
361
|
serverEndPoint:serverEndPointType | null,
|
|
363
362
|
data:Item,
|
|
364
363
|
schema:SchemaType,
|
|
365
|
-
path
|
|
364
|
+
path?:string | null
|
|
366
365
|
}) => {
|
|
367
366
|
// console.log({
|
|
368
367
|
// model,
|
|
@@ -616,11 +615,6 @@ export interface getGenericModelListProps {
|
|
|
616
615
|
loadedSchema?:SchemaType | boolean;
|
|
617
616
|
};
|
|
618
617
|
|
|
619
|
-
export interface DataSchemaColumnsType {
|
|
620
|
-
data: Item[];
|
|
621
|
-
schema?: SchemaType;
|
|
622
|
-
columns?: GridEnrichedBySchemaColDef[];
|
|
623
|
-
}
|
|
624
618
|
export const getGenericModelList = async ({
|
|
625
619
|
model,
|
|
626
620
|
serverEndPoint,
|
|
@@ -696,10 +690,10 @@ export const getGenericModelList = async ({
|
|
|
696
690
|
}
|
|
697
691
|
if (loadedSchema) {
|
|
698
692
|
// DEBUG console.log({ path, data });
|
|
699
|
-
return { data }
|
|
693
|
+
return <DataSchemaColumnsType>{ data };
|
|
700
694
|
}
|
|
701
695
|
// DEBUG console.log({ path, data, columns, schema });
|
|
702
|
-
return { data, columns, schema }
|
|
696
|
+
return <DataSchemaColumnsType>{ data, columns, schema };
|
|
703
697
|
};
|
|
704
698
|
|
|
705
699
|
export const getGenericModel = async ({
|
|
@@ -729,5 +723,5 @@ export const getGenericModel = async ({
|
|
|
729
723
|
? {}
|
|
730
724
|
: await getData(path, serverEndPoint);
|
|
731
725
|
// console.log({ schema, data }); // DEBUG
|
|
732
|
-
return { schema, data };
|
|
726
|
+
return <ItemSchemaColumnsType>{ schema, data };
|
|
733
727
|
};
|
|
@@ -6,20 +6,21 @@ import Autocomplete, { createFilterOptions } from '@mui/material/Autocomplete';
|
|
|
6
6
|
|
|
7
7
|
import { getTmpId } from '../../utils';
|
|
8
8
|
import { GridEnrichedBySchemaColDef, Item } from '../../utils';
|
|
9
|
+
import { OptionsACType } from '../../context/APIWrapperContext';
|
|
9
10
|
|
|
10
11
|
const filter = createFilterOptions();
|
|
11
12
|
|
|
12
|
-
|
|
13
|
-
field: string
|
|
14
|
-
id: number | string
|
|
15
|
-
value?: any
|
|
16
|
-
column: GridEnrichedBySchemaColDef
|
|
17
|
-
type: string
|
|
18
|
-
optionsAC:
|
|
19
|
-
isIndexField: boolean
|
|
20
|
-
multiple?: boolean
|
|
21
|
-
sx?: SxProps
|
|
22
|
-
}
|
|
13
|
+
interface SelectEditInputCellProps {
|
|
14
|
+
field: string;
|
|
15
|
+
id: number | string;
|
|
16
|
+
value?: any;
|
|
17
|
+
column: GridEnrichedBySchemaColDef;
|
|
18
|
+
type: string;
|
|
19
|
+
optionsAC: React.MutableRefObject<OptionsACType | null>;
|
|
20
|
+
isIndexField: boolean;
|
|
21
|
+
multiple?: boolean;
|
|
22
|
+
sx?: SxProps;
|
|
23
|
+
}
|
|
23
24
|
|
|
24
25
|
/**
|
|
25
26
|
*
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
DataGridBySchemaEditable example:
|
|
2
|
-
|
|
3
1
|
```js
|
|
2
|
+
/*
|
|
4
3
|
import DRFReactBySchemaProvider from '../context/DRFReactBySchemaProvider';
|
|
5
4
|
const data = {
|
|
6
5
|
data: [],
|
|
@@ -47,4 +46,5 @@ const Link = null;
|
|
|
47
46
|
LinkComponent={Link}
|
|
48
47
|
/>
|
|
49
48
|
</DRFReactBySchemaProvider>
|
|
49
|
+
*/
|
|
50
50
|
```
|
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import React, { useEffect, useState, useRef, forwardRef } from 'react';
|
|
2
|
-
|
|
3
|
-
// import Mask from 'string-mask';
|
|
2
|
+
import moment from 'moment';
|
|
4
3
|
import {
|
|
5
4
|
DataGrid,
|
|
6
5
|
GridEditInputCell,
|
|
7
6
|
GridRowModes,
|
|
8
7
|
GridActionsCellItem,
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
gridStringOrNumberComparator,
|
|
9
|
+
GridRowId
|
|
11
10
|
} from '@mui/x-data-grid';
|
|
12
11
|
import * as Yup from 'yup';
|
|
13
12
|
import Box from '@mui/material/Box';
|
|
@@ -29,11 +28,11 @@ import {
|
|
|
29
28
|
GridEnrichedBySchemaColDef,
|
|
30
29
|
isTmpId,
|
|
31
30
|
emptyByType,
|
|
32
|
-
buildGenericYupValidationSchema
|
|
31
|
+
buildGenericYupValidationSchema,
|
|
32
|
+
ItemSchemaColumnsType
|
|
33
33
|
} from '../utils';
|
|
34
34
|
import { SxProps } from '@mui/material';
|
|
35
|
-
import {
|
|
36
|
-
import { DRFReactBySchemaContext, DRFReactBySchemaContextType } from '../context/DRFReactBySchemaProvider';
|
|
35
|
+
import { DRFReactBySchemaContext, DRFReactBySchemaContextType } from '../context/DRFReactBySchemaContext';
|
|
37
36
|
import { quantityOnlyOperators } from './DataGridBySchemaEditable/utils';
|
|
38
37
|
import { CustomToolbar } from './DataGridBySchemaEditable/CustomToolbar';
|
|
39
38
|
import { SelectEditInputCell } from './DataGridBySchemaEditable/SelectEditInputCell';
|
|
@@ -41,9 +40,9 @@ import { GridDecimalInput } from './DataGridBySchemaEditable/GridDecimalInput';
|
|
|
41
40
|
import { GridPatternInput } from './DataGridBySchemaEditable/GridPatternInput';
|
|
42
41
|
import { FooterToolbar } from './DataGridBySchemaEditable/FooterToolbar';
|
|
43
42
|
import { ConfirmDialog } from './DataGridBySchemaEditable/ConfirmDialog';
|
|
43
|
+
import { APIWrapperContext } from '../context/APIWrapperContext';
|
|
44
44
|
|
|
45
45
|
const stringMask = require('string-mask');
|
|
46
|
-
const moment = require('moment');
|
|
47
46
|
|
|
48
47
|
interface DataGridBySchemaEditableProps {
|
|
49
48
|
schema: SchemaType;
|
|
@@ -60,8 +59,10 @@ interface DataGridBySchemaEditableProps {
|
|
|
60
59
|
indexFieldBasePath?: string;
|
|
61
60
|
stateToLink?: object;
|
|
62
61
|
minWidth?: number;
|
|
63
|
-
|
|
64
|
-
|
|
62
|
+
|
|
63
|
+
modelParentId?: Id;
|
|
64
|
+
modelParent?: string;
|
|
65
|
+
|
|
65
66
|
customColumnOperations?: (p: any) => GridEnrichedBySchemaColDef;
|
|
66
67
|
customLinkDestination?: (p: any) => string;
|
|
67
68
|
LinkComponent?: any;
|
|
@@ -91,8 +92,8 @@ const DataGridBySchemaEditable = forwardRef((
|
|
|
91
92
|
indexFieldBasePath = '',
|
|
92
93
|
stateToLink = {},
|
|
93
94
|
minWidth = 80,
|
|
94
|
-
|
|
95
|
-
|
|
95
|
+
modelParent,
|
|
96
|
+
modelParentId,
|
|
96
97
|
customColumnOperations,
|
|
97
98
|
customLinkDestination,
|
|
98
99
|
LinkComponent,
|
|
@@ -111,7 +112,10 @@ const DataGridBySchemaEditable = forwardRef((
|
|
|
111
112
|
const { serverEndPoint } = DRFReactBySchemaContext
|
|
112
113
|
? React.useContext(DRFReactBySchemaContext) as DRFReactBySchemaContextType
|
|
113
114
|
: { serverEndPoint: null };
|
|
114
|
-
|
|
115
|
+
|
|
116
|
+
const apiContext = React.useContext(APIWrapperContext);
|
|
117
|
+
|
|
118
|
+
const initialSnackBar:Item = {
|
|
115
119
|
open: false,
|
|
116
120
|
msg: '',
|
|
117
121
|
severity: 'info'
|
|
@@ -412,7 +416,7 @@ const DataGridBySchemaEditable = forwardRef((
|
|
|
412
416
|
if (!params.value || typeof params.value !== 'string') {
|
|
413
417
|
return params.value;
|
|
414
418
|
}
|
|
415
|
-
const formattedValue = new stringMask
|
|
419
|
+
const formattedValue = new stringMask(column.patternFormat, {}).apply(params.value);
|
|
416
420
|
return formattedValue;
|
|
417
421
|
};
|
|
418
422
|
if (isEditable) {
|
|
@@ -455,13 +459,19 @@ const DataGridBySchemaEditable = forwardRef((
|
|
|
455
459
|
};
|
|
456
460
|
|
|
457
461
|
const handleDeleteSave = () => {
|
|
462
|
+
if (idToRemove.current === null) {
|
|
463
|
+
setDialogOpen(false);
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
458
466
|
const newData = dataGrid.data.filter(row => row.id !== idToRemove.current);
|
|
459
467
|
setDataGrid({
|
|
460
468
|
...dataGrid,
|
|
461
469
|
data: newData
|
|
462
470
|
});
|
|
463
|
-
if (
|
|
464
|
-
onDeleteRelatedModel({
|
|
471
|
+
if (modelParent && modelParentId && apiContext) {
|
|
472
|
+
apiContext.onDeleteRelatedModel({
|
|
473
|
+
model: modelParent,
|
|
474
|
+
id: modelParentId,
|
|
465
475
|
relatedModel: model,
|
|
466
476
|
relatedModelId: idToRemove.current
|
|
467
477
|
});
|
|
@@ -574,8 +584,10 @@ const DataGridBySchemaEditable = forwardRef((
|
|
|
574
584
|
}
|
|
575
585
|
}
|
|
576
586
|
|
|
577
|
-
if (
|
|
578
|
-
const response = await onEditRelatedModelSave({
|
|
587
|
+
if (modelParent && modelParentId && apiContext) {
|
|
588
|
+
const response = await apiContext.onEditRelatedModelSave({
|
|
589
|
+
model: modelParent,
|
|
590
|
+
id: modelParentId,
|
|
579
591
|
relatedModel: model,
|
|
580
592
|
relatedModelId: newRow.id,
|
|
581
593
|
newRow,
|
|
@@ -583,13 +595,10 @@ const DataGridBySchemaEditable = forwardRef((
|
|
|
583
595
|
onlyAddExisting
|
|
584
596
|
});
|
|
585
597
|
|
|
586
|
-
const
|
|
587
|
-
const responseAsData = response as { data: Item };
|
|
588
|
-
const responseAsErrors = response as { errors: Item }
|
|
589
|
-
|
|
598
|
+
const responseAsData = response as ItemSchemaColumnsType;
|
|
590
599
|
if (
|
|
591
|
-
(onlyAddExisting &&
|
|
592
|
-
(parseInt(
|
|
600
|
+
(onlyAddExisting && typeof response != 'boolean') ||
|
|
601
|
+
(parseInt(response as string) && (createNewItem || !onlyAddExisting))
|
|
593
602
|
) {
|
|
594
603
|
updateOptionsAC();
|
|
595
604
|
const data = [...dataGrid.data];
|
|
@@ -597,7 +606,7 @@ const DataGridBySchemaEditable = forwardRef((
|
|
|
597
606
|
if (data[i].id === newRow.id) {
|
|
598
607
|
data[i] = onlyAddExisting ? responseAsData.data : newRow;
|
|
599
608
|
if (isTmpId(newRow.id)) {
|
|
600
|
-
const newId = parseInt(
|
|
609
|
+
const newId = parseInt(response as string);
|
|
601
610
|
data[i].id = newId || responseAsData.data.id;
|
|
602
611
|
}
|
|
603
612
|
|
|
@@ -643,8 +652,10 @@ const DataGridBySchemaEditable = forwardRef((
|
|
|
643
652
|
columns={preparedColumns}
|
|
644
653
|
onStateChange={(state) => {
|
|
645
654
|
if (setVisibleRows) {
|
|
646
|
-
const
|
|
647
|
-
|
|
655
|
+
const newVisibleRows = Object.entries(state.filter.visibleRowsLookup)
|
|
656
|
+
.filter(entry => { return entry[1] === true; })
|
|
657
|
+
.map((entry:Item) => { return entry[0]; }) as GridRowId[];
|
|
658
|
+
setVisibleRows(newVisibleRows);
|
|
648
659
|
}
|
|
649
660
|
}}
|
|
650
661
|
editMode="row"
|
|
@@ -3,8 +3,9 @@ import Alert, { AlertColor } from '@mui/material/Alert';
|
|
|
3
3
|
import List from '@mui/material/List';
|
|
4
4
|
import ListItem from '@mui/material/ListItem';
|
|
5
5
|
import { NumericFormat } from 'react-number-format';
|
|
6
|
+
import { GridRowId } from '@mui/x-data-grid';
|
|
7
|
+
|
|
6
8
|
import { Item } from '../utils';
|
|
7
|
-
import { DRFReactBySchemaContext, DRFReactBySchemaContextType } from '../context/DRFReactBySchemaProvider';
|
|
8
9
|
|
|
9
10
|
export interface SumRowsType {
|
|
10
11
|
rows: Item[],
|
|
@@ -14,7 +15,7 @@ export interface SumRowsType {
|
|
|
14
15
|
interface DataTotalsProps {
|
|
15
16
|
data?: Item[];
|
|
16
17
|
sumRows?: SumRowsType;
|
|
17
|
-
visibleRows
|
|
18
|
+
visibleRows: GridRowId[];
|
|
18
19
|
};
|
|
19
20
|
|
|
20
21
|
const DataTotals = ({
|
|
@@ -31,7 +32,7 @@ const DataTotals = ({
|
|
|
31
32
|
<ListItem key={`sumRows_${row.field}`}>
|
|
32
33
|
<NumericFormat
|
|
33
34
|
value={data.reduce((total, item) => {
|
|
34
|
-
if (parseFloat(item[row.field]) && visibleRows
|
|
35
|
+
if (parseFloat(item[row.field]) && visibleRows.includes(`${item.id}`)) {
|
|
35
36
|
return total + parseFloat(item[row.field]);
|
|
36
37
|
}
|
|
37
38
|
return total;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import Button from '@mui/material/Button';
|
|
3
|
+
import { DialogType } from '../context/APIWrapperContext';
|
|
4
|
+
|
|
5
|
+
interface DialogActionsProps {
|
|
6
|
+
setDialog: React.Dispatch<React.SetStateAction<DialogType>>;
|
|
7
|
+
handleSave: (p: any) => Promise<boolean | undefined>;
|
|
8
|
+
btnCancel?: string;
|
|
9
|
+
btnConfirm?: string;
|
|
10
|
+
};
|
|
11
|
+
export default function DialogActions ({
|
|
12
|
+
setDialog,
|
|
13
|
+
handleSave,
|
|
14
|
+
btnCancel = 'Cancelar',
|
|
15
|
+
btnConfirm = 'Salvar'
|
|
16
|
+
}: DialogActionsProps) {
|
|
17
|
+
return (
|
|
18
|
+
<>
|
|
19
|
+
<Button
|
|
20
|
+
onClick={() => {
|
|
21
|
+
setDialog({ open: false });
|
|
22
|
+
}}
|
|
23
|
+
>
|
|
24
|
+
{btnCancel}
|
|
25
|
+
</Button>
|
|
26
|
+
<Button
|
|
27
|
+
onClick={handleSave}
|
|
28
|
+
>
|
|
29
|
+
{btnConfirm}
|
|
30
|
+
</Button>
|
|
31
|
+
</>
|
|
32
|
+
);
|
|
33
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import Button from '@mui/material/Button';
|
|
3
|
+
import validator from '@rjsf/validator-ajv8';
|
|
4
|
+
import Form from '@rjsf/mui';
|
|
5
|
+
import { Item, SchemaType } from '../utils';
|
|
6
|
+
|
|
7
|
+
interface DialogJSONSchemaFormProps {
|
|
8
|
+
jsonSchemaFormRef: any;
|
|
9
|
+
schema: SchemaType;
|
|
10
|
+
uiSchema: SchemaType;
|
|
11
|
+
formData: Item;
|
|
12
|
+
onSubmit: ({ formData }:{ formData: Item }) => void;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export default function DialogJSONSchemaForm ({
|
|
16
|
+
jsonSchemaFormRef,
|
|
17
|
+
schema,
|
|
18
|
+
uiSchema,
|
|
19
|
+
formData,
|
|
20
|
+
onSubmit
|
|
21
|
+
}:DialogJSONSchemaFormProps) {
|
|
22
|
+
return (
|
|
23
|
+
<Form
|
|
24
|
+
// @ts-ignore
|
|
25
|
+
ref={jsonSchemaFormRef}
|
|
26
|
+
schema={schema}
|
|
27
|
+
uiSchema={uiSchema}
|
|
28
|
+
validator={validator}
|
|
29
|
+
formData={formData}
|
|
30
|
+
onSubmit={onSubmit}
|
|
31
|
+
>
|
|
32
|
+
<Button
|
|
33
|
+
type="submit"
|
|
34
|
+
sx={{ display: 'none' }}
|
|
35
|
+
>
|
|
36
|
+
Salvar
|
|
37
|
+
</Button>
|
|
38
|
+
</Form>
|
|
39
|
+
);
|
|
40
|
+
};
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import React, { useState, useEffect } from 'react';
|
|
2
2
|
import Box from '@mui/material/Box';
|
|
3
|
+
import { GridRowId } from '@mui/x-data-grid';
|
|
3
4
|
|
|
4
5
|
import DataGridBySchemaEditable from './DataGridBySchemaEditable';
|
|
5
6
|
import DataTotals, { SumRowsType } from './DataTotals';
|
|
6
7
|
import { Layout } from '../styles';
|
|
7
|
-
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
8
|
+
import { GridEnrichedBySchemaColDef, Item, SchemaType, DataSchemaColumnsType } from '../utils';
|
|
9
|
+
import { getGenericModelList, getGenericModelListProps } from '../api';
|
|
10
|
+
import { DRFReactBySchemaContext } from '../context/DRFReactBySchemaContext';
|
|
11
|
+
import { APIWrapperContext } from '../context/APIWrapperContext';
|
|
11
12
|
|
|
12
13
|
interface GenericModelListProps {
|
|
13
14
|
columnFields: string[];
|
|
@@ -20,9 +21,8 @@ interface GenericModelListProps {
|
|
|
20
21
|
sumRows?: SumRowsType;
|
|
21
22
|
isAutoHeight?: boolean;
|
|
22
23
|
model: string;
|
|
23
|
-
handleLoading: (p: any) => void;
|
|
24
24
|
forceReload: boolean;
|
|
25
|
-
LinkComponent
|
|
25
|
+
LinkComponent?: React.ReactNode;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
const GenericModelList = ({
|
|
@@ -36,24 +36,30 @@ const GenericModelList = ({
|
|
|
36
36
|
sumRows,
|
|
37
37
|
isAutoHeight = true,
|
|
38
38
|
model,
|
|
39
|
-
handleLoading,
|
|
40
39
|
forceReload = false,
|
|
41
40
|
LinkComponent = null
|
|
42
41
|
}: GenericModelListProps) => {
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
if (!serverEndPoint) {
|
|
42
|
+
const context = React.useContext(DRFReactBySchemaContext);
|
|
43
|
+
const apiContext = React.useContext(APIWrapperContext);
|
|
44
|
+
if (!context.serverEndPoint || !apiContext) {
|
|
47
45
|
console.error('Error: There is no endpoint defined in DRFReactBySchemaProvider!');
|
|
48
46
|
return (<></>);
|
|
49
47
|
}
|
|
48
|
+
const {
|
|
49
|
+
serverEndPoint,
|
|
50
|
+
isInBatches,
|
|
51
|
+
firstBatchLength
|
|
52
|
+
} = context;
|
|
53
|
+
const {
|
|
54
|
+
handleLoading
|
|
55
|
+
} = apiContext;
|
|
50
56
|
interface dataProps {
|
|
51
57
|
data: Item[];
|
|
52
58
|
schema: SchemaType;
|
|
53
59
|
columns: GridEnrichedBySchemaColDef[]
|
|
54
60
|
};
|
|
55
61
|
const [data, setData] = useState<DataSchemaColumnsType | boolean>(false);
|
|
56
|
-
const [visibleRows, setVisibleRows] = useState<
|
|
62
|
+
const [visibleRows, setVisibleRows] = useState<GridRowId[]>([]);
|
|
57
63
|
const [hideFooterPagination, setHideFooterPagination] = useState(false);
|
|
58
64
|
|
|
59
65
|
const finalCustomColumnOperations = (column: GridEnrichedBySchemaColDef) => {
|
|
@@ -116,7 +122,7 @@ const GenericModelList = ({
|
|
|
116
122
|
|
|
117
123
|
return (
|
|
118
124
|
<>
|
|
119
|
-
{
|
|
125
|
+
{typeof data !== 'boolean' && data.columns &&
|
|
120
126
|
<>
|
|
121
127
|
<Box sx={Layout.dataGridWithTabs}>
|
|
122
128
|
<DataGridBySchemaEditable
|
|
@@ -131,7 +137,11 @@ const GenericModelList = ({
|
|
|
131
137
|
hideFooterPagination={hideFooterPagination}
|
|
132
138
|
customColumnOperations={finalCustomColumnOperations}
|
|
133
139
|
customLinkDestination={customLinkDestination}
|
|
134
|
-
setVisibleRows={
|
|
140
|
+
setVisibleRows={(newVisibleRows:GridRowId[]) => {
|
|
141
|
+
if (JSON.stringify(newVisibleRows) !== JSON.stringify(visibleRows)) {
|
|
142
|
+
setVisibleRows(newVisibleRows);
|
|
143
|
+
}
|
|
144
|
+
}}
|
|
135
145
|
onDataChange={newData => {
|
|
136
146
|
setData({
|
|
137
147
|
...data,
|