next-recomponents 1.5.99 → 1.6.2

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.
@@ -8,10 +8,10 @@ export default function useResources<T extends Record<string, ItemsRecord>>({
8
8
  baseURI,
9
9
  endpoints,
10
10
  onError,
11
- }: Props) {
11
+ }: Props<T>) {
12
12
  const token = useToken();
13
13
 
14
- const [info, setInfo] = useState<Record<string, any>>(
14
+ const [info, setInfo] = useState<EnhancedEndpoints<T>>(
15
15
  Object.keys(endpoints).reduce((acc: any, key) => {
16
16
  const newAcc: any = {
17
17
  ...acc,
@@ -19,6 +19,8 @@ export default function useResources<T extends Record<string, ItemsRecord>>({
19
19
  loaded: false,
20
20
  id: endpoints[key]?.id,
21
21
  defaultParams: endpoints[key]?.defaultParams,
22
+ data: [],
23
+ selectedItem: {},
22
24
  },
23
25
  };
24
26
 
@@ -26,128 +28,26 @@ export default function useResources<T extends Record<string, ItemsRecord>>({
26
28
  }, {} as any)
27
29
  );
28
30
 
29
- const results = Object.keys(endpoints).reduce((acc: any, key) => {
30
- const endpoint = endpoints[key];
31
- const showFunc = async (
32
- { limit = 10, page = 1, merge = true, ...query }: ShowOptions,
33
- autoLoad: boolean = false
34
- ) => {
35
- const options = {
36
- method: "GET",
37
- url: `${baseURI}/${key}`,
38
- params: { limit, page, ...query },
39
- headers: { Authorization: token },
40
- };
41
-
42
- const newInfo = { ...info };
43
- newInfo[key] = newInfo[key] || {};
44
- newInfo[key].state = "loading";
45
- newInfo[key].errorMessage = "";
46
- newInfo[key].params = query;
47
- newInfo[key].loaded = true;
48
-
49
- setInfo(newInfo);
50
-
51
- try {
52
- const consulta = await axios.request(options);
53
- const d = consulta.data;
54
- newInfo[key].state = "success";
55
- newInfo[key].errorMessage = "";
56
-
57
- newInfo[key].data = merge
58
- ? page == 1
59
- ? d.data
60
- : [...d.data, ...(newInfo[key].data || [])]
61
- : d.data;
62
- newInfo[key].totalItems = d.totalItems;
63
- newInfo[key].totalPages = d.totalPages;
64
- newInfo[key].currentPage = d.currentPage;
65
-
66
- setInfo({ ...newInfo });
67
- return d.data;
68
- } catch (error: any) {
69
- const item = httpStatusCodes.find((s) => s.code == error.status);
70
-
71
- newInfo[key].state = "error";
72
- newInfo[key].errorMessage = item?.meaning;
73
- if (error.status == 403) {
74
- onError?.({ error, ...{ errorMessage: item?.meaning } });
75
- }
76
- setInfo({ ...newInfo });
77
- return error;
78
- }
79
- };
80
- const findFunc = async (
81
- id: number,
82
- query: any,
83
- autoLoad: boolean = false
84
- ) => {
85
- const options = {
86
- method: "GET",
87
- url: `${baseURI}/${key}/${id}`,
88
- params: { ...query },
89
- headers: { Authorization: token },
90
- };
91
- const newInfo = { ...info };
92
- newInfo[key] = newInfo[key] || {};
93
- newInfo[key].state = "loading";
94
- newInfo[key].errorMessage = "";
95
- newInfo[key].params = query;
96
- newInfo[key].loaded = true;
97
-
98
- setInfo(newInfo);
99
-
100
- try {
101
- const consulta = await axios.request(options);
102
- const d = consulta.data;
103
- newInfo[key].state = "success";
104
- newInfo[key].errorMessage = "";
105
- if (autoLoad) {
106
- } else {
107
- newInfo[key].selectedItem = d;
108
- const index = newInfo[key]?.data?.findIndex(
109
- (d: any) => d?.id == d?.id
110
- );
111
- if (index >= 0) {
112
- newInfo[key].data[index] = d;
113
- } else {
114
- if (newInfo[key]?.data) {
115
- newInfo[key].data.unshift(d);
116
- } else {
117
- newInfo[key].data = [d];
118
- }
119
- }
120
- }
121
- setInfo({ ...newInfo });
122
- return d;
123
- } catch (error: any) {
124
- const item = httpStatusCodes.find((s) => s.code == error.status);
125
-
126
- newInfo[key].state = "error";
127
- newInfo[key].errorMessage = item?.meaning || error.message;
128
- if (error.status == 403) {
129
- onError?.({ error, ...{ errorMessage: item?.meaning } });
130
- }
131
- setInfo({ ...newInfo });
132
- return error;
133
- }
134
- };
135
- acc[key] = {
136
- ...endpoint,
137
- loaded: false,
138
- show: showFunc,
139
- find: findFunc,
140
- create: async (data: Record<string, any> | Record<string, any>[]) => {
31
+ const results: EnhancedEndpoints<T> = Object.keys(endpoints).reduce(
32
+ (acc: any, key) => {
33
+ const endpoint = endpoints[key];
34
+ const showFunc = async (
35
+ { limit = 10, page = 1, merge = true, ...query }: ShowOptions,
36
+ autoLoad: boolean = false
37
+ ) => {
141
38
  const options = {
142
- method: "POST",
39
+ method: "GET",
143
40
  url: `${baseURI}/${key}`,
144
- data: Array.isArray(data) ? [...data] : { ...data },
41
+ params: { limit, page, ...query },
145
42
  headers: { Authorization: token },
146
43
  };
44
+
147
45
  const newInfo = { ...info };
148
- newInfo[key] = newInfo[key] || {};
46
+ // newInfo[key] = newInfo[key] || {};
149
47
  newInfo[key].state = "loading";
150
48
  newInfo[key].errorMessage = "";
49
+ newInfo[key].params = query;
50
+ newInfo[key].loaded = true;
151
51
 
152
52
  setInfo(newInfo);
153
53
 
@@ -156,61 +56,43 @@ export default function useResources<T extends Record<string, ItemsRecord>>({
156
56
  const d = consulta.data;
157
57
  newInfo[key].state = "success";
158
58
  newInfo[key].errorMessage = "";
159
- if (Array.isArray(data)) {
160
- for (let datum of data) {
161
- const index = newInfo[key]?.data?.findIndex(
162
- (d: any) => d?.id == datum?.id
163
- );
164
- if (index >= 0) {
165
- newInfo[key].data[index] = d;
166
- } else {
167
- if (newInfo[key]?.data) {
168
- newInfo[key].data.unshift(d);
169
- } else {
170
- newInfo[key].data = [d];
171
- }
172
- }
173
- }
174
- } else {
175
- newInfo[key].selectedItem = d;
176
- const index = newInfo[key]?.data?.findIndex(
177
- (d: any) => d?.id == d?.id
178
- );
179
- if (index >= 0) {
180
- newInfo[key].data[index] = d;
181
- } else {
182
- if (newInfo[key]?.data) {
183
- newInfo[key].data.unshift(d);
184
- } else {
185
- newInfo[key].data = [d];
186
- }
187
- }
188
- }
59
+
60
+ newInfo[key].data = merge
61
+ ? page == 1
62
+ ? d.data
63
+ : [...d.data, ...(newInfo[key].data || [])]
64
+ : d.data;
65
+ newInfo[key].totalItems = d.totalItems;
66
+ newInfo[key].totalPages = d.totalPages;
67
+ newInfo[key].currentPage = d.currentPage;
68
+
189
69
  setInfo({ ...newInfo });
190
- return d;
70
+ return d.data;
191
71
  } catch (error: any) {
192
72
  const item = httpStatusCodes.find((s) => s.code == error.status);
193
73
 
194
74
  newInfo[key].state = "error";
195
- newInfo[key].errorMessage = item?.meaning || error.message;
75
+ newInfo[key].errorMessage = item?.meaning;
196
76
  if (error.status == 403) {
197
77
  onError?.({ error, ...{ errorMessage: item?.meaning } });
198
78
  }
199
79
  setInfo({ ...newInfo });
200
80
  return error;
201
81
  }
202
- },
203
- update: async (id: number, data: any) => {
82
+ };
83
+ const findFunc = async (id: number, query: any) => {
204
84
  const options = {
205
- method: "PATCH",
85
+ method: "GET",
206
86
  url: `${baseURI}/${key}/${id}`,
207
- data: Array.isArray(data) ? [...data] : { ...data },
87
+ params: { ...query },
208
88
  headers: { Authorization: token },
209
89
  };
210
90
  const newInfo = { ...info };
211
- newInfo[key] = newInfo[key] || {};
91
+ // newInfo[key] = newInfo[key] || {};
212
92
  newInfo[key].state = "loading";
213
93
  newInfo[key].errorMessage = "";
94
+ newInfo[key].params = query;
95
+ newInfo[key].loaded = true;
214
96
 
215
97
  setInfo(newInfo);
216
98
 
@@ -219,10 +101,10 @@ export default function useResources<T extends Record<string, ItemsRecord>>({
219
101
  const d = consulta.data;
220
102
  newInfo[key].state = "success";
221
103
  newInfo[key].errorMessage = "";
104
+
222
105
  newInfo[key].selectedItem = d;
223
- const index = newInfo[key]?.data?.findIndex(
224
- (d: any) => d?.id == d?.id
225
- );
106
+ const index = newInfo[key]?.data?.findIndex((d: any) => d?.id == id);
107
+ console.log("index", index, newInfo[key]?.data);
226
108
  if (index >= 0) {
227
109
  newInfo[key].data[index] = d;
228
110
  } else {
@@ -232,6 +114,7 @@ export default function useResources<T extends Record<string, ItemsRecord>>({
232
114
  newInfo[key].data = [d];
233
115
  }
234
116
  }
117
+
235
118
  setInfo({ ...newInfo });
236
119
  return d;
237
120
  } catch (error: any) {
@@ -245,65 +128,182 @@ export default function useResources<T extends Record<string, ItemsRecord>>({
245
128
  setInfo({ ...newInfo });
246
129
  return error;
247
130
  }
248
- },
249
- remove: async (id: number) => {
250
- const options = {
251
- method: "DELETE",
252
- url: `${baseURI}/${key}/${id}`,
253
- headers: { Authorization: token },
254
- };
255
- const newInfo = { ...info };
256
- newInfo[key] = newInfo[key] || {};
257
- newInfo[key].state = "loading";
258
- newInfo[key].errorMessage = "";
131
+ };
132
+ acc[key] = {
133
+ ...endpoint,
134
+ loaded: false,
135
+ show: showFunc,
136
+ find: findFunc,
137
+ create: async (data: Record<string, any> | Record<string, any>[]) => {
138
+ const options = {
139
+ method: "POST",
140
+ url: `${baseURI}/${key}`,
141
+ data: Array.isArray(data) ? [...data] : { ...data },
142
+ headers: { Authorization: token },
143
+ };
144
+ const newInfo = { ...info };
145
+ // newInfo[key] = newInfo[key] || {};
146
+ newInfo[key].state = "loading";
147
+ newInfo[key].errorMessage = "";
259
148
 
260
- setInfo(newInfo);
149
+ setInfo(newInfo);
261
150
 
262
- try {
263
- const consulta = await axios.request(options);
264
- const d = consulta.data;
265
- newInfo[key].state = "success";
151
+ try {
152
+ const consulta = await axios.request(options);
153
+ const d = consulta.data;
154
+ newInfo[key].state = "success";
155
+ newInfo[key].errorMessage = "";
156
+ if (Array.isArray(data)) {
157
+ for (let datum of data) {
158
+ const index = newInfo[key]?.data?.findIndex(
159
+ (d: any) => d?.id == datum?.id
160
+ );
161
+ if (index >= 0) {
162
+ newInfo[key].data[index] = d;
163
+ } else {
164
+ if (newInfo[key]?.data) {
165
+ newInfo[key].data.unshift(d);
166
+ } else {
167
+ newInfo[key].data = [d];
168
+ }
169
+ }
170
+ }
171
+ newInfo[key].data = newInfo[key].data.flat();
172
+ } else {
173
+ newInfo[key].selectedItem = d;
174
+ const index = newInfo[key]?.data?.findIndex(
175
+ (d: any) => d?.id == data?.id
176
+ );
177
+ if (index >= 0) {
178
+ newInfo[key].data[index] = d;
179
+ } else {
180
+ if (newInfo[key]?.data) {
181
+ newInfo[key].data.unshift(d);
182
+ } else {
183
+ newInfo[key].data = [d];
184
+ }
185
+ }
186
+ }
187
+ setInfo({ ...newInfo });
188
+ return d;
189
+ } catch (error: any) {
190
+ const item = httpStatusCodes.find((s) => s.code == error.status);
191
+
192
+ newInfo[key].state = "error";
193
+ newInfo[key].errorMessage = item?.meaning || error.message;
194
+ if (error.status == 403) {
195
+ onError?.({ error, ...{ errorMessage: item?.meaning } });
196
+ }
197
+ setInfo({ ...newInfo });
198
+ return error;
199
+ }
200
+ },
201
+ update: async (id: number, data: any) => {
202
+ const options = {
203
+ method: "PATCH",
204
+ url: `${baseURI}/${key}/${id}`,
205
+ data: Array.isArray(data) ? [...data] : { ...data },
206
+ headers: { Authorization: token },
207
+ };
208
+ const newInfo = { ...info };
209
+ // newInfo[key] = newInfo[key] || {};
210
+ newInfo[key].state = "loading";
266
211
  newInfo[key].errorMessage = "";
267
- newInfo[key].selectedItem = d;
268
- const index = newInfo[key]?.data?.findIndex(
269
- (d: any) => d?.id == d?.id
270
- );
271
- if (index >= 0) {
272
- newInfo[key].data.splice(index, 1);
212
+
213
+ setInfo(newInfo);
214
+
215
+ try {
216
+ const consulta = await axios.request(options);
217
+ const d = consulta.data;
218
+ newInfo[key].state = "success";
219
+ newInfo[key].errorMessage = "";
220
+ newInfo[key].selectedItem = d;
221
+ const index = newInfo[key]?.data?.findIndex(
222
+ (d: any) => d?.id == id
223
+ );
224
+ if (index >= 0) {
225
+ newInfo[key].data[index] = d;
226
+ } else {
227
+ if (newInfo[key]?.data) {
228
+ newInfo[key].data.unshift(d);
229
+ } else {
230
+ newInfo[key].data = [d];
231
+ }
232
+ }
233
+ setInfo({ ...newInfo });
234
+ return d;
235
+ } catch (error: any) {
236
+ const item = httpStatusCodes.find((s) => s.code == error.status);
237
+
238
+ newInfo[key].state = "error";
239
+ newInfo[key].errorMessage = item?.meaning || error.message;
240
+ if (error.status == 403) {
241
+ onError?.({ error, ...{ errorMessage: item?.meaning } });
242
+ }
243
+ setInfo({ ...newInfo });
244
+ return error;
273
245
  }
274
- setInfo({ ...newInfo });
275
- return d.data;
276
- } catch (error: any) {
277
- const item = httpStatusCodes.find((s) => s.code == error.status);
246
+ },
247
+ remove: async (id: number) => {
248
+ const options = {
249
+ method: "DELETE",
250
+ url: `${baseURI}/${key}/${id}`,
251
+ headers: { Authorization: token },
252
+ };
253
+ const newInfo = { ...info };
254
+ // newInfo[key] = newInfo[key] || {};
255
+ newInfo[key].state = "loading";
256
+ newInfo[key].errorMessage = "";
278
257
 
279
- newInfo[key].state = "error";
280
- newInfo[key].errorMessage = item?.meaning || error.message;
281
- if (error.status == 403) {
282
- onError?.({ error, ...{ errorMessage: item?.meaning } });
258
+ setInfo(newInfo);
259
+
260
+ try {
261
+ const consulta = await axios.request(options);
262
+ const d = consulta.data;
263
+ newInfo[key].state = "success";
264
+ newInfo[key].errorMessage = "";
265
+ newInfo[key].selectedItem = d;
266
+ const index = newInfo[key]?.data?.findIndex(
267
+ (d: any) => d?.id == id
268
+ );
269
+ if (index >= 0) {
270
+ newInfo[key].data.splice(index, 1);
271
+ }
272
+ setInfo({ ...newInfo });
273
+ return d.data;
274
+ } catch (error: any) {
275
+ const item = httpStatusCodes.find((s) => s.code == error.status);
276
+
277
+ newInfo[key].state = "error";
278
+ newInfo[key].errorMessage = item?.meaning || error.message;
279
+ if (error.status == 403) {
280
+ onError?.({ error, ...{ errorMessage: item?.meaning } });
281
+ }
282
+ setInfo({ ...newInfo });
283
+ return error;
283
284
  }
284
- setInfo({ ...newInfo });
285
- return error;
286
- }
287
- },
288
- totalPages: info[key]?.totalPages,
289
- currentPage: info[key]?.currentPage,
290
- state: info[key]?.state || "success",
291
- errorMessage: info[key]?.errorMessage,
292
- params: info[key]?.params,
293
- setLoaded: () => {
294
- const newInfo = { ...info };
295
- newInfo[key].loaded = true;
296
- setInfo(newInfo);
297
- },
298
- getAllPages: async (limit = 10) => {
299
- console.log("Not aviable");
300
- },
301
- data: info[key]?.data || [],
302
- selectedItem: info[key]?.selectedItem || {},
303
- };
285
+ },
286
+ totalPages: info[key]?.totalPages,
287
+ currentPage: info[key]?.currentPage,
288
+ state: info[key]?.state || "success",
289
+ errorMessage: info[key]?.errorMessage,
290
+ params: info[key]?.params,
291
+ setLoaded: () => {
292
+ const newInfo = { ...info };
293
+ newInfo[key].loaded = true;
294
+ setInfo(newInfo);
295
+ },
296
+ getAllPages: async (limit = 10) => {
297
+ console.log("Not aviable");
298
+ },
299
+ data: info[key]?.data || [],
300
+ selectedItem: info[key]?.selectedItem || {},
301
+ };
304
302
 
305
- return acc;
306
- }, {} as EnhancedEndpoints<T>);
303
+ return acc;
304
+ },
305
+ {}
306
+ );
307
307
 
308
308
  async function doSome() {
309
309
  const key = Object.keys(info).find((k) => {
@@ -1,12 +1,13 @@
1
- export interface ItemsRecord {
2
- typeof: Object;
1
+ // Genérico con tipo por defecto
2
+ export interface ItemsRecord<T = any> {
3
+ typeof: T;
3
4
  id?: number | string | string[] | undefined;
4
5
  defaultParams?: Record<string, any>;
5
6
  }
6
7
 
7
- export interface Props {
8
+ export interface Props<T extends Record<string, ItemsRecord>> {
8
9
  baseURI: string;
9
- endpoints: Record<string, Partial<ItemsRecord>>;
10
+ endpoints: T;
10
11
  onError?: (error: any) => void;
11
12
  }
12
13
 
@@ -16,6 +17,7 @@ export interface ShowOptions {
16
17
  page?: number;
17
18
  [key: string | number]: string | number | undefined | null | boolean;
18
19
  }
20
+
19
21
  export type EnhancedEndpoints<T extends Record<string, ItemsRecord>> = {
20
22
  [K in keyof T]: {
21
23
  typeof: T[K]["typeof"];
@@ -34,15 +36,17 @@ export type EnhancedEndpoints<T extends Record<string, ItemsRecord>> = {
34
36
  props?: Record<string, string>
35
37
  ) => Promise<T[K]["typeof"]>;
36
38
  getAllPages: (limit: number) => Promise<Array<T[K]["typeof"]>>;
37
- data: T[K]["typeof"][];
39
+ data: Array<T[K]["typeof"]>;
38
40
  selectedItem: T[K]["typeof"];
39
41
  params: Record<string, any>;
40
42
  currentPage: number;
41
43
  totalItems: number;
42
44
  totalPages: number;
43
- errorMessage: string;
44
- autoLoad?: Boolean;
45
+ errorMessage: string | undefined;
46
+ autoLoad?: boolean;
45
47
  defaultParams?: Record<string, any>;
46
48
  id?: number | string;
49
+ loaded: boolean;
50
+ setLoaded: () => void;
47
51
  };
48
52
  };