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.mjs CHANGED
@@ -36194,7 +36194,7 @@ function TextArea({
36194
36194
  }
36195
36195
 
36196
36196
  // src/use-resources/index.ts
36197
- import { useEffect as useEffect4, useState as useState7 } from "react";
36197
+ import { useCallback, useEffect as useEffect4, useRef as useRef2, useState as useState7 } from "react";
36198
36198
 
36199
36199
  // src/use-resources/get.token.tsx
36200
36200
  import { useMemo as useMemo2 } from "react";
@@ -36285,6 +36285,27 @@ var httpStatusCodes = [
36285
36285
  var http_codes_default = httpStatusCodes;
36286
36286
 
36287
36287
  // src/use-resources/index.ts
36288
+ function getErrorMeaning(error) {
36289
+ var _a;
36290
+ return (_a = http_codes_default.find((s) => s.code === (error == null ? void 0 : error.status))) == null ? void 0 : _a.meaning;
36291
+ }
36292
+ function mergeDataArray(existing, incoming, matchId) {
36293
+ const idx = existing.findIndex((d) => (d == null ? void 0 : d.id) == matchId);
36294
+ if (idx >= 0) {
36295
+ return existing.map((d, i) => i === idx ? incoming : d);
36296
+ }
36297
+ return [incoming, ...existing];
36298
+ }
36299
+ function cloneKey(state, key) {
36300
+ var _a;
36301
+ return {
36302
+ ...state,
36303
+ [key]: {
36304
+ ...state[key],
36305
+ data: ((_a = state[key]) == null ? void 0 : _a.data) ? [...state[key].data] : []
36306
+ }
36307
+ };
36308
+ }
36288
36309
  function useResources({
36289
36310
  baseURI,
36290
36311
  endpoints,
@@ -36292,436 +36313,311 @@ function useResources({
36292
36313
  }) {
36293
36314
  const token = useToken();
36294
36315
  const [info, setInfo] = useState7(
36295
- Object.keys(endpoints).reduce((acc, key) => {
36316
+ () => Object.keys(endpoints).reduce((acc, key) => {
36296
36317
  var _a, _b;
36297
- const newAcc = {
36298
- ...acc,
36299
- [key]: {
36300
- loaded: false,
36301
- id: (_a = endpoints[key]) == null ? void 0 : _a.id,
36302
- defaultParams: (_b = endpoints[key]) == null ? void 0 : _b.defaultParams,
36303
- data: [],
36304
- selectedItem: {}
36305
- }
36318
+ acc[key] = {
36319
+ loaded: false,
36320
+ id: (_a = endpoints[key]) == null ? void 0 : _a.id,
36321
+ defaultParams: (_b = endpoints[key]) == null ? void 0 : _b.defaultParams,
36322
+ data: [],
36323
+ selectedItem: {},
36324
+ state: "success",
36325
+ errorMessage: ""
36306
36326
  };
36307
- return newAcc;
36327
+ return acc;
36308
36328
  }, {})
36309
36329
  );
36310
- const results = Object.keys(endpoints).reduce(
36311
- (acc, key) => {
36312
- var _a, _b, _c, _d, _e, _f, _g;
36313
- const endpoint = endpoints[key];
36314
- const showFunc = async ({ limit = 10, page = 1, merge = true, ...query }, autoLoad = false) => {
36315
- const options = {
36316
- method: "GET",
36317
- url: `${baseURI}/${key}`,
36318
- params: { limit, page, ...query },
36319
- headers: { Authorization: token }
36320
- };
36321
- const newInfo = { ...info };
36322
- newInfo[key].state = "loading";
36323
- newInfo[key].errorMessage = "";
36324
- newInfo[key].params = query;
36325
- newInfo[key].loaded = true;
36326
- setInfo(newInfo);
36327
- try {
36328
- const consulta = await axios.request(options);
36329
- const d = consulta.data;
36330
- newInfo[key].state = "success";
36331
- newInfo[key].errorMessage = "";
36332
- newInfo[key].data = merge ? page == 1 ? d.data : [...d.data, ...newInfo[key].data || []] : d.data;
36333
- newInfo[key].totalItems = d.totalItems;
36334
- newInfo[key].totalPages = d.totalPages;
36335
- newInfo[key].currentPage = d.currentPage;
36336
- setInfo({ ...newInfo });
36337
- return d.data;
36338
- } catch (error) {
36339
- const item = http_codes_default.find((s) => s.code == error.status);
36340
- newInfo[key].state = "error";
36341
- newInfo[key].errorMessage = item == null ? void 0 : item.meaning;
36342
- if (error.status == 403) {
36343
- onError == null ? void 0 : onError({ error, ...{ errorMessage: item == null ? void 0 : item.meaning } });
36344
- }
36345
- setInfo({ ...newInfo });
36346
- return error;
36347
- }
36348
- };
36349
- const findFunc = async (id, query) => {
36350
- var _a2, _b2, _c2;
36351
- const options = {
36352
- method: "GET",
36353
- url: `${baseURI}/${key}/${id}`,
36354
- params: { ...query },
36355
- headers: { Authorization: token }
36356
- };
36357
- const newInfo = { ...info };
36358
- newInfo[key].state = "loading";
36359
- newInfo[key].errorMessage = "";
36360
- newInfo[key].params = query;
36361
- newInfo[key].loaded = true;
36362
- setInfo(newInfo);
36363
- try {
36364
- const consulta = await axios.request(options);
36365
- const d = consulta.data;
36366
- newInfo[key].state = "success";
36367
- newInfo[key].errorMessage = "";
36368
- newInfo[key].selectedItem = d;
36369
- const index = (_b2 = (_a2 = newInfo[key]) == null ? void 0 : _a2.data) == null ? void 0 : _b2.findIndex((d2) => (d2 == null ? void 0 : d2.id) == id);
36370
- if (index >= 0) {
36371
- newInfo[key].data[index] = d;
36372
- } else {
36373
- if ((_c2 = newInfo[key]) == null ? void 0 : _c2.data) {
36374
- newInfo[key].data.unshift(d);
36375
- } else {
36376
- newInfo[key].data = [d];
36377
- }
36378
- }
36379
- setInfo({ ...newInfo });
36380
- return d;
36381
- } catch (error) {
36382
- const item = http_codes_default.find((s) => s.code == error.status);
36383
- newInfo[key].state = "error";
36384
- newInfo[key].errorMessage = (item == null ? void 0 : item.meaning) || error.message;
36385
- if (error.status == 403) {
36386
- onError == null ? void 0 : onError({ error, ...{ errorMessage: item == null ? void 0 : item.meaning } });
36387
- }
36388
- setInfo({ ...newInfo });
36389
- return error;
36330
+ const infoRef = useRef2(info);
36331
+ useEffect4(() => {
36332
+ infoRef.current = info;
36333
+ }, [info]);
36334
+ const setKeyLoading = useCallback((key) => {
36335
+ setInfo((prev) => ({
36336
+ ...cloneKey(prev, key),
36337
+ [key]: {
36338
+ ...cloneKey(prev, key)[key],
36339
+ state: "loading",
36340
+ errorMessage: ""
36341
+ }
36342
+ }));
36343
+ }, []);
36344
+ const setKeyError = useCallback(
36345
+ (key, error) => {
36346
+ const meaning = getErrorMeaning(error);
36347
+ if ((error == null ? void 0 : error.status) === 403) onError == null ? void 0 : onError({ error, errorMessage: meaning });
36348
+ setInfo((prev) => ({
36349
+ ...cloneKey(prev, key),
36350
+ [key]: {
36351
+ ...cloneKey(prev, key)[key],
36352
+ state: "error",
36353
+ errorMessage: meaning || (error == null ? void 0 : error.message)
36390
36354
  }
36391
- };
36392
- const mergeDataArray = (existingData, newItem, matchId) => {
36393
- if (!existingData) return [newItem];
36394
- const index = existingData.findIndex((d) => (d == null ? void 0 : d.id) == matchId);
36395
- if (index >= 0) {
36396
- return existingData.map(
36397
- (d, i) => i === index ? newItem : d
36398
- );
36355
+ }));
36356
+ },
36357
+ [onError]
36358
+ );
36359
+ const show = useCallback(
36360
+ async (key, { limit = 10, page = 1, merge = true, ...query }) => {
36361
+ setInfo((prev) => ({
36362
+ ...cloneKey(prev, key),
36363
+ [key]: {
36364
+ ...cloneKey(prev, key)[key],
36365
+ state: "loading",
36366
+ errorMessage: "",
36367
+ params: query,
36368
+ loaded: true
36399
36369
  }
36400
- return [newItem, ...existingData];
36401
- };
36402
- const bodyCreateFunc = async (data) => {
36403
- var _a2, _b2, _c2, _d2, _e2;
36404
- const options = {
36405
- method: "POST",
36406
- url: `${baseURI}/${key}`,
36407
- data: Array.isArray(data) ? [...data] : { ...data },
36370
+ }));
36371
+ try {
36372
+ const { data: res } = await axios.get(`${baseURI}/${key}`, {
36373
+ params: { limit, page, ...query },
36408
36374
  headers: { Authorization: token }
36409
- };
36410
- const newInfo = {
36411
- ...info,
36412
- [key]: {
36413
- ...info[key],
36414
- data: [...(_b2 = (_a2 = info[key]) == null ? void 0 : _a2.data) != null ? _b2 : []],
36415
- state: "loading",
36416
- errorMessage: ""
36417
- }
36418
- };
36419
- setInfo(newInfo);
36420
- try {
36421
- const consulta = await axios.request(options);
36422
- const d = consulta.data;
36423
- let updatedData;
36424
- if (Array.isArray(data)) {
36425
- updatedData = [...(_d2 = (_c2 = newInfo[key]) == null ? void 0 : _c2.data) != null ? _d2 : []];
36426
- for (const datum of data) {
36427
- updatedData = mergeDataArray(updatedData, d, datum == null ? void 0 : datum.id);
36428
- }
36429
- updatedData = updatedData.flat();
36430
- } else {
36431
- updatedData = mergeDataArray(
36432
- (_e2 = newInfo[key]) == null ? void 0 : _e2.data,
36433
- d,
36434
- data == null ? void 0 : data.id
36435
- );
36436
- }
36437
- setInfo({
36438
- ...newInfo,
36375
+ });
36376
+ setInfo((prev) => {
36377
+ var _a, _b;
36378
+ const prevData = (_b = (_a = prev[key]) == null ? void 0 : _a.data) != null ? _b : [];
36379
+ const merged = merge ? page === 1 ? res.data : [...prevData, ...res.data] : res.data;
36380
+ return {
36381
+ ...prev,
36439
36382
  [key]: {
36440
- ...newInfo[key],
36383
+ ...prev[key],
36441
36384
  state: "success",
36442
36385
  errorMessage: "",
36443
- selectedItem: Array.isArray(data) ? newInfo[key].selectedItem : d,
36444
- data: updatedData
36445
- }
36446
- });
36447
- return d;
36448
- } catch (error) {
36449
- const item = http_codes_default.find((s) => s.code == error.status);
36450
- if (error.status == 403) {
36451
- onError == null ? void 0 : onError({ error, errorMessage: item == null ? void 0 : item.meaning });
36452
- }
36453
- setInfo({
36454
- ...newInfo,
36455
- [key]: {
36456
- ...newInfo[key],
36457
- state: "error",
36458
- errorMessage: (item == null ? void 0 : item.meaning) || error.message
36386
+ data: merged,
36387
+ totalItems: res.totalItems,
36388
+ totalPages: res.totalPages,
36389
+ currentPage: res.currentPage
36459
36390
  }
36460
- });
36461
- return error;
36462
- }
36463
- };
36464
- const formCreateFunc = async (data) => {
36465
- var _a2, _b2, _c2, _d2, _e2;
36466
- const formData = new FormData();
36467
- if (Array.isArray(data)) {
36468
- data.forEach((item, i) => {
36469
- for (const [k, v] of Object.entries(item)) {
36470
- formData.append(`${k}[${i}]`, v);
36471
- }
36472
- });
36473
- } else {
36474
- for (const [k, v] of Object.entries(data)) {
36475
- formData.append(k, v);
36476
- }
36391
+ };
36392
+ });
36393
+ return res.data;
36394
+ } catch (error) {
36395
+ setKeyError(key, error);
36396
+ return error;
36397
+ }
36398
+ },
36399
+ [baseURI, token, setKeyError]
36400
+ );
36401
+ const find = useCallback(
36402
+ async (key, id, query) => {
36403
+ setInfo((prev) => ({
36404
+ ...cloneKey(prev, key),
36405
+ [key]: {
36406
+ ...cloneKey(prev, key)[key],
36407
+ state: "loading",
36408
+ errorMessage: "",
36409
+ params: query,
36410
+ loaded: true
36477
36411
  }
36478
- const options = {
36479
- method: "POST",
36480
- url: `${baseURI}/${key}`,
36481
- data: formData,
36482
- headers: {
36483
- Authorization: token,
36484
- "Content-Type": "multipart/form-data"
36485
- }
36486
- };
36487
- const newInfo = {
36488
- ...info,
36489
- [key]: {
36490
- ...info[key],
36491
- data: [...(_b2 = (_a2 = info[key]) == null ? void 0 : _a2.data) != null ? _b2 : []],
36492
- state: "loading",
36493
- errorMessage: ""
36494
- }
36495
- };
36496
- setInfo(newInfo);
36497
- try {
36498
- const consulta = await axios.request(options);
36499
- const d = consulta.data;
36500
- let updatedData;
36501
- if (Array.isArray(data)) {
36502
- updatedData = [...(_d2 = (_c2 = newInfo[key]) == null ? void 0 : _c2.data) != null ? _d2 : []];
36503
- for (const datum of data) {
36504
- updatedData = mergeDataArray(updatedData, d, datum == null ? void 0 : datum.id);
36505
- }
36506
- updatedData = updatedData.flat();
36507
- } else {
36508
- updatedData = mergeDataArray(
36509
- (_e2 = newInfo[key]) == null ? void 0 : _e2.data,
36510
- d,
36511
- data == null ? void 0 : data.id
36512
- );
36513
- }
36514
- setInfo({
36515
- ...newInfo,
36412
+ }));
36413
+ try {
36414
+ const { data: d } = await axios.get(`${baseURI}/${key}/${id}`, {
36415
+ params: query,
36416
+ headers: { Authorization: token }
36417
+ });
36418
+ setInfo((prev) => {
36419
+ var _a, _b;
36420
+ return {
36421
+ ...prev,
36516
36422
  [key]: {
36517
- ...newInfo[key],
36423
+ ...prev[key],
36518
36424
  state: "success",
36519
36425
  errorMessage: "",
36520
- selectedItem: Array.isArray(data) ? newInfo[key].selectedItem : d,
36521
- data: updatedData
36426
+ selectedItem: d,
36427
+ data: mergeDataArray((_b = (_a = prev[key]) == null ? void 0 : _a.data) != null ? _b : [], d, id)
36522
36428
  }
36523
- });
36524
- return d;
36525
- } catch (error) {
36526
- const item = http_codes_default.find((s) => s.code == error.status);
36527
- if (error.status == 403) {
36528
- onError == null ? void 0 : onError({ error, errorMessage: item == null ? void 0 : item.meaning });
36529
- }
36530
- setInfo({
36531
- ...newInfo,
36532
- [key]: {
36533
- ...newInfo[key],
36534
- state: "error",
36535
- errorMessage: (item == null ? void 0 : item.meaning) || error.message
36536
- }
36537
- });
36538
- return error;
36539
- }
36540
- };
36541
- acc[key] = {
36542
- ...endpoint,
36543
- loaded: false,
36544
- show: showFunc,
36545
- find: findFunc,
36546
- create: async (data) => {
36547
- const hasFile = Array.isArray(data) ? data.some(
36548
- (item) => Object.values(item).some((value) => value instanceof File)
36549
- ) : Object.values(data).some((value) => value instanceof File);
36550
- if (hasFile) {
36551
- return await formCreateFunc(data);
36552
- }
36553
- return await bodyCreateFunc(data);
36554
- },
36555
- update: async (id, data) => {
36556
- var _a2, _b2, _c2, _d2, _e2;
36557
- const options = {
36558
- method: "PATCH",
36559
- url: `${baseURI}/${key}/${id}`,
36560
- data: Array.isArray(data) ? [...data] : { ...data },
36561
- headers: { Authorization: token }
36562
36429
  };
36563
- const newInfo = {
36564
- ...info,
36430
+ });
36431
+ return d;
36432
+ } catch (error) {
36433
+ setKeyError(key, error);
36434
+ return error;
36435
+ }
36436
+ },
36437
+ [baseURI, token, setKeyError]
36438
+ );
36439
+ const buildFormData = (data) => {
36440
+ const fd = new FormData();
36441
+ if (Array.isArray(data)) {
36442
+ data.forEach((item, i) => {
36443
+ Object.entries(item).forEach(
36444
+ ([k, v]) => fd.append(`${k}[${i}]`, v)
36445
+ );
36446
+ });
36447
+ } else {
36448
+ Object.entries(data).forEach(([k, v]) => fd.append(k, v));
36449
+ }
36450
+ return fd;
36451
+ };
36452
+ 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);
36453
+ const create = useCallback(
36454
+ async (key, data) => {
36455
+ setKeyLoading(key);
36456
+ const isForm = hasFiles(data);
36457
+ const payload = isForm ? buildFormData(data) : Array.isArray(data) ? [...data] : { ...data };
36458
+ const headers = { Authorization: token };
36459
+ if (isForm) headers["Content-Type"] = "multipart/form-data";
36460
+ try {
36461
+ const { data: d } = await axios.post(`${baseURI}/${key}`, payload, {
36462
+ headers
36463
+ });
36464
+ setInfo((prev) => {
36465
+ var _a, _b;
36466
+ const prevData = (_b = (_a = prev[key]) == null ? void 0 : _a.data) != null ? _b : [];
36467
+ const incoming = Array.isArray(d) ? d : [d];
36468
+ const updatedData = incoming.reduce(
36469
+ (acc, item) => mergeDataArray(acc, item, item == null ? void 0 : item.id),
36470
+ [...prevData]
36471
+ );
36472
+ return {
36473
+ ...prev,
36565
36474
  [key]: {
36566
- ...info[key],
36567
- // ✅ copia superficial del key
36568
- data: [...(_b2 = (_a2 = info[key]) == null ? void 0 : _a2.data) != null ? _b2 : []],
36569
- // copia del array
36570
- state: "loading",
36571
- errorMessage: ""
36475
+ ...prev[key],
36476
+ state: "success",
36477
+ errorMessage: "",
36478
+ selectedItem: Array.isArray(data) ? prev[key].selectedItem : d,
36479
+ data: updatedData
36572
36480
  }
36573
36481
  };
36574
- setInfo(newInfo);
36575
- try {
36576
- const consulta = await axios.request(options);
36577
- const d = consulta.data;
36578
- const index = (_d2 = (_c2 = newInfo[key]) == null ? void 0 : _c2.data) == null ? void 0 : _d2.findIndex(
36579
- (item) => (item == null ? void 0 : item.id) == id
36580
- );
36581
- const updatedData = index >= 0 ? newInfo[key].data.map(
36582
- (item, i) => i === index ? { ...item, ...d } : item
36583
- ) : ((_e2 = newInfo[key]) == null ? void 0 : _e2.data) ? [d, ...newInfo[key].data] : [d];
36584
- const updatedInfo = {
36585
- ...newInfo,
36586
- [key]: {
36587
- ...newInfo[key],
36588
- state: "success",
36589
- errorMessage: "",
36590
- selectedItem: { ...newInfo[key].selectedItem, ...d },
36591
- data: updatedData
36592
- }
36593
- };
36594
- setInfo(updatedInfo);
36595
- return d;
36596
- } catch (error) {
36597
- const item = http_codes_default.find((s) => s.code == error.status);
36598
- setInfo({
36599
- ...newInfo,
36600
- [key]: {
36601
- ...newInfo[key],
36602
- state: "error",
36603
- errorMessage: (item == null ? void 0 : item.meaning) || error.message
36604
- }
36605
- });
36606
- if (error.status == 403) {
36607
- onError == null ? void 0 : onError({ error, ...{ errorMessage: item == null ? void 0 : item.meaning } });
36482
+ });
36483
+ return d;
36484
+ } catch (error) {
36485
+ setKeyError(key, error);
36486
+ return error;
36487
+ }
36488
+ },
36489
+ [baseURI, token, setKeyLoading, setKeyError]
36490
+ );
36491
+ const update = useCallback(
36492
+ async (key, id, data) => {
36493
+ setKeyLoading(key);
36494
+ try {
36495
+ const { data: d } = await axios.patch(
36496
+ `${baseURI}/${key}/${id}`,
36497
+ Array.isArray(data) ? [...data] : { ...data },
36498
+ { headers: { Authorization: token } }
36499
+ );
36500
+ setInfo((prev) => {
36501
+ var _a, _b, _c, _d;
36502
+ return {
36503
+ ...prev,
36504
+ [key]: {
36505
+ ...prev[key],
36506
+ state: "success",
36507
+ errorMessage: "",
36508
+ selectedItem: { ...prev[key].selectedItem, ...d },
36509
+ data: mergeDataArray(
36510
+ (_b = (_a = prev[key]) == null ? void 0 : _a.data) != null ? _b : [],
36511
+ { ...(_d = (_c = prev[key]) == null ? void 0 : _c.data) == null ? void 0 : _d.find((i) => (i == null ? void 0 : i.id) == id), ...d },
36512
+ id
36513
+ )
36608
36514
  }
36609
- return error;
36610
- }
36611
- },
36612
- remove: async (id) => {
36613
- var _a2, _b2, _c2, _d2, _e2;
36614
- const options = {
36615
- method: "DELETE",
36616
- url: `${baseURI}/${key}/${id}`,
36617
- headers: { Authorization: token }
36618
36515
  };
36619
- const newInfo = {
36620
- ...info,
36516
+ });
36517
+ return d;
36518
+ } catch (error) {
36519
+ setKeyError(key, error);
36520
+ return error;
36521
+ }
36522
+ },
36523
+ [baseURI, token, setKeyLoading, setKeyError]
36524
+ );
36525
+ const remove = useCallback(
36526
+ async (key, id) => {
36527
+ setKeyLoading(key);
36528
+ try {
36529
+ const { data: d } = await axios.delete(`${baseURI}/${key}/${id}`, {
36530
+ headers: { Authorization: token }
36531
+ });
36532
+ setInfo((prev) => {
36533
+ var _a, _b;
36534
+ return {
36535
+ ...prev,
36621
36536
  [key]: {
36622
- ...info[key],
36623
- data: [...(_b2 = (_a2 = info[key]) == null ? void 0 : _a2.data) != null ? _b2 : []],
36624
- state: "loading",
36625
- errorMessage: ""
36537
+ ...prev[key],
36538
+ state: "success",
36539
+ errorMessage: "",
36540
+ selectedItem: d,
36541
+ data: ((_b = (_a = prev[key]) == null ? void 0 : _a.data) != null ? _b : []).filter((item) => (item == null ? void 0 : item.id) != id)
36626
36542
  }
36627
36543
  };
36628
- setInfo(newInfo);
36629
- try {
36630
- const consulta = await axios.request(options);
36631
- const d = consulta.data;
36632
- setInfo({
36633
- ...newInfo,
36634
- [key]: {
36635
- ...newInfo[key],
36636
- state: "success",
36637
- errorMessage: "",
36638
- selectedItem: d,
36639
- 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 : []
36640
- // ✅ filter en lugar de splice
36641
- }
36642
- });
36643
- return d.data;
36644
- } catch (error) {
36645
- const item = http_codes_default.find((s) => s.code == error.status);
36646
- if (error.status == 403) {
36647
- onError == null ? void 0 : onError({ error, errorMessage: item == null ? void 0 : item.meaning });
36648
- }
36649
- setInfo({
36650
- ...newInfo,
36651
- [key]: {
36652
- ...newInfo[key],
36653
- state: "error",
36654
- errorMessage: (item == null ? void 0 : item.meaning) || error.message
36655
- }
36656
- });
36657
- return error;
36658
- }
36659
- },
36660
- totalPages: (_a = info[key]) == null ? void 0 : _a.totalPages,
36661
- currentPage: (_b = info[key]) == null ? void 0 : _b.currentPage,
36662
- state: ((_c = info[key]) == null ? void 0 : _c.state) || "success",
36663
- errorMessage: (_d = info[key]) == null ? void 0 : _d.errorMessage,
36664
- params: (_e = info[key]) == null ? void 0 : _e.params,
36665
- setLoaded: () => {
36666
- const newInfo = { ...info };
36667
- newInfo[key].loaded = true;
36668
- setInfo(newInfo);
36669
- },
36670
- getAllPages: async (limit = 100) => {
36671
- const allData = [];
36672
- let currentPage = 1;
36673
- let totalPages = 1;
36674
- while (currentPage <= totalPages) {
36675
- const response = await results[key].show({
36676
- limit,
36677
- page: currentPage
36678
- });
36679
- const currentInfo = info[key];
36680
- if (Array.isArray(response)) {
36681
- allData.push(...response);
36682
- }
36683
- currentPage = currentInfo.currentPage || currentPage + 1;
36684
- totalPages = currentInfo.totalPages || currentPage;
36685
- }
36686
- const newInfo = { ...info };
36687
- newInfo[key].data = allData;
36688
- newInfo[key].currentPage = currentPage;
36689
- newInfo[key].totalPages = totalPages;
36690
- setInfo(newInfo);
36691
- return allData;
36692
- },
36693
- data: ((_f = info[key]) == null ? void 0 : _f.data) || [],
36694
- selectedItem: ((_g = info[key]) == null ? void 0 : _g.selectedItem) || {}
36695
- };
36696
- return acc;
36544
+ });
36545
+ return d;
36546
+ } catch (error) {
36547
+ setKeyError(key, error);
36548
+ return error;
36549
+ }
36697
36550
  },
36698
- {}
36551
+ [baseURI, token, setKeyLoading, setKeyError]
36552
+ );
36553
+ const getAllPages = useCallback(
36554
+ async (key, limit = 100) => {
36555
+ var _a, _b;
36556
+ const allData = [];
36557
+ let page = 1;
36558
+ let totalPages = 1;
36559
+ do {
36560
+ const pageData = await show(key, { limit, page, merge: false });
36561
+ if (Array.isArray(pageData)) allData.push(...pageData);
36562
+ totalPages = (_b = (_a = infoRef.current[key]) == null ? void 0 : _a.totalPages) != null ? _b : page;
36563
+ page++;
36564
+ } while (page <= totalPages);
36565
+ setInfo((prev) => ({
36566
+ ...prev,
36567
+ [key]: { ...prev[key], data: allData }
36568
+ }));
36569
+ return allData;
36570
+ },
36571
+ [show]
36699
36572
  );
36700
- async function doSome() {
36701
- var _a, _b, _c, _d, _e;
36702
- const key = Object.keys(info).find((k) => {
36703
- return info[k].loaded === false;
36704
- });
36705
- if (key) {
36706
- if ((_a = info[key]) == null ? void 0 : _a.id) {
36707
- await results[key].find((_b = info[key]) == null ? void 0 : _b.id, (_c = info[key]) == null ? void 0 : _c.defaultParams);
36708
- } else if ((_d = info[key]) == null ? void 0 : _d.defaultParams) {
36709
- await results[key].show((_e = info[key]) == null ? void 0 : _e.defaultParams);
36710
- } else {
36711
- results[key].setLoaded();
36712
- }
36713
- }
36714
- }
36715
36573
  useEffect4(() => {
36716
- doSome();
36717
- }, [JSON.stringify(Object.values(info).map((v) => v.loaded))]);
36574
+ var _a, _b;
36575
+ const key = Object.keys(info).find((k) => info[k].loaded === false);
36576
+ if (!key) return;
36577
+ if ((_a = info[key]) == null ? void 0 : _a.id) {
36578
+ find(key, info[key].id, info[key].defaultParams);
36579
+ } else if ((_b = info[key]) == null ? void 0 : _b.defaultParams) {
36580
+ show(key, info[key].defaultParams);
36581
+ } else {
36582
+ setInfo((prev) => ({
36583
+ ...prev,
36584
+ [key]: { ...prev[key], loaded: true }
36585
+ }));
36586
+ }
36587
+ }, [
36588
+ Object.keys(info).map((k) => info[k].loaded).join(",")
36589
+ ]);
36590
+ const results = Object.keys(endpoints).reduce((acc, key) => {
36591
+ var _a, _b, _c, _d;
36592
+ const s = info[key];
36593
+ acc[key] = {
36594
+ ...endpoints[key],
36595
+ data: (_a = s == null ? void 0 : s.data) != null ? _a : [],
36596
+ selectedItem: (_b = s == null ? void 0 : s.selectedItem) != null ? _b : {},
36597
+ state: (_c = s == null ? void 0 : s.state) != null ? _c : "success",
36598
+ errorMessage: s == null ? void 0 : s.errorMessage,
36599
+ params: s == null ? void 0 : s.params,
36600
+ totalPages: s == null ? void 0 : s.totalPages,
36601
+ currentPage: s == null ? void 0 : s.currentPage,
36602
+ totalItems: s == null ? void 0 : s.totalItems,
36603
+ loaded: (_d = s == null ? void 0 : s.loaded) != null ? _d : false,
36604
+ show: (opts) => show(key, opts),
36605
+ find: (id, query) => find(key, id, query),
36606
+ create: (data) => create(key, data),
36607
+ update: (id, data) => update(key, id, data),
36608
+ remove: (id) => remove(key, id),
36609
+ getAllPages: (limit) => getAllPages(key, limit),
36610
+ setLoaded: () => setInfo((prev) => ({ ...prev, [key]: { ...prev[key], loaded: true } }))
36611
+ };
36612
+ return acc;
36613
+ }, {});
36718
36614
  return results;
36719
36615
  }
36720
36616
 
36721
36617
  // src/select/index.tsx
36722
36618
  import React5, {
36723
36619
  useEffect as useEffect5,
36724
- useRef as useRef2,
36620
+ useRef as useRef3,
36725
36621
  useState as useState8
36726
36622
  } from "react";
36727
36623
 
@@ -36780,7 +36676,7 @@ function Select({
36780
36676
  );
36781
36677
  const [hasEdition, setHasEdition] = useState8(false);
36782
36678
  const [highlightedIndex, setHighlightedIndex] = useState8(-1);
36783
- const inputRef = useRef2(null);
36679
+ const inputRef = useRef3(null);
36784
36680
  const validOption = (value) => {
36785
36681
  return options.find((opt) => opt.label == value);
36786
36682
  };
@@ -36828,7 +36724,7 @@ function Select({
36828
36724
  setInputValue(opt.label);
36829
36725
  setIsOpen(false);
36830
36726
  };
36831
- const containerRef = useRef2(null);
36727
+ const containerRef = useRef3(null);
36832
36728
  const [openUpwards, setOpenUpwards] = useState8(false);
36833
36729
  useEffect5(() => {
36834
36730
  if (isOpen && containerRef.current) {
@@ -36963,10 +36859,10 @@ function Select({
36963
36859
  }
36964
36860
 
36965
36861
  // src/modal/index.tsx
36966
- import { cloneElement as cloneElement2, useEffect as useEffect6, useMemo as useMemo4 } from "react";
36862
+ import { cloneElement as cloneElement2, useEffect as useEffect6, useMemo as useMemo3 } from "react";
36967
36863
 
36968
36864
  // src/pop/index.tsx
36969
- import { useState as useState9, useCallback, useRef as useRef3 } from "react";
36865
+ import { useState as useState9, useCallback as useCallback2, useRef as useRef4 } from "react";
36970
36866
 
36971
36867
  // src/pop/actions.tsx
36972
36868
  import { jsx as jsx11, jsxs as jsxs8 } from "react/jsx-runtime";
@@ -37211,8 +37107,8 @@ var INITIAL_STATE = {
37211
37107
  };
37212
37108
  function usePopup() {
37213
37109
  const [popup, setPopup] = useState9(INITIAL_STATE);
37214
- const messageRef = useRef3(null);
37215
- const open = useCallback(
37110
+ const messageRef = useRef4(null);
37111
+ const open = useCallback2(
37216
37112
  (partial) => {
37217
37113
  const { message, ...rest } = partial;
37218
37114
  messageRef.current = message;
@@ -37220,7 +37116,7 @@ function usePopup() {
37220
37116
  },
37221
37117
  []
37222
37118
  );
37223
- const close = useCallback((confirmed, value) => {
37119
+ const close = useCallback2((confirmed, value) => {
37224
37120
  setPopup((prev) => {
37225
37121
  var _a, _b;
37226
37122
  if (confirmed) (_a = prev.onConfirm) == null ? void 0 : _a.call(prev, value);
@@ -37228,7 +37124,7 @@ function usePopup() {
37228
37124
  return { ...prev, visible: false, inputValue: "" };
37229
37125
  });
37230
37126
  }, []);
37231
- const alert2 = useCallback(
37127
+ const alert2 = useCallback2(
37232
37128
  (message, color = "primary") => new Promise(
37233
37129
  (resolve) => open({
37234
37130
  type: "alert",
@@ -37240,7 +37136,7 @@ function usePopup() {
37240
37136
  ),
37241
37137
  [open]
37242
37138
  );
37243
- const modal = useCallback(
37139
+ const modal = useCallback2(
37244
37140
  (message, color = "primary", icons = false, full = false) => new Promise(
37245
37141
  (resolve) => open({
37246
37142
  type: "modal",
@@ -37254,7 +37150,7 @@ function usePopup() {
37254
37150
  ),
37255
37151
  [open]
37256
37152
  );
37257
- const confirm = useCallback(
37153
+ const confirm = useCallback2(
37258
37154
  (message, color = "primary") => new Promise(
37259
37155
  (resolve) => open({
37260
37156
  type: "confirm",
@@ -37266,7 +37162,7 @@ function usePopup() {
37266
37162
  ),
37267
37163
  [open]
37268
37164
  );
37269
- const prompt = useCallback(
37165
+ const prompt = useCallback2(
37270
37166
  (message, color = "primary") => new Promise(
37271
37167
  (resolve) => open({
37272
37168
  type: "prompt",
@@ -37278,7 +37174,7 @@ function usePopup() {
37278
37174
  ),
37279
37175
  [open]
37280
37176
  );
37281
- const updateMessage = useCallback((message) => {
37177
+ const updateMessage = useCallback2((message) => {
37282
37178
  messageRef.current = message;
37283
37179
  setPopup((prev) => ({ ...prev }));
37284
37180
  }, []);
@@ -37311,7 +37207,7 @@ function Modal({
37311
37207
  }) {
37312
37208
  const pop = usePopup();
37313
37209
  const hide = () => pop.close(false);
37314
- const childrenWithHide = useMemo4(
37210
+ const childrenWithHide = useMemo3(
37315
37211
  () => cloneElement2(children, { hide }),
37316
37212
  [children]
37317
37213
  );
@@ -37574,14 +37470,14 @@ function DocumentViewer({ item }) {
37574
37470
  // src/table3/index.tsx
37575
37471
  import React9, {
37576
37472
  useEffect as useEffect9,
37577
- useMemo as useMemo7,
37473
+ useMemo as useMemo6,
37578
37474
  useReducer as useReducer2,
37579
- useRef as useRef5,
37475
+ useRef as useRef6,
37580
37476
  useState as useState13
37581
37477
  } from "react";
37582
37478
 
37583
37479
  // src/table3/filter.tsx
37584
- import { useEffect as useEffect8, useMemo as useMemo5, useState as useState11 } from "react";
37480
+ import { useEffect as useEffect8, useMemo as useMemo4, useState as useState11 } from "react";
37585
37481
 
37586
37482
  // src/table3/filters.tsx
37587
37483
  import { jsx as jsx21, jsxs as jsxs13 } from "react/jsx-runtime";
@@ -37693,12 +37589,12 @@ function Filter({
37693
37589
  }) {
37694
37590
  const [visible, setVisible] = useState11(false);
37695
37591
  const [text, setText] = useState11("");
37696
- const items = useMemo5(
37592
+ const items = useMemo4(
37697
37593
  () => [...new Set(Object.values(objectData).map((o) => o[h]))],
37698
37594
  [objectData]
37699
37595
  );
37700
37596
  const [selected, setSelected] = useState11(items);
37701
- const itemsFiltered = useMemo5(
37597
+ const itemsFiltered = useMemo4(
37702
37598
  () => items.sort((a, b) => `${a}`.localeCompare(b)).filter((item) => {
37703
37599
  if (!text) return true;
37704
37600
  return `${item}`.toLowerCase().includes(text.toLowerCase());
@@ -38307,7 +38203,7 @@ function TableFooter({
38307
38203
  }
38308
38204
 
38309
38205
  // src/table3/dialog.tsx
38310
- import React8, { useMemo as useMemo6 } from "react";
38206
+ import React8, { useMemo as useMemo5 } from "react";
38311
38207
  import { jsx as jsx28, jsxs as jsxs19 } from "react/jsx-runtime";
38312
38208
  function Dialog3({
38313
38209
  modalRef,
@@ -38324,7 +38220,7 @@ function Dialog3({
38324
38220
  var _a;
38325
38221
  return (_a = modalRef.current) == null ? void 0 : _a.close();
38326
38222
  };
38327
- const clonedModal = useMemo6(() => {
38223
+ const clonedModal = useMemo5(() => {
38328
38224
  if (dialogRow && React8.isValidElement(children)) {
38329
38225
  return React8.cloneElement(children, {
38330
38226
  key: JSON.stringify(dialogRow),
@@ -38427,7 +38323,7 @@ function Table3({
38427
38323
  setObjectData(data);
38428
38324
  setPage(1);
38429
38325
  }, [data]);
38430
- const headers = useMemo7(() => {
38326
+ const headers = useMemo6(() => {
38431
38327
  if (!objectData) return [];
38432
38328
  return [
38433
38329
  ...new Set(
@@ -38435,12 +38331,12 @@ function Table3({
38435
38331
  )
38436
38332
  ];
38437
38333
  }, [objectData]);
38438
- const totalPages = useMemo7(() => {
38334
+ const totalPages = useMemo6(() => {
38439
38335
  if (!objectData) return 0;
38440
38336
  return maxItems ? Math.ceil(Object.keys(objectData).length / maxItems) : 1;
38441
38337
  }, [objectData, maxItems]);
38442
38338
  const [sort, setSort] = useState13(sortBy);
38443
- const modalRef = useRef5(null);
38339
+ const modalRef = useRef6(null);
38444
38340
  const [dialogRow, setDialogRow] = useState13({});
38445
38341
  const context = {
38446
38342
  objectData,