drf-react-by-schema 0.2.2 → 0.3.1
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 +84 -0
- package/dist/api.js +613 -0
- package/dist/components/DataGridBySchemaEditable/ConfirmDialog.d.ts +8 -0
- package/dist/components/DataGridBySchemaEditable/ConfirmDialog.js +21 -0
- package/dist/components/DataGridBySchemaEditable/CustomToolbar.d.ts +16 -0
- package/dist/components/DataGridBySchemaEditable/CustomToolbar.js +77 -0
- package/dist/components/DataGridBySchemaEditable/FooterToolbar.d.ts +16 -0
- package/dist/components/DataGridBySchemaEditable/FooterToolbar.js +41 -0
- package/dist/components/DataGridBySchemaEditable/GridDecimalInput.d.ts +8 -0
- package/dist/components/DataGridBySchemaEditable/GridDecimalInput.js +32 -0
- package/dist/components/DataGridBySchemaEditable/GridPatternInput.d.ts +8 -0
- package/dist/components/DataGridBySchemaEditable/GridPatternInput.js +30 -0
- package/dist/components/DataGridBySchemaEditable/InputInterval.d.ts +3 -0
- package/dist/components/DataGridBySchemaEditable/InputInterval.js +105 -0
- package/dist/components/DataGridBySchemaEditable/SelectEditInputCell.d.ts +33 -0
- package/dist/components/DataGridBySchemaEditable/SelectEditInputCell.js +140 -0
- package/dist/components/DataGridBySchemaEditable/utils.d.ts +15 -0
- package/{src/components/DataGridBySchemaEditable/utils.ts → dist/components/DataGridBySchemaEditable/utils.js} +32 -50
- package/dist/components/DataGridBySchemaEditable.d.ts +34 -0
- package/dist/components/DataGridBySchemaEditable.js +549 -0
- package/dist/components/DataTotals.d.ts +14 -0
- package/dist/components/DataTotals.js +23 -0
- package/dist/components/DialogActions.d.ts +10 -0
- package/dist/components/DialogActions.js +17 -0
- package/dist/components/DialogJSONSchemaForm.d.ts +12 -0
- package/dist/components/DialogJSONSchemaForm.js +20 -0
- package/dist/components/FormButtons.d.ts +14 -0
- package/dist/components/FormButtons.js +39 -0
- package/dist/components/GenericModelList.d.ts +19 -0
- package/dist/components/GenericModelList.js +113 -0
- package/dist/components/GenericRelatedModelList.d.ts +25 -0
- package/dist/components/GenericRelatedModelList.js +111 -0
- package/dist/components/TextFieldBySchema.js +39 -0
- package/dist/context/APIWrapper.d.ts +12 -0
- package/dist/context/APIWrapper.js +341 -0
- package/dist/context/APIWrapperContext.d.ts +75 -0
- package/dist/context/APIWrapperContext.js +15 -0
- package/dist/context/DRFReactBySchemaContext.d.ts +20 -0
- package/dist/context/DRFReactBySchemaContext.js +19 -0
- package/dist/context/DRFReactBySchemaProvider.d.ts +13 -0
- package/dist/context/DRFReactBySchemaProvider.js +53 -0
- package/dist/context/Form.d.ts +11 -0
- package/dist/context/Form.js +82 -0
- package/dist/context/Overlays.d.ts +4 -0
- package/dist/context/Overlays.js +87 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +14 -27
- package/{src/styles/index.ts → dist/styles/index.d.ts} +0 -1
- package/dist/styles/index.js +28 -0
- package/dist/styles/layout.d.ts +107 -0
- package/dist/styles/layout.js +56 -0
- package/dist/styles/theme.d.ts +21 -0
- package/dist/styles/theme.js +139 -0
- package/dist/utils.d.ts +75 -0
- package/dist/utils.js +268 -0
- package/package.json +15 -2
- package/.eslintrc.js +0 -14
- package/.gitlab-ci.yml +0 -14
- package/src/api.ts +0 -733
- package/src/components/DataGridBySchemaEditable/ConfirmDialog.tsx +0 -41
- package/src/components/DataGridBySchemaEditable/CustomToolbar.tsx +0 -93
- package/src/components/DataGridBySchemaEditable/FooterToolbar.tsx +0 -77
- package/src/components/DataGridBySchemaEditable/GridDecimalInput.tsx +0 -41
- package/src/components/DataGridBySchemaEditable/GridPatternInput.tsx +0 -37
- package/src/components/DataGridBySchemaEditable/InputInterval.tsx +0 -194
- package/src/components/DataGridBySchemaEditable/SelectEditInputCell.tsx +0 -153
- package/src/components/DataGridBySchemaEditable.md +0 -50
- package/src/components/DataGridBySchemaEditable.tsx +0 -747
- package/src/components/DataTotals.tsx +0 -56
- package/src/components/GenericModelList.tsx +0 -155
- package/src/context/DRFReactBySchemaProvider.md +0 -50
- package/src/context/DRFReactBySchemaProvider.tsx +0 -78
- package/src/index.ts +0 -64
- package/src/styles/layout.ts +0 -104
- package/src/styles/theme.ts +0 -190
- package/src/utils.ts +0 -321
- package/styleguide.config.js +0 -13
- package/tsconfig.json +0 -104
- package/webpack.config.js +0 -24
package/src/api.ts
DELETED
|
@@ -1,733 +0,0 @@
|
|
|
1
|
-
import axios, { AxiosError, AxiosResponse } from 'axios';
|
|
2
|
-
import { ServerResponse } from 'http';
|
|
3
|
-
import { string } from 'yup';
|
|
4
|
-
import { serverEndPointType } from './context/DRFReactBySchemaProvider';
|
|
5
|
-
import {
|
|
6
|
-
isTmpId,
|
|
7
|
-
emptyByType,
|
|
8
|
-
getChoiceByValue,
|
|
9
|
-
Field,
|
|
10
|
-
Item,
|
|
11
|
-
SchemaType,
|
|
12
|
-
GridEnrichedBySchemaColDef
|
|
13
|
-
} from './utils';
|
|
14
|
-
|
|
15
|
-
const moment = require('moment');
|
|
16
|
-
|
|
17
|
-
type Id = string | number | null;
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Interface for CRUD on API
|
|
21
|
-
*
|
|
22
|
-
* @interface TargetApiParams
|
|
23
|
-
*/
|
|
24
|
-
interface TargetApiParams {
|
|
25
|
-
path:string;
|
|
26
|
-
serverEndPoint:serverEndPointType | null;
|
|
27
|
-
data:Item;
|
|
28
|
-
id:Id;
|
|
29
|
-
};
|
|
30
|
-
interface TargetApiParamsOptionalId {
|
|
31
|
-
path:string;
|
|
32
|
-
serverEndPoint:serverEndPointType | null;
|
|
33
|
-
data:Item;
|
|
34
|
-
id?:Id;
|
|
35
|
-
};
|
|
36
|
-
const getOptions = async (path:string, serverEndPoint:serverEndPointType | null) => {
|
|
37
|
-
if (!serverEndPoint) {
|
|
38
|
-
console.log('Não há definição de API (serverEndPoint!');
|
|
39
|
-
return false;
|
|
40
|
-
}
|
|
41
|
-
const url = `${serverEndPoint.api}/${path}`;
|
|
42
|
-
try {
|
|
43
|
-
const { data } = await axios.options(url);
|
|
44
|
-
return data;
|
|
45
|
-
} catch (e) {
|
|
46
|
-
if ((<any>e).isAxiosError) {
|
|
47
|
-
const err = <AxiosError>e;
|
|
48
|
-
if (err.response?.status === 401) {
|
|
49
|
-
const isRefreshed = await refreshToken(serverEndPoint);
|
|
50
|
-
if (!isRefreshed) {
|
|
51
|
-
console.log('Token expirou! Deve-se fazer login de novo');
|
|
52
|
-
return false;
|
|
53
|
-
}
|
|
54
|
-
try {
|
|
55
|
-
const { data } = await axios.options(url);
|
|
56
|
-
return data;
|
|
57
|
-
} catch (e) {
|
|
58
|
-
console.log(`Error fetching options from ${url}`, e);
|
|
59
|
-
return false;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
console.log(`Error fetching options from ${url}`, e);
|
|
63
|
-
return false;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
const getSchema = async (path:string, serverEndPoint:serverEndPointType | null) => {
|
|
69
|
-
const options = await getOptions(path, serverEndPoint);
|
|
70
|
-
if (!options || !options.actions || !options.actions.POST) {
|
|
71
|
-
return false;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// Special default value of "currentUser":
|
|
75
|
-
let usuaria:Item | boolean = false;
|
|
76
|
-
const postActions:Record<string, Field> = options.actions.POST;
|
|
77
|
-
for (const [key, field] of Object.entries(postActions)) {
|
|
78
|
-
if (field.model_default === 'currentUser') {
|
|
79
|
-
if (!usuaria) {
|
|
80
|
-
usuaria = await isLoggedIn(serverEndPoint);
|
|
81
|
-
}
|
|
82
|
-
if (usuaria) {
|
|
83
|
-
options.actions.POST[key].model_default = {
|
|
84
|
-
id: usuaria.id,
|
|
85
|
-
label: usuaria.first_name
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
return options.actions.POST as SchemaType;
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
const getData = async (
|
|
95
|
-
path:string,
|
|
96
|
-
serverEndPoint:serverEndPointType | null,
|
|
97
|
-
route:string = 'api'
|
|
98
|
-
) => {
|
|
99
|
-
if (!serverEndPoint) {
|
|
100
|
-
return false;
|
|
101
|
-
}
|
|
102
|
-
const url = `${serverEndPoint[route as keyof serverEndPointType]}/${path}`;
|
|
103
|
-
try {
|
|
104
|
-
const { data } = await axios.get(url);
|
|
105
|
-
return data as Item[];
|
|
106
|
-
} catch (e) {
|
|
107
|
-
if ((<any>e).isAxiosError) {
|
|
108
|
-
const err = <AxiosError>e;
|
|
109
|
-
if (err.response?.status === 401) {
|
|
110
|
-
const isRefreshed = await refreshToken(serverEndPoint);
|
|
111
|
-
if (!isRefreshed) {
|
|
112
|
-
console.log('Token expirou! Deve-se fazer login de novo');
|
|
113
|
-
return false;
|
|
114
|
-
}
|
|
115
|
-
try {
|
|
116
|
-
const { data } = await axios.get(url);
|
|
117
|
-
return data as Item[];
|
|
118
|
-
} catch (e) {
|
|
119
|
-
console.log(`Error fetching data from ${url} after token refresh`, e);
|
|
120
|
-
return false;
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
console.log(`Error fetching data from ${url}`, e);
|
|
125
|
-
return false;
|
|
126
|
-
}
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
*
|
|
131
|
-
* @param param0
|
|
132
|
-
* @returns Id when succesfully updated, false otherwise
|
|
133
|
-
*/
|
|
134
|
-
export const updateData = async ({ path, serverEndPoint, data, id }: TargetApiParams) => {
|
|
135
|
-
if (!serverEndPoint) {
|
|
136
|
-
return ({ errors: 'Não há definição de API (serverEndPoint!' } as unknown) as AxiosResponse;
|
|
137
|
-
}
|
|
138
|
-
const url = `${serverEndPoint.api}/${path}/${id}/`;
|
|
139
|
-
try {
|
|
140
|
-
await axios.put(url, data);
|
|
141
|
-
return id;
|
|
142
|
-
} catch (e) {
|
|
143
|
-
if ((<any>e).isAxiosError) {
|
|
144
|
-
const err = <AxiosError>e;
|
|
145
|
-
if (err.response?.status === 401) {
|
|
146
|
-
const isRefreshed = await refreshToken(serverEndPoint);
|
|
147
|
-
if (!isRefreshed) {
|
|
148
|
-
console.log('Token expirou! Deve-se fazer login de novo');
|
|
149
|
-
return false;
|
|
150
|
-
}
|
|
151
|
-
try {
|
|
152
|
-
await axios.put(url, data);
|
|
153
|
-
return true;
|
|
154
|
-
} catch (e) {
|
|
155
|
-
console.log(`Error updating data at ${url}`, data, e);
|
|
156
|
-
return false;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
console.log(`Error updating data at ${url}`, data, err.response?.data);
|
|
160
|
-
return ({ errors: err.response?.data } as unknown) as AxiosResponse;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
};
|
|
164
|
-
|
|
165
|
-
export const partialUpdateData = async ({ path, serverEndPoint, data, id }: TargetApiParams) => {
|
|
166
|
-
if (!serverEndPoint) {
|
|
167
|
-
return ({ errors: 'Não há definição de API (serverEndPoint!' } as unknown) as AxiosResponse;
|
|
168
|
-
}
|
|
169
|
-
const url = `${serverEndPoint.api}/${path}/${id}/`;
|
|
170
|
-
try {
|
|
171
|
-
await axios.patch(url, data);
|
|
172
|
-
// DEBUG console.log({ path, data, id });
|
|
173
|
-
return id;
|
|
174
|
-
} catch (e) {
|
|
175
|
-
if ((<any>e).isAxiosError) {
|
|
176
|
-
const err = <AxiosError>e;
|
|
177
|
-
if (err.response?.status === 401) {
|
|
178
|
-
const isRefreshed = await refreshToken(serverEndPoint);
|
|
179
|
-
if (!isRefreshed) {
|
|
180
|
-
console.log('Token expirou! Deve-se fazer login de novo');
|
|
181
|
-
return false;
|
|
182
|
-
}
|
|
183
|
-
try {
|
|
184
|
-
await axios.patch(url, data);
|
|
185
|
-
return true;
|
|
186
|
-
} catch (e) {
|
|
187
|
-
console.log(`Error partial updating data at ${url}`, data, e);
|
|
188
|
-
return false;
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
console.log(`Error partial updating data at ${url}`, data, err.response?.data);
|
|
192
|
-
return ({ errors: err.response?.data } as unknown) as AxiosResponse;
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
};
|
|
196
|
-
|
|
197
|
-
export const createData = async ({ path, serverEndPoint, data }: Omit<TargetApiParams, 'id'>) => {
|
|
198
|
-
if (!serverEndPoint) {
|
|
199
|
-
return ({ errors: 'Não há definição de API (serverEndPoint!' } as unknown) as AxiosResponse;
|
|
200
|
-
}
|
|
201
|
-
const url = `${serverEndPoint.api}/${path}/`;
|
|
202
|
-
try {
|
|
203
|
-
const ret = await axios.post(url, data);
|
|
204
|
-
return ret;
|
|
205
|
-
} catch (e) {
|
|
206
|
-
if ((<any>e).isAxiosError) {
|
|
207
|
-
const err = <AxiosError>e;
|
|
208
|
-
if (err.response?.status === 401) {
|
|
209
|
-
const isRefreshed = await refreshToken(serverEndPoint);
|
|
210
|
-
if (!isRefreshed) {
|
|
211
|
-
console.log('Token expirou! Deve-se fazer login de novo');
|
|
212
|
-
return false;
|
|
213
|
-
}
|
|
214
|
-
try {
|
|
215
|
-
const ret = await axios.post(url, data);
|
|
216
|
-
return ret;
|
|
217
|
-
} catch (e) {
|
|
218
|
-
console.log(`Error creating data at ${url}`, data, e);
|
|
219
|
-
return false;
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
console.log(`Error creating data at ${url}`, data, err.response?.data);
|
|
223
|
-
return ({ errors: err.response?.data } as unknown) as AxiosResponse;
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
};
|
|
227
|
-
|
|
228
|
-
export const deleteData = async (path:string, serverEndPoint:serverEndPointType | null, id:Id) => {
|
|
229
|
-
if (!serverEndPoint) {
|
|
230
|
-
console.log ('Não há definição de API (serverEndPoint!');
|
|
231
|
-
return false;
|
|
232
|
-
}
|
|
233
|
-
const url = `${serverEndPoint.api}/${path}/${id}`;
|
|
234
|
-
try {
|
|
235
|
-
await axios.delete(url);
|
|
236
|
-
return true;
|
|
237
|
-
} catch (e) {
|
|
238
|
-
if ((<any>e).isAxiosError) {
|
|
239
|
-
const err = <AxiosError>e;
|
|
240
|
-
if (err.response?.status === 401) {
|
|
241
|
-
const isRefreshed = await refreshToken(serverEndPoint);
|
|
242
|
-
if (!isRefreshed) {
|
|
243
|
-
console.log('Token expirou! Deve-se fazer login de novo');
|
|
244
|
-
return false;
|
|
245
|
-
}
|
|
246
|
-
try {
|
|
247
|
-
await axios.delete(url);
|
|
248
|
-
return true;
|
|
249
|
-
} catch (e) {
|
|
250
|
-
console.log(`Error deleting data from ${url}`, e);
|
|
251
|
-
return false;
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
console.log(`Error deleting data from ${url}`, e);
|
|
255
|
-
return false;
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
};
|
|
259
|
-
|
|
260
|
-
export const createOrUpdateData = async ({ path, serverEndPoint, data, id }: TargetApiParamsOptionalId) => {
|
|
261
|
-
if (isTmpId(id)) {
|
|
262
|
-
id = null;
|
|
263
|
-
}
|
|
264
|
-
if (id) {
|
|
265
|
-
const responseUpdate = await updateData({ path, serverEndPoint, data, id });
|
|
266
|
-
return responseUpdate;
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
const responseCreate = await createData({ path, serverEndPoint, data });
|
|
270
|
-
if (!responseCreate || Object.prototype.hasOwnProperty.call(responseCreate, 'errors')) {
|
|
271
|
-
return responseCreate;
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
const responseUpdate = await updateData({ path, serverEndPoint, data, id: responseCreate.data.id });
|
|
275
|
-
return responseUpdate;
|
|
276
|
-
};
|
|
277
|
-
|
|
278
|
-
const prepareDataBySchema = ({
|
|
279
|
-
data,
|
|
280
|
-
schema,
|
|
281
|
-
parentIsField = false
|
|
282
|
-
}: {
|
|
283
|
-
data:Item,
|
|
284
|
-
schema:SchemaType,
|
|
285
|
-
parentIsField?:boolean
|
|
286
|
-
}) => {
|
|
287
|
-
// console.log('Entered prepareDataBySchema', schema);
|
|
288
|
-
const dbData:Item = {};
|
|
289
|
-
for (const [key, field] of Object.entries(schema)) {
|
|
290
|
-
// console.log(key, field, data);
|
|
291
|
-
if (!(key in data) || (key === 'id' && isTmpId(data[key]))) {
|
|
292
|
-
continue;
|
|
293
|
-
}
|
|
294
|
-
if (!data[key]) {
|
|
295
|
-
dbData[key] = emptyByType(field, true);
|
|
296
|
-
continue;
|
|
297
|
-
}
|
|
298
|
-
// console.log({ key, data: data[key] });
|
|
299
|
-
|
|
300
|
-
// OneToMany or ManyToMany relations:
|
|
301
|
-
if (field.type === 'field' && field.child) {
|
|
302
|
-
const dataArr:Item[] = data[key];
|
|
303
|
-
dbData[key] = dataArr.map(row => {
|
|
304
|
-
return (field.child?.children)
|
|
305
|
-
? prepareDataBySchema({
|
|
306
|
-
data: row,
|
|
307
|
-
schema: field.child.children,
|
|
308
|
-
parentIsField: true
|
|
309
|
-
})
|
|
310
|
-
: row;
|
|
311
|
-
});
|
|
312
|
-
continue;
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
// Nested Object:
|
|
316
|
-
if (field.type === 'nested object' && field.children && typeof data[key] === 'object') {
|
|
317
|
-
dbData[key] = (isTmpId(data[key].id))
|
|
318
|
-
? prepareDataBySchema({
|
|
319
|
-
data: data[key] as Item,
|
|
320
|
-
schema: field.children
|
|
321
|
-
})
|
|
322
|
-
: data[key].id;
|
|
323
|
-
continue;
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
// Choices:
|
|
327
|
-
if (field.type === 'choice') {
|
|
328
|
-
dbData[key] = data[key].value;
|
|
329
|
-
continue;
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
// Date:
|
|
333
|
-
if (field.type === 'date') {
|
|
334
|
-
const date = moment(data[key]);
|
|
335
|
-
dbData[key] = date.format('YYYY-MM-DD');
|
|
336
|
-
continue;
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
// DateTime:
|
|
340
|
-
if (field.type === 'datetime') {
|
|
341
|
-
const date = moment(data[key]);
|
|
342
|
-
dbData[key] = date.format('YYYY-MM-DDTHH:mm');
|
|
343
|
-
continue;
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
// Default:
|
|
347
|
-
dbData[key] = data[key];
|
|
348
|
-
}
|
|
349
|
-
return dbData;
|
|
350
|
-
};
|
|
351
|
-
|
|
352
|
-
export const updateDataBySchema = async ({
|
|
353
|
-
model,
|
|
354
|
-
modelObjectId,
|
|
355
|
-
serverEndPoint,
|
|
356
|
-
data,
|
|
357
|
-
schema,
|
|
358
|
-
path = null
|
|
359
|
-
}: {
|
|
360
|
-
model:string,
|
|
361
|
-
modelObjectId:Id,
|
|
362
|
-
serverEndPoint:serverEndPointType | null,
|
|
363
|
-
data:Item,
|
|
364
|
-
schema:SchemaType,
|
|
365
|
-
path:string | null
|
|
366
|
-
}) => {
|
|
367
|
-
// console.log({
|
|
368
|
-
// model,
|
|
369
|
-
// modelObjectId,
|
|
370
|
-
// data,
|
|
371
|
-
// schema
|
|
372
|
-
// });
|
|
373
|
-
const dbData:Item = prepareDataBySchema({ data, schema });
|
|
374
|
-
|
|
375
|
-
if (!path) {
|
|
376
|
-
path = model;
|
|
377
|
-
}
|
|
378
|
-
// DEBUG console.log({ model, modelObjectId, path, data, dbData });
|
|
379
|
-
const response = await createOrUpdateData({
|
|
380
|
-
path,
|
|
381
|
-
serverEndPoint,
|
|
382
|
-
data: dbData,
|
|
383
|
-
id: modelObjectId
|
|
384
|
-
});
|
|
385
|
-
|
|
386
|
-
if (typeof response !== 'object' && parseInt(response as string)) {
|
|
387
|
-
return response;
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
console.log('ERRO MAIN', response);
|
|
391
|
-
return response;
|
|
392
|
-
};
|
|
393
|
-
|
|
394
|
-
export const addExistingRelatedModel = async ({
|
|
395
|
-
model,
|
|
396
|
-
serverEndPoint,
|
|
397
|
-
id,
|
|
398
|
-
data
|
|
399
|
-
}: {
|
|
400
|
-
model:string,
|
|
401
|
-
serverEndPoint:serverEndPointType | null,
|
|
402
|
-
id:Id,
|
|
403
|
-
data:Item
|
|
404
|
-
}) => {
|
|
405
|
-
const response = await partialUpdateData({ path: model, serverEndPoint, data, id });
|
|
406
|
-
// DEBUG console.log({ model, id, data, response });
|
|
407
|
-
return response;
|
|
408
|
-
};
|
|
409
|
-
|
|
410
|
-
const getDataGridColumns = (
|
|
411
|
-
schema:SchemaType,
|
|
412
|
-
columnFields:string[] = [],
|
|
413
|
-
hiddenFields:string[] = [],
|
|
414
|
-
creatableFields:string[] = []
|
|
415
|
-
) => {
|
|
416
|
-
if (!schema) {
|
|
417
|
-
return false;
|
|
418
|
-
}
|
|
419
|
-
columnFields = (!columnFields || columnFields.length === 0)
|
|
420
|
-
? Object.keys(schema)
|
|
421
|
-
: columnFields.filter(field => {
|
|
422
|
-
return Object.prototype.hasOwnProperty.call(schema, field);
|
|
423
|
-
});
|
|
424
|
-
|
|
425
|
-
return columnFields.map(field => {
|
|
426
|
-
const column = {
|
|
427
|
-
field,
|
|
428
|
-
headerName: schema[field].label,
|
|
429
|
-
hide: (hiddenFields.includes(field)),
|
|
430
|
-
creatable: (creatableFields.includes(field)),
|
|
431
|
-
width: 100
|
|
432
|
-
};
|
|
433
|
-
return column;
|
|
434
|
-
});
|
|
435
|
-
};
|
|
436
|
-
|
|
437
|
-
export const getAutoComplete = async ({
|
|
438
|
-
model,
|
|
439
|
-
serverEndPoint
|
|
440
|
-
}: {
|
|
441
|
-
model: string,
|
|
442
|
-
serverEndPoint:serverEndPointType | null
|
|
443
|
-
}) => {
|
|
444
|
-
const data = await getData(model, serverEndPoint, 'autocomplete');
|
|
445
|
-
return data;
|
|
446
|
-
};
|
|
447
|
-
|
|
448
|
-
export const getJSONSchema = async ({
|
|
449
|
-
model,
|
|
450
|
-
serverEndPoint,
|
|
451
|
-
id = 'create'
|
|
452
|
-
}: {
|
|
453
|
-
model:string,
|
|
454
|
-
serverEndPoint:serverEndPointType | null,
|
|
455
|
-
id?:Id
|
|
456
|
-
}) => {
|
|
457
|
-
if (!serverEndPoint) {
|
|
458
|
-
console.log('Não há definição de API (serverEndPoint!');
|
|
459
|
-
return false;
|
|
460
|
-
}
|
|
461
|
-
const url = `${serverEndPoint.JSONSchema}/${model}/${id}/`;
|
|
462
|
-
try {
|
|
463
|
-
const { data } = await axios.get(url);
|
|
464
|
-
return data;
|
|
465
|
-
} catch (e) {
|
|
466
|
-
if ((<any>e).isAxiosError) {
|
|
467
|
-
const err = <AxiosError>e;
|
|
468
|
-
if (err.response?.status === 401) {
|
|
469
|
-
const isRefreshed = await refreshToken(serverEndPoint);
|
|
470
|
-
if (!isRefreshed) {
|
|
471
|
-
console.log('Token expirou! Deve-se fazer login de novo');
|
|
472
|
-
return false;
|
|
473
|
-
}
|
|
474
|
-
try {
|
|
475
|
-
const { data } = await axios.get(url);
|
|
476
|
-
return data;
|
|
477
|
-
} catch (e) {
|
|
478
|
-
console.log(`Error fetching JSONSchema data from ${url}`, e);
|
|
479
|
-
return false;
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
console.log(`Error fetching JSONSchema data from ${url}`, e);
|
|
483
|
-
return false;
|
|
484
|
-
}
|
|
485
|
-
}
|
|
486
|
-
};
|
|
487
|
-
|
|
488
|
-
export const createOrUpdateJSONSchema = async ({
|
|
489
|
-
model,
|
|
490
|
-
serverEndPoint,
|
|
491
|
-
id = 'create',
|
|
492
|
-
formData
|
|
493
|
-
}: {
|
|
494
|
-
model:string,
|
|
495
|
-
serverEndPoint:serverEndPointType | null,
|
|
496
|
-
id?:Id,
|
|
497
|
-
formData:Item
|
|
498
|
-
}) => {
|
|
499
|
-
if (!serverEndPoint) {
|
|
500
|
-
return { errors: 'Não há definição de API (serverEndPoint!' };
|
|
501
|
-
}
|
|
502
|
-
let url = `${serverEndPoint.JSONSchema}/${model}/`;
|
|
503
|
-
if (id !== 'create') {
|
|
504
|
-
url += `${id}/`;
|
|
505
|
-
}
|
|
506
|
-
try {
|
|
507
|
-
const { data } = (id === 'create')
|
|
508
|
-
? await axios.post(url, formData)
|
|
509
|
-
: await axios.patch(url, formData);
|
|
510
|
-
return data;
|
|
511
|
-
} catch (e) {
|
|
512
|
-
if ((<any>e).isAxiosError) {
|
|
513
|
-
const err = <AxiosError>e;
|
|
514
|
-
if (err.response?.status === 401) {
|
|
515
|
-
const isRefreshed = await refreshToken(serverEndPoint);
|
|
516
|
-
if (!isRefreshed) {
|
|
517
|
-
console.log('Token expirou! Deve-se fazer login de novo');
|
|
518
|
-
return { errors: 'Token expirou! Deve-se fazer login de novo!' };
|
|
519
|
-
}
|
|
520
|
-
try {
|
|
521
|
-
const { data } = (id === 'create')
|
|
522
|
-
? await axios.post(url, formData)
|
|
523
|
-
: await axios.patch(url, formData);
|
|
524
|
-
return data;
|
|
525
|
-
} catch (e) {
|
|
526
|
-
console.log(`Error partially updating or creating JSONSchema data from ${url}`, e);
|
|
527
|
-
return { errors: 'Erro ao salvar alterações em item!' };
|
|
528
|
-
}
|
|
529
|
-
}
|
|
530
|
-
console.log(`Error partially updating or creating JSONSchema data from ${url}`, e);
|
|
531
|
-
return { errors: 'Erro ao salvar alterações em item!' };
|
|
532
|
-
}
|
|
533
|
-
}
|
|
534
|
-
};
|
|
535
|
-
|
|
536
|
-
export const loginByPayload = async (payload:Item, serverEndPoint:serverEndPointType | null) => {
|
|
537
|
-
if (!serverEndPoint) {
|
|
538
|
-
console.log('Não há definição de API (serverEndPoint!');
|
|
539
|
-
return false;
|
|
540
|
-
}
|
|
541
|
-
const url = serverEndPoint.getToken;
|
|
542
|
-
if (!url) {
|
|
543
|
-
console.log('Erro no loginByPayload: faltou a configuração de url getToken!');
|
|
544
|
-
return false;
|
|
545
|
-
}
|
|
546
|
-
try {
|
|
547
|
-
const { data } = await axios.post(url, payload);
|
|
548
|
-
localStorage.setItem('refreshToken', data.refresh);
|
|
549
|
-
setAuthToken(data.access);
|
|
550
|
-
return true;
|
|
551
|
-
} catch (e) {
|
|
552
|
-
console.log('Erro no loginByPayload!', e);
|
|
553
|
-
setAuthToken(null);
|
|
554
|
-
return false;
|
|
555
|
-
}
|
|
556
|
-
};
|
|
557
|
-
|
|
558
|
-
export const setAuthToken = (token:string | null) => {
|
|
559
|
-
if (token) {
|
|
560
|
-
localStorage.setItem('token', token);
|
|
561
|
-
axios.defaults.headers.common.Authorization = `Bearer ${token}`;
|
|
562
|
-
return;
|
|
563
|
-
}
|
|
564
|
-
localStorage.removeItem('token');
|
|
565
|
-
localStorage.removeItem('refreshToken');
|
|
566
|
-
delete axios.defaults.headers.common.Authorization;
|
|
567
|
-
};
|
|
568
|
-
|
|
569
|
-
const refreshToken = async (serverEndPoint:serverEndPointType | null) => {
|
|
570
|
-
if (!serverEndPoint) {
|
|
571
|
-
console.log('Não há definição de API (serverEndPoint!');
|
|
572
|
-
return false;
|
|
573
|
-
}
|
|
574
|
-
const refreshToken = localStorage.getItem('refreshToken');
|
|
575
|
-
// console.log('entrou refreshToken', refreshToken);
|
|
576
|
-
setAuthToken(null);
|
|
577
|
-
if (!refreshToken) {
|
|
578
|
-
return false;
|
|
579
|
-
}
|
|
580
|
-
try {
|
|
581
|
-
const { data } = await axios.post(`${serverEndPoint.refreshToken}`, {
|
|
582
|
-
refresh: refreshToken
|
|
583
|
-
});
|
|
584
|
-
setAuthToken(data.access);
|
|
585
|
-
return true;
|
|
586
|
-
} catch (e) {
|
|
587
|
-
console.log('Failed refreshing token', e);
|
|
588
|
-
return false;
|
|
589
|
-
}
|
|
590
|
-
};
|
|
591
|
-
|
|
592
|
-
export const isLoggedIn = async (serverEndPoint:serverEndPointType | null) => {
|
|
593
|
-
const token = localStorage.getItem('token');
|
|
594
|
-
setAuthToken(token);
|
|
595
|
-
if (!token) {
|
|
596
|
-
return false;
|
|
597
|
-
}
|
|
598
|
-
const usuaria = await getData('minhaconta', serverEndPoint);
|
|
599
|
-
if (!usuaria) {
|
|
600
|
-
console.log('Erro ao recuperar dados de usuária!');
|
|
601
|
-
return false;
|
|
602
|
-
}
|
|
603
|
-
return usuaria;
|
|
604
|
-
};
|
|
605
|
-
|
|
606
|
-
export interface getGenericModelListProps {
|
|
607
|
-
model:string;
|
|
608
|
-
serverEndPoint:serverEndPointType | null;
|
|
609
|
-
id?:Id;
|
|
610
|
-
relatedModel?:string;
|
|
611
|
-
relatedModelId?:Id;
|
|
612
|
-
columnFields:string[];
|
|
613
|
-
hiddenFields?:string[];
|
|
614
|
-
creatableFields?:string[];
|
|
615
|
-
isInBatches?:boolean;
|
|
616
|
-
loadedSchema?:SchemaType | boolean;
|
|
617
|
-
};
|
|
618
|
-
|
|
619
|
-
export interface DataSchemaColumnsType {
|
|
620
|
-
data: Item[];
|
|
621
|
-
schema?: SchemaType;
|
|
622
|
-
columns?: GridEnrichedBySchemaColDef[];
|
|
623
|
-
}
|
|
624
|
-
export const getGenericModelList = async ({
|
|
625
|
-
model,
|
|
626
|
-
serverEndPoint,
|
|
627
|
-
id = '',
|
|
628
|
-
relatedModel = '',
|
|
629
|
-
relatedModelId = '',
|
|
630
|
-
columnFields,
|
|
631
|
-
hiddenFields = ['id'],
|
|
632
|
-
creatableFields = [],
|
|
633
|
-
isInBatches = false,
|
|
634
|
-
loadedSchema
|
|
635
|
-
}: getGenericModelListProps) => {
|
|
636
|
-
let path = `${model}/${id}`;
|
|
637
|
-
let schemaPath = model;
|
|
638
|
-
let schema = loadedSchema;
|
|
639
|
-
let columns;
|
|
640
|
-
if (!isTmpId(id) && relatedModel) {
|
|
641
|
-
path += `/${relatedModel}/${relatedModelId}`;
|
|
642
|
-
schemaPath += `/${id}/${relatedModel}`;
|
|
643
|
-
}
|
|
644
|
-
|
|
645
|
-
// Only get schema and columns if not in batches or in first batch:
|
|
646
|
-
if (!schema) {
|
|
647
|
-
schema = await getSchema(schemaPath, serverEndPoint);
|
|
648
|
-
if (!schema) {
|
|
649
|
-
return false;
|
|
650
|
-
}
|
|
651
|
-
columns = getDataGridColumns(
|
|
652
|
-
schema,
|
|
653
|
-
columnFields,
|
|
654
|
-
hiddenFields,
|
|
655
|
-
creatableFields
|
|
656
|
-
);
|
|
657
|
-
if (!columns) {
|
|
658
|
-
return false;
|
|
659
|
-
}
|
|
660
|
-
}
|
|
661
|
-
|
|
662
|
-
let data:Item[] = [];
|
|
663
|
-
|
|
664
|
-
if (!id || (id && !relatedModelId)) {
|
|
665
|
-
if (isInBatches) {
|
|
666
|
-
path += (loadedSchema)
|
|
667
|
-
? '?is_last_batch=1'
|
|
668
|
-
: '?is_first_batch=1';
|
|
669
|
-
}
|
|
670
|
-
const ret = await getData(path, serverEndPoint);
|
|
671
|
-
if (ret === false) {
|
|
672
|
-
return false;
|
|
673
|
-
}
|
|
674
|
-
|
|
675
|
-
data = ret.map(row => {
|
|
676
|
-
const newRow:Item = {};
|
|
677
|
-
for (const [key, field] of Object.entries(schema as SchemaType)) {
|
|
678
|
-
if (!(key in row)) {
|
|
679
|
-
continue;
|
|
680
|
-
}
|
|
681
|
-
if (field.type === 'choice') {
|
|
682
|
-
newRow[key] = row[key]
|
|
683
|
-
? {
|
|
684
|
-
value: row[key],
|
|
685
|
-
display_name: getChoiceByValue(row[key], field.choices)
|
|
686
|
-
}
|
|
687
|
-
: emptyByType(field);
|
|
688
|
-
continue;
|
|
689
|
-
}
|
|
690
|
-
newRow[key] = (row[key])
|
|
691
|
-
? row[key]
|
|
692
|
-
: emptyByType(field);
|
|
693
|
-
}
|
|
694
|
-
return newRow;
|
|
695
|
-
});
|
|
696
|
-
}
|
|
697
|
-
if (loadedSchema) {
|
|
698
|
-
// DEBUG console.log({ path, data });
|
|
699
|
-
return { data } as DataSchemaColumnsType;
|
|
700
|
-
}
|
|
701
|
-
// DEBUG console.log({ path, data, columns, schema });
|
|
702
|
-
return { data, columns, schema } as DataSchemaColumnsType;
|
|
703
|
-
};
|
|
704
|
-
|
|
705
|
-
export const getGenericModel = async ({
|
|
706
|
-
model,
|
|
707
|
-
serverEndPoint,
|
|
708
|
-
id = '',
|
|
709
|
-
relatedModel = '',
|
|
710
|
-
relatedModelId = ''
|
|
711
|
-
}: {
|
|
712
|
-
model:string,
|
|
713
|
-
serverEndPoint:serverEndPointType | null,
|
|
714
|
-
id?:Id,
|
|
715
|
-
relatedModel?:string,
|
|
716
|
-
relatedModelId?:string
|
|
717
|
-
}) => {
|
|
718
|
-
let path = `${model}/${id}`;
|
|
719
|
-
let schemaPath = model;
|
|
720
|
-
if (id && relatedModel) {
|
|
721
|
-
path += `/${relatedModel}/${relatedModelId}`;
|
|
722
|
-
schemaPath += `/${id}/${relatedModel}`;
|
|
723
|
-
}
|
|
724
|
-
const schema = await getSchema(schemaPath, serverEndPoint);
|
|
725
|
-
if (!schema) {
|
|
726
|
-
return false;
|
|
727
|
-
}
|
|
728
|
-
const data = (!id || (id && relatedModel && !relatedModelId))
|
|
729
|
-
? {}
|
|
730
|
-
: await getData(path, serverEndPoint);
|
|
731
|
-
// console.log({ schema, data }); // DEBUG
|
|
732
|
-
return { schema, data };
|
|
733
|
-
};
|