From 5fbae765afb2ffde913743f4b81c049d0fe0e653 Mon Sep 17 00:00:00 2001 From: Michal Masrna Date: Sun, 12 Nov 2023 00:07:32 +0100 Subject: [PATCH 1/2] Require old password on password change --- src/components/Profile/PasswordChangeForm.tsx | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/components/Profile/PasswordChangeForm.tsx b/src/components/Profile/PasswordChangeForm.tsx index ab61904a..74daf837 100644 --- a/src/components/Profile/PasswordChangeForm.tsx +++ b/src/components/Profile/PasswordChangeForm.tsx @@ -1,5 +1,5 @@ import {useMutation} from '@tanstack/react-query' -import axios from 'axios' +import axios, {AxiosError} from 'axios' import {FC} from 'react' import {SubmitHandler, useForm} from 'react-hook-form' @@ -11,11 +11,13 @@ import {Dialog} from '../Dialog/Dialog' import {FormInput} from '../FormItems/FormInput/FormInput' type PasswordChangeDialogValues = { + old_password: string new_password1: string new_password2: string } const defaultValues: PasswordChangeDialogValues = { + old_password: '', new_password1: '', new_password2: '', } @@ -25,8 +27,12 @@ interface PasswordChangeDialogProps { close: () => void } +interface ChangePasswordErrorResponseData { + old_password?: string[] +} + export const PasswordChangeDialog: FC = ({open, close}) => { - const {handleSubmit, reset, control, getValues} = useForm({defaultValues}) + const {handleSubmit, reset, control, getValues, setError} = useForm({defaultValues}) const onSuccess = () => { alert('Zmena hesla prebehla úspešne.') @@ -34,11 +40,20 @@ export const PasswordChangeDialog: FC = ({open, close close() } + const onError = (error: AxiosError) => { + if (error.response?.status === 400) { + if (error.response.data.old_password) { + setError('old_password', {type: 'manual', message: error.response.data.old_password[0]}) // TODO: We might want to simplify/reword this message + } + } + } + const {mutate: submitFormData} = useMutation({ mutationFn: (data: PasswordChangeDialogValues) => { return axios.post(`/api/user/password/change`, data) }, onSuccess: onSuccess, + onError: onError, }) const onSubmit: SubmitHandler = async (data) => { @@ -54,6 +69,13 @@ export const PasswordChangeDialog: FC = ({open, close title="Zmena hesla" contentText={
+ Date: Sun, 12 Nov 2023 01:07:28 +0100 Subject: [PATCH 2/2] Added at the beginning of error message star for consistency --- src/components/Profile/PasswordChangeForm.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/Profile/PasswordChangeForm.tsx b/src/components/Profile/PasswordChangeForm.tsx index 74daf837..522974c1 100644 --- a/src/components/Profile/PasswordChangeForm.tsx +++ b/src/components/Profile/PasswordChangeForm.tsx @@ -43,7 +43,7 @@ export const PasswordChangeDialog: FC = ({open, close const onError = (error: AxiosError) => { if (error.response?.status === 400) { if (error.response.data.old_password) { - setError('old_password', {type: 'manual', message: error.response.data.old_password[0]}) // TODO: We might want to simplify/reword this message + setError('old_password', {type: 'manual', message: `* ${error.response.data.old_password[0]}`}) // TODO: We might want to simplify/reword this message } } }