next-recomponents 2.0.36 → 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,398 +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;
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)
36347
36354
  }
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;
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
36390
36369
  }
36391
- };
36392
- const bodyCreateFunc = async (data) => {
36393
- var _a2, _b2, _c2, _d2, _e2, _f2;
36394
- const options = {
36395
- method: "POST",
36396
- url: `${baseURI}/${key}`,
36397
- data: Array.isArray(data) ? [...data] : { ...data },
36370
+ }));
36371
+ try {
36372
+ const { data: res } = await axios.get(`${baseURI}/${key}`, {
36373
+ params: { limit, page, ...query },
36398
36374
  headers: { Authorization: token }
36399
- };
36400
- const newInfo = { ...info };
36401
- newInfo[key].state = "loading";
36402
- newInfo[key].errorMessage = "";
36403
- setInfo(newInfo);
36404
- try {
36405
- const consulta = await axios.request(options);
36406
- const d = consulta.data;
36407
- newInfo[key].state = "success";
36408
- newInfo[key].errorMessage = "";
36409
- if (Array.isArray(data)) {
36410
- for (let datum of data) {
36411
- const index = (_b2 = (_a2 = newInfo[key]) == null ? void 0 : _a2.data) == null ? void 0 : _b2.findIndex(
36412
- (d2) => (d2 == null ? void 0 : d2.id) == (datum == null ? void 0 : datum.id)
36413
- );
36414
- if (index >= 0) {
36415
- newInfo[key].data[index] = d;
36416
- } else {
36417
- if ((_c2 = newInfo[key]) == null ? void 0 : _c2.data) {
36418
- newInfo[key].data.unshift(d);
36419
- } else {
36420
- newInfo[key].data = [d];
36421
- }
36422
- }
36423
- }
36424
- newInfo[key].data = newInfo[key].data.flat();
36425
- } else {
36426
- newInfo[key].selectedItem = d;
36427
- const index = (_e2 = (_d2 = newInfo[key]) == null ? void 0 : _d2.data) == null ? void 0 : _e2.findIndex(
36428
- (d2) => (d2 == null ? void 0 : d2.id) == (data == null ? void 0 : data.id)
36429
- );
36430
- if (index >= 0) {
36431
- newInfo[key].data[index] = d;
36432
- } else {
36433
- if ((_f2 = newInfo[key]) == null ? void 0 : _f2.data) {
36434
- newInfo[key].data.unshift(d);
36435
- } else {
36436
- newInfo[key].data = [d];
36437
- }
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,
36382
+ [key]: {
36383
+ ...prev[key],
36384
+ state: "success",
36385
+ errorMessage: "",
36386
+ data: merged,
36387
+ totalItems: res.totalItems,
36388
+ totalPages: res.totalPages,
36389
+ currentPage: res.currentPage
36438
36390
  }
36439
- }
36440
- setInfo({ ...newInfo });
36441
- return d;
36442
- } catch (error) {
36443
- const item = http_codes_default.find((s) => s.code == error.status);
36444
- newInfo[key].state = "error";
36445
- newInfo[key].errorMessage = (item == null ? void 0 : item.meaning) || error.message;
36446
- if (error.status == 403) {
36447
- onError == null ? void 0 : onError({ error, ...{ errorMessage: item == null ? void 0 : item.meaning } });
36448
- }
36449
- setInfo({ ...newInfo });
36450
- return error;
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
36451
36411
  }
36452
- };
36453
- const formCreateFunc = async (data) => {
36454
- var _a2, _b2, _c2, _d2, _e2, _f2;
36455
- const newInfo = { ...info };
36456
- newInfo[key].state = "loading";
36457
- newInfo[key].errorMessage = "";
36458
- setInfo(newInfo);
36459
- try {
36460
- const formData = new FormData();
36461
- if (Array.isArray(data)) {
36462
- data.forEach((item, i) => {
36463
- for (const [k, v] of Object.entries(item)) {
36464
- formData.append(`${k}[${i}]`, v);
36465
- }
36466
- });
36467
- } else {
36468
- for (const [k, v] of Object.entries(data)) {
36469
- formData.append(k, v);
36470
- }
36471
- }
36472
- const options = {
36473
- method: "POST",
36474
- url: `${baseURI}/${key}`,
36475
- data: formData,
36476
- headers: {
36477
- Authorization: token,
36478
- "Content-Type": "multipart/form-data"
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,
36422
+ [key]: {
36423
+ ...prev[key],
36424
+ state: "success",
36425
+ errorMessage: "",
36426
+ selectedItem: d,
36427
+ data: mergeDataArray((_b = (_a = prev[key]) == null ? void 0 : _a.data) != null ? _b : [], d, id)
36479
36428
  }
36480
36429
  };
36481
- const consulta = await axios.request(options);
36482
- const d = consulta.data;
36483
- newInfo[key].state = "success";
36484
- newInfo[key].errorMessage = "";
36485
- if (Array.isArray(data)) {
36486
- for (let datum of data) {
36487
- const index = (_b2 = (_a2 = newInfo[key]) == null ? void 0 : _a2.data) == null ? void 0 : _b2.findIndex(
36488
- (d2) => (d2 == null ? void 0 : d2.id) == (datum == null ? void 0 : datum.id)
36489
- );
36490
- if (index >= 0) {
36491
- newInfo[key].data[index] = d;
36492
- } else {
36493
- if ((_c2 = newInfo[key]) == null ? void 0 : _c2.data) {
36494
- newInfo[key].data.unshift(d);
36495
- } else {
36496
- newInfo[key].data = [d];
36497
- }
36498
- }
36499
- }
36500
- newInfo[key].data = newInfo[key].data.flat();
36501
- } else {
36502
- newInfo[key].selectedItem = d;
36503
- const index = (_e2 = (_d2 = newInfo[key]) == null ? void 0 : _d2.data) == null ? void 0 : _e2.findIndex(
36504
- (d2) => (d2 == null ? void 0 : d2.id) == (data == null ? void 0 : data.id)
36505
- );
36506
- if (index >= 0) {
36507
- newInfo[key].data[index] = d;
36508
- } else {
36509
- if ((_f2 = newInfo[key]) == null ? void 0 : _f2.data) {
36510
- newInfo[key].data.unshift(d);
36511
- } else {
36512
- newInfo[key].data = [d];
36513
- }
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,
36474
+ [key]: {
36475
+ ...prev[key],
36476
+ state: "success",
36477
+ errorMessage: "",
36478
+ selectedItem: Array.isArray(data) ? prev[key].selectedItem : d,
36479
+ data: updatedData
36514
36480
  }
36515
- }
36516
- setInfo({ ...newInfo });
36517
- return d;
36518
- } catch (error) {
36519
- const item = http_codes_default.find((s) => s.code == error.status);
36520
- newInfo[key].state = "error";
36521
- newInfo[key].errorMessage = (item == null ? void 0 : item.meaning) || error.message;
36522
- if (error.status == 403) {
36523
- onError == null ? void 0 : onError({ error, ...{ errorMessage: item == null ? void 0 : item.meaning } });
36524
- }
36525
- setInfo({ ...newInfo });
36526
- return error;
36527
- }
36528
- };
36529
- acc[key] = {
36530
- ...endpoint,
36531
- loaded: false,
36532
- show: showFunc,
36533
- find: findFunc,
36534
- create: async (data) => {
36535
- const hasFile = Array.isArray(data) ? data.some(
36536
- (item) => Object.values(item).some((value) => value instanceof File)
36537
- ) : Object.values(data).some((value) => value instanceof File);
36538
- if (hasFile) {
36539
- return await formCreateFunc(data);
36540
- }
36541
- return await bodyCreateFunc(data);
36542
- },
36543
- update: async (id, data) => {
36544
- var _a2, _b2, _c2;
36545
- const options = {
36546
- method: "PATCH",
36547
- url: `${baseURI}/${key}/${id}`,
36548
- data: Array.isArray(data) ? [...data] : { ...data },
36549
- headers: { Authorization: token }
36550
36481
  };
36551
- const newInfo = { ...info };
36552
- newInfo[key].state = "loading";
36553
- newInfo[key].errorMessage = "";
36554
- setInfo(newInfo);
36555
- try {
36556
- const consulta = await axios.request(options);
36557
- const d = consulta.data;
36558
- newInfo[key].state = "success";
36559
- newInfo[key].errorMessage = "";
36560
- newInfo[key].selectedItem = { ...newInfo[key].selectedItem, ...d };
36561
- const index = (_b2 = (_a2 = newInfo[key]) == null ? void 0 : _a2.data) == null ? void 0 : _b2.findIndex(
36562
- (d2) => (d2 == null ? void 0 : d2.id) == id
36563
- );
36564
- if (index >= 0) {
36565
- newInfo[key].data[index] = { ...newInfo[key].data[index], ...d };
36566
- } else {
36567
- if ((_c2 = newInfo[key]) == null ? void 0 : _c2.data) {
36568
- newInfo[key].data.unshift(d);
36569
- } else {
36570
- newInfo[key].data = [d];
36571
- }
36572
- }
36573
- setInfo({ ...newInfo });
36574
- return d;
36575
- } catch (error) {
36576
- const item = http_codes_default.find((s) => s.code == error.status);
36577
- newInfo[key].state = "error";
36578
- newInfo[key].errorMessage = (item == null ? void 0 : item.meaning) || error.message;
36579
- if (error.status == 403) {
36580
- 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
+ )
36581
36514
  }
36582
- setInfo({ ...newInfo });
36583
- return error;
36584
- }
36585
- },
36586
- remove: async (id) => {
36587
- var _a2, _b2;
36588
- const options = {
36589
- method: "DELETE",
36590
- url: `${baseURI}/${key}/${id}`,
36591
- headers: { Authorization: token }
36592
36515
  };
36593
- const newInfo = { ...info };
36594
- newInfo[key].state = "loading";
36595
- newInfo[key].errorMessage = "";
36596
- setInfo(newInfo);
36597
- try {
36598
- const consulta = await axios.request(options);
36599
- const d = consulta.data;
36600
- newInfo[key].state = "success";
36601
- newInfo[key].errorMessage = "";
36602
- newInfo[key].selectedItem = d;
36603
- const index = (_b2 = (_a2 = newInfo[key]) == null ? void 0 : _a2.data) == null ? void 0 : _b2.findIndex(
36604
- (d2) => (d2 == null ? void 0 : d2.id) == id
36605
- );
36606
- if (index >= 0) {
36607
- newInfo[key].data.splice(index, 1);
36608
- }
36609
- setInfo({ ...newInfo });
36610
- return d.data;
36611
- } catch (error) {
36612
- const item = http_codes_default.find((s) => s.code == error.status);
36613
- newInfo[key].state = "error";
36614
- newInfo[key].errorMessage = (item == null ? void 0 : item.meaning) || error.message;
36615
- if (error.status == 403) {
36616
- onError == null ? void 0 : onError({ error, ...{ errorMessage: item == null ? void 0 : item.meaning } });
36617
- }
36618
- setInfo({ ...newInfo });
36619
- return error;
36620
- }
36621
- },
36622
- totalPages: (_a = info[key]) == null ? void 0 : _a.totalPages,
36623
- currentPage: (_b = info[key]) == null ? void 0 : _b.currentPage,
36624
- state: ((_c = info[key]) == null ? void 0 : _c.state) || "success",
36625
- errorMessage: (_d = info[key]) == null ? void 0 : _d.errorMessage,
36626
- params: (_e = info[key]) == null ? void 0 : _e.params,
36627
- setLoaded: () => {
36628
- const newInfo = { ...info };
36629
- newInfo[key].loaded = true;
36630
- setInfo(newInfo);
36631
- },
36632
- getAllPages: async (limit = 100) => {
36633
- const allData = [];
36634
- let currentPage = 1;
36635
- let totalPages = 1;
36636
- while (currentPage <= totalPages) {
36637
- const response = await results[key].show({
36638
- limit,
36639
- page: currentPage
36640
- });
36641
- const currentInfo = info[key];
36642
- if (Array.isArray(response)) {
36643
- allData.push(...response);
36644
- }
36645
- currentPage = currentInfo.currentPage || currentPage + 1;
36646
- totalPages = currentInfo.totalPages || currentPage;
36647
- }
36648
- const newInfo = { ...info };
36649
- newInfo[key].data = allData;
36650
- newInfo[key].currentPage = currentPage;
36651
- newInfo[key].totalPages = totalPages;
36652
- setInfo(newInfo);
36653
- return allData;
36654
- },
36655
- data: ((_f = info[key]) == null ? void 0 : _f.data) || [],
36656
- selectedItem: ((_g = info[key]) == null ? void 0 : _g.selectedItem) || {}
36657
- };
36658
- return acc;
36516
+ });
36517
+ return d;
36518
+ } catch (error) {
36519
+ setKeyError(key, error);
36520
+ return error;
36521
+ }
36659
36522
  },
