@zauru-sdk/hooks 2.31.0 → 2.31.1

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.
@@ -15,8 +15,13 @@ type CatalogType<T> = {
15
15
  * otherParams: {
16
16
  includeDiscounts: "true",
17
17
  }
18
+ *
19
+ * staleWhileRevalidate: true — if Redux already has data, show it at once and
20
+ * refetch in the background; on success data is replaced silently, on error the
21
+ * cache is kept. When there is no cached data, behavior matches a normal load
22
+ * (loading true until the first response).
18
23
  */
19
- export declare function useGetReduxCatalog<T>(CATALOG_NAME: CATALOGS_NAMES, { online, wheres, otherParams }?: ReduxParamsConfig): CatalogType<T>;
24
+ export declare function useGetReduxCatalog<T>(CATALOG_NAME: CATALOGS_NAMES, { online, staleWhileRevalidate, wheres, otherParams, }?: ReduxParamsConfig): CatalogType<T>;
20
25
  /**
21
26
  *
22
27
  * ======================= HOOKS
@@ -17,8 +17,13 @@ const redux_1 = require("@zauru-sdk/redux");
17
17
  * otherParams: {
18
18
  includeDiscounts: "true",
19
19
  }
20
+ *
21
+ * staleWhileRevalidate: true — if Redux already has data, show it at once and
22
+ * refetch in the background; on success data is replaced silently, on error the
23
+ * cache is kept. When there is no cached data, behavior matches a normal load
24
+ * (loading true until the first response).
20
25
  */
21
- function useGetReduxCatalog(CATALOG_NAME, { online = false, wheres = [], otherParams } = {}) {
26
+ function useGetReduxCatalog(CATALOG_NAME, { online = false, staleWhileRevalidate = false, wheres = [], otherParams, } = {}) {
22
27
  const dispatch = (0, redux_1.useAppDispatch)();
23
28
  const fetcher = (0, react_1.useFetcher)();
24
29
  const [fetchTriggered, setFetchTriggered] = (0, react_2.useState)(false);
@@ -36,14 +41,28 @@ function useGetReduxCatalog(CATALOG_NAME, { online = false, wheres = [], otherPa
36
41
  * Lógica:
37
42
  * - Si `online: true`, forzamos el fetch.
38
43
  * - Si `reFetch: true`, también forzamos el fetch.
39
- * - Si `online: false` y tenemos data local, NO hacemos fetch (solo mostramos lo que hay).
40
- * - Si `online: false` y NO hay data local, hacemos fetch (porque no hay nada que mostrar).
44
+ * - Si `staleWhileRevalidate: true` y hay data en Redux, forzamos fetch en segundo plano
45
+ * sin poner `loading: true` (se muestra la caché hasta que llegue respuesta o falle).
46
+ * - Si `online: false` y SÍ tenemos data local y no aplica lo anterior, NO hacemos fetch.
47
+ * - Si no hay data local, SÍ hacemos fetch (porque no hay nada que mostrar).
41
48
  */
42
49
  (0, react_2.useEffect)(() => {
43
- const mustFetch = online || catalogData?.reFetch || !hasLocalData;
50
+ const revalidateInBackground = Boolean(staleWhileRevalidate) && hasLocalData;
51
+ const mustFetch = online ||
52
+ catalogData?.reFetch ||
53
+ !hasLocalData ||
54
+ revalidateInBackground;
44
55
  if (mustFetch) {
45
- setData((prev) => ({ ...prev, loading: true }));
46
- dispatch((0, redux_1.catalogsFetchStart)(CATALOG_NAME));
56
+ if (revalidateInBackground) {
57
+ setData({
58
+ data: catalogData?.data || [],
59
+ loading: false,
60
+ });
61
+ }
62
+ else {
63
+ setData((prev) => ({ ...prev, loading: true }));
64
+ dispatch((0, redux_1.catalogsFetchStart)(CATALOG_NAME));
65
+ }
47
66
  // Construimos el query para `wheres` y `otherParams`
48
67
  const encodedWheres = (wheres || []).map((where) => encodeURIComponent(where));
49
68
  const wheresQueryParam = encodedWheres.join("&");
@@ -93,7 +112,7 @@ function useGetReduxCatalog(CATALOG_NAME, { online = false, wheres = [], otherPa
93
112
  });
94
113
  }
95
114
  // eslint-disable-next-line react-hooks/exhaustive-deps
96
- }, [online, catalogData?.reFetch, hasLocalData]);
115
+ }, [online, staleWhileRevalidate, catalogData?.reFetch, hasLocalData]);
97
116
  /**
98
117
  * Efecto que observa la finalización del fetch (fetcher.state === "idle")
99
118
  * y decide qué hacer con la data o error recibidos.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zauru-sdk/hooks",
3
- "version": "2.31.0",
3
+ "version": "2.31.1",
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.30.0",
30
30
  "@zauru-sdk/icons": "^2.0.188",
31
- "@zauru-sdk/redux": "^2.31.0",
31
+ "@zauru-sdk/redux": "^2.31.1",
32
32
  "@zauru-sdk/types": "^2.30.0",
33
33
  "react": "^18.2.0",
34
34
  "react-dom": "^18.2.0"
35
35
  },
36
- "gitHead": "2f9ee02a1ef76fbf71637b8455e4840786489628"
36
+ "gitHead": "220d939967adc34a44c6cf0671f0234253bbfa45"
37
37
  }