@zauru-sdk/hooks 2.0.121 → 2.0.123
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/esm/catalogs.js +30 -25
- package/dist/esm/templates.js +5 -6
- package/package.json +2 -2
package/dist/esm/catalogs.js
CHANGED
|
@@ -21,6 +21,7 @@ const redux_1 = require("@zauru-sdk/redux");
|
|
|
21
21
|
function useGetReduxCatalog(CATALOG_NAME, { online = false, wheres = [], otherParams } = {}) {
|
|
22
22
|
const dispatch = (0, redux_1.useAppDispatch)();
|
|
23
23
|
const fetcher = (0, react_1.useFetcher)();
|
|
24
|
+
const [fetchTriggered, setFetchTriggered] = (0, react_2.useState)(false);
|
|
24
25
|
const catalogData = (0, redux_1.useAppSelector)((state) => state.catalogs[CATALOG_NAME]);
|
|
25
26
|
// Verifica si ya tenemos algo en Redux
|
|
26
27
|
const hasLocalData = Boolean(catalogData?.data?.length);
|
|
@@ -61,20 +62,26 @@ function useGetReduxCatalog(CATALOG_NAME, { online = false, wheres = [], otherPa
|
|
|
61
62
|
: "";
|
|
62
63
|
// Disparamos la carga a través de fetcher
|
|
63
64
|
try {
|
|
65
|
+
setFetchTriggered(true);
|
|
64
66
|
fetcher.load(`/api/catalogs?catalog=${CATALOG_NAME}${queryString}`);
|
|
65
67
|
}
|
|
66
68
|
catch (error) {
|
|
67
|
-
console.error(error);
|
|
68
69
|
// Si hay datos locales, mostramos los datos locales
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
70
|
+
if (hasLocalData) {
|
|
71
|
+
console.error("Hubo un error pero hay datos locales en la consulta de", CATALOG_NAME, " Error: ", error);
|
|
72
|
+
setData({
|
|
73
|
+
data: catalogData?.data || [],
|
|
74
|
+
loading: false,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
console.error("Hubo un error y no hay datos locales en la consulta de", CATALOG_NAME, " Error: ", error);
|
|
79
|
+
(0, index_js_1.showAlert)({
|
|
80
|
+
type: "error",
|
|
81
|
+
title: "Error al cargar el catálogo",
|
|
82
|
+
description: `Error al cargar el catálogo ${CATALOG_NAME}, por favor intente nuevamente. Error: ${error}`,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
78
85
|
}
|
|
79
86
|
}
|
|
80
87
|
else {
|
|
@@ -92,36 +99,31 @@ function useGetReduxCatalog(CATALOG_NAME, { online = false, wheres = [], otherPa
|
|
|
92
99
|
* y decide qué hacer con la data o error recibidos.
|
|
93
100
|
*/
|
|
94
101
|
(0, react_2.useEffect)(() => {
|
|
102
|
+
// Solo ejecuto la lógica si ya disparé al menos un fetch
|
|
103
|
+
if (!fetchTriggered)
|
|
104
|
+
return;
|
|
95
105
|
// Cuando fetcher se pone en "idle", significa que ya terminó la petición.
|
|
96
106
|
if (fetcher.state === "idle") {
|
|
97
107
|
// Caso: tenemos algo en fetcher.data
|
|
98
108
|
if (fetcher.data) {
|
|
99
109
|
const possibleError = fetcher.data;
|
|
100
110
|
const possibleData = fetcher.data;
|
|
101
|
-
// Si es un error "clásico"
|
|
102
|
-
if (possibleError?.
|
|
103
|
-
// Aquí interpretamos que la API pudo haber respondido algo tipo { description, ... } => error
|
|
111
|
+
// Si es un error "clásico", lo manejamos
|
|
112
|
+
if (possibleError?.error) {
|
|
104
113
|
// Pero OJO: si ya teníamos datos locales, no mostramos error "fatal" para no romper el fallback
|
|
105
114
|
if (hasLocalData) {
|
|
106
|
-
|
|
107
|
-
// pero no borramos lo que hay en data
|
|
108
|
-
(0, index_js_1.showAlert)({
|
|
109
|
-
type: possibleError.type || "error",
|
|
110
|
-
title: possibleError.title || "Error",
|
|
111
|
-
description: possibleError.description,
|
|
112
|
-
});
|
|
113
|
-
// Marcamos loading false pero dejamos intacto `data.data`
|
|
114
|
-
setData((prev) => ({ ...prev, loading: false }));
|
|
115
|
+
console.log("Hay error en la respuesta pero hay datos locales en la consulta de", CATALOG_NAME);
|
|
115
116
|
}
|
|
116
117
|
else {
|
|
118
|
+
console.log("Hay error en la respuesta y no hay datos locales en la consulta de", CATALOG_NAME);
|
|
117
119
|
// No hay datos locales -> mostramos error y no tenemos fallback
|
|
118
120
|
(0, index_js_1.showAlert)({
|
|
119
121
|
type: "error",
|
|
120
122
|
title: possibleError.title || "Error",
|
|
121
123
|
description: possibleError.description,
|
|
122
124
|
});
|
|
123
|
-
setData((prev) => ({ ...prev, loading: false }));
|
|
124
125
|
}
|
|
126
|
+
setData((prev) => ({ ...prev, loading: false }));
|
|
125
127
|
}
|
|
126
128
|
else {
|
|
127
129
|
// Caso: la respuesta sí es data real
|
|
@@ -141,6 +143,7 @@ function useGetReduxCatalog(CATALOG_NAME, { online = false, wheres = [], otherPa
|
|
|
141
143
|
// Por alguna razón no llegó el array esperado
|
|
142
144
|
// Revisamos si hay fallback local
|
|
143
145
|
if (hasLocalData) {
|
|
146
|
+
console.log("Hubo un error en el parseo de la respuesta pero hay datos locales en la consulta de", CATALOG_NAME, " retornó: ", newData);
|
|
144
147
|
// No reportar error: usamos lo local
|
|
145
148
|
setData((prev) => ({ ...prev, loading: false }));
|
|
146
149
|
}
|
|
@@ -151,6 +154,7 @@ function useGetReduxCatalog(CATALOG_NAME, { online = false, wheres = [], otherPa
|
|
|
151
154
|
title: "Error al recibir el catálogo",
|
|
152
155
|
description: `No se obtuvo la propiedad "${CATALOG_NAME}" en la respuesta.`,
|
|
153
156
|
});
|
|
157
|
+
console.log("Hubo un error en parseo de la respuesta y no hay datos locales en la consulta de", CATALOG_NAME, " retornó: ", newData);
|
|
154
158
|
setData((prev) => ({ ...prev, loading: false }));
|
|
155
159
|
}
|
|
156
160
|
}
|
|
@@ -160,17 +164,18 @@ function useGetReduxCatalog(CATALOG_NAME, { online = false, wheres = [], otherPa
|
|
|
160
164
|
// fetcher.state === "idle" pero fetcher.data es null/undefined => seguramente hubo error global
|
|
161
165
|
if (hasLocalData) {
|
|
162
166
|
// Fallback a datos locales
|
|
163
|
-
|
|
167
|
+
console.log("No hay datos en la respuesta pero hay datos locales en la consulta de", CATALOG_NAME);
|
|
164
168
|
}
|
|
165
169
|
else {
|
|
166
170
|
// Ni API ni local data => error
|
|
171
|
+
console.log("No hay datos en la respuesta y no hay datos locales en la consulta de", CATALOG_NAME);
|
|
167
172
|
(0, index_js_1.showAlert)({
|
|
168
173
|
type: "error",
|
|
169
174
|
title: "Error al cargar el catálogo",
|
|
170
175
|
description: `No se obtuvo respuesta y no hay datos locales para ${CATALOG_NAME}`,
|
|
171
176
|
});
|
|
172
|
-
setData((prev) => ({ ...prev, loading: false }));
|
|
173
177
|
}
|
|
178
|
+
setData((prev) => ({ ...prev, loading: false }));
|
|
174
179
|
}
|
|
175
180
|
}
|
|
176
181
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
package/dist/esm/templates.js
CHANGED
|
@@ -9,6 +9,7 @@ function useGetTemplateObject(TEMPLATE_NAME, config = {}) {
|
|
|
9
9
|
try {
|
|
10
10
|
const dispatch = (0, redux_1.useAppDispatch)();
|
|
11
11
|
const fetcher = (0, react_1.useFetcher)();
|
|
12
|
+
const [fetchTriggered, setFetchTriggered] = (0, react_2.useState)(false);
|
|
12
13
|
// Obtenemos del store lo que ya se tenga
|
|
13
14
|
const objectData = (0, redux_1.useAppSelector)((state) => state.templates[TEMPLATE_NAME]);
|
|
14
15
|
// Verifica si ya tenemos algo en Redux
|
|
@@ -32,6 +33,7 @@ function useGetTemplateObject(TEMPLATE_NAME, config = {}) {
|
|
|
32
33
|
dispatch((0, redux_1.templateFetchStart)(TEMPLATE_NAME));
|
|
33
34
|
// Aquí hacemos la llamada a la API a través del fetcher
|
|
34
35
|
try {
|
|
36
|
+
setFetchTriggered(true);
|
|
35
37
|
fetcher.load(`/api/templates?object=${TEMPLATE_NAME}`);
|
|
36
38
|
}
|
|
37
39
|
catch (error) {
|
|
@@ -63,6 +65,9 @@ function useGetTemplateObject(TEMPLATE_NAME, config = {}) {
|
|
|
63
65
|
* Decidimos si lo que vino es error o data, y procedemos.
|
|
64
66
|
*/
|
|
65
67
|
(0, react_2.useEffect)(() => {
|
|
68
|
+
// Solo ejecuto la lógica si ya disparé al menos un fetch
|
|
69
|
+
if (!fetchTriggered)
|
|
70
|
+
return;
|
|
66
71
|
if (fetcher.state === "idle") {
|
|
67
72
|
if (fetcher.data) {
|
|
68
73
|
// Podría ser un error o ser la data
|
|
@@ -72,12 +77,6 @@ function useGetTemplateObject(TEMPLATE_NAME, config = {}) {
|
|
|
72
77
|
if (possibleError.description) {
|
|
73
78
|
// Ya teníamos datos locales?
|
|
74
79
|
if (hasLocalData) {
|
|
75
|
-
// Mostramos alert, pero NO borramos los datos locales
|
|
76
|
-
(0, index_js_1.showAlert)({
|
|
77
|
-
type: possibleError.type || "error",
|
|
78
|
-
title: possibleError.title || "Error",
|
|
79
|
-
description: possibleError.description,
|
|
80
|
-
});
|
|
81
80
|
// Dejamos loading en false, data queda como estaba
|
|
82
81
|
setData((prev) => ({ ...prev, loading: false }));
|
|
83
82
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zauru-sdk/hooks",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.123",
|
|
4
4
|
"description": "Hooks reutilizables dentro de las webapps de Zauru.",
|
|
5
5
|
"main": "./dist/esm/index.js",
|
|
6
6
|
"module": "./dist/esm/index.js",
|
|
@@ -33,5 +33,5 @@
|
|
|
33
33
|
"react": "^18.2.0",
|
|
34
34
|
"react-dom": "^18.2.0"
|
|
35
35
|
},
|
|
36
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "a09530092b4fe47f91456c18b9463fe012737e09"
|
|
37
37
|
}
|