From ea6750d5212a9e773e8c5a7142599f9d5fe9e31e Mon Sep 17 00:00:00 2001 From: Ivan <8611739+IRBorisov@users.noreply.github.com> Date: Thu, 30 Jan 2025 11:38:49 +0300 Subject: [PATCH] B: Fix change-password implementation --- .../src/backend/auth/useResetPassword.tsx | 14 ++---- .../frontend/src/pages/PasswordChangePage.tsx | 47 +++++++++---------- 2 files changed, 27 insertions(+), 34 deletions(-) diff --git a/rsconcept/frontend/src/backend/auth/useResetPassword.tsx b/rsconcept/frontend/src/backend/auth/useResetPassword.tsx index f7de68a9..fbb82e74 100644 --- a/rsconcept/frontend/src/backend/auth/useResetPassword.tsx +++ b/rsconcept/frontend/src/backend/auth/useResetPassword.tsx @@ -1,18 +1,15 @@ -import { useMutation, useQueryClient } from '@tanstack/react-query'; +import { useMutation } from '@tanstack/react-query'; import { authApi, IPasswordTokenDTO, IResetPasswordDTO } from './api'; export const useResetPassword = () => { - const client = useQueryClient(); const validateMutation = useMutation({ - mutationKey: ['reset-password'], - mutationFn: authApi.validatePasswordToken, - onSuccess: () => client.invalidateQueries({ queryKey: [authApi.baseKey] }) + mutationKey: ['validate-token'], + mutationFn: authApi.validatePasswordToken }); const resetMutation = useMutation({ mutationKey: ['reset-password'], - mutationFn: authApi.resetPassword, - onSuccess: () => client.invalidateQueries({ queryKey: [authApi.baseKey] }) + mutationFn: authApi.resetPassword }); return { validateToken: ( @@ -24,7 +21,6 @@ export const useResetPassword = () => { onSuccess?: () => void ) => resetMutation.mutate(data, { onSuccess }), isPending: resetMutation.isPending || validateMutation.isPending, - error: resetMutation.error, - reset: resetMutation.reset + error: resetMutation.error ?? validateMutation.error }; }; diff --git a/rsconcept/frontend/src/pages/PasswordChangePage.tsx b/rsconcept/frontend/src/pages/PasswordChangePage.tsx index aa7ee837..eee77731 100644 --- a/rsconcept/frontend/src/pages/PasswordChangePage.tsx +++ b/rsconcept/frontend/src/pages/PasswordChangePage.tsx @@ -6,9 +6,8 @@ import { useEffect, useState } from 'react'; import { useConceptNavigation } from '@/app/Navigation/NavigationContext'; import { urls } from '@/app/urls'; -import { IResetPasswordDTO } from '@/backend/auth/api'; import { useResetPassword } from '@/backend/auth/useResetPassword'; -import { ErrorData } from '@/components/info/InfoError'; +import InfoError, { ErrorData } from '@/components/info/InfoError'; import Loader from '@/components/ui/Loader'; import SubmitButton from '@/components/ui/SubmitButton'; import TextInput from '@/components/ui/TextInput'; @@ -16,11 +15,11 @@ import useQueryStrings from '@/hooks/useQueryStrings'; export function Component() { const router = useConceptNavigation(); - const token = useQueryStrings().get('token'); + const token = useQueryStrings().get('token') ?? ''; - const { validateToken, resetPassword, isPending, error, reset } = useResetPassword(); + const { validateToken, resetPassword, isPending, error } = useResetPassword(); - const [isTokenValid, setIsTokenValid] = useState(false); + const [isValidated, setIsValidated] = useState(false); const [newPassword, setNewPassword] = useState(''); const [newPasswordRepeat, setNewPasswordRepeat] = useState(''); @@ -32,30 +31,27 @@ export function Component() { function handleSubmit(event: React.FormEvent) { event.preventDefault(); if (!isPending) { - const data: IResetPasswordDTO = { - password: newPassword, - token: token! - }; - resetPassword(data, () => { - router.replace(urls.home); - router.push(urls.login); - }); + resetPassword( + { + password: newPassword, + token: token + }, + () => { + router.replace(urls.home); + router.push(urls.login); + } + ); } } useEffect(() => { - reset(); - }, [newPassword, newPasswordRepeat, reset]); + if (!isValidated && !isPending) { + validateToken({ token: token }); + setIsValidated(true); + } + }, [token, validateToken, isValidated, isPending]); - useEffect(() => { - validateToken({ token: token ?? '' }, () => setIsTokenValid(true)); - }, [token, validateToken]); - - if (error) { - return ; - } - - if (isPending || !isTokenValid) { + if (isPending) { return ; } @@ -92,6 +88,7 @@ export function Component() { loading={isPending} disabled={!canSubmit} /> + {error ? : null} ); } @@ -101,5 +98,5 @@ function ProcessError({ error }: { error: ErrorData }): React.ReactElement { if (axios.isAxiosError(error) && error.response && error.response.status === 404) { return
Данная ссылка не действительна
; } - throw error as Error; + return ; }