mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 04:46:40 +07:00
removed redux completely
This commit is contained in:
parent
26e6d8d41b
commit
a4bb07e9cf
323 changed files with 2464 additions and 3348 deletions
40
src/hooks/auth/useRestoreRequestForm.ts
Normal file
40
src/hooks/auth/useRestoreRequestForm.ts
Normal file
|
@ -0,0 +1,40 @@
|
|||
import { Asserts, object, string } from 'yup';
|
||||
import { ERRORS } from '~/constants/errors';
|
||||
import { FormikConfig, useFormik } from 'formik';
|
||||
import { useCallback } from 'react';
|
||||
import { showErrorToast } from '~/utils/errors/showToast';
|
||||
import { getValidationErrors } from '~/utils/errors/getValidationErrors';
|
||||
|
||||
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: '' },
|
||||
});
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue