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.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,391 +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;
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)
36365
36372
  }
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;
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
36408
36387
  }
36409
- };
36410
- const bodyCreateFunc = async (data) => {
36411
- var _a2, _b2, _c2, _d2, _e2, _f2;
36412
- const options = {
36413
- method: "POST",
36414
- url: `${baseURI}/${key}`,
36415
- 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 },
36416
36392
  headers: { Authorization: token }
36417
- };
36418
- const newInfo = { ...info };
36419
- newInfo[key].state = "loading";
36420
- newInfo[key].errorMessage = "";
36421
- setInfo(newInfo);
36422
- try {
36423
- const consulta = await import_axios.default.request(options);
36424
- const d = consulta.data;
36425
- newInfo[key].state = "success";
36426
- newInfo[key].errorMessage = "";
36427
- if (Array.isArray(data)) {
36428
- for (let datum of data) {
36429
- const index = (_b2 = (_a2 = newInfo[key]) == null ? void 0 : _a2.data) == null ? void 0 : _b2.findIndex(
36430
- (d2) => (d2 == null ? void 0 : d2.id) == (datum == null ? void 0 : datum.id)
36431
- );
36432
- if (index >= 0) {
36433
- newInfo[key].data[index] = d;
36434
- } else {
36435
- if ((_c2 = newInfo[key]) == null ? void 0 : _c2.data) {
36436
- newInfo[key].data.unshift(d);
36437
- } else {
36438
- newInfo[key].data = [d];
36439
- }
36440
- }
36441
- }
36442
- newInfo[key].data = newInfo[key].data.flat();
36443
- } else {
36444
- newInfo[key].selectedItem = d;
36445
- const index = (_e2 = (_d2 = newInfo[key]) == null ? void 0 : _d2.data) == null ? void 0 : _e2.findIndex(
36446
- (d2) => (d2 == null ? void 0 : d2.id) == (data == null ? void 0 : data.id)
36447
- );
36448
- if (index >= 0) {
36449
- newInfo[key].data[index] = d;
36450
- } else {
36451
- if ((_f2 = newInfo[key]) == null ? void 0 : _f2.data) {
36452
- newInfo[key].data.unshift(d);
36453
- } else {
36454
- newInfo[key].data = [d];
36455
- }
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,
36400
+ [key]: {
36401
+ ...prev[key],
36402
+ state: "success",
36403
+ errorMessage: "",
36404
+ data: merged,
36405
+ totalItems: res.totalItems,
36406
+ totalPages: res.totalPages,
36407
+ currentPage: res.currentPage
36456
36408
  }
36457
- }
36458
- setInfo({ ...newInfo });
36459
- return d;
36460
- } catch (error) {
36461
- const item = http_codes_default.find((s) => s.code == error.status);
36462
- newInfo[key].state = "error";
36463
- newInfo[key].errorMessage = (item == null ? void 0 : item.meaning) || error.message;
36464
- if (error.status == 403) {
36465
- onError == null ? void 0 : onError({ error, ...{ errorMessage: item == null ? void 0 : item.meaning } });
36466
- }
36467
- setInfo({ ...newInfo });
36468
- return error;
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
36469
36429
  }
36470
- };
36471
- const formCreateFunc = async (data) => {
36472
- var _a2, _b2, _c2, _d2, _e2, _f2;
36473
- const newInfo = { ...info };
36474
- newInfo[key].state = "loading";
36475
- newInfo[key].errorMessage = "";
36476
- setInfo(newInfo);
36477
- try {
36478
- const formData = new FormData();
36479
- if (Array.isArray(data)) {
36480
- data.forEach((item, i) => {
36481
- for (const [k, v] of Object.entries(item)) {
36482
- formData.append(`${k}[${i}]`, v);
36483
- }
36484
- });
36485
- } else {
36486
- for (const [k, v] of Object.entries(data)) {
36487
- formData.append(k, v);
36488
- }
36489
- }
36490
- const options = {
36491
- method: "POST",
36492
- url: `${baseURI}/${key}`,
36493
- data: formData,
36494
- headers: {
36495
- Authorization: token,
36496
- "Content-Type": "multipart/form-data"
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,
36440
+ [key]: {
36441
+ ...prev[key],
36442
+ state: "success",
36443
+ errorMessage: "",
36444
+ selectedItem: d,
36445
+ data: mergeDataArray((_b = (_a = prev[key]) == null ? void 0 : _a.data) != null ? _b : [], d, id)
36497
36446
  }
36498
36447
  };
36499
- const consulta = await import_axios.default.request(options);
36500
- const d = consulta.data;
36501
- newInfo[key].state = "success";
36502
- newInfo[key].errorMessage = "";
36503
- if (Array.isArray(data)) {
36504
- for (let datum of data) {
36505
- const index = (_b2 = (_a2 = newInfo[key]) == null ? void 0 : _a2.data) == null ? void 0 : _b2.findIndex(
36506
- (d2) => (d2 == null ? void 0 : d2.id) == (datum == null ? void 0 : datum.id)
36507
- );
36508
- if (index >= 0) {
36509
- newInfo[key].data[index] = d;
36510
- } else {
36511
- if ((_c2 = newInfo[key]) == null ? void 0 : _c2.data) {
36512
- newInfo[key].data.unshift(d);
36513
- } else {
36514
- newInfo[key].data = [d];
36515
- }
36516
- }
36517
- }
36518
- newInfo[key].data = newInfo[key].data.flat();
36519
- } else {
36520
- newInfo[key].selectedItem = d;
36521
- const index = (_e2 = (_d2 = newInfo[key]) == null ? void 0 : _d2.data) == null ? void 0 : _e2.findIndex(
36522
- (d2) => (d2 == null ? void 0 : d2.id) == (data == null ? void 0 : data.id)
36523
- );
36524
- if (index >= 0) {
36525
- newInfo[key].data[index] = d;
36526
- } else {
36527
- if ((_f2 = newInfo[key]) == null ? void 0 : _f2.data) {
36528
- newInfo[key].data.unshift(d);
36529
- } else {
36530
- newInfo[key].data = [d];
36531
- }
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,
36492
+ [key]: {
36493
+ ...prev[key],
36494
+ state: "success",
36495
+ errorMessage: "",
36496
+ selectedItem: Array.isArray(data) ? prev[key].selectedItem : d,
36497
+ data: updatedData
36532
36498
  }
36533
- }
36534
- setInfo({ ...newInfo });
36535
- return d;
36536
- } catch (error) {
36537
- const item = http_codes_default.find((s) => s.code == error.status);
36538
- newInfo[key].state = "error";
36539
- newInfo[key].errorMessage = (item == null ? void 0 : item.meaning) || error.message;
36540
- if (error.status == 403) {
36541
- onError == null ? void 0 : onError({ error, ...{ errorMessage: item == null ? void 0 : item.meaning } });
36542
- }
36543
- setInfo({ ...newInfo });
36544
- return error;
36545
- }
36546
- };
36547
- acc[key] = {
36548
- ...endpoint,
36549
- loaded: false,
36550
- show: showFunc,
36551
- find: findFunc,
36552
- create: async (data) => {
36553
- const hasFile = Array.isArray(data) ? data.some(
36554
- (item) => Object.values(item).some((value) => value instanceof File)
36555
- ) : Object.values(data).some((value) => value instanceof File);
36556
- if (hasFile) {
36557
- return await formCreateFunc(data);
36558
- }
36559
- return await bodyCreateFunc(data);
36560
- },
36561
- update: async (id, data) => {
36562
- var _a2, _b2, _c2;
36563
- const options = {
36564
- method: "PATCH",
36565
- url: `${baseURI}/${key}/${id}`,
36566
- data: Array.isArray(data) ? [...data] : { ...data },
36567
- headers: { Authorization: token }
36568
36499
  };
36569
- const newInfo = { ...info };
36570
- newInfo[key].state = "loading";
36571
- newInfo[key].errorMessage = "";
36572
- setInfo(newInfo);
36573
- try {
36574
- const consulta = await import_axios.default.request(options);
36575
- const d = consulta.data;
36576
- newInfo[key].state = "success";
36577
- newInfo[key].errorMessage = "";
36578
- newInfo[key].selectedItem = { ...newInfo[key].selectedItem, ...d };
36579
- const index = (_b2 = (_a2 = newInfo[key]) == null ? void 0 : _a2.data) == null ? void 0 : _b2.findIndex(
36580
- (d2) => (d2 == null ? void 0 : d2.id) == id
36581
- );
36582
- if (index >= 0) {
36583
- newInfo[key].data[index] = { ...newInfo[key].data[index], ...d };
36584
- } else {
36585
- if ((_c2 = newInfo[key]) == null ? void 0 : _c2.data) {
36586
- newInfo[key].data.unshift(d);
36587
- } else {
36588
- newInfo[key].data = [d];
36589
- }
36590
- }
36591
- setInfo({ ...newInfo });
36592
- return d;
36593
- } catch (error) {
36594
- const item = http_codes_default.find((s) => s.code == error.status);
36595
- newInfo[key].state = "error";
36596
- newInfo[key].errorMessage = (item == null ? void 0 : item.meaning) || error.message;
36597
- if (error.status == 403) {
36598
- 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
+ )
36599
36532
  }
36600
- setInfo({ ...newInfo });
36601
- return error;
36602
- }
36603
- },
36604
- remove: async (id) => {
36605
- var _a2, _b2;
36606
- const options = {
36607
- method: "DELETE",
36608
- url: `${baseURI}/${key}/${id}`,
36609
- headers: { Authorization: token }
36610
36533
  };
36611
- const newInfo = { ...info };
36612
- newInfo[key].state = "loading";
36613
- newInfo[key].errorMessage = "";
36614
- setInfo(newInfo);
36615
- try {
36616
- const consulta = await import_axios.default.request(options);
36617
- const d = consulta.data;
36618
- newInfo[key].state = "success";
36619
- newInfo[key].errorMessage = "";
36620
- newInfo[key].selectedItem = d;
36621
- const index = (_b2 = (_a2 = newInfo[key]) == null ? void 0 : _a2.data) == null ? void 0 : _b2.findIndex(
36622
- (d2) => (d2 == null ? void 0 : d2.id) == id
36623
- );
36624
- if (index >= 0) {
36625
- newInfo[key].data.splice(index, 1);
36626
- }
36627
- setInfo({ ...newInfo });
36628
- return d.data;
36629
- } catch (error) {
36630
- const item = http_codes_default.find((s) => s.code == error.status);
36631
- newInfo[key].state = "error";
36632
- newInfo[key].errorMessage = (item == null ? void 0 : item.meaning) || error.message;
36633
- if (error.status == 403) {
36634
- onError == null ? void 0 : onError({ error, ...{ errorMessage: item == null ? void 0 : item.meaning } });
36635
- }
36636
- setInfo({ ...newInfo });
36637
- return error;
36638
- }
36639
- },
36640
- totalPages: (_a = info[key]) == null ? void 0 : _a.totalPages,
36641
- currentPage: (_b = info[key]) == null ? void 0 : _b.currentPage,
36642
- state: ((_c = info[key]) == null ? void 0 : _c.state) || "success",
36643
- errorMessage: (_d = info[key]) == null ? void 0 : _d.errorMessage,
36644
- params: (_e = info[key]) == null ? void 0 : _e.params,
36645
- setLoaded: () => {
36646
- const newInfo = { ...info };
36647
- newInfo[key].loaded = true;
36648
- setInfo(newInfo);
36649
- },
36650
- getAllPages: async (limit = 100) => {
36651
- const allData = [];
36652
- let currentPage = 1;
36653
- let totalPages = 1;
36654
- while (currentPage <= totalPages) {
36655
- const response = await results[key].show({
36656
- limit,
36657
- page: currentPage
36658
- });
36659
- const currentInfo = info[key];
36660
- if (Array.isArray(response)) {
36661
- allData.push(...response);
36662
- }
36663
- currentPage = currentInfo.currentPage || currentPage + 1;
36664
- totalPages = currentInfo.totalPages || currentPage;
36665
- }
36666
- const newInfo = { ...info };
36667
- newInfo[key].data = allData;
36668
- newInfo[key].currentPage = currentPage;
36669
- newInfo[key].totalPages = totalPages;
36670
- setInfo(newInfo);
36671
- return allData;
36672
- },
36673
- data: ((_f = info[key]) == null ? void 0 : _f.data) || [],
36674
- selectedItem: ((_g = info[key]) == null ? void 0 : _g.selectedItem) || {}
36675
- };
36676
- return acc;
36534
+ });
36535
+ return d;
36536
+ } catch (error) {
36537
+ setKeyError(key, error);
36538
+ return error;
36539
+ }
36677
36540
  },
36678
- {}
36541
+ [baseURI, token, setKeyLoading, setKeyError]
36679
36542
  );
36680
- async function doSome() {
36681
- var _a, _b, _c, _d, _e;
36682
- const key = Object.keys(info).find((k) => {
36683
- return info[k].loaded === false;
36684
- });
36685
- if (key) {
36686
- if ((_a = info[key]) == null ? void 0 : _a.id) {
36687
- await results[key].find((_b = info[key]) == null ? void 0 : _b.id, (_c = info[key]) == null ? void 0 : _c.defaultParams);
36688
- } else if ((_d = info[key]) == null ? void 0 : _d.defaultParams) {
36689
- await results[key].show((_e = info[key]) == null ? void 0 : _e.defaultParams);
36690
- } else {
36691
- results[key].setLoaded();
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,
36554
+ [key]: {
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)
36560
+ }
36561
+ };
36562
+ });
36563
+ return d;
36564
+ } catch (error) {
36565
+ setKeyError(key, error);
36566
+ return error;
36692
36567
  }
36693
- }
36694
- }
36568
+ },
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]
36590
+ );
36695
36591
  (0, import_react8.useEffect)(() => {
36696
- doSome();
36697
- }, [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
+ }, {});
36698
36632
  return results;
36699
36633
  }
36700
36634