next-recomponents 2.0.35 → 2.0.38

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,6 +129,22 @@ 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
+
132
148
  const bodyCreateFunc = async (
133
149
  data: Record<string, any> | Record<string, any>[],
134
150
  ) => {
@@ -138,146 +154,158 @@ export default function useResources<T extends Record<string, ItemsRecord>>({
138
154
  data: Array.isArray(data) ? [...data] : { ...data },
139
155
  headers: { Authorization: token },
140
156
  };
141
- const newInfo = { ...info };
142
- // newInfo[key] = newInfo[key] || {};
143
- newInfo[key].state = "loading";
144
- newInfo[key].errorMessage = "";
157
+
158
+ const newInfo = {
159
+ ...info,
160
+ [key]: {
161
+ ...info[key],
162
+ data: [...(info[key]?.data ?? [])],
163
+ state: "loading",
164
+ errorMessage: "",
165
+ },
166
+ };
145
167
 
146
168
  setInfo(newInfo);
147
169
 
148
170
  try {
149
171
  const consulta = await axios.request(options);
150
172
  const d = consulta.data;
151
- newInfo[key].state = "success";
152
- newInfo[key].errorMessage = "";
173
+
174
+ let updatedData: any[];
175
+
153
176
  if (Array.isArray(data)) {
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
- }
177
+ updatedData = [...(newInfo[key]?.data ?? [])];
178
+ for (const datum of data) {
179
+ updatedData = mergeDataArray(updatedData, d, datum?.id);
167
180
  }
168
- newInfo[key].data = newInfo[key].data.flat();
181
+ updatedData = updatedData.flat();
169
182
  } else {
170
- newInfo[key].selectedItem = d;
171
- const index = newInfo[key]?.data?.findIndex(
172
- (d: any) => d?.id == data?.id,
183
+ updatedData = mergeDataArray(
184
+ newInfo[key]?.data,
185
+ d,
186
+ (data as any)?.id,
173
187
  );
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
- }
183
188
  }
184
- setInfo({ ...newInfo });
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
+
185
201
  return d;
186
202
  } catch (error: any) {
187
203
  const item = httpStatusCodes.find((s) => s.code == error.status);
188
204
 
189
- newInfo[key].state = "error";
190
- newInfo[key].errorMessage = item?.meaning || error.message;
191
205
  if (error.status == 403) {
192
- onError?.({ error, ...{ errorMessage: item?.meaning } });
206
+ onError?.({ error, errorMessage: item?.meaning });
193
207
  }
194
- setInfo({ ...newInfo });
208
+
209
+ setInfo({
210
+ ...newInfo,
211
+ [key]: {
212
+ ...newInfo[key],
213
+ state: "error",
214
+ errorMessage: item?.meaning || error.message,
215
+ },
216
+ });
217
+
195
218
  return error;
196
219
  }
197
220
  };
221
+
198
222
  const formCreateFunc = async (
199
223
  data: Record<string, any> | Record<string, any>[],
200
224
  ) => {
201
- const newInfo = { ...info };
202
- newInfo[key].state = "loading";
203
- newInfo[key].errorMessage = "";
204
- setInfo(newInfo);
225
+ const formData = new FormData();
205
226
 
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);
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);
220
231
  }
232
+ });
233
+ } else {
234
+ for (const [k, v] of Object.entries(data)) {
235
+ formData.append(k, v);
221
236
  }
237
+ }
222
238
 
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
- };
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);
232
260
 
261
+ try {
233
262
  const consulta = await axios.request(options);
234
263
  const d = consulta.data;
235
264
 
236
- newInfo[key].state = "success";
237
- newInfo[key].errorMessage = "";
265
+ let updatedData: any[];
238
266
 
239
267
  if (Array.isArray(data)) {
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
- }
268
+ updatedData = [...(newInfo[key]?.data ?? [])];
269
+ for (const datum of data) {
270
+ updatedData = mergeDataArray(updatedData, d, datum?.id);
253
271
  }
254
- newInfo[key].data = newInfo[key].data.flat();
272
+ updatedData = updatedData.flat();
255
273
  } else {
256
- newInfo[key].selectedItem = d;
257
- const index = newInfo[key]?.data?.findIndex(
258
- (d: any) => d?.id == data?.id,
274
+ updatedData = mergeDataArray(
275
+ newInfo[key]?.data,
276
+ d,
277
+ (data as any)?.id,
259
278
  );
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
- }
269
279
  }
