next-recomponents 2.0.38 → 2.0.40

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.
@@ -129,22 +129,6 @@ export default function useResources<T extends Record<string, ItemsRecord>>({
129
129
  return error;
130
130
  }
131
131
  };
132
- // Helper para actualizar el array sin mutar
133
- const mergeDataArray = (
134
- existingData: any[] | undefined,
135
- newItem: any,
136
- matchId?: any,
137
- ): any[] => {
138
- if (!existingData) return [newItem];
139
- const index = existingData.findIndex((d: any) => d?.id == matchId);
140
- if (index >= 0) {
141
- return existingData.map((d: any, i: number) =>
142
- i === index ? newItem : d,
143
- );
144
- }
145
- return [newItem, ...existingData];
146
- };
147
-
148
132
  const bodyCreateFunc = async (
149
133
  data: Record<string, any> | Record<string, any>[],
150
134
  ) => {
@@ -154,158 +138,146 @@ export default function useResources<T extends Record<string, ItemsRecord>>({
154
138
  data: Array.isArray(data) ? [...data] : { ...data },
155
139
  headers: { Authorization: token },
156
140
  };
157
-
158
- const newInfo = {
159
- ...info,
160
- [key]: {
161
- ...info[key],
162
- data: [...(info[key]?.data ?? [])],
163
- state: "loading",
164
- errorMessage: "",
165
- },
166
- };
141
+ const newInfo = { ...info };
142
+ // newInfo[key] = newInfo[key] || {};
143
+ newInfo[key].state = "loading";
144
+ newInfo[key].errorMessage = "";
167
145
 
168
146
  setInfo(newInfo);
169
147
 
170
148
  try {
171
149
  const consulta = await axios.request(options);
172
150
  const d = consulta.data;
173
-
174
- let updatedData: any[];
175
-
151
+ newInfo[key].state = "success";
152
+ newInfo[key].errorMessage = "";
176
153
  if (Array.isArray(data)) {
177
- updatedData = [...(newInfo[key]?.data ?? [])];
178
- for (const datum of data) {
179
- updatedData = mergeDataArray(updatedData, d, datum?.id);
154
+ for (let datum of data) {
155
+ const index = newInfo[key]?.data?.findIndex(
156
+ (d: any) => d?.id == datum?.id,
157
+ );
158
+ if (index >= 0) {
159
+ newInfo[key].data[index] = d;
160
+ } else {
161
+ if (newInfo[key]?.data) {
162
+ newInfo[key].data.unshift(d);
163
+ } else {
164
+ newInfo[key].data = [d];
165
+ }
166
+ }
180
167
  }
181
- updatedData = updatedData.flat();
168
+ newInfo[key].data = newInfo[key].data.flat();
182
169
  } else {
183
- updatedData = mergeDataArray(
184
- newInfo[key]?.data,
185
- d,
186
- (data as any)?.id,
170
+ newInfo[key].selectedItem = d;
171
+ const index = newInfo[key]?.data?.findIndex(
172
+ (d: any) => d?.id == data?.id,
187
173
  );
174
+ if (index >= 0) {
175
+ newInfo[key].data[index] = d;
176
+ } else {
177
+ if (newInfo[key]?.data) {
178
+ newInfo[key].data.unshift(d);
179
+ } else {
180
+ newInfo[key].data = [d];
181
+ }
182
+ }
188
183
  }
189
-
190
- setInfo({
191
- ...newInfo,
192
- [key]: {
193
- ...newInfo[key],
194
- state: "success",
195
- errorMessage: "",
196
- selectedItem: Array.isArray(data) ? newInfo[key].selectedItem : d,
197
- data: updatedData,
198
- },
199
- });
200
-
184
+ setInfo({ ...newInfo });
201
185
  return d;
202
186
  } catch (error: any) {
203
187
  const item = httpStatusCodes.find((s) => s.code == error.status);
204
188
 
189
+ newInfo[key].state = "error";
190
+ newInfo[key].errorMessage = item?.meaning || error.message;
205
191
  if (error.status == 403) {
206
- onError?.({ error, errorMessage: item?.meaning });
192
+ onError?.({ error, ...{ errorMessage: item?.meaning } });
207
193
  }
208
-
209
- setInfo({
210
- ...newInfo,
211
- [key]: {
212
- ...newInfo[key],
213
- state: "error",
214
- errorMessage: item?.meaning || error.message,
215
- },
216
- });
217
-
194
+ setInfo({ ...newInfo });
218
195
  return error;
219
196
  }
220
197
  };
221
-
222
198
  const formCreateFunc = async (
223
199
  data: Record<string, any> | Record<string, any>[],
224
200
  ) => {
225
- const formData = new FormData();
201
+ const newInfo = { ...info };
202
+ newInfo[key].state = "loading";
203
+ newInfo[key].errorMessage = "";
204
+ setInfo(newInfo);
226
205
 
227
- if (Array.isArray(data)) {
228
- data.forEach((item, i) => {
229
- for (const [k, v] of Object.entries(item)) {
230
- formData.append(`${k}[${i}]`, v as any);
206
+ try {
207
+ // Convertir datos a FormData
208
+ const formData = new FormData();
209
+
210
+ if (Array.isArray(data)) {
211
+ // Suponiendo que la API acepta múltiples entradas con la misma clave
212
+ data.forEach((item, i) => {
213
+ for (const [k, v] of Object.entries(item)) {
214
+ formData.append(`${k}[${i}]`, v as any);
215
+ }
216
+ });
217
+ } else {
218
+ for (const [k, v] of Object.entries(data)) {
219
+ formData.append(k, v);
231
220
  }
232
- });
233
- } else {
234
- for (const [k, v] of Object.entries(data)) {
235
- formData.append(k, v);
236
221
  }
237
- }
238
222
 
239
- const options = {
240
- method: "POST",
241
- url: `${baseURI}/${key}`,
242
- data: formData,
243
- headers: {
244
- Authorization: token,
245
- "Content-Type": "multipart/form-data",
246
- },
247
- };
248
-
249
- const newInfo = {
250
- ...info,
251
- [key]: {
252
- ...info[key],
253
- data: [...(info[key]?.data ?? [])],
254
- state: "loading",
255
- errorMessage: "",
256
- },
257
- };
258
-
259
- setInfo(newInfo);
223
+ const options = {
224
+ method: "POST",
225
+ url: `${baseURI}/${key}`,
226
+ data: formData,
227
+ headers: {
228
+ Authorization: token,
229
+ "Content-Type": "multipart/form-data",
230
+ },
231
+ };
260
232
 
261
- try {
262
233
  const consulta = await axios.request(options);
263
234
  const d = consulta.data;
264
235
 
265
- let updatedData: any[];
236
+ newInfo[key].state = "success";
237
+ newInfo[key].errorMessage = "";
266
238
 
267
239
  if (Array.isArray(data)) {
268
- updatedData = [...(newInfo[key]?.data ?? [])];
269
- for (const datum of data) {
270
- updatedData = mergeDataArray(updatedData, d, datum?.id);
240
+ for (let datum of data) {
241
+ const index = newInfo[key]?.data?.findIndex(
242
+ (d: any) => d?.id == datum?.id,
243
+ );
244
+ if (index >= 0) {
245
+ newInfo[key].data[index] = d;
246
+ } else {
247
+ if (newInfo[key]?.data) {
248
+ newInfo[key].data.unshift(d);
249
+ } else {
250
+ newInfo[key].data = [d];
251
+ }
252
+ }
271
253
  }
272
- updatedData = updatedData.flat();
254
+ newInfo[key].data = newInfo[key].data.flat();
273
255
  } else {
274
- updatedData = mergeDataArray(
275
- newInfo[key]?.data,
276
- d,
277
- (data as any)?.id,
256
+ newInfo[key].selectedItem = d;
257
+ const index = newInfo[key]?.data?.findIndex(
258
+ (d: any) => d?.id == data?.id,
278
259
  );
260
+ if (index >= 0) {
261
+ newInfo[key].data[index] = d;
262
+ } else {
263
+ if (newInfo[key]?.data) {
264
+ newInfo[key].data.unshift(d);
265
+ } else {
266
+ newInfo[key].data = [d];
267
+ }
268
+ }
279
269
  }
280
270
 
281
- setInfo({
282
- ...newInfo,
283
- [key]: {
284
- ...newInfo[key],
285
- state: "success",
286
- errorMessage: "",
287
- selectedItem: Array.isArray(data) ? newInfo[key].selectedItem : d,
288
- data: updatedData,
289
- },
290
- });
291
-
271
+ setInfo({ ...newInfo });
292
272
  return d;
293
273
  } catch (error: any) {
294
274
  const item = httpStatusCodes.find((s) => s.code == error.status);
295
-
275
+ newInfo[key].state = "error";
276
+ newInfo[key].errorMessage = item?.meaning || error.message;
296
277
  if (error.status == 403) {
297
- onError?.({ error, errorMessage: item?.meaning });
278
+ onError?.({ error, ...{ errorMessage: item?.meaning } });
298
279
  }
299
-
300
- setInfo({
301
- ...newInfo,
302
- [key]: {
303
- ...newInfo[key],
304
- state: "error",
305
- errorMessage: item?.meaning || error.message,
306
- },
307
- });
308
-
280
+ setInfo({ ...newInfo });
309
281
  return error;
310
282
  }
311
283
  };
@@ -334,68 +306,43 @@ export default function useResources<T extends Record<string, ItemsRecord>>({
334
306
  data: Array.isArray(data) ? [...data] : { ...data },
335
307
  headers: { Authorization: token },
336
308
  };
337
-
338
- const newInfo = {
339
- ...info,
340
- [key]: {
341
- ...info[key], // ✅ copia superficial del key
342
- data: [...(info[key]?.data ?? [])], // ✅ copia del array
343
- state: "loading",
344
- errorMessage: "",
345
- },
346
- };
309
+ const newInfo = { ...info };
310
+ // newInfo[key] = newInfo[key] || {};
311
+ newInfo[key].state = "loading";
312
+ newInfo[key].errorMessage = "";
347
313
 
348
314
  setInfo(newInfo);
349
315
 
350
316
  try {
351
317
  const consulta = await axios.request(options);
352
318
  const d = consulta.data;
353
-
319
+ newInfo[key].state = "success";
320
+ newInfo[key].errorMessage = "";
321
+ newInfo[key].selectedItem = { ...newInfo[key].selectedItem, ...d };
354
322
  const index = newInfo[key]?.data?.findIndex(
355
- (item: any) => item?.id == id,
323
+ (d: any) => d?.id == id,
356
324
  );
357
325
 
358
- const updatedData =
359
- index >= 0
360
- ? newInfo[key].data.map(
361
- (
362
- item: any,
363
- i: number, // ✅ map en lugar de mutación
364
- ) => (i === index ? { ...item, ...d } : item),
365
- )
366
- : newInfo[key]?.data
367
- ? [d, ...newInfo[key].data] // ✅ nuevo array en lugar de unshift
368
- : [d];
369
-
370
- const updatedInfo = {
371
- ...newInfo,
372
- [key]: {
373
- ...newInfo[key],
374
- state: "success",
375
- errorMessage: "",
376
- selectedItem: { ...newInfo[key].selectedItem, ...d },
377
- data: updatedData,
378
- },
379
- };
380
-
381
- setInfo(updatedInfo);
326
+ if (index >= 0) {
327
+ newInfo[key].data[index] = { ...newInfo[key].data[index], ...d };
328
+ } else {
329
+ if (newInfo[key]?.data) {
330
+ newInfo[key].data.unshift(d);
331
+ } else {
332
+ newInfo[key].data = [d];
333
+ }
334
+ }
335
+ setInfo({ ...newInfo });
382
336
  return d;
383
337
  } catch (error: any) {
384
338
  const item = httpStatusCodes.find((s) => s.code == error.status);
385
339
 
386
- setInfo({
387
- ...newInfo,
388
- [key]: {
389
- ...newInfo[key],
390
- state: "error",
391
- errorMessage: item?.meaning || error.message,
392
- },
393
- });
394
-
340
+ newInfo[key].state = "error";
341
+ newInfo[key].errorMessage = item?.meaning || error.message;
395
342
  if (error.status == 403) {
396
343
  onError?.({ error, ...{ errorMessage: item?.meaning } });
397
344
  }
398
-
345
+ setInfo({ ...newInfo });
399
346
  return error;
400
347
  }
401
348
  },
@@ -405,53 +352,36 @@ export default function useResources<T extends Record<string, ItemsRecord>>({
405
352
  url: `${baseURI}/${key}/${id}`,
406
353
  headers: { Authorization: token },
407
354
  };
408
-
409
- const newInfo = {
410
- ...info,
411
- [key]: {
412
- ...info[key],
413
- data: [...(info[key]?.data ?? [])],
414
- state: "loading",
415
- errorMessage: "",
416
- },
417
- };
355
+ const newInfo = { ...info };
356
+ // newInfo[key] = newInfo[key] || {};
357
+ newInfo[key].state = "loading";
358
+ newInfo[key].errorMessage = "";
418
359
 
419
360
  setInfo(newInfo);
420
361
 
421
362
  try {
422
363
  const consulta = await axios.request(options);
423
364
  const d = consulta.data;
424
-
425
- setInfo({
426
- ...newInfo,
427
- [key]: {
428
- ...newInfo[key],
429
- state: "success",
430
- errorMessage: "",
431
- selectedItem: d,
432
- data:
433
- newInfo[key]?.data?.filter((item: any) => item?.id != id) ??
434
- [], // ✅ filter en lugar de splice
435
- },
436
- });
437
-
365
+ newInfo[key].state = "success";
366
+ newInfo[key].errorMessage = "";
367
+ newInfo[key].selectedItem = d;
368
+ const index = newInfo[key]?.data?.findIndex(
369
+ (d: any) => d?.id == id,
370
+ );
371
+ if (index >= 0) {
372
+ newInfo[key].data.splice(index, 1);
373
+ }
374
+ setInfo({ ...newInfo });
438
375
  return d.data;
439
376
  } catch (error: any) {
440
377
  const item = httpStatusCodes.find((s) => s.code == error.status);
441
378
 
379
+ newInfo[key].state = "error";
380
+ newInfo[key].errorMessage = item?.meaning || error.message;
442
381
  if (error.status == 403) {
443
- onError?.({ error, errorMessage: item?.meaning });
382
+ onError?.({ error, ...{ errorMessage: item?.meaning } });
444
383
  }
445
-
446
- setInfo({
447
- ...newInfo,
448
- [key]: {
449
- ...newInfo[key],
450
- state: "error",
451
- errorMessage: item?.meaning || error.message,
452
- },
453
- });
454
-
384
+ setInfo({ ...newInfo });
455
385
  return error;
456
386
  }
457
387
  },