1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-26 05:16:41 +07:00

removed redux completely

This commit is contained in:
Fedor Katurov 2022-01-09 19:03:01 +07:00
parent 26e6d8d41b
commit a4bb07e9cf
323 changed files with 2464 additions and 3348 deletions

View file

@ -0,0 +1,48 @@
import { Asserts, object, string } from 'yup';
import { ERRORS } from '~/constants/errors';
import { useCallback } from 'react';
import { FormikConfig, useFormik } from 'formik';
import { IUser } from '~/types/auth';
import { showToastSuccess } from '~/utils/toast';
import { getRandomPhrase } from '~/constants/phrases';
import { showErrorToast } from '~/utils/errors/showToast';
import { getValidationErrors } from '~/utils/errors/getValidationErrors';
const validationSchema = object({
username: string().required(ERRORS.REQUIRED),
password: string().required(ERRORS.REQUIRED),
});
export type LoginFormData = Asserts<typeof validationSchema>;
export const useLoginForm = (
fetcher: (username: string, password: string) => Promise<IUser>,
onSuccess: () => void
) => {
const onSubmit = useCallback<FormikConfig<LoginFormData>['onSubmit']>(
async (values, { setErrors }) => {
try {
await fetcher(values.username, values.password);
onSuccess();
showToastSuccess(getRandomPhrase('WELCOME'));
} catch (error) {
showErrorToast(error);
const validationErrors = getValidationErrors(error);
if (validationErrors) {
setErrors(validationErrors);
}
}
},
[fetcher, onSuccess]
);
return useFormik({
validationSchema,
onSubmit,
initialValues: {
username: '',
password: '',
},
});
};