270
280
 
271
- setInfo({ ...newInfo });
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
+
272
292
  return d;
273
293
  } catch (error: any) {
274
294
  const item = httpStatusCodes.find((s) => s.code == error.status);
275
- newInfo[key].state = "error";
276
- newInfo[key].errorMessage = item?.meaning || error.message;
295
+
277
296
  if (error.status == 403) {
278
- onError?.({ error, ...{ errorMessage: item?.meaning } });
297
+ onError?.({ error, errorMessage: item?.meaning });
279
298
  }
280
- setInfo({ ...newInfo });
299
+
300
+ setInfo({
301
+ ...newInfo,
302
+ [key]: {
303
+ ...newInfo[key],
304
+ state: "error",
305
+ errorMessage: item?.meaning || error.message,
306
+ },
307
+ });
308
+
281
309
  return error;
282
310
  }
283
311
  };
@@ -306,43 +334,68 @@ export default function useResources<T extends Record<string, ItemsRecord>>({
306
334
  data: Array.isArray(data) ? [...data] : { ...data },
307
335
  headers: { Authorization: token },
308
336
  };
309
- const newInfo = { ...info };
310
- // newInfo[key] = newInfo[key] || {};
311
- newInfo[key].state = "loading";
312
- newInfo[key].errorMessage = "";
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
+ };
313
347
 
314
348
  setInfo(newInfo);
315
349
 
316
350
  try {
317
351
  const consulta = await axios.request(options);
318
352
  const d = consulta.data;
319
- newInfo[key].state = "success";
320
- newInfo[key].errorMessage = "";
321
- newInfo[key].selectedItem = { ...newInfo[key].selectedItem, ...d };
353
+
322
354
  const index = newInfo[key]?.data?.findIndex(
323
- (d: any) => d?.id == id,
355
+ (item: any) => item?.id == id,
324
356
  );
325
357
 
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 });
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);
336
382
  return d;
337
383
  } catch (error: any) {
338
384
  const item = httpStatusCodes.find((s) => s.code == error.status);
339
385
 
340
- newInfo[key].state = "error";
341
- newInfo[key].errorMessage = item?.meaning || error.message;
386
+ setInfo({
387
+ ...newInfo,
388
+ [key]: {
389
+ ...newInfo[key],
390
+ state: "error",
391
+ errorMessage: item?.meaning || error.message,
392
+ },
393
+ });
394
+
342
395
  if (error.status == 403) {
343
396
  onError?.({ error, ...{ errorMessage: item?.meaning } });
344
397
  }
345
- setInfo({ ...newInfo });
398
+
346
399
  return error;
347
400
  }
348
401
  },
@@ -352,36 +405,53 @@ export default function useResources<T extends Record<string, ItemsRecord>>({
352
405
  url: `${baseURI}/${key}/${id}`,
353
406
  headers: { Authorization: token },
354
407
  };
355
- const newInfo = { ...info };
356
- // newInfo[key] = newInfo[key] || {};
357
- newInfo[key].state = "loading";
358
- newInfo[key].errorMessage = "";
408
+
409
+ const newInfo = {
410
+ ...info,
411
+ [key]: {
412
+ ...info[key],
413
+ data: [...(info[key]?.data ?? [])],
414
+ state: "loading",
415
+ errorMessage: "",
416
+ },
417
+ };
359
418
 
360
419
  setInfo(newInfo);
361
420
 
362
421
  try {
363
422
  const consulta = await axios.request(options);
364
423
  const d = consulta.data;
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 });
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
+
375
438
  return d.data;
376
439
  } catch (error: any) {
377
440
  const item = httpStatusCodes.find((s) => s.code == error.status);
378
441
 
379
- newInfo[key].state = "error";
380
- newInfo[key].errorMessage = item?.meaning || error.message;
381
442
  if (error.status == 403) {
382
- onError?.({ error, ...{ errorMessage: item?.meaning } });
443
+ onError?.({ error, errorMessage: item?.meaning });
383
444
  }
384
- setInfo({ ...newInfo });
445
+
446
+ setInfo({
447
+ ...newInfo,
448
+ [key]: {
449
+ ...newInfo[key],
450
+ state: "error",
451
+ errorMessage: item?.meaning || error.message,
452
+ },
453
+ });
454
+
385
455
  return error;
386
456
  }
387
457
  },