@zauru-sdk/hooks 2.0.120 → 2.0.121
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/templates.js +129 -38
- package/dist/templates.d.ts +2 -5
- package/package.json +3 -3
package/dist/esm/templates.js
CHANGED
|
@@ -5,60 +5,151 @@ const react_1 = require("@remix-run/react");
|
|
|
5
5
|
const redux_1 = require("@zauru-sdk/redux");
|
|
6
6
|
const react_2 = require("react");
|
|
7
7
|
const index_js_1 = require("./index.js");
|
|
8
|
-
|
|
8
|
+
function useGetTemplateObject(TEMPLATE_NAME, config = {}) {
|
|
9
9
|
try {
|
|
10
|
-
const fetcher = (0, react_1.useFetcher)();
|
|
11
10
|
const dispatch = (0, redux_1.useAppDispatch)();
|
|
11
|
+
const fetcher = (0, react_1.useFetcher)();
|
|
12
|
+
// Obtenemos del store lo que ya se tenga
|
|
12
13
|
const objectData = (0, redux_1.useAppSelector)((state) => state.templates[TEMPLATE_NAME]);
|
|
14
|
+
// Verifica si ya tenemos algo en Redux
|
|
15
|
+
const hasLocalData = objectData?.data && Object.keys(objectData.data).length > 0;
|
|
16
|
+
// Estado local para data y loading
|
|
13
17
|
const [data, setData] = (0, react_2.useState)({
|
|
14
|
-
data: objectData?.data
|
|
15
|
-
|
|
16
|
-
: {},
|
|
17
|
-
loading: objectData.loading,
|
|
18
|
+
data: hasLocalData ? objectData?.data : {},
|
|
19
|
+
loading: false,
|
|
18
20
|
});
|
|
21
|
+
/**
|
|
22
|
+
* 1) Efecto para decidir si hacemos fetch o no.
|
|
23
|
+
* - Si `config.online === true`, forzamos fetch.
|
|
24
|
+
* - Si `objectData.reFetch === true`, forzamos fetch (si así lo manejas en redux).
|
|
25
|
+
* - Si no tenemos datos locales, forzamos fetch.
|
|
26
|
+
* - En caso contrario, mostramos datos locales y no hacemos fetch.
|
|
27
|
+
*/
|
|
19
28
|
(0, react_2.useEffect)(() => {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
}, [fetcher.data]);
|
|
28
|
-
(0, react_2.useEffect)(() => {
|
|
29
|
-
if (fetcher.state === "idle" && fetcher.data != null) {
|
|
30
|
-
const receivedData = fetcher.data;
|
|
31
|
-
if (receivedData) {
|
|
32
|
-
setData({ data: receivedData[TEMPLATE_NAME], loading: false });
|
|
33
|
-
dispatch((0, redux_1.templateFetchSuccess)({
|
|
34
|
-
name: TEMPLATE_NAME,
|
|
35
|
-
data: receivedData[TEMPLATE_NAME],
|
|
36
|
-
}));
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}, [fetcher, dispatch, TEMPLATE_NAME]);
|
|
40
|
-
(0, react_2.useEffect)(() => {
|
|
41
|
-
if ((objectData?.data && Object.keys(objectData?.data).length <= 0) ||
|
|
42
|
-
config?.online) {
|
|
29
|
+
const mustFetch = config.online || objectData?.reFetch || !hasLocalData;
|
|
30
|
+
if (mustFetch) {
|
|
31
|
+
setData((prev) => ({ ...prev, loading: true }));
|
|
32
|
+
dispatch((0, redux_1.templateFetchStart)(TEMPLATE_NAME));
|
|
33
|
+
// Aquí hacemos la llamada a la API a través del fetcher
|
|
43
34
|
try {
|
|
44
|
-
setData({ ...data, loading: true });
|
|
45
|
-
dispatch((0, redux_1.templateFetchStart)(TEMPLATE_NAME));
|
|
46
35
|
fetcher.load(`/api/templates?object=${TEMPLATE_NAME}`);
|
|
47
36
|
}
|
|
48
|
-
catch (
|
|
37
|
+
catch (error) {
|
|
38
|
+
console.error(error);
|
|
39
|
+
// Si hay datos locales, los conservamos; si no, data queda vacío
|
|
40
|
+
setData({
|
|
41
|
+
data: hasLocalData ? objectData?.data : {},
|
|
42
|
+
loading: false,
|
|
43
|
+
});
|
|
49
44
|
(0, index_js_1.showAlert)({
|
|
50
45
|
type: "error",
|
|
51
|
-
title: `
|
|
52
|
-
description:
|
|
46
|
+
title: `Error al cargar la plantilla: ${TEMPLATE_NAME}`,
|
|
47
|
+
description: `Error: ${String(error)}`,
|
|
53
48
|
});
|
|
54
49
|
}
|
|
55
50
|
}
|
|
56
|
-
|
|
51
|
+
else {
|
|
52
|
+
// Si NO debemos hacer fetch, aseguramos loading en false
|
|
53
|
+
// y usamos lo que haya en Redux
|
|
54
|
+
setData({
|
|
55
|
+
data: hasLocalData ? objectData?.data : {},
|
|
56
|
+
loading: false,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
60
|
+
}, [config.online, objectData?.reFetch, hasLocalData, TEMPLATE_NAME]);
|
|
61
|
+
/**
|
|
62
|
+
* 2) Efecto para cuando el fetcher termina (fetcher.state === "idle").
|
|
63
|
+
* Decidimos si lo que vino es error o data, y procedemos.
|
|
64
|
+
*/
|
|
65
|
+
(0, react_2.useEffect)(() => {
|
|
66
|
+
if (fetcher.state === "idle") {
|
|
67
|
+
if (fetcher.data) {
|
|
68
|
+
// Podría ser un error o ser la data
|
|
69
|
+
const possibleError = fetcher.data;
|
|
70
|
+
const possibleData = fetcher.data;
|
|
71
|
+
// Si detectamos que es un objeto con 'description', interpretamos error
|
|
72
|
+
if (possibleError.description) {
|
|
73
|
+
// Ya teníamos datos locales?
|
|
74
|
+
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
|
+
// Dejamos loading en false, data queda como estaba
|
|
82
|
+
setData((prev) => ({ ...prev, loading: false }));
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
// No hay datos locales, error fatal
|
|
86
|
+
(0, index_js_1.showAlert)({
|
|
87
|
+
type: possibleError.type || "error",
|
|
88
|
+
title: possibleError.title || "Error",
|
|
89
|
+
description: possibleError.description,
|
|
90
|
+
});
|
|
91
|
+
setData({ data: {}, loading: false });
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
// Caso: es data real (ajusta la forma en que tu backend envía la info)
|
|
96
|
+
const newData = possibleData[TEMPLATE_NAME];
|
|
97
|
+
if (newData) {
|
|
98
|
+
// Guardamos en redux y en el state local
|
|
99
|
+
dispatch((0, redux_1.templateFetchSuccess)({
|
|
100
|
+
name: TEMPLATE_NAME,
|
|
101
|
+
data: newData,
|
|
102
|
+
}));
|
|
103
|
+
setData({ data: newData, loading: false });
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
// No llegó la propiedad esperada en la respuesta
|
|
107
|
+
if (hasLocalData) {
|
|
108
|
+
// No reportar error, usamos lo local
|
|
109
|
+
setData((prev) => ({ ...prev, loading: false }));
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
// No tenemos nada local -> error
|
|
113
|
+
(0, index_js_1.showAlert)({
|
|
114
|
+
type: "error",
|
|
115
|
+
title: `Error al recibir la plantilla: ${TEMPLATE_NAME}`,
|
|
116
|
+
description: `No se encontró "${TEMPLATE_NAME}" en la respuesta de la API.`,
|
|
117
|
+
});
|
|
118
|
+
setData((prev) => ({ ...prev, loading: false }));
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
// fetcher.state === "idle" pero fetcher.data = null/undefined
|
|
125
|
+
// probablemente un error global (red, servidor caído, etc.)
|
|
126
|
+
if (hasLocalData) {
|
|
127
|
+
// Fallback a datos locales
|
|
128
|
+
setData((prev) => ({ ...prev, loading: false }));
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
// Ni API ni local data => error
|
|
132
|
+
(0, index_js_1.showAlert)({
|
|
133
|
+
type: "error",
|
|
134
|
+
title: `Error al cargar la plantilla ${TEMPLATE_NAME}`,
|
|
135
|
+
description: `No se obtuvo respuesta y no hay datos locales.`,
|
|
136
|
+
});
|
|
137
|
+
setData({ data: {}, loading: false });
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
142
|
+
}, [fetcher.state]);
|
|
143
|
+
// Retornamos el estado final
|
|
57
144
|
return data;
|
|
58
145
|
}
|
|
59
|
-
catch (
|
|
60
|
-
|
|
146
|
+
catch (err) {
|
|
147
|
+
console.error(err);
|
|
148
|
+
return {
|
|
149
|
+
data: {},
|
|
150
|
+
loading: false,
|
|
151
|
+
};
|
|
61
152
|
}
|
|
62
|
-
}
|
|
153
|
+
}
|
|
63
154
|
const useGetReceptionTemplate = (config) => useGetTemplateObject("receptionTemplate", config);
|
|
64
155
|
exports.useGetReceptionTemplate = useGetReceptionTemplate;
|
package/dist/templates.d.ts
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
};
|
|
4
|
-
export declare const useGetReceptionTemplate: (config?: ConfigProps) => {
|
|
1
|
+
import { ReduxParamsConfig } from "@zauru-sdk/redux";
|
|
2
|
+
export declare const useGetReceptionTemplate: (config?: ReduxParamsConfig) => {
|
|
5
3
|
loading: boolean;
|
|
6
4
|
data: string;
|
|
7
5
|
};
|
|
8
|
-
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zauru-sdk/hooks",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.121",
|
|
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.121",
|
|
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": "1ab1f6514793de7ce7f19aa022f7642db78ac9ad"
|
|
37
37
|
}
|