From 406da1b5f09facf7bbfadbc0a0e37bc1fceb03c0 Mon Sep 17 00:00:00 2001 From: lemmyMwaura Date: Wed, 7 Aug 2024 08:27:17 +0300 Subject: [PATCH] refactor: convert usequery hook to normal fetch fn (no need for caching) --- .../src/hooks/api/secretSharing/queries.ts | 37 +++++++------------ .../components/PasswordContainer.tsx | 15 ++++---- 2 files changed, 20 insertions(+), 32 deletions(-) diff --git a/frontend/src/hooks/api/secretSharing/queries.ts b/frontend/src/hooks/api/secretSharing/queries.ts index 02aa20f4a..de9556a09 100644 --- a/frontend/src/hooks/api/secretSharing/queries.ts +++ b/frontend/src/hooks/api/secretSharing/queries.ts @@ -68,29 +68,18 @@ export const useGetActiveSharedSecretById = ({ }); }; -export const useValidateSecretPassword = ({ - sharedSecretId, - hashedHex, - userPassword, -}: { - sharedSecretId: string; - hashedHex: string; - userPassword: string; -}) => { - return useQuery( - [`validateSecretPass-${sharedSecretId}`], - async () => { - const { data } = await apiRequest.post( - `/api/v1/secret-sharing/public/${sharedSecretId}/validate`, - { - hashedHex, - password: userPassword - } - ); - return data; - }, +export const fetchIsSecretPasswordValid = async ( + sharedSecretId: string, + hashedHex: string, + password: string, +) => { + const { data } = await apiRequest.post( + `/api/v1/secret-sharing/public/${sharedSecretId}/validate`, { - enabled: Boolean(sharedSecretId) && Boolean(userPassword), + hashedHex, + password } - ) -} + ); + + return data; +}; diff --git a/frontend/src/views/ViewSecretPublicPage/components/PasswordContainer.tsx b/frontend/src/views/ViewSecretPublicPage/components/PasswordContainer.tsx index c433bf1fe..d3316416e 100644 --- a/frontend/src/views/ViewSecretPublicPage/components/PasswordContainer.tsx +++ b/frontend/src/views/ViewSecretPublicPage/components/PasswordContainer.tsx @@ -4,7 +4,7 @@ import { faArrowRight, faSpinner } from "@fortawesome/free-solid-svg-icons"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { Button, IconButton, Input } from "@app/components/v2"; -import { useValidateSecretPassword } from "@app/hooks/api/secretSharing"; +import { fetchIsSecretPasswordValid } from "@app/hooks/api/secretSharing"; import { createNotification } from "@app/components/notifications"; type Props = { @@ -16,11 +16,6 @@ type Props = { export const PasswordContainer = ({ secretId, hashedHex, handlePassMatch }: Props) => { const [password, setPassword] = useState('') const [isLoading, setLoading] = useState(false) - const { refetch } = useValidateSecretPassword({ - sharedSecretId: secretId, - hashedHex, - userPassword: password, - }) const handleChange = (e: ChangeEvent) => { setPassword(e.target.value) @@ -30,9 +25,13 @@ export const PasswordContainer = ({ secretId, hashedHex, handlePassMatch }: Prop setLoading(true); try { - const { data: freshData } = await refetch(); + const data = await fetchIsSecretPasswordValid( + secretId, + hashedHex, + password, + ) - if (freshData?.isValid === true) { + if (data?.isValid === true) { handlePassMatch(true); } else { createNotification({