next-recomponents 2.0.38 → 2.0.39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -36303,6 +36303,27 @@ var httpStatusCodes = [
36303
36303
  var http_codes_default = httpStatusCodes;
36304
36304
 
36305
36305
  // src/use-resources/index.ts
36306
+ function getErrorMeaning(error) {
36307
+ var _a;
36308
+ return (_a = http_codes_default.find((s) => s.code === (error == null ? void 0 : error.status))) == null ? void 0 : _a.meaning;
36309
+ }
36310
+ function mergeDataArray(existing, incoming, matchId) {
36311
+ const idx = existing.findIndex((d) => (d == null ? void 0 : d.id) == matchId);
36312
+ if (idx >= 0) {
36313
+ return existing.map((d, i) => i === idx ? incoming : d);
36314
+ }
36315
+ return [incoming, ...existing];
36316
+ }
36317
+ function cloneKey(state, key) {
36318
+ var _a;
36319
+ return {
36320
+ ...state,
36321
+ [key]: {
36322
+ ...state[key],
36323
+ data: ((_a = state[key]) == null ? void 0 : _a.data) ? [...state[key].data] : []
36324
+ }
36325
+ };
36326
+ }
36306
36327
  function useResources({
36307
36328
  baseURI,
36308
36329
  endpoints,
@@ -36310,429 +36331,304 @@ function useResources({
36310
36331
  }) {
36311
36332
  const token = useToken();
36312
36333
  const [info, setInfo] = (0, import_react8.useState)(
36313
- Object.keys(endpoints).reduce((acc, key) => {
36334
+ () => Object.keys(endpoints).reduce((acc, key) => {
36314
36335
  var _a, _b;
36315
- const newAcc = {
36316
- ...acc,
36317
- [key]: {
36318
- loaded: false,
36319
- id: (_a = endpoints[key]) == null ? void 0 : _a.id,
36320
- defaultParams: (_b = endpoints[key]) == null ? void 0 : _b.defaultParams,
36321
- data: [],
36322
- selectedItem: {}
36323
- }
36336
+ acc[key] = {
36337
+ loaded: false,
36338
+ id: (_a = endpoints[key]) == null ? void 0 : _a.id,
36339
+ defaultParams: (_b = endpoints[key]) == null ? void 0 : _b.defaultParams,
36340
+ data: [],
36341
+ selectedItem: {},
36342
+ state: "success",
36343
+ errorMessage: ""
36324
36344
  };
36325
- return newAcc;
36345
+ return acc;
36326
36346
  }, {})
36327
36347
  );
36328
- const results = Object.keys(endpoints).reduce(
36329
- (acc, key) => {
36330
- var _a, _b, _c, _d, _e, _f, _g;
36331
- const endpoint = endpoints[key];
36332
- const showFunc = async ({ limit = 10, page = 1, merge = true, ...query }, autoLoad = false) => {
36333
- const options = {
36334
- method: "GET",
36335
- url: `${baseURI}/${key}`,
36336
- params: { limit, page, ...query },
36337
- headers: { Authorization: token }
36338
- };
36339
- const newInfo = { ...info };
36340
- newInfo[key].state = "loading";
36341
- newInfo[key].errorMessage = "";
36342
- newInfo[key].params = query;
36343
- newInfo[key].loaded = true;
36344
- setInfo(newInfo);
36345
- try {
36346
- const consulta = await import_axios.default.request(options);
36347
- const d = consulta.data;
36348
- newInfo[key].state = "success";
36349
- newInfo[key].errorMessage = "";
36350
- newInfo[key].data = merge ? page == 1 ? d.data : [...d.data, ...newInfo[key].data || []] : d.data;
36351
- newInfo[key].totalItems = d.totalItems;
36352
- newInfo[key].totalPages = d.totalPages;
36353
- newInfo[key].currentPage = d.currentPage;
36354
- setInfo({ ...newInfo });
36355
- return d.data;
36356
- } catch (error) {
36357
- const item = http_codes_default.find((s) => s.code == error.status);
36358
- newInfo[key].state = "error";
36359
- newInfo[key].errorMessage = item == null ? void 0 : item.meaning;
36360
- if (error.status == 403) {
36361
- onError == null ? void 0 : onError({ error, ...{ errorMessage: item == null ? void 0 : item.meaning } });
36362
- }
36363
- setInfo({ ...newInfo });
36364
- return error;
36365
- }
36366
- };
36367
- const findFunc = async (id, query) => {
36368
- var _a2, _b2, _c2;
36369
- const options = {
36370
- method: "GET",
36371
- url: `${baseURI}/${key}/${id}`,
36372
- params: { ...query },
36373
- headers: { Authorization: token }
36374
- };
36375
- const newInfo = { ...info };
36376
- newInfo[key].state = "loading";
36377
- newInfo[key].errorMessage = "";
36378
- newInfo[key].params = query;
36379
- newInfo[key].loaded = true;
36380
- setInfo(newInfo);
36381
- try {
36382
- const consulta = await import_axios.default.request(options);
36383
- const d = consulta.data;
36384
- newInfo[key].state = "success";
36385
- newInfo[key].errorMessage = "";
36386
- newInfo[key].selectedItem = d;
36387
- const index = (_b2 = (_a2 = newInfo[key]) == null ? void 0 : _a2.data) == null ? void 0 : _b2.findIndex((d2) => (d2 == null ? void 0 : d2.id) == id);
36388
- if (index >= 0) {
36389
- newInfo[key].data[index] = d;
36390
- } else {
36391
- if ((_c2 = newInfo[key]) == null ? void 0 : _c2.data) {
36392
- newInfo[key].data.unshift(d);
36393
- } else {
36394
- newInfo[key].data = [d];
36395
- }
36396
- }
36397
- setInfo({ ...newInfo });
36398
- return d;
36399
- } catch (error) {
36400
- const item = http_codes_default.find((s) => s.code == error.status);
36401
- newInfo[key].state = "error";
36402
- newInfo[key].errorMessage = (item == null ? void 0 : item.meaning) || error.message;
36403
- if (error.status == 403) {
36404
- onError == null ? void 0 : onError({ error, ...{ errorMessage: item == null ? void 0 : item.meaning } });
36405
- }
36406
- setInfo({ ...newInfo });
36407
- return error;
36348
+ const infoRef = (0, import_react8.useRef)(info);
36349
+ (0, import_react8.useEffect)(() => {
36350
+ infoRef.current = info;
36351
+ }, [info]);
36352
+ const setKeyLoading = (0, import_react8.useCallback)((key) => {
36353
+ setInfo((prev) => ({
36354
+ ...cloneKey(prev, key),
36355
+ [key]: {
36356
+ ...cloneKey(prev, key)[key],
36357
+ state: "loading",
36358
+ errorMessage: ""
36359
+ }
36360
+ }));
36361
+ }, []);
36362
+ const setKeyError = (0, import_react8.useCallback)(
36363
+ (key, error) => {
36364
+ const meaning = getErrorMeaning(error);
36365
+ if ((error == null ? void 0 : error.status) === 403) onError == null ? void 0 : onError({ error, errorMessage: meaning });
36366
+ setInfo((prev) => ({
36367
+ ...cloneKey(prev, key),
36368
+ [key]: {
36369
+ ...cloneKey(prev, key)[key],
36370
+ state: "error",
36371
+ errorMessage: meaning || (error == null ? void 0 : error.message)
36408
36372
  }
36409
- };
36410
- const mergeDataArray = (existingData, newItem, matchId) => {
36411
- if (!existingData) return [newItem];
36412
- const index = existingData.findIndex((d) => (d == null ? void 0 : d.id) == matchId);
36413
- if (index >= 0) {
36414
- return existingData.map(
36415
- (d, i) => i === index ? newItem : d
36416
- );
36373
+ }));
36374
+ },
36375
+ [onError]
36376
+ );
36377
+ const show = (0, import_react8.useCallback)(
36378
+ async (key, { limit = 10, page = 1, merge = true, ...query }) => {
36379
+ setInfo((prev) => ({
36380
+ ...cloneKey(prev, key),
36381
+ [key]: {
36382
+ ...cloneKey(prev, key)[key],
36383
+ state: "loading",
36384
+ errorMessage: "",
36385
+ params: query,
36386
+ loaded: true
36417
36387
  }
36418
- return [newItem, ...existingData];
36419
- };
36420
- const bodyCreateFunc = async (data) => {
36421
- var _a2, _b2, _c2, _d2, _e2;
36422
- const options = {
36423
- method: "POST",
36424
- url: `${baseURI}/${key}`,
36425
- data: Array.isArray(data) ? [...data] : { ...data },
36388
+ }));
36389
+ try {
36390
+ const { data: res } = await import_axios.default.get(`${baseURI}/${key}`, {
36391
+ params: { limit, page, ...query },
36426
36392
  headers: { Authorization: token }
36427
- };
36428
- const newInfo = {
36429
- ...info,
36430
- [key]: {
36431
- ...info[key],
36432
- data: [...(_b2 = (_a2 = info[key]) == null ? void 0 : _a2.data) != null ? _b2 : []],
36433
- state: "loading",
36434
- errorMessage: ""
36435
- }
36436
- };
36437
- setInfo(newInfo);
36438
- try {
36439
- const consulta = await import_axios.default.request(options);
36440
- const d = consulta.data;
36441
- let updatedData;
36442
- if (Array.isArray(data)) {
36443
- updatedData = [...(_d2 = (_c2 = newInfo[key]) == null ? void 0 : _c2.data) != null ? _d2 : []];
36444
- for (const datum of data) {
36445
- updatedData = mergeDataArray(updatedData, d, datum == null ? void 0 : datum.id);
36446
- }
36447
- updatedData = updatedData.flat();
36448
- } else {
36449
- updatedData = mergeDataArray(
36450
- (_e2 = newInfo[key]) == null ? void 0 : _e2.data,
36451
- d,
36452
- data == null ? void 0 : data.id
36453
- );
36454
- }
36455
- setInfo({
36456
- ...newInfo,
36393
+ });
36394
+ setInfo((prev) => {
36395
+ var _a, _b;
36396
+ const prevData = (_b = (_a = prev[key]) == null ? void 0 : _a.data) != null ? _b : [];
36397
+ const merged = merge ? page === 1 ? res.data : [...prevData, ...res.data] : res.data;
36398
+ return {
36399
+ ...prev,
36457
36400
  [key]: {
36458
- ...newInfo[key],
36401
+ ...prev[key],
36459
36402
  state: "success",
36460
36403
  errorMessage: "",
36461
- selectedItem: Array.isArray(data) ? newInfo[key].selectedItem : d,
36462
- data: updatedData
36463
- }
36464
- });
36465
- return d;
36466
- } catch (error) {
36467
- const item = http_codes_default.find((s) => s.code == error.status);
36468
- if (error.status == 403) {
36469
- onError == null ? void 0 : onError({ error, errorMessage: item == null ? void 0 : item.meaning });
36470
- }
36471
- setInfo({
36472
- ...newInfo,
36473
- [key]: {
36474
- ...newInfo[key],
36475
- state: "error",
36476
- errorMessage: (item == null ? void 0 : item.meaning) || error.message
36404
+ data: merged,
36405
+ totalItems: res.totalItems,
36406
+ totalPages: res.totalPages,
36407
+ currentPage: res.currentPage
36477
36408
  }
36478
- });
36479
- return error;
36480
- }
36481
- };
36482
- const formCreateFunc = async (data) => {
36483
- var _a2, _b2, _c2, _d2, _e2;
36484
- const formData = new FormData();
36485
- if (Array.isArray(data)) {
36486
- data.forEach((item, i) => {
36487
- for (const [k, v] of Object.entries(item)) {
36488
- formData.append(`${k}[${i}]`, v);
36489
- }
36490
- });
36491
- } else {
36492
- for (const [k, v] of Object.entries(data)) {
36493
- formData.append(k, v);
36494
- }
36409
+ };
36410
+ });
36411
+ return res.data;
36412
+ } catch (error) {
36413
+ setKeyError(key, error);
36414
+ return error;
36415
+ }
36416
+ },
36417
+ [baseURI, token, setKeyError]
36418
+ );
36419
+ const find = (0, import_react8.useCallback)(
36420
+ async (key, id, query) => {
36421
+ setInfo((prev) => ({
36422
+ ...cloneKey(prev, key),
36423
+ [key]: {
36424
+ ...cloneKey(prev, key)[key],
36425
+ state: "loading",
36426
+ errorMessage: "",
36427
+ params: query,
36428
+ loaded: true
36495
36429
  }
36496
- const options = {
36497
- method: "POST",
36498
- url: `${baseURI}/${key}`,
36499
- data: formData,
36500
- headers: {
36501
- Authorization: token,
36502
- "Content-Type": "multipart/form-data"
36503
- }
36504
- };
36505
- const newInfo = {
36506
- ...info,
36507
- [key]: {
36508
- ...info[key],
36509
- data: [...(_b2 = (_a2 = info[key]) == null ? void 0 : _a2.data) != null ? _b2 : []],
36510
- state: "loading",
36511
- errorMessage: ""
36512
- }
36513
- };
36514
- setInfo(newInfo);
36515
- try {
36516
- const consulta = await import_axios.default.request(options);
36517
- const d = consulta.data;
36518
- let updatedData;
36519
- if (Array.isArray(data)) {
36520
- updatedData = [...(_d2 = (_c2 = newInfo[key]) == null ? void 0 : _c2.data) != null ? _d2 : []];
36521
- for (const datum of data) {
36522
- updatedData = mergeDataArray(updatedData, d, datum == null ? void 0 : datum.id);
36523
- }
36524
- updatedData = updatedData.flat();
36525
- } else {
36526
- updatedData = mergeDataArray(
36527
- (_e2 = newInfo[key]) == null ? void 0 : _e2.data,
36528
- d,
36529
- data == null ? void 0 : data.id
36530
- );
36531
- }
36532
- setInfo({
36533
- ...newInfo,
36430
+ }));
36431
+ try {
36432
+ const { data: d } = await import_axios.default.get(`${baseURI}/${key}/${id}`, {
36433
+ params: query,
36434
+ headers: { Authorization: token }
36435
+ });
36436
+ setInfo((prev) => {
36437
+ var _a, _b;
36438
+ return {
36439
+ ...prev,
36534
36440
  [key]: {
36535
- ...newInfo[key],
36441
+ ...prev[key],
36536
36442
  state: "success",
36537
36443
  errorMessage: "",
36538
- selectedItem: Array.isArray(data) ? newInfo[key].selectedItem : d,
36539
- data: updatedData
36444
+ selectedItem: d,
36445
+ data: mergeDataArray((_b = (_a = prev[key]) == null ? void 0 : _a.data) != null ? _b : [], d, id)
36540
36446
  }
36541
- });
36542
- return d;
36543
- } catch (error) {
36544
- const item = http_codes_default.find((s) => s.code == error.status);
36545
- if (error.status == 403) {
36546
- onError == null ? void 0 : onError({ error, errorMessage: item == null ? void 0 : item.meaning });
36547
- }
36548
- setInfo({
36549
- ...newInfo,
36550
- [key]: {
36551
- ...newInfo[key],
36552
- state: "error",
36553
- errorMessage: (item == null ? void 0 : item.meaning) || error.message
36554
- }
36555
- });
36556
- return error;
36557
- }
36558
- };
36559
- acc[key] = {
36560
- ...endpoint,
36561
- loaded: false,
36562
- show: showFunc,
36563
- find: findFunc,
36564
- create: async (data) => {
36565
- const hasFile = Array.isArray(data) ? data.some(
36566
- (item) => Object.values(item).some((value) => value instanceof File)
36567
- ) : Object.values(data).some((value) => value instanceof File);
36568
- if (hasFile) {
36569
- return await formCreateFunc(data);
36570
- }
36571
- return await bodyCreateFunc(data);
36572
- },
36573
- update: async (id, data) => {
36574
- var _a2, _b2, _c2, _d2, _e2;
36575
- const options = {
36576
- method: "PATCH",
36577
- url: `${baseURI}/${key}/${id}`,
36578
- data: Array.isArray(data) ? [...data] : { ...data },
36579
- headers: { Authorization: token }
36580
36447
  };
36581
- const newInfo = {
36582
- ...info,
36448
+ });
36449
+ return d;
36450
+ } catch (error) {
36451
+ setKeyError(key, error);
36452
+ return error;
36453
+ }
36454
+ },
36455
+ [baseURI, token, setKeyError]
36456
+ );
36457
+ const buildFormData = (data) => {
36458
+ const fd = new FormData();
36459
+ if (Array.isArray(data)) {
36460
+ data.forEach((item, i) => {
36461
+ Object.entries(item).forEach(
36462
+ ([k, v]) => fd.append(`${k}[${i}]`, v)
36463
+ );
36464
+ });
36465
+ } else {
36466
+ Object.entries(data).forEach(([k, v]) => fd.append(k, v));
36467
+ }
36468
+ return fd;
36469
+ };
36470
+ const hasFiles = (data) => Array.isArray(data) ? data.some((item) => Object.values(item).some((v) => v instanceof File)) : Object.values(data).some((v) => v instanceof File);
36471
+ const create = (0, import_react8.useCallback)(
36472
+ async (key, data) => {
36473
+ setKeyLoading(key);
36474
+ const isForm = hasFiles(data);
36475
+ const payload = isForm ? buildFormData(data) : Array.isArray(data) ? [...data] : { ...data };
36476
+ const headers = { Authorization: token };
36477
+ if (isForm) headers["Content-Type"] = "multipart/form-data";
36478
+ try {
36479
+ const { data: d } = await import_axios.default.post(`${baseURI}/${key}`, payload, {
36480
+ headers
36481
+ });
36482
+ setInfo((prev) => {
36483
+ var _a, _b;
36484
+ const prevData = (_b = (_a = prev[key]) == null ? void 0 : _a.data) != null ? _b : [];
36485
+ const incoming = Array.isArray(d) ? d : [d];
36486
+ const updatedData = incoming.reduce(
36487
+ (acc, item) => mergeDataArray(acc, item, item == null ? void 0 : item.id),
36488
+ [...prevData]
36489
+ );
36490
+ return {
36491
+ ...prev,
36583
36492
  [key]: {
36584
- ...info[key],
36585
- // ✅ copia superficial del key
36586
- data: [...(_b2 = (_a2 = info[key]) == null ? void 0 : _a2.data) != null ? _b2 : []],
36587
- // copia del array
36588
- state: "loading",
36589
- errorMessage: ""
36493
+ ...prev[key],
36494
+ state: "success",
36495
+ errorMessage: "",
36496
+ selectedItem: Array.isArray(data) ? prev[key].selectedItem : d,
36497
+ data: updatedData
36590
36498
  }
36591
36499
  };
36592
- setInfo(newInfo);
36593
- try {
36594
- const consulta = await import_axios.default.request(options);
36595
- const d = consulta.data;
36596
- const index = (_d2 = (_c2 = newInfo[key]) == null ? void 0 : _c2.data) == null ? void 0 : _d2.findIndex(
36597
- (item) => (item == null ? void 0 : item.id) == id
36598
- );
36599
- const updatedData = index >= 0 ? newInfo[key].data.map(
36600
- (item, i) => i === index ? { ...item, ...d } : item
36601
- ) : ((_e2 = newInfo[key]) == null ? void 0 : _e2.data) ? [d, ...newInfo[key].data] : [d];
36602
- const updatedInfo = {
36603
- ...newInfo,
36604
- [key]: {
36605
- ...newInfo[key],
36606
- state: "success",
36607
- errorMessage: "",
36608
- selectedItem: { ...newInfo[key].selectedItem, ...d },
36609
- data: updatedData
36610
- }
36611
- };
36612
- setInfo(updatedInfo);
36613
- return d;
36614
- } catch (error) {
36615
- const item = http_codes_default.find((s) => s.code == error.status);
36616
- setInfo({
36617
- ...newInfo,
36618
- [key]: {
36619
- ...newInfo[key],
36620
- state: "error",
36621
- errorMessage: (item == null ? void 0 : item.meaning) || error.message
36622
- }
36623
- });
36624
- if (error.status == 403) {
36625
- onError == null ? void 0 : onError({ error, ...{ errorMessage: item == null ? void 0 : item.meaning } });
36500
+ });
36501
+ return d;
36502
+ } catch (error) {
36503
+ setKeyError(key, error);
36504
+ return error;
36505
+ }
36506
+ },
36507
+ [baseURI, token, setKeyLoading, setKeyError]
36508
+ );
36509
+ const update = (0, import_react8.useCallback)(
36510
+ async (key, id, data) => {
36511
+ setKeyLoading(key);
36512
+ try {
36513
+ const { data: d } = await import_axios.default.patch(
36514
+ `${baseURI}/${key}/${id}`,
36515
+ Array.isArray(data) ? [...data] : { ...data },
36516
+ { headers: { Authorization: token } }
36517
+ );
36518
+ setInfo((prev) => {
36519
+ var _a, _b, _c, _d;
36520
+ return {
36521
+ ...prev,
36522
+ [key]: {
36523
+ ...prev[key],
36524
+ state: "success",
36525
+ errorMessage: "",
36526
+ selectedItem: { ...prev[key].selectedItem, ...d },
36527
+ data: mergeDataArray(
36528
+ (_b = (_a = prev[key]) == null ? void 0 : _a.data) != null ? _b : [],
36529
+ { ...(_d = (_c = prev[key]) == null ? void 0 : _c.data) == null ? void 0 : _d.find((i) => (i == null ? void 0 : i.id) == id), ...d },
36530
+ id
36531
+ )
36626
36532
  }
36627
- return error;
36628
- }
36629
- },
36630
- remove: async (id) => {
36631
- var _a2, _b2, _c2, _d2, _e2;
36632
- const options = {
36633
- method: "DELETE",
36634
- url: `${baseURI}/${key}/${id}`,
36635
- headers: { Authorization: token }
36636
36533
  };
36637
- const newInfo = {
36638
- ...info,
36534
+ });
36535
+ return d;
36536
+ } catch (error) {
36537
+ setKeyError(key, error);
36538
+ return error;
36539
+ }
36540
+ },
36541
+ [baseURI, token, setKeyLoading, setKeyError]
36542
+ );
36543
+ const remove = (0, import_react8.useCallback)(
36544
+ async (key, id) => {
36545
+ setKeyLoading(key);
36546
+ try {
36547
+ const { data: d } = await import_axios.default.delete(`${baseURI}/${key}/${id}`, {
36548
+ headers: { Authorization: token }
36549
+ });
36550
+ setInfo((prev) => {
36551
+ var _a, _b;
36552
+ return {
36553
+ ...prev,
36639
36554
  [key]: {
36640
- ...info[key],
36641
- data: [...(_b2 = (_a2 = info[key]) == null ? void 0 : _a2.data) != null ? _b2 : []],
36642
- state: "loading",
36643
- errorMessage: ""
36555
+ ...prev[key],
36556
+ state: "success",
36557
+ errorMessage: "",
36558
+ selectedItem: d,
36559
+ data: ((_b = (_a = prev[key]) == null ? void 0 : _a.data) != null ? _b : []).filter((item) => (item == null ? void 0 : item.id) != id)
36644
36560
  }
36645
36561
  };
36646
- setInfo(newInfo);
36647
- try {
36648
- const consulta = await import_axios.default.request(options);
36649
- const d = consulta.data;
36650
- setInfo({
36651
- ...newInfo,
36652
- [key]: {
36653
- ...newInfo[key],
36654
- state: "success",
36655
- errorMessage: "",
36656
- selectedItem: d,
36657
- data: (_e2 = (_d2 = (_c2 = newInfo[key]) == null ? void 0 : _c2.data) == null ? void 0 : _d2.filter((item) => (item == null ? void 0 : item.id) != id)) != null ? _e2 : []
36658
- // ✅ filter en lugar de splice
36659
- }
36660
- });
36661
- return d.data;
36662
- } catch (error) {
36663
- const item = http_codes_default.find((s) => s.code == error.status);
36664
- if (error.status == 403) {
36665
- onError == null ? void 0 : onError({ error, errorMessage: item == null ? void 0 : item.meaning });
36666
- }
36667
- setInfo({
36668
- ...newInfo,
36669
- [key]: {
36670
- ...newInfo[key],
36671
- state: "error",
36672
- errorMessage: (item == null ? void 0 : item.meaning) || error.message
36673
- }
36674
- });
36675
- return error;
36676
- }
36677
- },
36678
- totalPages: (_a = info[key]) == null ? void 0 : _a.totalPages,
36679
- currentPage: (_b = info[key]) == null ? void 0 : _b.currentPage,
36680
- state: ((_c = info[key]) == null ? void 0 : _c.state) || "success",
36681
- errorMessage: (_d = info[key]) == null ? void 0 : _d.errorMessage,
36682
- params: (_e = info[key]) == null ? void 0 : _e.params,
36683
- setLoaded: () => {
36684
- const newInfo = { ...info };
36685
- newInfo[key].loaded = true;
36686
- setInfo(newInfo);
36687
- },
36688
- getAllPages: async (limit = 100) => {
36689
- const allData = [];
36690
- let currentPage = 1;
36691
- let totalPages = 1;
36692
- while (currentPage <= totalPages) {
36693
- const response = await results[key].show({
36694
- limit,
36695
- page: currentPage
36696
- });
36697
- const currentInfo = info[key];
36698
- if (Array.isArray(response)) {
36699
- allData.push(...response);
36700
- }
36701
- currentPage = currentInfo.currentPage || currentPage + 1;
36702
- totalPages = currentInfo.totalPages || currentPage;
36703
- }
36704
- const newInfo = { ...info };
36705
- newInfo[key].data = allData;
36706
- newInfo[key].currentPage = currentPage;
36707
- newInfo[key].totalPages = totalPages;
36708
- setInfo(newInfo);
36709
- return allData;
36710
- },
36711
- data: ((_f = info[key]) == null ? void 0 : _f.data) || [],
36712
- selectedItem: ((_g = info[key]) == null ? void 0 : _g.selectedItem) || {}
36713
- };
36714
- return acc;
36562
+ });
36563
+ return d;
36564
+ } catch (error) {
36565
+ setKeyError(key, error);
36566
+ return error;
36567
+ }
36715
36568
  },
36716
- {}
36569
+ [baseURI, token, setKeyLoading, setKeyError]
36570
+ );
36571
+ const getAllPages = (0, import_react8.useCallback)(
36572
+ async (key, limit = 100) => {
36573
+ var _a, _b;
36574
+ const allData = [];
36575
+ let page = 1;
36576
+ let totalPages = 1;
36577
+ do {
36578
+ const pageData = await show(key, { limit, page, merge: false });
36579
+ if (Array.isArray(pageData)) allData.push(...pageData);
36580
+ totalPages = (_b = (_a = infoRef.current[key]) == null ? void 0 : _a.totalPages) != null ? _b : page;
36581
+ page++;
36582
+ } while (page <= totalPages);
36583
+ setInfo((prev) => ({
36584
+ ...prev,
36585
+ [key]: { ...prev[key], data: allData }
36586
+ }));
36587
+ return allData;
36588
+ },
36589
+ [show]
36717
36590
  );
36718
- async function doSome() {
36719
- var _a, _b, _c, _d, _e;
36720
- const key = Object.keys(info).find((k) => {
36721
- return info[k].loaded === false;
36722
- });
36723
- if (key) {
36724
- if ((_a = info[key]) == null ? void 0 : _a.id) {
36725
- await results[key].find((_b = info[key]) == null ? void 0 : _b.id, (_c = info[key]) == null ? void 0 : _c.defaultParams);
36726
- } else if ((_d = info[key]) == null ? void 0 : _d.defaultParams) {
36727
- await results[key].show((_e = info[key]) == null ? void 0 : _e.defaultParams);
36728
- } else {
36729
- results[key].setLoaded();
36730
- }
36731
- }
36732
- }
36733
36591
  (0, import_react8.useEffect)(() => {
36734
- doSome();
36735
- }, [JSON.stringify(Object.values(info).map((v) => v.loaded))]);
36592
+ var _a, _b;
36593
+ const key = Object.keys(info).find((k) => info[k].loaded === false);
36594
+ if (!key) return;
36595
+ if ((_a = info[key]) == null ? void 0 : _a.id) {
36596
+ find(key, info[key].id, info[key].defaultParams);
36597
+ } else if ((_b = info[key]) == null ? void 0 : _b.defaultParams) {
36598
+ show(key, info[key].defaultParams);
36599
+ } else {
36600
+ setInfo((prev) => ({
36601
+ ...prev,
36602
+ [key]: { ...prev[key], loaded: true }
36603
+ }));
36604
+ }
36605
+ }, [
36606
+ Object.keys(info).map((k) => info[k].loaded).join(",")
36607
+ ]);
36608
+ const results = Object.keys(endpoints).reduce((acc, key) => {
36609
+ var _a, _b, _c, _d;
36610
+ const s = info[key];
36611
+ acc[key] = {
36612
+ ...endpoints[key],
36613
+ data: (_a = s == null ? void 0 : s.data) != null ? _a : [],
36614
+ selectedItem: (_b = s == null ? void 0 : s.selectedItem) != null ? _b : {},
36615
+ state: (_c = s == null ? void 0 : s.state) != null ? _c : "success",
36616
+ errorMessage: s == null ? void 0 : s.errorMessage,
36617
+ params: s == null ? void 0 : s.params,
36618
+ totalPages: s == null ? void 0 : s.totalPages,
36619
+ currentPage: s == null ? void 0 : s.currentPage,
36620
+ totalItems: s == null ? void 0 : s.totalItems,
36621
+ loaded: (_d = s == null ? void 0 : s.loaded) != null ? _d : false,
36622
+ show: (opts) => show(key, opts),
36623
+ find: (id, query) => find(key, id, query),
36624
+ create: (data) => create(key, data),
36625
+ update: (id, data) => update(key, id, data),
36626
+ remove: (id) => remove(key, id),
36627
+ getAllPages: (limit) => getAllPages(key, limit),
36628
+ setLoaded: () => setInfo((prev) => ({ ...prev, [key]: { ...prev[key], loaded: true } }))
36629
+ };
36630
+ return acc;
36631
+ }, {});
36736
36632
  return results;
36737
36633
  }
36738
36634