drf-react-by-schema 0.5.4 → 0.6.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 +14 -16
- package/dist/api.js +88 -59
- package/dist/components/DataGridBySchemaEditable/FooterToolbar.d.ts +1 -1
- package/dist/components/DataGridBySchemaEditable/FooterToolbar.js +5 -11
- package/dist/components/DataGridBySchemaEditable/SelectEditInputCell.d.ts +5 -5
- package/dist/components/DataGridBySchemaEditable/SelectEditInputCell.js +32 -34
- package/dist/components/DataGridBySchemaEditable.d.ts +4 -1
- package/dist/components/DataGridBySchemaEditable.js +187 -134
- package/dist/components/GenericModelList.d.ts +3 -2
- package/dist/components/GenericModelList.js +68 -43
- package/dist/utils.d.ts +14 -1
- package/package.json +3 -2
package/dist/api.d.ts
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
import { serverEndPointType } from './context/DRFReactBySchemaContext';
|
|
2
|
-
import { Item, SchemaType, modelOptionsType, DataSchemaColumnsType, ItemSchemaColumnsType } from './utils';
|
|
2
|
+
import { Item, SchemaType, modelOptionsType, DataSchemaColumnsType, ItemSchemaColumnsType, PaginatedResult } from './utils';
|
|
3
|
+
import { GridFilterModel } from '@mui/x-data-grid';
|
|
3
4
|
type Id = string | number | null;
|
|
4
|
-
/**
|
|
5
|
-
* Interface for CRUD on API
|
|
6
|
-
*
|
|
7
|
-
* @interface TargetApiParams
|
|
8
|
-
*/
|
|
9
5
|
interface TargetApiParams {
|
|
10
6
|
path: string;
|
|
11
7
|
serverEndPoint: serverEndPointType | null;
|
|
@@ -20,15 +16,15 @@ interface TargetApiParamsOptionalId {
|
|
|
20
16
|
}
|
|
21
17
|
/**
|
|
22
18
|
*
|
|
23
|
-
* @param
|
|
19
|
+
* @param options - params
|
|
24
20
|
* @returns Id when succesfully updated, false otherwise
|
|
25
21
|
*/
|
|
26
22
|
export declare const updateData: ({ path, serverEndPoint, data, id }: TargetApiParams) => Promise<any>;
|
|
27
23
|
export declare const partialUpdateData: ({ path, serverEndPoint, data, id }: TargetApiParams) => Promise<any>;
|
|
28
24
|
export declare const createData: ({ path, serverEndPoint, data }: Omit<TargetApiParams, 'id'>) => Promise<any>;
|
|
29
25
|
export declare const deleteData: (path: string, serverEndPoint: serverEndPointType | null, id: Id) => Promise<boolean>;
|
|
30
|
-
export declare const createOrUpdateData: ({ path, serverEndPoint, data, id }: TargetApiParamsOptionalId) => Promise<any>;
|
|
31
|
-
export declare const updateDataBySchema: ({ model, modelObjectId, serverEndPoint, data, schema, path }: {
|
|
26
|
+
export declare const createOrUpdateData: ({ path, serverEndPoint, data, id, }: TargetApiParamsOptionalId) => Promise<any>;
|
|
27
|
+
export declare const updateDataBySchema: ({ model, modelObjectId, serverEndPoint, data, schema, path, }: {
|
|
32
28
|
model: string;
|
|
33
29
|
modelObjectId: Id;
|
|
34
30
|
serverEndPoint: serverEndPointType | null;
|
|
@@ -36,22 +32,22 @@ export declare const updateDataBySchema: ({ model, modelObjectId, serverEndPoint
|
|
|
36
32
|
schema: SchemaType;
|
|
37
33
|
path?: string | null | undefined;
|
|
38
34
|
}) => Promise<any>;
|
|
39
|
-
export declare const addExistingRelatedModel: ({ model, serverEndPoint, id, data }: {
|
|
35
|
+
export declare const addExistingRelatedModel: ({ model, serverEndPoint, id, data, }: {
|
|
40
36
|
model: string;
|
|
41
37
|
serverEndPoint: serverEndPointType | null;
|
|
42
38
|
id: Id;
|
|
43
39
|
data: Item;
|
|
44
40
|
}) => Promise<any>;
|
|
45
|
-
export declare const getAutoComplete: ({ model, serverEndPoint }: {
|
|
41
|
+
export declare const getAutoComplete: ({ model, serverEndPoint, }: {
|
|
46
42
|
model: string;
|
|
47
43
|
serverEndPoint: serverEndPointType | null;
|
|
48
44
|
}) => Promise<false | Item[]>;
|
|
49
|
-
export declare const getJSONSchema: ({ model, serverEndPoint, id }: {
|
|
45
|
+
export declare const getJSONSchema: ({ model, serverEndPoint, id, }: {
|
|
50
46
|
model: string;
|
|
51
47
|
serverEndPoint: serverEndPointType | null;
|
|
52
48
|
id?: Id | undefined;
|
|
53
49
|
}) => Promise<any>;
|
|
54
|
-
export declare const createOrUpdateJSONSchema: ({ model, serverEndPoint, id, formData }: {
|
|
50
|
+
export declare const createOrUpdateJSONSchema: ({ model, serverEndPoint, id, formData, }: {
|
|
55
51
|
model: string;
|
|
56
52
|
serverEndPoint: serverEndPointType | null;
|
|
57
53
|
id?: Id | undefined;
|
|
@@ -59,7 +55,7 @@ export declare const createOrUpdateJSONSchema: ({ model, serverEndPoint, id, for
|
|
|
59
55
|
}) => Promise<any>;
|
|
60
56
|
export declare const loginByPayload: (payload: Item, serverEndPoint: serverEndPointType | null) => Promise<boolean>;
|
|
61
57
|
export declare const setAuthToken: (token: string | null) => void;
|
|
62
|
-
export declare const isLoggedIn: (serverEndPoint: serverEndPointType | null) => Promise<false | Item[]>;
|
|
58
|
+
export declare const isLoggedIn: (serverEndPoint: serverEndPointType | null) => Promise<false | Item[] | PaginatedResult>;
|
|
63
59
|
export interface getGenericModelListProps {
|
|
64
60
|
model: string;
|
|
65
61
|
serverEndPoint: serverEndPointType | null;
|
|
@@ -72,9 +68,11 @@ export interface getGenericModelListProps {
|
|
|
72
68
|
isInBatches?: boolean;
|
|
73
69
|
loadedSchema?: SchemaType | boolean;
|
|
74
70
|
loadedModelOptions?: modelOptionsType | boolean;
|
|
71
|
+
page?: number;
|
|
72
|
+
filter?: GridFilterModel;
|
|
75
73
|
}
|
|
76
|
-
export declare const getGenericModelList: ({ model, serverEndPoint, id, relatedModel, relatedModelId, columnFields, hiddenFields, creatableFields, isInBatches, loadedSchema, loadedModelOptions, }: getGenericModelListProps) => Promise<false | DataSchemaColumnsType>;
|
|
77
|
-
export declare const getGenericModel: ({ model, serverEndPoint, id, relatedModel, relatedModelId }: {
|
|
74
|
+
export declare const getGenericModelList: ({ model, serverEndPoint, id, relatedModel, relatedModelId, columnFields, hiddenFields, creatableFields, isInBatches, loadedSchema, loadedModelOptions, page, filter, }: getGenericModelListProps) => Promise<false | DataSchemaColumnsType>;
|
|
75
|
+
export declare const getGenericModel: ({ model, serverEndPoint, id, relatedModel, relatedModelId, }: {
|
|
78
76
|
model: string;
|
|
79
77
|
serverEndPoint: serverEndPointType | null;
|
|
80
78
|
id?: Id | undefined;
|
package/dist/api.js
CHANGED
|
@@ -16,8 +16,6 @@ exports.getGenericModel = exports.getGenericModelList = exports.isLoggedIn = exp
|
|
|
16
16
|
const axios_1 = __importDefault(require("axios"));
|
|
17
17
|
const moment_1 = __importDefault(require("moment"));
|
|
18
18
|
const utils_1 = require("./utils");
|
|
19
|
-
;
|
|
20
|
-
;
|
|
21
19
|
const getOptions = (path, serverEndPoint) => __awaiter(void 0, void 0, void 0, function* () {
|
|
22
20
|
var _a;
|
|
23
21
|
if (!serverEndPoint) {
|
|
@@ -30,7 +28,7 @@ const getOptions = (path, serverEndPoint) => __awaiter(void 0, void 0, void 0, f
|
|
|
30
28
|
return data;
|
|
31
29
|
}
|
|
32
30
|
catch (e) {
|
|
33
|
-
if (
|
|
31
|
+
if (axios_1.default.isAxiosError(e)) {
|
|
34
32
|
const err = e;
|
|
35
33
|
if (((_a = err.response) === null || _a === void 0 ? void 0 : _a.status) === 401) {
|
|
36
34
|
const isRefreshed = yield refreshToken(serverEndPoint);
|
|
@@ -68,7 +66,7 @@ const getSchema = (path, serverEndPoint) => __awaiter(void 0, void 0, void 0, fu
|
|
|
68
66
|
if (usuaria) {
|
|
69
67
|
options.actions.POST[key].model_default = {
|
|
70
68
|
id: usuaria.id,
|
|
71
|
-
label: usuaria.first_name
|
|
69
|
+
label: usuaria.first_name,
|
|
72
70
|
};
|
|
73
71
|
}
|
|
74
72
|
}
|
|
@@ -79,7 +77,7 @@ const getSchema = (path, serverEndPoint) => __awaiter(void 0, void 0, void 0, fu
|
|
|
79
77
|
delete modelOptions.actions;
|
|
80
78
|
return { schema: options.actions.POST, modelOptions };
|
|
81
79
|
});
|
|
82
|
-
const getData = (path, serverEndPoint, route = 'api') => __awaiter(void 0, void 0, void 0, function* () {
|
|
80
|
+
const getData = ({ path, serverEndPoint, route = 'api', page, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
83
81
|
var _b;
|
|
84
82
|
if (!serverEndPoint) {
|
|
85
83
|
return false;
|
|
@@ -87,10 +85,10 @@ const getData = (path, serverEndPoint, route = 'api') => __awaiter(void 0, void
|
|
|
87
85
|
const url = `${serverEndPoint[route]}/${path}`;
|
|
88
86
|
try {
|
|
89
87
|
const { data } = yield axios_1.default.get(url);
|
|
90
|
-
return data;
|
|
88
|
+
return typeof page === 'undefined' ? data : data;
|
|
91
89
|
}
|
|
92
90
|
catch (e) {
|
|
93
|
-
if (
|
|
91
|
+
if (axios_1.default.isAxiosError(e)) {
|
|
94
92
|
const err = e;
|
|
95
93
|
if (((_b = err.response) === null || _b === void 0 ? void 0 : _b.status) === 401) {
|
|
96
94
|
const isRefreshed = yield refreshToken(serverEndPoint);
|
|
@@ -114,7 +112,7 @@ const getData = (path, serverEndPoint, route = 'api') => __awaiter(void 0, void
|
|
|
114
112
|
});
|
|
115
113
|
/**
|
|
116
114
|
*
|
|
117
|
-
* @param
|
|
115
|
+
* @param options - params
|
|
118
116
|
* @returns Id when succesfully updated, false otherwise
|
|
119
117
|
*/
|
|
120
118
|
const updateData = ({ path, serverEndPoint, data, id }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
@@ -127,7 +125,7 @@ const updateData = ({ path, serverEndPoint, data, id }) => __awaiter(void 0, voi
|
|
|
127
125
|
yield axios_1.default.put(url, data);
|
|
128
126
|
}
|
|
129
127
|
catch (e) {
|
|
130
|
-
if (
|
|
128
|
+
if (axios_1.default.isAxiosError(e)) {
|
|
131
129
|
const err = e;
|
|
132
130
|
if (((_c = err.response) === null || _c === void 0 ? void 0 : _c.status) === 401) {
|
|
133
131
|
const isRefreshed = yield refreshToken(serverEndPoint);
|
|
@@ -162,7 +160,7 @@ const partialUpdateData = ({ path, serverEndPoint, data, id }) => __awaiter(void
|
|
|
162
160
|
// DEBUG console.log({ path, data, id });
|
|
163
161
|
}
|
|
164
162
|
catch (e) {
|
|
165
|
-
if (
|
|
163
|
+
if (axios_1.default.isAxiosError(e)) {
|
|
166
164
|
const err = e;
|
|
167
165
|
if (((_f = err.response) === null || _f === void 0 ? void 0 : _f.status) === 401) {
|
|
168
166
|
const isRefreshed = yield refreshToken(serverEndPoint);
|
|
@@ -197,7 +195,7 @@ const createData = ({ path, serverEndPoint, data }) => __awaiter(void 0, void 0,
|
|
|
197
195
|
ret = yield axios_1.default.post(url, data);
|
|
198
196
|
}
|
|
199
197
|
catch (e) {
|
|
200
|
-
if (
|
|
198
|
+
if (axios_1.default.isAxiosError(e)) {
|
|
201
199
|
const err = e;
|
|
202
200
|
if (((_l = err.response) === null || _l === void 0 ? void 0 : _l.status) === 401) {
|
|
203
201
|
const isRefreshed = yield refreshToken(serverEndPoint);
|
|
@@ -232,7 +230,7 @@ const deleteData = (path, serverEndPoint, id) => __awaiter(void 0, void 0, void
|
|
|
232
230
|
yield axios_1.default.delete(url);
|
|
233
231
|
}
|
|
234
232
|
catch (e) {
|
|
235
|
-
if (
|
|
233
|
+
if (axios_1.default.isAxiosError(e)) {
|
|
236
234
|
const err = e;
|
|
237
235
|
if (((_p = err.response) === null || _p === void 0 ? void 0 : _p.status) === 401) {
|
|
238
236
|
const isRefreshed = yield refreshToken(serverEndPoint);
|
|
@@ -256,7 +254,7 @@ const deleteData = (path, serverEndPoint, id) => __awaiter(void 0, void 0, void
|
|
|
256
254
|
return true;
|
|
257
255
|
});
|
|
258
256
|
exports.deleteData = deleteData;
|
|
259
|
-
const createOrUpdateData = ({ path, serverEndPoint, data, id }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
257
|
+
const createOrUpdateData = ({ path, serverEndPoint, data, id, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
260
258
|
if ((0, utils_1.isTmpId)(id)) {
|
|
261
259
|
id = null;
|
|
262
260
|
}
|
|
@@ -268,11 +266,16 @@ const createOrUpdateData = ({ path, serverEndPoint, data, id }) => __awaiter(voi
|
|
|
268
266
|
if (!responseCreate || Object.prototype.hasOwnProperty.call(responseCreate, 'errors')) {
|
|
269
267
|
return responseCreate;
|
|
270
268
|
}
|
|
271
|
-
const responseUpdate = yield (0, exports.updateData)({
|
|
269
|
+
const responseUpdate = yield (0, exports.updateData)({
|
|
270
|
+
path,
|
|
271
|
+
serverEndPoint,
|
|
272
|
+
data,
|
|
273
|
+
id: responseCreate.data.id,
|
|
274
|
+
});
|
|
272
275
|
return responseUpdate;
|
|
273
276
|
});
|
|
274
277
|
exports.createOrUpdateData = createOrUpdateData;
|
|
275
|
-
const prepareDataBySchema = ({ data, schema
|
|
278
|
+
const prepareDataBySchema = ({ data, schema }) => {
|
|
276
279
|
// console.log('Entered prepareDataBySchema', schema);
|
|
277
280
|
const dbData = {};
|
|
278
281
|
for (const [key, field] of Object.entries(schema)) {
|
|
@@ -288,13 +291,12 @@ const prepareDataBySchema = ({ data, schema, parentIsField = false }) => {
|
|
|
288
291
|
// OneToMany or ManyToMany relations:
|
|
289
292
|
if (field.type === 'field' && field.child) {
|
|
290
293
|
const dataArr = data[key];
|
|
291
|
-
dbData[key] = dataArr.map(row => {
|
|
294
|
+
dbData[key] = dataArr.map((row) => {
|
|
292
295
|
var _a;
|
|
293
296
|
return ((_a = field.child) === null || _a === void 0 ? void 0 : _a.children)
|
|
294
297
|
? prepareDataBySchema({
|
|
295
298
|
data: row,
|
|
296
299
|
schema: field.child.children,
|
|
297
|
-
parentIsField: true
|
|
298
300
|
})
|
|
299
301
|
: row;
|
|
300
302
|
});
|
|
@@ -302,10 +304,10 @@ const prepareDataBySchema = ({ data, schema, parentIsField = false }) => {
|
|
|
302
304
|
}
|
|
303
305
|
// Nested Object:
|
|
304
306
|
if (field.type === 'nested object' && field.children && typeof data[key] === 'object') {
|
|
305
|
-
dbData[key] = (
|
|
307
|
+
dbData[key] = (0, utils_1.isTmpId)(data[key].id)
|
|
306
308
|
? prepareDataBySchema({
|
|
307
309
|
data: data[key],
|
|
308
|
-
schema: field.children
|
|
310
|
+
schema: field.children,
|
|
309
311
|
})
|
|
310
312
|
: data[key].id;
|
|
311
313
|
continue;
|
|
@@ -333,7 +335,7 @@ const prepareDataBySchema = ({ data, schema, parentIsField = false }) => {
|
|
|
333
335
|
// console.log({dbData});
|
|
334
336
|
return dbData;
|
|
335
337
|
};
|
|
336
|
-
const updateDataBySchema = ({ model, modelObjectId, serverEndPoint, data, schema, path = null }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
338
|
+
const updateDataBySchema = ({ model, modelObjectId, serverEndPoint, data, schema, path = null, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
337
339
|
/*console.log({
|
|
338
340
|
model,
|
|
339
341
|
modelObjectId,
|
|
@@ -349,7 +351,7 @@ const updateDataBySchema = ({ model, modelObjectId, serverEndPoint, data, schema
|
|
|
349
351
|
path,
|
|
350
352
|
serverEndPoint,
|
|
351
353
|
data: dbData,
|
|
352
|
-
id: modelObjectId
|
|
354
|
+
id: modelObjectId,
|
|
353
355
|
});
|
|
354
356
|
if (typeof response !== 'object' && parseInt(response)) {
|
|
355
357
|
return response;
|
|
@@ -358,7 +360,7 @@ const updateDataBySchema = ({ model, modelObjectId, serverEndPoint, data, schema
|
|
|
358
360
|
return response;
|
|
359
361
|
});
|
|
360
362
|
exports.updateDataBySchema = updateDataBySchema;
|
|
361
|
-
const addExistingRelatedModel = ({ model, serverEndPoint, id, data }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
363
|
+
const addExistingRelatedModel = ({ model, serverEndPoint, id, data, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
362
364
|
const response = yield (0, exports.partialUpdateData)({ path: model, serverEndPoint, data, id });
|
|
363
365
|
// DEBUG console.log({ model, id, data, response });
|
|
364
366
|
return response;
|
|
@@ -368,28 +370,32 @@ const getDataGridColumns = (schema, columnFields = [], hiddenFields = [], creata
|
|
|
368
370
|
if (!schema) {
|
|
369
371
|
return false;
|
|
370
372
|
}
|
|
371
|
-
columnFields =
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
373
|
+
columnFields =
|
|
374
|
+
!columnFields || columnFields.length === 0
|
|
375
|
+
? Object.keys(schema)
|
|
376
|
+
: columnFields.filter((field) => {
|
|
377
|
+
return Object.prototype.hasOwnProperty.call(schema, field);
|
|
378
|
+
});
|
|
379
|
+
return columnFields.map((field) => {
|
|
377
380
|
const column = {
|
|
378
381
|
field,
|
|
379
382
|
headerName: schema[field].label,
|
|
380
|
-
hide:
|
|
381
|
-
creatable:
|
|
382
|
-
width: 100
|
|
383
|
+
hide: hiddenFields.includes(field),
|
|
384
|
+
creatable: creatableFields.includes(field) || schema[field].creatable,
|
|
385
|
+
width: 100,
|
|
383
386
|
};
|
|
384
387
|
return column;
|
|
385
388
|
});
|
|
386
389
|
};
|
|
387
|
-
const getAutoComplete = ({ model, serverEndPoint }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
388
|
-
const data = yield getData(model, serverEndPoint, 'autocomplete');
|
|
390
|
+
const getAutoComplete = ({ model, serverEndPoint, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
391
|
+
const data = yield getData({ path: model, serverEndPoint, route: 'autocomplete' });
|
|
392
|
+
if (data === false) {
|
|
393
|
+
return false;
|
|
394
|
+
}
|
|
389
395
|
return data;
|
|
390
396
|
});
|
|
391
397
|
exports.getAutoComplete = getAutoComplete;
|
|
392
|
-
const getJSONSchema = ({ model, serverEndPoint, id = 'create' }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
398
|
+
const getJSONSchema = ({ model, serverEndPoint, id = 'create', }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
393
399
|
var _q;
|
|
394
400
|
if (!serverEndPoint) {
|
|
395
401
|
console.log('Não há definição de API (serverEndPoint!');
|
|
@@ -401,7 +407,7 @@ const getJSONSchema = ({ model, serverEndPoint, id = 'create' }) => __awaiter(vo
|
|
|
401
407
|
return data;
|
|
402
408
|
}
|
|
403
409
|
catch (e) {
|
|
404
|
-
if (
|
|
410
|
+
if (axios_1.default.isAxiosError(e)) {
|
|
405
411
|
const err = e;
|
|
406
412
|
if (((_q = err.response) === null || _q === void 0 ? void 0 : _q.status) === 401) {
|
|
407
413
|
const isRefreshed = yield refreshToken(serverEndPoint);
|
|
@@ -424,7 +430,7 @@ const getJSONSchema = ({ model, serverEndPoint, id = 'create' }) => __awaiter(vo
|
|
|
424
430
|
}
|
|
425
431
|
});
|
|
426
432
|
exports.getJSONSchema = getJSONSchema;
|
|
427
|
-
const createOrUpdateJSONSchema = ({ model, serverEndPoint, id = 'create', formData }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
433
|
+
const createOrUpdateJSONSchema = ({ model, serverEndPoint, id = 'create', formData, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
428
434
|
var _r;
|
|
429
435
|
if (!serverEndPoint) {
|
|
430
436
|
return { errors: 'Não há definição de API (serverEndPoint!' };
|
|
@@ -434,13 +440,11 @@ const createOrUpdateJSONSchema = ({ model, serverEndPoint, id = 'create', formDa
|
|
|
434
440
|
url += `${id}/`;
|
|
435
441
|
}
|
|
436
442
|
try {
|
|
437
|
-
const { data } =
|
|
438
|
-
? yield axios_1.default.post(url, formData)
|
|
439
|
-
: yield axios_1.default.patch(url, formData);
|
|
443
|
+
const { data } = id === 'create' ? yield axios_1.default.post(url, formData) : yield axios_1.default.patch(url, formData);
|
|
440
444
|
return data;
|
|
441
445
|
}
|
|
442
446
|
catch (e) {
|
|
443
|
-
if (
|
|
447
|
+
if (axios_1.default.isAxiosError(e)) {
|
|
444
448
|
const err = e;
|
|
445
449
|
if (((_r = err.response) === null || _r === void 0 ? void 0 : _r.status) === 401) {
|
|
446
450
|
const isRefreshed = yield refreshToken(serverEndPoint);
|
|
@@ -449,7 +453,7 @@ const createOrUpdateJSONSchema = ({ model, serverEndPoint, id = 'create', formDa
|
|
|
449
453
|
return { errors: 'Token expirou! Deve-se fazer login de novo!' };
|
|
450
454
|
}
|
|
451
455
|
try {
|
|
452
|
-
const { data } =
|
|
456
|
+
const { data } = id === 'create'
|
|
453
457
|
? yield axios_1.default.post(url, formData)
|
|
454
458
|
: yield axios_1.default.patch(url, formData);
|
|
455
459
|
return data;
|
|
@@ -512,7 +516,7 @@ const refreshToken = (serverEndPoint) => __awaiter(void 0, void 0, void 0, funct
|
|
|
512
516
|
}
|
|
513
517
|
try {
|
|
514
518
|
const { data } = yield axios_1.default.post(`${serverEndPoint.refreshToken}`, {
|
|
515
|
-
refresh: refreshToken
|
|
519
|
+
refresh: refreshToken,
|
|
516
520
|
});
|
|
517
521
|
(0, exports.setAuthToken)(data.access);
|
|
518
522
|
return true;
|
|
@@ -528,7 +532,7 @@ const isLoggedIn = (serverEndPoint) => __awaiter(void 0, void 0, void 0, functio
|
|
|
528
532
|
if (!token) {
|
|
529
533
|
return false;
|
|
530
534
|
}
|
|
531
|
-
const usuaria = yield getData('minhaconta', serverEndPoint);
|
|
535
|
+
const usuaria = yield getData({ path: 'minhaconta', serverEndPoint });
|
|
532
536
|
if (!usuaria) {
|
|
533
537
|
console.log('Erro ao recuperar dados de usuária!');
|
|
534
538
|
return false;
|
|
@@ -536,8 +540,7 @@ const isLoggedIn = (serverEndPoint) => __awaiter(void 0, void 0, void 0, functio
|
|
|
536
540
|
return usuaria;
|
|
537
541
|
});
|
|
538
542
|
exports.isLoggedIn = isLoggedIn;
|
|
539
|
-
|
|
540
|
-
const getGenericModelList = ({ model, serverEndPoint, id = '', relatedModel = '', relatedModelId = '', columnFields, hiddenFields = ['id'], creatableFields = [], isInBatches = false, loadedSchema, loadedModelOptions, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
543
|
+
const getGenericModelList = ({ model, serverEndPoint, id = '', relatedModel = '', relatedModelId = '', columnFields, hiddenFields = ['id'], creatableFields = [], isInBatches = false, loadedSchema, loadedModelOptions, page, filter, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
541
544
|
let path = `${model}/${id}`;
|
|
542
545
|
let schemaPath = model;
|
|
543
546
|
let schema = loadedSchema;
|
|
@@ -547,6 +550,31 @@ const getGenericModelList = ({ model, serverEndPoint, id = '', relatedModel = ''
|
|
|
547
550
|
path += `/${relatedModel}/${relatedModelId}`;
|
|
548
551
|
schemaPath += `/${id}/${relatedModel}`;
|
|
549
552
|
}
|
|
553
|
+
if (page || filter) {
|
|
554
|
+
path += '?';
|
|
555
|
+
}
|
|
556
|
+
if (page) {
|
|
557
|
+
path += `page=${page + 1}`;
|
|
558
|
+
}
|
|
559
|
+
if (filter) {
|
|
560
|
+
const filtersArr = [];
|
|
561
|
+
if (filter.quickFilterValues && filter.quickFilterValues.length > 0 && filter.quickFilterValues[0]) {
|
|
562
|
+
filtersArr.push(`search=${filter.quickFilterValues[0]}`);
|
|
563
|
+
}
|
|
564
|
+
for (const item of filter.items) {
|
|
565
|
+
if (!item.operatorValue) {
|
|
566
|
+
continue;
|
|
567
|
+
}
|
|
568
|
+
const queryParam = item.value
|
|
569
|
+
? `columnField=${item.columnField}&operatorValue=${item.operatorValue}&value=${item.value}`
|
|
570
|
+
: `columnField=${item.columnField}&operatorValue=${item.operatorValue}`;
|
|
571
|
+
filtersArr.push(queryParam);
|
|
572
|
+
}
|
|
573
|
+
if (page) {
|
|
574
|
+
path += '&';
|
|
575
|
+
}
|
|
576
|
+
path += filtersArr.join('&');
|
|
577
|
+
}
|
|
550
578
|
// Only get schema and columns if not in batches or in first batch:
|
|
551
579
|
if (!schema) {
|
|
552
580
|
const options = yield getSchema(schemaPath, serverEndPoint);
|
|
@@ -560,18 +588,21 @@ const getGenericModelList = ({ model, serverEndPoint, id = '', relatedModel = ''
|
|
|
560
588
|
return false;
|
|
561
589
|
}
|
|
562
590
|
}
|
|
591
|
+
let rowCount = 0;
|
|
563
592
|
let data = [];
|
|
564
593
|
if (!id || (id && !relatedModelId)) {
|
|
565
594
|
if (isInBatches) {
|
|
566
|
-
path +=
|
|
567
|
-
? '?is_last_batch=1'
|
|
568
|
-
: '?is_first_batch=1';
|
|
595
|
+
path += loadedSchema ? '?is_last_batch=1' : '?is_first_batch=1';
|
|
569
596
|
}
|
|
570
|
-
const ret = yield getData(path, serverEndPoint);
|
|
597
|
+
const ret = yield getData({ path, serverEndPoint, page });
|
|
571
598
|
if (ret === false) {
|
|
572
599
|
return false;
|
|
573
600
|
}
|
|
574
|
-
|
|
601
|
+
const dataRaw = 'results' in ret ? ret.results : ret;
|
|
602
|
+
if ('results' in ret) {
|
|
603
|
+
rowCount = ret.count;
|
|
604
|
+
}
|
|
605
|
+
data = dataRaw.map((row) => {
|
|
575
606
|
const newRow = {};
|
|
576
607
|
for (const [key, field] of Object.entries(schema)) {
|
|
577
608
|
if (!(key in row)) {
|
|
@@ -581,27 +612,25 @@ const getGenericModelList = ({ model, serverEndPoint, id = '', relatedModel = ''
|
|
|
581
612
|
newRow[key] = row[key]
|
|
582
613
|
? {
|
|
583
614
|
value: row[key],
|
|
584
|
-
display_name: (0, utils_1.getChoiceByValue)(row[key], field.choices)
|
|
615
|
+
display_name: (0, utils_1.getChoiceByValue)(row[key], field.choices),
|
|
585
616
|
}
|
|
586
617
|
: (0, utils_1.emptyByType)(field);
|
|
587
618
|
continue;
|
|
588
619
|
}
|
|
589
|
-
newRow[key] =
|
|
590
|
-
? row[key]
|
|
591
|
-
: (0, utils_1.emptyByType)(field);
|
|
620
|
+
newRow[key] = row[key] ? row[key] : (0, utils_1.emptyByType)(field);
|
|
592
621
|
}
|
|
593
622
|
return newRow;
|
|
594
623
|
});
|
|
595
624
|
}
|
|
596
625
|
if (loadedSchema) {
|
|
597
626
|
// DEBUG console.log({ path, data });
|
|
598
|
-
return { data };
|
|
627
|
+
return { data, rowCount };
|
|
599
628
|
}
|
|
600
629
|
// DEBUG console.log({ path, data, columns, schema });
|
|
601
|
-
return { data, columns, schema, modelOptions };
|
|
630
|
+
return { data, columns, schema, modelOptions, rowCount };
|
|
602
631
|
});
|
|
603
632
|
exports.getGenericModelList = getGenericModelList;
|
|
604
|
-
const getGenericModel = ({ model, serverEndPoint, id = '', relatedModel = '', relatedModelId = '' }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
633
|
+
const getGenericModel = ({ model, serverEndPoint, id = '', relatedModel = '', relatedModelId = '', }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
605
634
|
let path = `${model}/${id}`;
|
|
606
635
|
let schemaPath = model;
|
|
607
636
|
if (id && relatedModel) {
|
|
@@ -613,9 +642,9 @@ const getGenericModel = ({ model, serverEndPoint, id = '', relatedModel = '', re
|
|
|
613
642
|
return false;
|
|
614
643
|
}
|
|
615
644
|
const { schema, modelOptions } = options;
|
|
616
|
-
const data =
|
|
645
|
+
const data = !id || (id && relatedModel && !relatedModelId)
|
|
617
646
|
? {}
|
|
618
|
-
: yield getData(path, serverEndPoint);
|
|
647
|
+
: yield getData({ path, serverEndPoint });
|
|
619
648
|
// console.log({ schema, data }); // DEBUG
|
|
620
649
|
return { schema, modelOptions, data };
|
|
621
650
|
});
|
|
@@ -13,5 +13,5 @@ type FooterToolbarProps = {
|
|
|
13
13
|
indexField: string;
|
|
14
14
|
isEditable: boolean;
|
|
15
15
|
};
|
|
16
|
-
export declare const FooterToolbar: ({ name, setRowModesModel, dataGrid, setDataGrid, emptyItem, indexField, isEditable }: FooterToolbarProps) => JSX.Element;
|
|
16
|
+
export declare const FooterToolbar: ({ name, setRowModesModel, dataGrid, setDataGrid, emptyItem, indexField, isEditable, }: FooterToolbarProps) => JSX.Element;
|
|
17
17
|
export {};
|
|
@@ -9,16 +9,13 @@ const x_data_grid_1 = require("@mui/x-data-grid");
|
|
|
9
9
|
const Button_1 = __importDefault(require("@mui/material/Button"));
|
|
10
10
|
const Add_1 = __importDefault(require("@mui/icons-material/Add"));
|
|
11
11
|
const utils_1 = require("../../utils");
|
|
12
|
-
const FooterToolbar = ({ name, setRowModesModel, dataGrid, setDataGrid, emptyItem, indexField, isEditable }) => {
|
|
12
|
+
const FooterToolbar = ({ name, setRowModesModel, dataGrid, setDataGrid, emptyItem, indexField, isEditable, }) => {
|
|
13
13
|
const handleClick = () => {
|
|
14
14
|
const id = (0, utils_1.getTmpId)();
|
|
15
15
|
emptyItem.current.id = id;
|
|
16
|
-
const newData = [
|
|
17
|
-
Object.assign({}, emptyItem.current),
|
|
18
|
-
...dataGrid.data
|
|
19
|
-
];
|
|
16
|
+
const newData = [Object.assign({}, emptyItem.current), ...dataGrid.data];
|
|
20
17
|
setDataGrid({
|
|
21
|
-
data: newData
|
|
18
|
+
data: newData,
|
|
22
19
|
});
|
|
23
20
|
setRowModesModel((oldModel) => (Object.assign(Object.assign({}, oldModel), { [id]: { mode: x_data_grid_1.GridRowModes.Edit, fieldToFocus: indexField } })));
|
|
24
21
|
// Ugly hack to scroll to top, since scroll to cell is only available in Pro
|
|
@@ -32,10 +29,7 @@ const FooterToolbar = ({ name, setRowModesModel, dataGrid, setDataGrid, emptyIte
|
|
|
32
29
|
}
|
|
33
30
|
};
|
|
34
31
|
return (react_1.default.createElement(x_data_grid_1.GridFooterContainer, null,
|
|
35
|
-
isEditable &&
|
|
36
|
-
|
|
37
|
-
react_1.default.createElement(x_data_grid_1.GridFooter, { sx: (isEditable)
|
|
38
|
-
? { border: 'none' }
|
|
39
|
-
: { width: '100%' } })));
|
|
32
|
+
isEditable && (react_1.default.createElement(Button_1.default, { color: "primary", startIcon: react_1.default.createElement(Add_1.default, null), onClick: handleClick, sx: { ml: 2 } }, "Adicionar")),
|
|
33
|
+
react_1.default.createElement(x_data_grid_1.GridFooter, { sx: isEditable ? { border: 'none' } : { width: '100%' } })));
|
|
40
34
|
};
|
|
41
35
|
exports.FooterToolbar = FooterToolbar;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import React from
|
|
2
|
-
import { SxProps } from
|
|
3
|
-
import { GridEnrichedBySchemaColDef } from
|
|
4
|
-
import { OptionsACType, OnEditModelType } from
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { SxProps } from "@mui/material";
|
|
3
|
+
import { GridEnrichedBySchemaColDef } from "../../utils";
|
|
4
|
+
import { OptionsACType, OnEditModelType } from "../../context/APIWrapperContext";
|
|
5
5
|
interface SelectEditInputCellProps {
|
|
6
6
|
field: string;
|
|
7
7
|
id: number | string;
|
|
@@ -30,5 +30,5 @@ interface SelectEditInputCellProps {
|
|
|
30
30
|
* }
|
|
31
31
|
* @returns {*} {JSX.Element}
|
|
32
32
|
*/
|
|
33
|
-
export declare function SelectEditInputCell({ field, id, value, column, type, optionsAC, isIndexField, multiple, onEditModel, sx }: SelectEditInputCellProps): JSX.Element;
|
|
33
|
+
export declare function SelectEditInputCell({ field, id, value, column, type, optionsAC, isIndexField, multiple, onEditModel, sx, }: SelectEditInputCellProps): JSX.Element;
|
|
34
34
|
export {};
|
|
@@ -60,7 +60,7 @@ const filter = (0, Autocomplete_1.createFilterOptions)();
|
|
|
60
60
|
* }
|
|
61
61
|
* @returns {*} {JSX.Element}
|
|
62
62
|
*/
|
|
63
|
-
function SelectEditInputCell({ field, id, value, column, type, optionsAC, isIndexField, multiple = false, onEditModel, sx = {} }) {
|
|
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
66
|
console.log(column);
|
|
@@ -68,29 +68,28 @@ function SelectEditInputCell({ field, id, value, column, type, optionsAC, isInde
|
|
|
68
68
|
yield apiRef.current.setEditCellValue({ id, field, value: newValue });
|
|
69
69
|
apiRef.current.stopCellEditMode({ id, field });
|
|
70
70
|
});
|
|
71
|
-
const labelKey =
|
|
72
|
-
?
|
|
73
|
-
:
|
|
74
|
-
const valueKey =
|
|
75
|
-
?
|
|
76
|
-
:
|
|
71
|
+
const labelKey = ["field", "nested object"].includes(type) || isIndexField
|
|
72
|
+
? "label"
|
|
73
|
+
: "display_name";
|
|
74
|
+
const valueKey = ["field", "nested object"].includes(type) || isIndexField
|
|
75
|
+
? "id"
|
|
76
|
+
: "value";
|
|
77
77
|
let creatableProps = {};
|
|
78
78
|
// if (column.creatable || isIndexField) {
|
|
79
79
|
if (column.creatable) {
|
|
80
80
|
creatableProps = {
|
|
81
|
-
freesolo:
|
|
81
|
+
freesolo: "true",
|
|
82
82
|
filterOptions: (options, params) => {
|
|
83
83
|
const filtered = filter(options, params);
|
|
84
|
-
const inputValue =
|
|
85
|
-
? params.inputValue
|
|
86
|
-
: '';
|
|
84
|
+
const inputValue = params.inputValue ? params.inputValue : "";
|
|
87
85
|
const inputValueLower = inputValue.trim().toLowerCase();
|
|
88
86
|
// Suggest the creation of a new value
|
|
89
|
-
const isExisting = options.some(option => inputValueLower ===
|
|
90
|
-
|
|
87
|
+
const isExisting = options.some((option) => inputValueLower ===
|
|
88
|
+
option[labelKey].trim().toLowerCase());
|
|
89
|
+
if (inputValue !== "" && !isExisting) {
|
|
91
90
|
filtered.push({
|
|
92
91
|
inputValue,
|
|
93
|
-
[labelKey]: `Criar "${inputValue}"
|
|
92
|
+
[labelKey]: `Criar "${inputValue}"`,
|
|
94
93
|
});
|
|
95
94
|
}
|
|
96
95
|
return filtered;
|
|
@@ -98,7 +97,7 @@ function SelectEditInputCell({ field, id, value, column, type, optionsAC, isInde
|
|
|
98
97
|
handleHomeEndKeys: true,
|
|
99
98
|
getOptionLabel: (option) => {
|
|
100
99
|
// Value selected with enter, right from the input
|
|
101
|
-
if (typeof option ===
|
|
100
|
+
if (typeof option === "string") {
|
|
102
101
|
return option;
|
|
103
102
|
}
|
|
104
103
|
// Criar "xxx" option created dynamically
|
|
@@ -110,12 +109,13 @@ function SelectEditInputCell({ field, id, value, column, type, optionsAC, isInde
|
|
|
110
109
|
},
|
|
111
110
|
renderOption: (props, option) => {
|
|
112
111
|
return (react_1.default.createElement("li", Object.assign({ key: option[valueKey] }, props), option[labelKey]));
|
|
113
|
-
}
|
|
112
|
+
},
|
|
114
113
|
};
|
|
115
114
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
115
|
+
return (react_1.default.createElement(Autocomplete_1.default, Object.assign({ key: field, id: field, value: value, options: optionsAC.current && optionsAC.current[field]
|
|
116
|
+
? optionsAC.current[field]
|
|
117
|
+
: [], selectOnFocus: true, autoHighlight: true, multiple: multiple, isOptionEqualToValue: (option, value) => {
|
|
118
|
+
return option[labelKey] === value[labelKey];
|
|
119
119
|
}, getOptionLabel: (option) => {
|
|
120
120
|
return option[labelKey];
|
|
121
121
|
}, onChange: (e, value) => {
|
|
@@ -125,32 +125,30 @@ function SelectEditInputCell({ field, id, value, column, type, optionsAC, isInde
|
|
|
125
125
|
return;
|
|
126
126
|
}
|
|
127
127
|
let newValue = value;
|
|
128
|
-
if (typeof newValue ===
|
|
128
|
+
if (typeof newValue === "string") {
|
|
129
129
|
const tmpId = (0, utils_1.getTmpId)();
|
|
130
130
|
newValue = {
|
|
131
131
|
[valueKey]: tmpId,
|
|
132
|
-
[labelKey]: newValue
|
|
132
|
+
[labelKey]: newValue,
|
|
133
133
|
};
|
|
134
134
|
}
|
|
135
135
|
if (newValue && newValue.inputValue) {
|
|
136
136
|
const tmpId = (0, utils_1.getTmpId)();
|
|
137
137
|
newValue = {
|
|
138
138
|
[valueKey]: tmpId,
|
|
139
|
-
[labelKey]: newValue.inputValue
|
|
139
|
+
[labelKey]: newValue.inputValue,
|
|
140
140
|
};
|
|
141
141
|
}
|
|
142
142
|
handleChange(newValue);
|
|
143
|
-
}, fullWidth: true, renderInput: params => (react_1.default.createElement(react_1.default.Fragment, null, column.creatable
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
params.InputProps.endAdornment)) }) }))
|
|
154
|
-
: react_1.default.createElement(TextField_1.default, Object.assign({}, params, { sx: sx })))) }, creatableProps)));
|
|
143
|
+
}, fullWidth: true, renderInput: (params) => (react_1.default.createElement(react_1.default.Fragment, null, column.creatable ? (react_1.default.createElement(TextField_1.default, Object.assign({}, params, { margin: "normal", InputProps: Object.assign(Object.assign({}, params.InputProps), { endAdornment: (react_1.default.createElement(react_1.default.Fragment, null,
|
|
144
|
+
!multiple && onEditModel && value && (react_1.default.createElement(IconButton_1.default, { size: "small", onClick: () => {
|
|
145
|
+
onEditModel({
|
|
146
|
+
model: field,
|
|
147
|
+
id: value[valueKey],
|
|
148
|
+
labelKey,
|
|
149
|
+
});
|
|
150
|
+
} },
|
|
151
|
+
react_1.default.createElement(Edit_1.default, null))),
|
|
152
|
+
params.InputProps.endAdornment)) }) }))) : (react_1.default.createElement(TextField_1.default, Object.assign({}, params, { sx: sx }))))) }, creatableProps)));
|
|
155
153
|
}
|
|
156
154
|
exports.SelectEditInputCell = SelectEditInputCell;
|