@zauru-sdk/hooks 2.0.122 → 2.0.151
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 +7 -2
- package/dist/esm/templates.js +7 -8
- package/package.json +3 -3
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,6 +62,7 @@ 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) {
|
|
@@ -68,7 +70,7 @@ function useGetReduxCatalog(CATALOG_NAME, { online = false, wheres = [], otherPa
|
|
|
68
70
|
if (hasLocalData) {
|
|
69
71
|
console.error("Hubo un error pero hay datos locales en la consulta de", CATALOG_NAME, " Error: ", error);
|
|
70
72
|
setData({
|
|
71
|
-
data: catalogData?.data || [],
|
|
73
|
+
data: (catalogData?.data || []),
|
|
72
74
|
loading: false,
|
|
73
75
|
});
|
|
74
76
|
}
|
|
@@ -86,7 +88,7 @@ function useGetReduxCatalog(CATALOG_NAME, { online = false, wheres = [], otherPa
|
|
|
86
88
|
// Si no vamos a hacer fetch, asegurarnos de que loading esté en false
|
|
87
89
|
// y mantener lo que ya tengamos en Redux
|
|
88
90
|
setData({
|
|
89
|
-
data: catalogData?.data || [],
|
|
91
|
+
data: (catalogData?.data || []),
|
|
90
92
|
loading: false,
|
|
91
93
|
});
|
|
92
94
|
}
|
|
@@ -97,6 +99,9 @@ function useGetReduxCatalog(CATALOG_NAME, { online = false, wheres = [], otherPa
|
|
|
97
99
|
* y decide qué hacer con la data o error recibidos.
|
|
98
100
|
*/
|
|
99
101
|
(0, react_2.useEffect)(() => {
|
|
102
|
+
// Solo ejecuto la lógica si ya disparé al menos un fetch
|
|
103
|
+
if (!fetchTriggered)
|
|
104
|
+
return;
|
|
100
105
|
// Cuando fetcher se pone en "idle", significa que ya terminó la petición.
|
|
101
106
|
if (fetcher.state === "idle") {
|
|
102
107
|
// Caso: tenemos algo en fetcher.data
|
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
|
|
@@ -26,12 +27,13 @@ function useGetTemplateObject(TEMPLATE_NAME, config = {}) {
|
|
|
26
27
|
* - En caso contrario, mostramos datos locales y no hacemos fetch.
|
|
27
28
|
*/
|
|
28
29
|
(0, react_2.useEffect)(() => {
|
|
29
|
-
const mustFetch = config.online ||
|
|
30
|
+
const mustFetch = config.online || !hasLocalData;
|
|
30
31
|
if (mustFetch) {
|
|
31
32
|
setData((prev) => ({ ...prev, loading: true }));
|
|
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) {
|
|
@@ -57,12 +59,15 @@ function useGetTemplateObject(TEMPLATE_NAME, config = {}) {
|
|
|
57
59
|
});
|
|
58
60
|
}
|
|
59
61
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
60
|
-
}, [config.online,
|
|
62
|
+
}, [config.online, hasLocalData, TEMPLATE_NAME]);
|
|
61
63
|
/**
|
|
62
64
|
* 2) Efecto para cuando el fetcher termina (fetcher.state === "idle").
|
|
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.151",
|
|
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",
|
|
@@ -28,10 +28,10 @@
|
|
|
28
28
|
"@remix-run/react": "^2.8.1",
|
|
29
29
|
"@zauru-sdk/common": "^2.0.118",
|
|
30
30
|
"@zauru-sdk/icons": "^2.0.99",
|
|
31
|
-
"@zauru-sdk/redux": "^2.0.
|
|
31
|
+
"@zauru-sdk/redux": "^2.0.151",
|
|
32
32
|
"@zauru-sdk/types": "^2.0.109",
|
|
33
33
|
"react": "^18.2.0",
|
|
34
34
|
"react-dom": "^18.2.0"
|
|
35
35
|
},
|
|
36
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "280ffceba6c2884b151241feaac047038077b3a4"
|
|
37
37
|
}
|