36660
- {}
36523
+ [baseURI, token, setKeyLoading, setKeyError]
36661
36524
  );
36662
- async function doSome() {
36663
- var _a, _b, _c, _d, _e;
36664
- const key = Object.keys(info).find((k) => {
36665
- return info[k].loaded === false;
36666
- });
36667
- if (key) {
36668
- if ((_a = info[key]) == null ? void 0 : _a.id) {
36669
- await results[key].find((_b = info[key]) == null ? void 0 : _b.id, (_c = info[key]) == null ? void 0 : _c.defaultParams);
36670
- } else if ((_d = info[key]) == null ? void 0 : _d.defaultParams) {
36671
- await results[key].show((_e = info[key]) == null ? void 0 : _e.defaultParams);
36672
- } else {
36673
- results[key].setLoaded();
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,
36536
+ [key]: {
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)
36542
+ }
36543
+ };
36544
+ });
36545
+ return d;
36546
+ } catch (error) {
36547
+ setKeyError(key, error);
36548
+ return error;
36674
36549
  }
36675
- }
36676
- }
36550
+ },
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]
36572
+ );
36677
36573
  useEffect4(() => {
36678
- doSome();
36679
- }, [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
+ }, {});
36680
36614
  return results;
36681
36615
  }
36682
36616
 
