next-recomponents 1.5.91 → 1.5.94

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -89,7 +89,7 @@ interface Props$1 {
89
89
  onError?: (error: any) => void;
90
90
  }
91
91
 
92
- declare function useResources<T extends Record<string, ItemsRecord>>({ baseURI, endpoints, onError, }: Props$1): any;
92
+ declare function useResources<T extends Record<string, ItemsRecord>>({ baseURI, endpoints, onError, }: Props$1): Record<string, any>;
93
93
 
94
94
  interface CustomSelectProps extends React$1.DetailedHTMLProps<React$1.InputHTMLAttributes<HTMLInputElement>, HTMLInputElement> {
95
95
  label?: string;
package/dist/index.d.ts CHANGED
@@ -89,7 +89,7 @@ interface Props$1 {
89
89
  onError?: (error: any) => void;
90
90
  }
91
91
 
92
- declare function useResources<T extends Record<string, ItemsRecord>>({ baseURI, endpoints, onError, }: Props$1): any;
92
+ declare function useResources<T extends Record<string, ItemsRecord>>({ baseURI, endpoints, onError, }: Props$1): Record<string, any>;
93
93
 
94
94
  interface CustomSelectProps extends React$1.DetailedHTMLProps<React$1.InputHTMLAttributes<HTMLInputElement>, HTMLInputElement> {
95
95
  label?: string;
package/dist/index.js CHANGED
@@ -32290,21 +32290,12 @@ function useResources({
32290
32290
  onError
32291
32291
  }) {
32292
32292
  const token = useToken();
32293
- const [info, setInfo] = (0, import_react28.useState)({});
32294
- const result = (0, import_react28.useMemo)(() => {
32295
- return Object.keys(endpoints).reduce((acc, key) => __async(null, null, function* () {
32296
- var _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
32293
+ const [info, setInfo] = (0, import_react28.useState)(
32294
+ Object.keys(endpoints).reduce((acc, key) => {
32295
+ var _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
32297
32296
  const endpoint = endpoints[key];
32298
- const showFunc = (_a) => __async(null, null, function* () {
32299
- var _b = _a, {
32300
- limit = 10,
32301
- page = 1,
32302
- merge = true
32303
- } = _b, query = __objRest(_b, [
32304
- "limit",
32305
- "page",
32306
- "merge"
32307
- ]);
32297
+ const showFunc = (_a, autoLoad = false) => __async(null, null, function* () {
32298
+ var _b = _a, { limit = 10, page = 1, merge = true } = _b, query = __objRest(_b, ["limit", "page", "merge"]);
32308
32299
  const options = {
32309
32300
  method: "GET",
32310
32301
  url: `${baseURI}/${key}`,
@@ -32323,10 +32314,13 @@ function useResources({
32323
32314
  const d = consulta.data;
32324
32315
  newInfo[key].state = "success";
32325
32316
  newInfo[key].errorMessage = "";
32326
- newInfo[key].data = merge ? page == 1 ? d.data : [...d.data, ...newInfo[key].data || []] : d.data;
32327
- newInfo[key].totalItems = d.totalItems;
32328
- newInfo[key].totalPages = d.totalPages;
32329
- newInfo[key].currentPage = d.currentPage;
32317
+ if (autoLoad) {
32318
+ } else {
32319
+ newInfo[key].data = merge ? page == 1 ? d.data : [...d.data, ...newInfo[key].data || []] : d.data;
32320
+ newInfo[key].totalItems = d.totalItems;
32321
+ newInfo[key].totalPages = d.totalPages;
32322
+ newInfo[key].currentPage = d.currentPage;
32323
+ }
32330
32324
  setInfo(__spreadValues({}, newInfo));
32331
32325
  return d.data;
32332
32326
  } catch (error) {
@@ -32340,29 +32334,28 @@ function useResources({
32340
32334
  return error;
32341
32335
  }
32342
32336
  });
32343
- acc[key] = __spreadProps(__spreadValues({}, endpoint), {
32344
- loaded: false,
32345
- show: showFunc,
32346
- find: (id3, query) => __async(null, null, function* () {
32347
- var _a, _b, _c2;
32348
- const options = {
32349
- method: "GET",
32350
- url: `${baseURI}/${key}/${id3}`,
32351
- params: __spreadValues({}, query),
32352
- headers: { Authorization: token }
32353
- };
32354
- const newInfo = __spreadValues({}, info);
32355
- newInfo[key] = newInfo[key] || {};
32356
- newInfo[key].state = "loading";
32337
+ const findFunc = (id3, query, autoLoad = false) => __async(null, null, function* () {
32338
+ var _a, _b, _c2;
32339
+ const options = {
32340
+ method: "GET",
32341
+ url: `${baseURI}/${key}/${id3}`,
32342
+ params: __spreadValues({}, query),
32343
+ headers: { Authorization: token }
32344
+ };
32345
+ const newInfo = __spreadValues({}, info);
32346
+ newInfo[key] = newInfo[key] || {};
32347
+ newInfo[key].state = "loading";
32348
+ newInfo[key].errorMessage = "";
32349
+ newInfo[key].params = query;
32350
+ newInfo[key].loader = true;
32351
+ setInfo(newInfo);
32352
+ try {
32353
+ const consulta = yield import_axios.default.request(options);
32354
+ const d = consulta.data;
32355
+ newInfo[key].state = "success";
32357
32356
  newInfo[key].errorMessage = "";
32358
- newInfo[key].params = query;
32359
- newInfo[key].loader = true;
32360
- setInfo(newInfo);
32361
- try {
32362
- const consulta = yield import_axios.default.request(options);
32363
- const d = consulta.data;
32364
- newInfo[key].state = "success";
32365
- newInfo[key].errorMessage = "";
32357
+ if (autoLoad) {
32358
+ } else {
32366
32359
  newInfo[key].selectedItem = d;
32367
32360
  const index = (_b = (_a = newInfo[key]) == null ? void 0 : _a.data) == null ? void 0 : _b.findIndex(
32368
32361
  (d2) => (d2 == null ? void 0 : d2.id) == (d2 == null ? void 0 : d2.id)
@@ -32376,19 +32369,24 @@ function useResources({
32376
32369
  newInfo[key].data = [d];
32377
32370
  }
32378
32371
  }
32379
- setInfo(__spreadValues({}, newInfo));
32380
- return d;
32381
- } catch (error) {
32382
- const item = http_codes_default.find((s) => s.code == error.status);
32383
- newInfo[key].state = "error";
32384
- newInfo[key].errorMessage = (item == null ? void 0 : item.meaning) || error.message;
32385
- if (error.status == 403) {
32386
- onError == null ? void 0 : onError(__spreadValues({ error }, { errorMessage: item == null ? void 0 : item.meaning }));
32387
- }
32388
- setInfo(__spreadValues({}, newInfo));
32389
- return error;
32390
32372
  }
32391
- }),
32373
+ setInfo(__spreadValues({}, newInfo));
32374
+ return d;
32375
+ } catch (error) {
32376
+ const item = http_codes_default.find((s) => s.code == error.status);
32377
+ newInfo[key].state = "error";
32378
+ newInfo[key].errorMessage = (item == null ? void 0 : item.meaning) || error.message;
32379
+ if (error.status == 403) {
32380
+ onError == null ? void 0 : onError(__spreadValues({ error }, { errorMessage: item == null ? void 0 : item.meaning }));
32381
+ }
32382
+ setInfo(__spreadValues({}, newInfo));
32383
+ return error;
32384
+ }
32385
+ });
32386
+ acc[key] = __spreadProps(__spreadValues({}, endpoint), {
32387
+ loaded: !((_c = acc[key]) == null ? void 0 : _c.loaded) ? ((_d = acc[key]) == null ? void 0 : _d.id) ? findFunc(acc[key].id, (_e = acc[key]) == null ? void 0 : _e.defaultParams) : ((_f = acc[key]) == null ? void 0 : _f.defaultParams) ? showFunc(acc[key].defaultParams) : true : true,
32388
+ show: showFunc,
32389
+ find: findFunc,
32392
32390
  create: (data) => __async(null, null, function* () {
32393
32391
  var _a, _b, _c2, _d2, _e2, _f2;
32394
32392
  const options = {
@@ -32531,28 +32529,21 @@ function useResources({
32531
32529
  return error;
32532
32530
  }
32533
32531
  }),
32534
- totalPages: (_c = info[key]) == null ? void 0 : _c.totalPages,
32535
- currentPage: (_d = info[key]) == null ? void 0 : _d.currentPage,
32536
- state: ((_e = info[key]) == null ? void 0 : _e.state) || "success",
32537
- errorMessage: (_f = info[key]) == null ? void 0 : _f.errorMessage,
32538
- params: (_g = info[key]) == null ? void 0 : _g.params,
32532
+ totalPages: (_g = info[key]) == null ? void 0 : _g.totalPages,
32533
+ currentPage: (_h = info[key]) == null ? void 0 : _h.currentPage,
32534
+ state: ((_i = info[key]) == null ? void 0 : _i.state) || "success",
32535
+ errorMessage: (_j = info[key]) == null ? void 0 : _j.errorMessage,
32536
+ params: (_k = info[key]) == null ? void 0 : _k.params,
32539
32537
  getAllPages: (limit = 10) => __async(null, null, function* () {
32540
32538
  console.log("Not aviable");
32541
32539
  }),
32542
- data: ((_h = info[key]) == null ? void 0 : _h.data) || [],
32543
- selectedItem: ((_i = info[key]) == null ? void 0 : _i.selectedItem) || {}
32540
+ data: ((_l = info[key]) == null ? void 0 : _l.data) || [],
32541
+ selectedItem: ((_m = info[key]) == null ? void 0 : _m.selectedItem) || {}
32544
32542
  });
32545
- if (!acc[key].loaded) {
32546
- if ((_j = acc[key]) == null ? void 0 : _j.id) {
32547
- yield acc[key].find(acc[key].id, (_k = acc[key]) == null ? void 0 : _k.defaultParams);
32548
- } else if ((_l = acc[key]) == null ? void 0 : _l.defaultParams) {
32549
- yield acc[key].show(acc[key].defaultParams);
32550
- }
32551
- }
32552
32543
  return acc;
32553
- }), {});
32554
- }, [info, token, endpoints]);
32555
- return result;
32544
+ }, {})
32545
+ );
32546
+ return info;
32556
32547
  }
32557
32548
 
32558
32549
  // src/select/index.tsx
package/dist/index.mjs CHANGED
@@ -32183,7 +32183,7 @@ function TextArea(_a) {
32183
32183
  }
32184
32184
 
32185
32185
  // src/use-resources/index.ts
32186
- import { useMemo as useMemo9, useState as useState8 } from "react";
32186
+ import { useState as useState8 } from "react";
32187
32187
 
32188
32188
  // src/use-resources/get.token.tsx
32189
32189
  import { useMemo as useMemo8 } from "react";
@@ -32280,21 +32280,12 @@ function useResources({
32280
32280
  onError
32281
32281
  }) {
32282
32282
  const token = useToken();
32283
- const [info, setInfo] = useState8({});
32284
- const result = useMemo9(() => {
32285
- return Object.keys(endpoints).reduce((acc, key) => __async(null, null, function* () {
32286
- var _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
32283
+ const [info, setInfo] = useState8(
32284
+ Object.keys(endpoints).reduce((acc, key) => {
32285
+ var _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
32287
32286
  const endpoint = endpoints[key];
32288
- const showFunc = (_a) => __async(null, null, function* () {
32289
- var _b = _a, {
32290
- limit = 10,
32291
- page = 1,
32292
- merge = true
32293
- } = _b, query = __objRest(_b, [
32294
- "limit",
32295
- "page",
32296
- "merge"
32297
- ]);
32287
+ const showFunc = (_a, autoLoad = false) => __async(null, null, function* () {
32288
+ var _b = _a, { limit = 10, page = 1, merge = true } = _b, query = __objRest(_b, ["limit", "page", "merge"]);
32298
32289
  const options = {
32299
32290
  method: "GET",
32300
32291
  url: `${baseURI}/${key}`,
@@ -32313,10 +32304,13 @@ function useResources({
32313
32304
  const d = consulta.data;
32314
32305
  newInfo[key].state = "success";
32315
32306
  newInfo[key].errorMessage = "";
32316
- newInfo[key].data = merge ? page == 1 ? d.data : [...d.data, ...newInfo[key].data || []] : d.data;
32317
- newInfo[key].totalItems = d.totalItems;
32318
- newInfo[key].totalPages = d.totalPages;
32319
- newInfo[key].currentPage = d.currentPage;
32307
+ if (autoLoad) {
32308
+ } else {
32309
+ newInfo[key].data = merge ? page == 1 ? d.data : [...d.data, ...newInfo[key].data || []] : d.data;
32310
+ newInfo[key].totalItems = d.totalItems;
32311
+ newInfo[key].totalPages = d.totalPages;
32312
+ newInfo[key].currentPage = d.currentPage;
32313
+ }
32320
32314
  setInfo(__spreadValues({}, newInfo));
32321
32315
  return d.data;
32322
32316
  } catch (error) {
@@ -32330,29 +32324,28 @@ function useResources({
32330
32324
  return error;
32331
32325
  }
32332
32326
  });
32333
- acc[key] = __spreadProps(__spreadValues({}, endpoint), {
32334
- loaded: false,
32335
- show: showFunc,
32336
- find: (id3, query) => __async(null, null, function* () {
32337
- var _a, _b, _c2;
32338
- const options = {
32339
- method: "GET",
32340
- url: `${baseURI}/${key}/${id3}`,
32341
- params: __spreadValues({}, query),
32342
- headers: { Authorization: token }
32343
- };
32344
- const newInfo = __spreadValues({}, info);
32345
- newInfo[key] = newInfo[key] || {};
32346
- newInfo[key].state = "loading";
32327
+ const findFunc = (id3, query, autoLoad = false) => __async(null, null, function* () {
32328
+ var _a, _b, _c2;
32329
+ const options = {
32330
+ method: "GET",
32331
+ url: `${baseURI}/${key}/${id3}`,
32332
+ params: __spreadValues({}, query),
32333
+ headers: { Authorization: token }
32334
+ };
32335
+ const newInfo = __spreadValues({}, info);
32336
+ newInfo[key] = newInfo[key] || {};
32337
+ newInfo[key].state = "loading";
32338
+ newInfo[key].errorMessage = "";
32339
+ newInfo[key].params = query;
32340
+ newInfo[key].loader = true;
32341
+ setInfo(newInfo);
32342
+ try {
32343
+ const consulta = yield axios.request(options);
32344
+ const d = consulta.data;
32345
+ newInfo[key].state = "success";
32347
32346
  newInfo[key].errorMessage = "";
32348
- newInfo[key].params = query;
32349
- newInfo[key].loader = true;
32350
- setInfo(newInfo);
32351
- try {
32352
- const consulta = yield axios.request(options);
32353
- const d = consulta.data;
32354
- newInfo[key].state = "success";
32355
- newInfo[key].errorMessage = "";
32347
+ if (autoLoad) {
32348
+ } else {
32356
32349
  newInfo[key].selectedItem = d;
32357
32350
  const index = (_b = (_a = newInfo[key]) == null ? void 0 : _a.data) == null ? void 0 : _b.findIndex(
32358
32351
  (d2) => (d2 == null ? void 0 : d2.id) == (d2 == null ? void 0 : d2.id)
@@ -32366,19 +32359,24 @@ function useResources({
32366
32359
  newInfo[key].data = [d];
32367
32360
  }
32368
32361
  }
32369
- setInfo(__spreadValues({}, newInfo));
32370
- return d;
32371
- } catch (error) {
32372
- const item = http_codes_default.find((s) => s.code == error.status);
32373
- newInfo[key].state = "error";
32374
- newInfo[key].errorMessage = (item == null ? void 0 : item.meaning) || error.message;
32375
- if (error.status == 403) {
32376
- onError == null ? void 0 : onError(__spreadValues({ error }, { errorMessage: item == null ? void 0 : item.meaning }));
32377
- }
32378
- setInfo(__spreadValues({}, newInfo));
32379
- return error;
32380
32362
  }
32381
- }),
32363
+ setInfo(__spreadValues({}, newInfo));
32364
+ return d;
32365
+ } catch (error) {
32366
+ const item = http_codes_default.find((s) => s.code == error.status);
32367
+ newInfo[key].state = "error";
32368
+ newInfo[key].errorMessage = (item == null ? void 0 : item.meaning) || error.message;
32369
+ if (error.status == 403) {
32370
+ onError == null ? void 0 : onError(__spreadValues({ error }, { errorMessage: item == null ? void 0 : item.meaning }));
32371
+ }
32372
+ setInfo(__spreadValues({}, newInfo));
32373
+ return error;
32374
+ }
32375
+ });
32376
+ acc[key] = __spreadProps(__spreadValues({}, endpoint), {
32377
+ loaded: !((_c = acc[key]) == null ? void 0 : _c.loaded) ? ((_d = acc[key]) == null ? void 0 : _d.id) ? findFunc(acc[key].id, (_e = acc[key]) == null ? void 0 : _e.defaultParams) : ((_f = acc[key]) == null ? void 0 : _f.defaultParams) ? showFunc(acc[key].defaultParams) : true : true,
32378
+ show: showFunc,
32379
+ find: findFunc,
32382
32380
  create: (data) => __async(null, null, function* () {
32383
32381
  var _a, _b, _c2, _d2, _e2, _f2;
32384
32382
  const options = {
@@ -32521,28 +32519,21 @@ function useResources({
32521
32519
  return error;
32522
32520
  }
32523
32521
  }),
32524
- totalPages: (_c = info[key]) == null ? void 0 : _c.totalPages,
32525
- currentPage: (_d = info[key]) == null ? void 0 : _d.currentPage,
32526
- state: ((_e = info[key]) == null ? void 0 : _e.state) || "success",
32527
- errorMessage: (_f = info[key]) == null ? void 0 : _f.errorMessage,
32528
- params: (_g = info[key]) == null ? void 0 : _g.params,
32522
+ totalPages: (_g = info[key]) == null ? void 0 : _g.totalPages,
32523
+ currentPage: (_h = info[key]) == null ? void 0 : _h.currentPage,
32524
+ state: ((_i = info[key]) == null ? void 0 : _i.state) || "success",
32525
+ errorMessage: (_j = info[key]) == null ? void 0 : _j.errorMessage,
32526
+ params: (_k = info[key]) == null ? void 0 : _k.params,
32529
32527
  getAllPages: (limit = 10) => __async(null, null, function* () {
32530
32528
  console.log("Not aviable");
32531
32529
  }),
32532
- data: ((_h = info[key]) == null ? void 0 : _h.data) || [],
32533
- selectedItem: ((_i = info[key]) == null ? void 0 : _i.selectedItem) || {}
32530
+ data: ((_l = info[key]) == null ? void 0 : _l.data) || [],
32531
+ selectedItem: ((_m = info[key]) == null ? void 0 : _m.selectedItem) || {}
32534
32532
  });
32535
- if (!acc[key].loaded) {
32536
- if ((_j = acc[key]) == null ? void 0 : _j.id) {
32537
- yield acc[key].find(acc[key].id, (_k = acc[key]) == null ? void 0 : _k.defaultParams);
32538
- } else if ((_l = acc[key]) == null ? void 0 : _l.defaultParams) {
32539
- yield acc[key].show(acc[key].defaultParams);
32540
- }
32541
- }
32542
32533
  return acc;
32543
- }), {});
32544
- }, [info, token, endpoints]);
32545
- return result;
32534
+ }, {})
32535
+ );
32536
+ return info;
32546
32537
  }
32547
32538
 
32548
32539
  // src/select/index.tsx
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-recomponents",
3
- "version": "1.5.91",
3
+ "version": "1.5.94",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -10,284 +10,13 @@ export default function useResources<T extends Record<string, ItemsRecord>>({
10
10
  onError,
11
11
  }: Props) {
12
12
  const token = useToken();
13
- const [info, setInfo] = useState<Record<string, any>>({});
14
-
15
- // const result = useMemo(() => {
16
- // const r = {} as EnhancedEndpoints<T>;
17
-
18
- // for (const key in endpoints) {
19
- // const endpoint = endpoints[key];
20
- // const showFunc = async ({
21
- // limit = 10,
22
- // page = 1,
23
- // merge = true,
24
- // ...query
25
- // }: ShowOptions) => {
26
- // const options = {
27
- // method: "GET",
28
- // url: `${baseURI}/${key}`,
29
- // params: { limit, page, ...query },
30
- // headers: { Authorization: token },
31
- // };
32
-
33
- // const newInfo = { ...info };
34
- // newInfo[key] = newInfo[key] || {};
35
- // newInfo[key].state = "loading";
36
- // newInfo[key].errorMessage = "";
37
- // newInfo[key].params = query;
38
-
39
- // setInfo(newInfo);
40
-
41
- // try {
42
- // const consulta = await axios.request(options);
43
- // const d = consulta.data;
44
- // newInfo[key].state = "success";
45
- // newInfo[key].errorMessage = "";
46
- // newInfo[key].data = merge
47
- // ? page == 1
48
- // ? d.data
49
- // : [...d.data, ...(newInfo[key].data || [])]
50
- // : d.data;
51
- // newInfo[key].totalItems = d.totalItems;
52
- // newInfo[key].totalPages = d.totalPages;
53
- // newInfo[key].currentPage = d.currentPage;
54
- // setInfo({ ...newInfo });
55
- // return d.data;
56
- // } catch (error: any) {
57
- // const item = httpStatusCodes.find((s) => s.code == error.status);
58
-
59
- // newInfo[key].state = "error";
60
- // newInfo[key].errorMessage = item?.meaning;
61
- // if (error.status == 403) {
62
- // onError?.({ error, ...{ errorMessage: item?.meaning } });
63
- // }
64
- // setInfo({ ...newInfo });
65
- // return error;
66
- // }
67
- // };
68
- // (r as any)[key] = {
69
- // ...endpoint,
70
- // show: showFunc,
71
- // find: async (id: number, query: any) => {
72
- // const options = {
73
- // method: "GET",
74
- // url: `${baseURI}/${key}/${id}`,
75
- // params: { ...query },
76
- // headers: { Authorization: token },
77
- // };
78
- // const newInfo = { ...info };
79
- // newInfo[key] = newInfo[key] || {};
80
- // newInfo[key].state = "loading";
81
- // newInfo[key].errorMessage = "";
82
- // newInfo[key].params = query;
83
-
84
- // setInfo(newInfo);
85
-
86
- // try {
87
- // const consulta = await axios.request(options);
88
- // const d = consulta.data;
89
- // newInfo[key].state = "success";
90
- // newInfo[key].errorMessage = "";
91
- // newInfo[key].selectedItem = d;
92
- // const index = newInfo[key]?.data?.findIndex(
93
- // (d: any) => d?.id == d?.id
94
- // );
95
- // if (index >= 0) {
96
- // newInfo[key].data[index] = d;
97
- // } else {
98
- // if (newInfo[key]?.data) {
99
- // newInfo[key].data.unshift(d);
100
- // } else {
101
- // newInfo[key].data = [d];
102
- // }
103
- // }
104
- // setInfo({ ...newInfo });
105
- // return d;
106
- // } catch (error: any) {
107
- // const item = httpStatusCodes.find((s) => s.code == error.status);
108
-
109
- // newInfo[key].state = "error";
110
- // newInfo[key].errorMessage = item?.meaning || error.message;
111
- // if (error.status == 403) {
112
- // onError?.({ error, ...{ errorMessage: item?.meaning } });
113
- // }
114
- // setInfo({ ...newInfo });
115
- // return error;
116
- // }
117
- // },
118
- // create: async (data: Record<string, any> | Record<string, any>[]) => {
119
- // const options = {
120
- // method: "POST",
121
- // url: `${baseURI}/${key}`,
122
- // data: Array.isArray(data) ? [...data] : { ...data },
123
- // headers: { Authorization: token },
124
- // };
125
- // const newInfo = { ...info };
126
- // newInfo[key] = newInfo[key] || {};
127
- // newInfo[key].state = "loading";
128
- // newInfo[key].errorMessage = "";
129
-
130
- // setInfo(newInfo);
131
-
132
- // try {
133
- // const consulta = await axios.request(options);
134
- // const d = consulta.data;
135
- // newInfo[key].state = "success";
136
- // newInfo[key].errorMessage = "";
137
- // if (Array.isArray(data)) {
138
- // for (let datum of data) {
139
- // const index = newInfo[key]?.data?.findIndex(
140
- // (d: any) => d?.id == datum?.id
141
- // );
142
- // if (index >= 0) {
143
- // newInfo[key].data[index] = d;
144
- // } else {
145
- // if (newInfo[key]?.data) {
146
- // newInfo[key].data.unshift(d);
147
- // } else {
148
- // newInfo[key].data = [d];
149
- // }
150
- // }
151
- // }
152
- // } else {
153
- // newInfo[key].selectedItem = d;
154
- // const index = newInfo[key]?.data?.findIndex(
155
- // (d: any) => d?.id == d?.id
156
- // );
157
- // if (index >= 0) {
158
- // newInfo[key].data[index] = d;
159
- // } else {
160
- // if (newInfo[key]?.data) {
161
- // newInfo[key].data.unshift(d);
162
- // } else {
163
- // newInfo[key].data = [d];
164
- // }
165
- // }
166
- // }
167
- // setInfo({ ...newInfo });
168
- // return d;
169
- // } catch (error: any) {
170
- // const item = httpStatusCodes.find((s) => s.code == error.status);
171
-
172
- // newInfo[key].state = "error";
173
- // newInfo[key].errorMessage = item?.meaning || error.message;
174
- // if (error.status == 403) {
175
- // onError?.({ error, ...{ errorMessage: item?.meaning } });
176
- // }
177
- // setInfo({ ...newInfo });
178
- // return error;
179
- // }
180
- // },
181
- // update: async (id: number, data: any) => {
182
- // const options = {
183
- // method: "PATCH",
184
- // url: `${baseURI}/${key}/${id}`,
185
- // data: Array.isArray(data) ? [...data] : { ...data },
186
- // headers: { Authorization: token },
187
- // };
188
- // const newInfo = { ...info };
189
- // newInfo[key] = newInfo[key] || {};
190
- // newInfo[key].state = "loading";
191
- // newInfo[key].errorMessage = "";
192
-
193
- // setInfo(newInfo);
194
-
195
- // try {
196
- // const consulta = await axios.request(options);
197
- // const d = consulta.data;
198
- // newInfo[key].state = "success";
199
- // newInfo[key].errorMessage = "";
200
- // newInfo[key].selectedItem = d;
201
- // const index = newInfo[key]?.data?.findIndex(
202
- // (d: any) => d?.id == d?.id
203
- // );
204
- // if (index >= 0) {
205
- // newInfo[key].data[index] = d;
206
- // } else {
207
- // if (newInfo[key]?.data) {
208
- // newInfo[key].data.unshift(d);
209
- // } else {
210
- // newInfo[key].data = [d];
211
- // }
212
- // }
213
- // setInfo({ ...newInfo });
214
- // return d;
215
- // } catch (error: any) {
216
- // const item = httpStatusCodes.find((s) => s.code == error.status);
217
-
218
- // newInfo[key].state = "error";
219
- // newInfo[key].errorMessage = item?.meaning || error.message;
220
- // if (error.status == 403) {
221
- // onError?.({ error, ...{ errorMessage: item?.meaning } });
222
- // }
223
- // setInfo({ ...newInfo });
224
- // return error;
225
- // }
226
- // },
227
- // remove: async (id: number) => {
228
- // const options = {
229
- // method: "DELETE",
230
- // url: `${baseURI}/${key}/${id}`,
231
- // headers: { Authorization: token },
232
- // };
233
- // const newInfo = { ...info };
234
- // newInfo[key] = newInfo[key] || {};
235
- // newInfo[key].state = "loading";
236
- // newInfo[key].errorMessage = "";
237
-
238
- // setInfo(newInfo);
239
-
240
- // try {
241
- // const consulta = await axios.request(options);
242
- // const d = consulta.data;
243
- // newInfo[key].state = "success";
244
- // newInfo[key].errorMessage = "";
245
- // newInfo[key].selectedItem = d;
246
- // const index = newInfo[key]?.data?.findIndex(
247
- // (d: any) => d?.id == d?.id
248
- // );
249
- // if (index >= 0) {
250
- // newInfo[key].data.splice(index, 1);
251
- // }
252
- // setInfo({ ...newInfo });
253
- // return d.data;
254
- // } catch (error: any) {
255
- // const item = httpStatusCodes.find((s) => s.code == error.status);
256
-
257
- // newInfo[key].state = "error";
258
- // newInfo[key].errorMessage = item?.meaning || error.message;
259
- // if (error.status == 403) {
260
- // onError?.({ error, ...{ errorMessage: item?.meaning } });
261
- // }
262
- // setInfo({ ...newInfo });
263
- // return error;
264
- // }
265
- // },
266
- // totalPages: info[key]?.totalPages,
267
- // currentPage: info[key]?.currentPage,
268
- // state: info[key]?.state || "success",
269
- // errorMessage: info[key]?.errorMessage,
270
- // params: info[key]?.params,
271
- // getAllPages: async (limit = 10) => {
272
- // console.log("Not aviable");
273
- // },
274
- // data: info[key]?.data || [],
275
- // selectedItem: info[key]?.selectedItem || {},
276
- // };
277
- // }
278
-
279
- // return r;
280
- // }, [info, token, endpoints]);
281
-
282
- const result = useMemo(() => {
283
- return Object.keys(endpoints).reduce(async (acc: any, key) => {
13
+ const [info, setInfo] = useState<Record<string, any>>(
14
+ Object.keys(endpoints).reduce((acc: any, key) => {
284
15
  const endpoint = endpoints[key];
285
- const showFunc = async ({
286
- limit = 10,
287
- page = 1,
288
- merge = true,
289
- ...query
290
- }: ShowOptions) => {
16
+ const showFunc = async (
17
+ { limit = 10, page = 1, merge = true, ...query }: ShowOptions,
18
+ autoLoad: boolean = false
19
+ ) => {
291
20
  const options = {
292
21
  method: "GET",
293
22
  url: `${baseURI}/${key}`,
@@ -309,14 +38,17 @@ export default function useResources<T extends Record<string, ItemsRecord>>({
309
38
  const d = consulta.data;
310
39
  newInfo[key].state = "success";
311
40
  newInfo[key].errorMessage = "";
312
- newInfo[key].data = merge
313
- ? page == 1
314
- ? d.data
315
- : [...d.data, ...(newInfo[key].data || [])]
316
- : d.data;
317
- newInfo[key].totalItems = d.totalItems;
318
- newInfo[key].totalPages = d.totalPages;
319
- newInfo[key].currentPage = d.currentPage;
41
+ if (autoLoad) {
42
+ } else {
43
+ newInfo[key].data = merge
44
+ ? page == 1
45
+ ? d.data
46
+ : [...d.data, ...(newInfo[key].data || [])]
47
+ : d.data;
48
+ newInfo[key].totalItems = d.totalItems;
49
+ newInfo[key].totalPages = d.totalPages;
50
+ newInfo[key].currentPage = d.currentPage;
51
+ }
320
52
  setInfo({ ...newInfo });
321
53
  return d.data;
322
54
  } catch (error: any) {
@@ -331,31 +63,33 @@ export default function useResources<T extends Record<string, ItemsRecord>>({
331
63
  return error;
332
64
  }
333
65
  };
334
- acc[key] = {
335
- ...endpoint,
336
- loaded: false,
337
- show: showFunc,
338
- find: async (id: number, query: any) => {
339
- const options = {
340
- method: "GET",
341
- url: `${baseURI}/${key}/${id}`,
342
- params: { ...query },
343
- headers: { Authorization: token },
344
- };
345
- const newInfo = { ...info };
346
- newInfo[key] = newInfo[key] || {};
347
- newInfo[key].state = "loading";
348
- newInfo[key].errorMessage = "";
349
- newInfo[key].params = query;
350
- newInfo[key].loader = true;
66
+ const findFunc = async (
67
+ id: number,
68
+ query: any,
69
+ autoLoad: boolean = false
70
+ ) => {
71
+ const options = {
72
+ method: "GET",
73
+ url: `${baseURI}/${key}/${id}`,
74
+ params: { ...query },
75
+ headers: { Authorization: token },
76
+ };
77
+ const newInfo = { ...info };
78
+ newInfo[key] = newInfo[key] || {};
79
+ newInfo[key].state = "loading";
80
+ newInfo[key].errorMessage = "";
81
+ newInfo[key].params = query;
82
+ newInfo[key].loader = true;
351
83
 
352
- setInfo(newInfo);
84
+ setInfo(newInfo);
353
85
 
354
- try {
355
- const consulta = await axios.request(options);
356
- const d = consulta.data;
357
- newInfo[key].state = "success";
358
- newInfo[key].errorMessage = "";
86
+ try {
87
+ const consulta = await axios.request(options);
88
+ const d = consulta.data;
89
+ newInfo[key].state = "success";
90
+ newInfo[key].errorMessage = "";
91
+ if (autoLoad) {
92
+ } else {
359
93
  newInfo[key].selectedItem = d;
360
94
  const index = newInfo[key]?.data?.findIndex(
361
95
  (d: any) => d?.id == d?.id
@@ -369,20 +103,32 @@ export default function useResources<T extends Record<string, ItemsRecord>>({
369
103
  newInfo[key].data = [d];
370
104
  }
371
105
  }
372
- setInfo({ ...newInfo });
373
- return d;
374
- } catch (error: any) {
375
- const item = httpStatusCodes.find((s) => s.code == error.status);
106
+ }
107
+ setInfo({ ...newInfo });
108
+ return d;
109
+ } catch (error: any) {
110
+ const item = httpStatusCodes.find((s) => s.code == error.status);
376
111
 
377
- newInfo[key].state = "error";
378
- newInfo[key].errorMessage = item?.meaning || error.message;
379
- if (error.status == 403) {
380
- onError?.({ error, ...{ errorMessage: item?.meaning } });
381
- }
382
- setInfo({ ...newInfo });
383
- return error;
112
+ newInfo[key].state = "error";
113
+ newInfo[key].errorMessage = item?.meaning || error.message;
114
+ if (error.status == 403) {
115
+ onError?.({ error, ...{ errorMessage: item?.meaning } });
384
116
  }
385
- },
117
+ setInfo({ ...newInfo });
118
+ return error;
119
+ }
120
+ };
121
+ acc[key] = {
122
+ ...endpoint,
123
+ loaded: !acc[key]?.loaded
124
+ ? acc[key]?.id
125
+ ? findFunc(acc[key].id, acc[key]?.defaultParams)
126
+ : acc[key]?.defaultParams
127
+ ? showFunc(acc[key].defaultParams)
128
+ : true
129
+ : true,
130
+ show: showFunc,
131
+ find: findFunc,
386
132
  create: async (data: Record<string, any> | Record<string, any>[]) => {
387
133
  const options = {
388
134
  method: "POST",
@@ -542,32 +288,10 @@ export default function useResources<T extends Record<string, ItemsRecord>>({
542
288
  data: info[key]?.data || [],
543
289
  selectedItem: info[key]?.selectedItem || {},
544
290
  };
545
- if (!acc[key].loaded) {
546
- if (acc[key]?.id) {
547
- await acc[key].find(acc[key].id, acc[key]?.defaultParams);
548
- } else if (acc[key]?.defaultParams) {
549
- await acc[key].show(acc[key].defaultParams);
550
- }
551
- }
291
+
552
292
  return acc;
553
- }, {} as EnhancedEndpoints<T>);
554
- }, [info, token, endpoints]);
293
+ }, {} as EnhancedEndpoints<T>)
294
+ );
555
295
 
556
- // async function loadAll() {
557
- // for (let item in result) {
558
- // if (result[item]?.id && !result[item].loaded) {
559
- // await result[item].find(result[item].id, {
560
- // ...result[item].defaultParams,
561
- // });
562
- // return;
563
- // } else if (result[item]?.defaultParams && !result[item].loaded) {
564
- // await result[item].show({ ...result[item].defaultParams });
565
- // return;
566
- // }
567
- // }
568
- // }
569
- // useEffect(() => {
570
- // loadAll();
571
- // }, []);
572
- return result;
296
+ return info;
573
297
  }