@zauru-sdk/hooks 2.0.120 → 2.0.122

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.
@@ -64,17 +64,22 @@ function useGetReduxCatalog(CATALOG_NAME, { online = false, wheres = [], otherPa
64
64
  fetcher.load(`/api/catalogs?catalog=${CATALOG_NAME}${queryString}`);
65
65
  }
66
66
  catch (error) {
67
- console.error(error);
68
67
  // Si hay datos locales, mostramos los datos locales
69
- setData({
70
- data: catalogData?.data || [],
71
- loading: false,
72
- });
73
- (0, index_js_1.showAlert)({
74
- type: "error",
75
- title: "Error al cargar el catálogo",
76
- description: `Error al cargar el catálogo ${CATALOG_NAME}, por favor intente nuevamente. Error: ${error}`,
77
- });
68
+ if (hasLocalData) {
69
+ console.error("Hubo un error pero hay datos locales en la consulta de", CATALOG_NAME, " Error: ", error);
70
+ setData({
71
+ data: catalogData?.data || [],
72
+ loading: false,
73
+ });
74
+ }
75
+ else {
76
+ console.error("Hubo un error y no hay datos locales en la consulta de", CATALOG_NAME, " Error: ", error);
77
+ (0, index_js_1.showAlert)({
78
+ type: "error",
79
+ title: "Error al cargar el catálogo",
80
+ description: `Error al cargar el catálogo ${CATALOG_NAME}, por favor intente nuevamente. Error: ${error}`,
81
+ });
82
+ }
78
83
  }
79
84
  }
80
85
  else {
@@ -98,30 +103,22 @@ function useGetReduxCatalog(CATALOG_NAME, { online = false, wheres = [], otherPa
98
103
  if (fetcher.data) {
99
104
  const possibleError = fetcher.data;
100
105
  const possibleData = fetcher.data;
101
- // Si es un error "clásico" con description, lo manejamos
102
- if (possibleError?.description) {
103
- // Aquí interpretamos que la API pudo haber respondido algo tipo { description, ... } => error
106
+ // Si es un error "clásico", lo manejamos
107
+ if (possibleError?.error) {
104
108
  // Pero OJO: si ya teníamos datos locales, no mostramos error "fatal" para no romper el fallback
105
109
  if (hasLocalData) {
106
- // Ya tenemos datos en Redux, así que sólo mostramos el alert informativo,
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 }));
110
+ console.log("Hay error en la respuesta pero hay datos locales en la consulta de", CATALOG_NAME);
115
111
  }
116
112
  else {
113
+ console.log("Hay error en la respuesta y no hay datos locales en la consulta de", CATALOG_NAME);
117
114
  // No hay datos locales -> mostramos error y no tenemos fallback
118
115
  (0, index_js_1.showAlert)({
119
116
  type: "error",
120
117
  title: possibleError.title || "Error",
121
118
  description: possibleError.description,
122
119
  });
123
- setData((prev) => ({ ...prev, loading: false }));
124
120
  }
121
+ setData((prev) => ({ ...prev, loading: false }));
125
122
  }
126
123
  else {
127
124
  // Caso: la respuesta sí es data real
@@ -141,6 +138,7 @@ function useGetReduxCatalog(CATALOG_NAME, { online = false, wheres = [], otherPa
141
138
  // Por alguna razón no llegó el array esperado
142
139
  // Revisamos si hay fallback local
143
140
  if (hasLocalData) {
141
+ console.log("Hubo un error en el parseo de la respuesta pero hay datos locales en la consulta de", CATALOG_NAME, " retornó: ", newData);
144
142
  // No reportar error: usamos lo local
145
143
  setData((prev) => ({ ...prev, loading: false }));
146
144
  }
@@ -151,6 +149,7 @@ function useGetReduxCatalog(CATALOG_NAME, { online = false, wheres = [], otherPa
151
149
  title: "Error al recibir el catálogo",
152
150
  description: `No se obtuvo la propiedad "${CATALOG_NAME}" en la respuesta.`,
153
151
  });
152
+ console.log("Hubo un error en parseo de la respuesta y no hay datos locales en la consulta de", CATALOG_NAME, " retornó: ", newData);
154
153
  setData((prev) => ({ ...prev, loading: false }));
155
154
  }
156
155
  }
@@ -160,17 +159,18 @@ function useGetReduxCatalog(CATALOG_NAME, { online = false, wheres = [], otherPa
160
159
  // fetcher.state === "idle" pero fetcher.data es null/undefined => seguramente hubo error global
161
160
  if (hasLocalData) {
162
161
  // Fallback a datos locales
163
- setData((prev) => ({ ...prev, loading: false }));
162
+ console.log("No hay datos en la respuesta pero hay datos locales en la consulta de", CATALOG_NAME);
164
163
  }
165
164
  else {
166
165
  // Ni API ni local data => error
166
+ console.log("No hay datos en la respuesta y no hay datos locales en la consulta de", CATALOG_NAME);
167
167
  (0, index_js_1.showAlert)({
168
168
  type: "error",
169
169
  title: "Error al cargar el catálogo",
170
170
  description: `No se obtuvo respuesta y no hay datos locales para ${CATALOG_NAME}`,
171
171
  });
172
- setData((prev) => ({ ...prev, loading: false }));
173
172
  }
173
+ setData((prev) => ({ ...prev, loading: false }));
174
174
  }
175
175
  }
176
176
  // eslint-disable-next-line react-hooks/exhaustive-deps
@@ -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
- const useGetTemplateObject = (TEMPLATE_NAME, config = { online: false }) => {
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 && Object.keys(objectData?.data).length
15
- ? objectData?.data
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
- if (fetcher.data?.title) {
21
- (0, index_js_1.showAlert)({
22
- description: fetcher.data?.description,
23
- title: fetcher.data?.title,
24
- type: fetcher.data?.type,
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 (ex) {
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: `Ocurrió un error al cargar el object de templates: ${TEMPLATE_NAME}.`,
52
- description: "Error: " + ex,
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 (ex) {
60
- return { data: {}, loading: false };
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;
@@ -1,8 +1,5 @@
1
- type ConfigProps = {
2
- online?: boolean;
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.120",
3
+ "version": "2.0.122",
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.120",
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": "05212e4cb104943ac0c6050ada75859622537c6a"
36
+ "gitHead": "3fec09bd29ced6f895b20e729d358448e3883f79"
37
37
  }