36683
36617
  // src/select/index.tsx
36684
36618
  import React5, {
36685
36619
  useEffect as useEffect5,
36686
- useRef as useRef2,
36620
+ useRef as useRef3,
36687
36621
  useState as useState8
36688
36622
  } from "react";
36689
36623
 
@@ -36742,7 +36676,7 @@ function Select({
36742
36676
  );
36743
36677
  const [hasEdition, setHasEdition] = useState8(false);
36744
36678
  const [highlightedIndex, setHighlightedIndex] = useState8(-1);
36745
- const inputRef = useRef2(null);
36679
+ const inputRef = useRef3(null);
36746
36680
  const validOption = (value) => {
36747
36681
  return options.find((opt) => opt.label == value);
36748
36682
  };
@@ -36790,7 +36724,7 @@ function Select({
36790
36724
  setInputValue(opt.label);
36791
36725
  setIsOpen(false);
36792
36726
  };
36793
- const containerRef = useRef2(null);
36727
+ const containerRef = useRef3(null);
36794
36728
  const [openUpwards, setOpenUpwards] = useState8(false);
36795
36729
  useEffect5(() => {
36796
36730
  if (isOpen && containerRef.current) {
@@ -36925,10 +36859,10 @@ function Select({
36925
36859
  }
36926
36860
 
36927
36861
  // src/modal/index.tsx
36928
- 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";
36929
36863
 
36930
36864
  // src/pop/index.tsx
36931
- import { useState as useState9, useCallback, useRef as useRef3 } from "react";
36865
+ import { useState as useState9, useCallback as useCallback2, useRef as useRef4 } from "react";
36932
36866
 
36933
36867
  // src/pop/actions.tsx
36934
36868
  import { jsx as jsx11, jsxs as jsxs8 } from "react/jsx-runtime";
@@ -37173,8 +37107,8 @@ var INITIAL_STATE = {
37173
37107
  };
37174
37108
  function usePopup() {
37175
37109
  const [popup, setPopup] = useState9(INITIAL_STATE);
37176
- const messageRef = useRef3(null);
37177
- const open = useCallback(
37110
+ const messageRef = useRef4(null);
37111
+ const open = useCallback2(
37178
37112
  (partial) => {
37179
37113
  const { message, ...rest } = partial;
37180
37114
  messageRef.current = message;
@@ -37182,7 +37116,7 @@ function usePopup() {
37182
37116
  },
37183
37117
  []
37184
37118
  );
37185
- const close = useCallback((confirmed, value) => {
37119
+ const close = useCallback2((confirmed, value) => {
37186
37120
  setPopup((prev) => {
37187
37121
  var _a, _b;
37188
37122
  if (confirmed) (_a = prev.onConfirm) == null ? void 0 : _a.call(prev, value);
@@ -37190,7 +37124,7 @@ function usePopup() {
37190
37124
  return { ...prev, visible: false, inputValue: "" };
37191
37125
  });
37192
37126
  }, []);
37193
- const alert2 = useCallback(
37127
+ const alert2 = useCallback2(
37194
37128
  (message, color = "primary") => new Promise(
37195
37129
  (resolve) => open({
37196
37130
  type: "alert",
@@ -37202,7 +37136,7 @@ function usePopup() {
37202
37136
  ),
37203
37137
  [open]
37204
37138
  );
37205
- const modal = useCallback(
37139
+ const modal = useCallback2(
37206
37140
  (message, color = "primary", icons = false, full = false) => new Promise(
37207
37141
  (resolve) => open({
37208
37142
  type: "modal",
@@ -37216,7 +37150,7 @@ function usePopup() {
37216
37150
  ),
37217
37151
  [open]
37218
37152
  );
37219
- const confirm = useCallback(
37153
+ const confirm = useCallback2(
37220
37154
  (message, color = "primary") => new Promise(
37221
37155
  (resolve) => open({
37222
37156
  type: "confirm",
@@ -37228,7 +37162,7 @@ function usePopup() {
37228
37162
  ),
37229
37163
  [open]
37230
37164
  );
37231
- const prompt = useCallback(
37165
+ const prompt = useCallback2(
37232
37166
  (message, color = "primary") => new Promise(
37233
37167
  (resolve) => open({
37234
37168
  type: "prompt",
@@ -37240,7 +37174,7 @@ function usePopup() {
37240
37174
  ),
37241
37175
  [open]
37242
37176
  );
37243
- const updateMessage = useCallback((message) => {
37177
+ const updateMessage = useCallback2((message) => {
37244
37178
  messageRef.current = message;
37245
37179
  setPopup((prev) => ({ ...prev }));
37246
37180
  }, []);
@@ -37273,7 +37207,7 @@ function Modal({
37273
37207
  }) {
37274
37208
  const pop = usePopup();
37275
37209
  const hide = () => pop.close(false);
37276
- const childrenWithHide = useMemo4(
37210
+ const childrenWithHide = useMemo3(
37277
37211
  () => cloneElement2(children, { hide }),
37278
37212
  [children]
37279
37213
  );
@@ -37536,14 +37470,14 @@ function DocumentViewer({ item }) {
37536
37470
  // src/table3/index.tsx
37537
37471
  import React9, {
37538
37472
  useEffect as useEffect9,
37539
- useMemo as useMemo7,
37473
+ useMemo as useMemo6,
37540
37474
  useReducer as useReducer2,
37541
- useRef as useRef5,
37475
+ useRef as useRef6,
37542
37476
  useState as useState13
37543
37477
  } from "react";
37544
37478
 
37545
37479
  // src/table3/filter.tsx
37546
- 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";
37547
37481
 
37548
37482
  // src/table3/filters.tsx
37549
37483
  import { jsx as jsx21, jsxs as jsxs13 } from "react/jsx-runtime";
@@ -37655,12 +37589,12 @@ function Filter({
37655
37589
  }) {
37656
37590
  const [visible, setVisible] = useState11(false);
37657
37591
  const [text, setText] = useState11("");
37658
- const items = useMemo5(
37592
+ const items = useMemo4(
37659
37593
  () => [...new Set(Object.values(objectData).map((o) => o[h]))],
37660
37594
  [objectData]
37661
37595
  );
37662
37596
  const [selected, setSelected] = useState11(items);
37663
- const itemsFiltered = useMemo5(
37597
+ const itemsFiltered = useMemo4(
37664
37598
  () => items.sort((a, b) => `${a}`.localeCompare(b)).filter((item) => {
37665
37599
  if (!text) return true;
37666
37600
  return `${item}`.toLowerCase().includes(text.toLowerCase());
@@ -38269,7 +38203,7 @@ function TableFooter({
38269
38203
  }
38270
38204
 
38271
38205
  // src/table3/dialog.tsx
38272
- import React8, { useMemo as useMemo6 } from "react";
38206
+ import React8, { useMemo as useMemo5 } from "react";
38273
38207
  import { jsx as jsx28, jsxs as jsxs19 } from "react/jsx-runtime";
38274
38208
  function Dialog3({
38275
38209
  modalRef,
@@ -38286,7 +38220,7 @@ function Dialog3({
38286
38220
  var _a;
38287
38221
  return (_a = modalRef.current) == null ? void 0 : _a.close();
38288
38222
  };
38289
- const clonedModal = useMemo6(() => {
38223
+ const clonedModal = useMemo5(() => {
38290
38224
  if (dialogRow && React8.isValidElement(children)) {
38291
38225
  return React8.cloneElement(children, {
38292
38226
  key: JSON.stringify(dialogRow),
@@ -38389,7 +38323,7 @@ function Table3({
38389
38323
  setObjectData(data);
38390
38324
  setPage(1);
38391
38325
  }, [data]);
38392
- const headers = useMemo7(() => {
38326
+ const headers = useMemo6(() => {
38393
38327
  if (!objectData) return [];
38394
38328
  return [
38395
38329
  ...new Set(
@@ -38397,12 +38331,12 @@ function Table3({
38397
38331
  )
38398
38332
  ];
38399
38333
  }, [objectData]);
38400
- const totalPages = useMemo7(() => {
38334
+ const totalPages = useMemo6(() => {
38401
38335
  if (!objectData) return 0;
38402
38336
  return maxItems ? Math.ceil(Object.keys(objectData).length / maxItems) : 1;
38403
38337
  }, [objectData, maxItems]);
38404
38338
  const [sort, setSort] = useState13(sortBy);
38405
- const modalRef = useRef5(null);
38339
+ const modalRef = useRef6(null);
38406
38340
  const [dialogRow, setDialogRow] = useState13({});
38407
38341
  const context = {
38408
38342
  objectData,