@zauru-sdk/hooks 1.0.13

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.
@@ -0,0 +1,51 @@
1
+ import { useFetcher } from "@remix-run/react";
2
+ import { setSessionValue, useAppDispatch, useAppSelector, } from "@zauru-sdk/redux";
3
+ import { useEffect, useState } from "react";
4
+ import { showAlert } from "src";
5
+ /**
6
+ *
7
+ * @param attribute
8
+ * @returns
9
+ */
10
+ export const useGetSessionAttribute = (name, type) => {
11
+ const fetcher = useFetcher();
12
+ const dispatch = useAppDispatch();
13
+ const sessionData = useAppSelector((state) => state.session[name]);
14
+ const [data, setData] = useState(sessionData);
15
+ useEffect(() => {
16
+ if (fetcher.data?.title) {
17
+ showAlert({
18
+ description: fetcher.data?.description,
19
+ title: fetcher.data?.title,
20
+ type: fetcher.data?.type,
21
+ });
22
+ }
23
+ }, [fetcher.data]);
24
+ useEffect(() => {
25
+ if (fetcher.state === "idle" && fetcher.data != null) {
26
+ const receivedData = fetcher.data;
27
+ if (receivedData) {
28
+ setData(receivedData.data);
29
+ dispatch(setSessionValue({
30
+ name: name,
31
+ data: receivedData.data,
32
+ }));
33
+ }
34
+ }
35
+ }, [fetcher, dispatch, name]);
36
+ useEffect(() => {
37
+ if (!sessionData) {
38
+ try {
39
+ fetcher.load(`/api/session?name=${name}&type=${type}`);
40
+ }
41
+ catch (ex) {
42
+ showAlert({
43
+ type: "error",
44
+ title: `Ocurrió un error al cargar la variable de configuración: ${name}.`,
45
+ description: "Error: " + ex,
46
+ });
47
+ }
48
+ }
49
+ }, []);
50
+ return data;
51
+ };
@@ -0,0 +1,8 @@
1
+ type ConfigProps = {
2
+ online?: boolean;
3
+ };
4
+ export declare const useGetReceptionTemplate: (config?: ConfigProps) => {
5
+ loading: boolean;
6
+ data: string;
7
+ };
8
+ export {};
@@ -0,0 +1,54 @@
1
+ import { useFetcher } from "@remix-run/react";
2
+ import { templateFetchStart, templateFetchSuccess, useAppDispatch, useAppSelector, } from "@zauru-sdk/redux";
3
+ import { useEffect, useState } from "react";
4
+ import { showAlert } from "src";
5
+ const useGetTemplateObject = (TEMPLATE_NAME, config = { online: false }) => {
6
+ const fetcher = useFetcher();
7
+ const dispatch = useAppDispatch();
8
+ const objectData = useAppSelector((state) => state.templates[TEMPLATE_NAME]);
9
+ const [data, setData] = useState({
10
+ data: Object.keys(objectData?.data).length
11
+ ? objectData?.data
12
+ : {},
13
+ loading: objectData.loading,
14
+ });
15
+ useEffect(() => {
16
+ if (fetcher.data?.title) {
17
+ showAlert({
18
+ description: fetcher.data?.description,
19
+ title: fetcher.data?.title,
20
+ type: fetcher.data?.type,
21
+ });
22
+ }
23
+ }, [fetcher.data]);
24
+ useEffect(() => {
25
+ if (fetcher.state === "idle" && fetcher.data != null) {
26
+ const receivedData = fetcher.data;
27
+ if (receivedData) {
28
+ setData({ data: receivedData[TEMPLATE_NAME], loading: false });
29
+ dispatch(templateFetchSuccess({
30
+ name: TEMPLATE_NAME,
31
+ data: receivedData[TEMPLATE_NAME],
32
+ }));
33
+ }
34
+ }
35
+ }, [fetcher, dispatch, TEMPLATE_NAME]);
36
+ useEffect(() => {
37
+ if (Object.keys(objectData?.data).length <= 0 || config?.online) {
38
+ try {
39
+ setData({ ...data, loading: true });
40
+ dispatch(templateFetchStart(TEMPLATE_NAME));
41
+ fetcher.load(`/api/templates?object=${TEMPLATE_NAME}`);
42
+ }
43
+ catch (ex) {
44
+ showAlert({
45
+ type: "error",
46
+ title: `Ocurrió un error al cargar el object de templates: ${TEMPLATE_NAME}.`,
47
+ description: "Error: " + ex,
48
+ });
49
+ }
50
+ }
51
+ }, []);
52
+ return data;
53
+ };
54
+ export const useGetReceptionTemplate = (config) => useGetTemplateObject("receptionTemplate", config);
@@ -0,0 +1 @@
1
+ export default function useWindowDimensions(): number;
@@ -0,0 +1,27 @@
1
+ import { useState, useEffect } from "react";
2
+ function getWindowDimensions() {
3
+ if (typeof window !== "undefined") {
4
+ const { innerWidth: width } = window;
5
+ return width;
6
+ }
7
+ // Devolver un valor predeterminado si window no está definido
8
+ return 1000;
9
+ }
10
+ export default function useWindowDimensions() {
11
+ const [windowDimensions, setWindowDimensions] = useState(getWindowDimensions);
12
+ useEffect(() => {
13
+ if (typeof window !== "undefined") {
14
+ const handleResize = () => {
15
+ setWindowDimensions(getWindowDimensions());
16
+ };
17
+ // Use window load event to ensure accurate window size on initial load
18
+ window.addEventListener("load", handleResize);
19
+ window.addEventListener("resize", handleResize);
20
+ return () => {
21
+ window.removeEventListener("load", handleResize);
22
+ window.removeEventListener("resize", handleResize);
23
+ };
24
+ }
25
+ }, []);
26
+ return windowDimensions;
27
+ }
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@zauru-sdk/hooks",
3
+ "version": "1.0.13",
4
+ "description": "Hooks reutilizables dentro de las webapps de Zauru.",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "type": "module",
8
+ "publishConfig": {
9
+ "access": "public"
10
+ },
11
+ "scripts": {
12
+ "build": "tsc",
13
+ "test": "echo \"Error: no test specified\" && exit 1"
14
+ },
15
+ "author": "Oscar Cuéllar",
16
+ "license": "MIT",
17
+ "devDependencies": {
18
+ "@types/react": "^18.2.20",
19
+ "@types/react-dom": "^18.2.7",
20
+ "eslint": "^8.38.0",
21
+ "eslint-config-prettier": "^9.0.0",
22
+ "eslint-import-resolver-typescript": "^3.6.1",
23
+ "eslint-plugin-import": "^2.28.1",
24
+ "eslint-plugin-jsx-a11y": "^6.7.1",
25
+ "eslint-plugin-react": "^7.33.2",
26
+ "eslint-plugin-react-hooks": "^4.6.0",
27
+ "tailwindcss": "^3.4.1",
28
+ "typescript": "^5.1.6"
29
+ },
30
+ "dependencies": {
31
+ "@remix-run/react": "^2.8.1",
32
+ "@zauru-sdk/common": "^1.0.13",
33
+ "@zauru-sdk/graphql": "^1.0.9",
34
+ "@zauru-sdk/icons": "^1.0.13",
35
+ "@zauru-sdk/redux": "^1.0.13",
36
+ "@zauru-sdk/services": "^1.0.13",
37
+ "@zauru-sdk/types": "^1.0.13",
38
+ "@zauru-sdk/utils": "^1.0.13",
39
+ "react": "^18.2.0",
40
+ "react-dom": "^18.2.0"
41
+ },
42
+ "engines": {
43
+ "node": ">=18.0.0"
44
+ },
45
+ "gitHead": "9bca61fddd7c538e7f95a489d59436b5557c61cc"
46
+ }
package/src/alerts.ts ADDED
@@ -0,0 +1,43 @@
1
+ import type { FetcherWithComponents } from "@remix-run/react";
2
+ import { useEffect } from "react";
3
+ import { showAlert } from "src";
4
+
5
+ export const useValidateNotifications = (source: {
6
+ fetcher?: FetcherWithComponents<any>;
7
+ actionData?: any;
8
+ loaderData?: any;
9
+ }) => {
10
+ const { actionData, fetcher, loaderData } = source;
11
+
12
+ useEffect(() => {
13
+ if (loaderData?.title) {
14
+ showAlert({
15
+ description: loaderData?.description?.toString() ?? "",
16
+ title: loaderData?.title,
17
+ type: loaderData?.type as any,
18
+ });
19
+ }
20
+ }, [loaderData]);
21
+
22
+ useEffect(() => {
23
+ if (fetcher?.data?.title) {
24
+ showAlert({
25
+ description: fetcher.data?.description,
26
+ title: fetcher.data?.title,
27
+ type: fetcher.data?.type,
28
+ });
29
+ }
30
+ }, [fetcher?.data]);
31
+
32
+ useEffect(() => {
33
+ if (actionData?.title) {
34
+ showAlert({
35
+ description: actionData?.description ?? "",
36
+ title: actionData?.title,
37
+ type: actionData?.type as any,
38
+ });
39
+ }
40
+ }, [actionData]);
41
+
42
+ return null;
43
+ };
@@ -0,0 +1,74 @@
1
+ import { useFetcher } from "@remix-run/react";
2
+ import {
3
+ AUTOMATIC_NUMBER_NAMES,
4
+ automaticNumberFetchStart,
5
+ automaticNumberFetchSuccess,
6
+ useAppDispatch,
7
+ useAppSelector,
8
+ } from "@zauru-sdk/redux";
9
+ import { useEffect, useState } from "react";
10
+ import { showAlert } from "src";
11
+
12
+ type ProfileType<T> = {
13
+ data: T;
14
+ loading: boolean;
15
+ };
16
+
17
+ export const useGetAutomaticNumber = <T>(
18
+ AUTOMATIC_NUMBER_NAME: AUTOMATIC_NUMBER_NAMES
19
+ ): ProfileType<T> => {
20
+ const fetcher = useFetcher<any>();
21
+ const dispatch = useAppDispatch();
22
+ const objectData = useAppSelector(
23
+ (state) => state.automaticNumbers[AUTOMATIC_NUMBER_NAME]
24
+ );
25
+ const [data, setData] = useState<ProfileType<T>>({
26
+ data: Object.keys(objectData?.data).length
27
+ ? (objectData?.data as T)
28
+ : ({} as T),
29
+ loading: objectData.loading,
30
+ });
31
+
32
+ useEffect(() => {
33
+ if (fetcher.data?.title) {
34
+ showAlert({
35
+ description: fetcher.data?.description,
36
+ title: fetcher.data?.title,
37
+ type: fetcher.data?.type,
38
+ });
39
+ }
40
+ }, [fetcher.data]);
41
+
42
+ useEffect(() => {
43
+ if (fetcher.state === "idle" && fetcher.data != null) {
44
+ const receivedData = fetcher.data as { [key: string]: T };
45
+ if (receivedData) {
46
+ setData({ data: receivedData[AUTOMATIC_NUMBER_NAME], loading: false });
47
+ dispatch(
48
+ automaticNumberFetchSuccess({
49
+ name: AUTOMATIC_NUMBER_NAME,
50
+ data: receivedData[AUTOMATIC_NUMBER_NAME],
51
+ })
52
+ );
53
+ }
54
+ }
55
+ }, [fetcher, dispatch, AUTOMATIC_NUMBER_NAME]);
56
+
57
+ useEffect(() => {
58
+ if (Object.keys(objectData?.data).length <= 0) {
59
+ try {
60
+ setData({ ...data, loading: true });
61
+ dispatch(automaticNumberFetchStart(AUTOMATIC_NUMBER_NAME));
62
+ fetcher.load(`/api/automaticNumbers?object=${AUTOMATIC_NUMBER_NAME}`);
63
+ } catch (ex) {
64
+ showAlert({
65
+ type: "error",
66
+ title: `Ocurrió un error al cargar el object de automatic numbers: ${AUTOMATIC_NUMBER_NAME}.`,
67
+ description: "Error: " + ex,
68
+ });
69
+ }
70
+ }
71
+ }, []);
72
+
73
+ return data;
74
+ };