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