mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-24 20:36:40 +07:00
42 lines
1.1 KiB
TypeScript
42 lines
1.1 KiB
TypeScript
import { useCallback } from 'react';
|
|
|
|
import { FormikConfig, useFormik } from 'formik';
|
|
import { Asserts, object, string } from 'yup';
|
|
|
|
import { ERRORS } from '~/constants/errors';
|
|
import { getValidationErrors } from '~/utils/errors/getValidationErrors';
|
|
import { showErrorToast } from '~/utils/errors/showToast';
|
|
|
|
const validationSchema = object({
|
|
field: string().required(ERRORS.REQUIRED),
|
|
});
|
|
|
|
type RestoreRequestData = Asserts<typeof validationSchema>;
|
|
|
|
export const useRestoreRequestForm = (
|
|
fetcher: (field: string) => Promise<unknown>,
|
|
onSuccess: () => void,
|
|
) => {
|
|
const onSubmit = useCallback<FormikConfig<RestoreRequestData>['onSubmit']>(
|
|
async (values, { setErrors }) => {
|
|
try {
|
|
await fetcher(values.field);
|
|
onSuccess();
|
|
} catch (error) {
|
|
showErrorToast(error);
|
|
|
|
const validationErrors = getValidationErrors(error);
|
|
if (validationErrors) {
|
|
setErrors(validationErrors);
|
|
}
|
|
}
|
|
},
|
|
[fetcher, onSuccess],
|
|
);
|
|
|
|
return useFormik({
|
|
onSubmit,
|
|
validationSchema,
|
|
initialValues: { field: '' },
|
|
});
|
|
};
|