mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-24 20:36:40 +07:00
fixed error handling on user form
This commit is contained in:
parent
216cec0bd1
commit
35fd30daab
4 changed files with 32 additions and 26 deletions
|
@ -16,9 +16,6 @@ import styles from './styles.module.scss';
|
||||||
|
|
||||||
interface UserSettingsViewProps {}
|
interface UserSettingsViewProps {}
|
||||||
|
|
||||||
const getError = (error?: string) =>
|
|
||||||
error && has(error, ERROR_LITERAL) ? error : undefined;
|
|
||||||
|
|
||||||
const UserSettingsView: FC<UserSettingsViewProps> = () => {
|
const UserSettingsView: FC<UserSettingsViewProps> = () => {
|
||||||
const { values, handleChange, errors } = useSettings();
|
const { values, handleChange, errors } = useSettings();
|
||||||
const { isPhone } = useWindowSize();
|
const { isPhone } = useWindowSize();
|
||||||
|
@ -41,7 +38,7 @@ const UserSettingsView: FC<UserSettingsViewProps> = () => {
|
||||||
value={values.fullname}
|
value={values.fullname}
|
||||||
handler={handleChange('fullname')}
|
handler={handleChange('fullname')}
|
||||||
title="Полное имя"
|
title="Полное имя"
|
||||||
error={getError(errors.fullname)}
|
error={errors.fullname}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<Textarea
|
<Textarea
|
||||||
|
@ -79,14 +76,14 @@ const UserSettingsView: FC<UserSettingsViewProps> = () => {
|
||||||
value={values.username}
|
value={values.username}
|
||||||
handler={handleChange('username')}
|
handler={handleChange('username')}
|
||||||
title="Логин"
|
title="Логин"
|
||||||
error={getError(errors.username)}
|
error={errors.username}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<InputText
|
<InputText
|
||||||
value={values.email}
|
value={values.email}
|
||||||
handler={handleChange('email')}
|
handler={handleChange('email')}
|
||||||
title="E-mail"
|
title="E-mail"
|
||||||
error={getError(errors.email)}
|
error={errors.email}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<InputText
|
<InputText
|
||||||
|
@ -94,7 +91,7 @@ const UserSettingsView: FC<UserSettingsViewProps> = () => {
|
||||||
handler={handleChange('newPassword')}
|
handler={handleChange('newPassword')}
|
||||||
title="Новый пароль"
|
title="Новый пароль"
|
||||||
type="password"
|
type="password"
|
||||||
error={getError(errors.newPassword)}
|
error={errors.newPassword}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<InputText
|
<InputText
|
||||||
|
@ -102,7 +99,7 @@ const UserSettingsView: FC<UserSettingsViewProps> = () => {
|
||||||
handler={handleChange('password')}
|
handler={handleChange('password')}
|
||||||
title="Старый пароль"
|
title="Старый пароль"
|
||||||
type="password"
|
type="password"
|
||||||
error={getError(errors.password)}
|
error={errors.password}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<div className={styles.small}>
|
<div className={styles.small}>
|
||||||
|
|
|
@ -9,7 +9,10 @@ import { showErrorToast } from '~/utils/errors/showToast';
|
||||||
|
|
||||||
export const usePatchUser = () => {
|
export const usePatchUser = () => {
|
||||||
const { update } = useUser();
|
const { update } = useUser();
|
||||||
const { uploadFile } = useUploader(UploadSubject.Avatar, UploadTarget.Profiles);
|
const { uploadFile } = useUploader(
|
||||||
|
UploadSubject.Avatar,
|
||||||
|
UploadTarget.Profiles,
|
||||||
|
);
|
||||||
|
|
||||||
const save = useCallback(
|
const save = useCallback(
|
||||||
async (user: Partial<ApiUpdateUserRequest['user']>) => {
|
async (user: Partial<ApiUpdateUserRequest['user']>) => {
|
||||||
|
@ -17,7 +20,7 @@ export const usePatchUser = () => {
|
||||||
await update(result.user);
|
await update(result.user);
|
||||||
return result.user;
|
return result.user;
|
||||||
},
|
},
|
||||||
[update]
|
[update],
|
||||||
);
|
);
|
||||||
|
|
||||||
const updatePhoto = useCallback(
|
const updatePhoto = useCallback(
|
||||||
|
@ -29,7 +32,7 @@ export const usePatchUser = () => {
|
||||||
showErrorToast(error);
|
showErrorToast(error);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[uploadFile, save]
|
[uploadFile, save],
|
||||||
);
|
);
|
||||||
|
|
||||||
return { updatePhoto, save };
|
return { updatePhoto, save };
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
import { hasPath, path } from '~/utils/ramda';
|
import { hasPath, path } from '~/utils/ramda';
|
||||||
|
|
||||||
export const getValidationErrors = (error: unknown): Record<string, string> | undefined => {
|
export const getValidationErrors = (
|
||||||
|
error: unknown,
|
||||||
|
): Record<string, string> | undefined => {
|
||||||
if (hasPath(['response', 'data', 'errors'], error)) {
|
if (hasPath(['response', 'data', 'errors'], error)) {
|
||||||
return path(['response', 'data', 'errors'], error);
|
return path(['response', 'data', 'errors'], error);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,11 @@
|
||||||
import { FC, PropsWithChildren, useCallback, useRef } from 'react';
|
import { FC, PropsWithChildren, useCallback, useRef } from 'react';
|
||||||
|
|
||||||
import { FormikConfig, useFormik, FormikProvider, useFormikContext } from 'formik';
|
import {
|
||||||
|
FormikConfig,
|
||||||
|
useFormik,
|
||||||
|
FormikProvider,
|
||||||
|
useFormikContext,
|
||||||
|
} from 'formik';
|
||||||
import { Asserts, object, string } from 'yup';
|
import { Asserts, object, string } from 'yup';
|
||||||
|
|
||||||
import { ERRORS } from '~/constants/errors';
|
import { ERRORS } from '~/constants/errors';
|
||||||
|
@ -13,15 +18,11 @@ import { showErrorToast } from '~/utils/errors/showToast';
|
||||||
import { showToastSuccess } from '~/utils/toast';
|
import { showToastSuccess } from '~/utils/toast';
|
||||||
|
|
||||||
const validationSchema = object({
|
const validationSchema = object({
|
||||||
username: string()
|
username: string().default('').required(ERRORS.REQUIRED),
|
||||||
.default('')
|
|
||||||
.required(ERRORS.REQUIRED),
|
|
||||||
fullname: string().default(''),
|
fullname: string().default(''),
|
||||||
newPassword: string().optional(),
|
newPassword: string().optional(),
|
||||||
description: string().default(''),
|
description: string().default(''),
|
||||||
email: string()
|
email: string().default('').email(ERRORS.NOT_AN_EMAIL),
|
||||||
.default('')
|
|
||||||
.email(ERRORS.NOT_AN_EMAIL),
|
|
||||||
password: string().optional(),
|
password: string().optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -29,7 +30,7 @@ export type ProfileFormData = Asserts<typeof validationSchema>;
|
||||||
|
|
||||||
export const useSettingsForm = (
|
export const useSettingsForm = (
|
||||||
values: ProfileFormData,
|
values: ProfileFormData,
|
||||||
submitter: (data: ProfileFormData) => Promise<IUser>
|
submitter: (data: ProfileFormData) => Promise<IUser>,
|
||||||
) => {
|
) => {
|
||||||
const initialValues = useRef(values).current;
|
const initialValues = useRef(values).current;
|
||||||
|
|
||||||
|
@ -39,7 +40,9 @@ export const useSettingsForm = (
|
||||||
const fields = {
|
const fields = {
|
||||||
...values,
|
...values,
|
||||||
password: values.password?.length ? values.password : undefined,
|
password: values.password?.length ? values.password : undefined,
|
||||||
new_password: values.newPassword?.length ? values.newPassword : undefined,
|
new_password: values.newPassword?.length
|
||||||
|
? values.newPassword
|
||||||
|
: undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
const result = await submitter(fields);
|
const result = await submitter(fields);
|
||||||
|
@ -51,11 +54,12 @@ export const useSettingsForm = (
|
||||||
|
|
||||||
const validationErrors = getValidationErrors(error);
|
const validationErrors = getValidationErrors(error);
|
||||||
if (validationErrors) {
|
if (validationErrors) {
|
||||||
|
console.log(validationErrors);
|
||||||
setErrors(validationErrors);
|
setErrors(validationErrors);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[submitter]
|
[submitter],
|
||||||
);
|
);
|
||||||
|
|
||||||
return useFormik({
|
return useFormik({
|
||||||
|
@ -70,11 +74,11 @@ export const SettingsProvider: FC<PropsWithChildren<{}>> = ({ children }) => {
|
||||||
const { save } = usePatchUser();
|
const { save } = usePatchUser();
|
||||||
|
|
||||||
const formik = useSettingsForm(
|
const formik = useSettingsForm(
|
||||||
{ ...user, password: '', newPassword: '' },
|
{ ...user, password: '', newPassword: '' },
|
||||||
save
|
save,
|
||||||
);
|
);
|
||||||
|
|
||||||
return <FormikProvider value={formik}>{children}</FormikProvider>
|
return <FormikProvider value={formik}>{children}</FormikProvider>;
|
||||||
}
|
};
|
||||||
|
|
||||||
export const useSettings = () => useFormikContext<ProfileFormData>();
|
export const useSettings = () => useFormikContext<ProfileFormData>();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue