mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 12:56:41 +07:00
#34 added isLoading and error handling to useCommentFormFormik
This commit is contained in:
parent
8ae2dc02f0
commit
d7b4fbf535
2 changed files with 50 additions and 13 deletions
|
@ -2,6 +2,7 @@ import React, { FC, useState } from 'react';
|
||||||
import { CommentFormValues, useCommentFormFormik } from '~/utils/hooks/useCommentFormFormik';
|
import { CommentFormValues, useCommentFormFormik } from '~/utils/hooks/useCommentFormFormik';
|
||||||
import { FormikProvider } from 'formik';
|
import { FormikProvider } from 'formik';
|
||||||
import { LocalCommentFormTextarea } from '~/components/comment/LocalCommentFormTextarea';
|
import { LocalCommentFormTextarea } from '~/components/comment/LocalCommentFormTextarea';
|
||||||
|
import { Button } from '~/components/input/Button';
|
||||||
|
|
||||||
interface IProps {}
|
interface IProps {}
|
||||||
|
|
||||||
|
@ -13,12 +14,19 @@ const initialValues: CommentFormValues = {
|
||||||
|
|
||||||
const LocalCommentForm: FC<IProps> = () => {
|
const LocalCommentForm: FC<IProps> = () => {
|
||||||
const [textarea, setTextarea] = useState<HTMLTextAreaElement>();
|
const [textarea, setTextarea] = useState<HTMLTextAreaElement>();
|
||||||
const { formik, isLoading } = useCommentFormFormik(initialValues, console.log);
|
const { formik } = useCommentFormFormik(initialValues);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
<form onSubmit={formik.handleSubmit}>
|
||||||
<FormikProvider value={formik}>
|
<FormikProvider value={formik}>
|
||||||
<LocalCommentFormTextarea isLoading={isLoading} setRef={setTextarea} />
|
<LocalCommentFormTextarea isLoading={formik.isSubmitting} setRef={setTextarea} />
|
||||||
|
{formik.isSubmitting && <div>LOADING</div>}
|
||||||
|
{!!formik.status && <div>error: {formik.status}</div>}
|
||||||
|
<Button size="small" disabled={formik.isSubmitting}>
|
||||||
|
SEND
|
||||||
|
</Button>
|
||||||
</FormikProvider>
|
</FormikProvider>
|
||||||
|
</form>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { IFile } from '~/redux/types';
|
import { IComment, IFile } from '~/redux/types';
|
||||||
import { useRef, useState } from 'react';
|
import { useCallback, useRef } from 'react';
|
||||||
import { useFormik, useFormikContext } from 'formik';
|
import { FormikHelpers, useFormik, useFormikContext } from 'formik';
|
||||||
import { object, string, array } from 'yup';
|
import { array, object, string } from 'yup';
|
||||||
|
|
||||||
export interface CommentFormValues {
|
export interface CommentFormValues {
|
||||||
text: string;
|
text: string;
|
||||||
|
@ -15,21 +15,50 @@ const validationSchema = object().shape({
|
||||||
songs: array(),
|
songs: array(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const useCommentFormFormik = (
|
const submitComment = async (
|
||||||
|
id: IComment['id'],
|
||||||
values: CommentFormValues,
|
values: CommentFormValues,
|
||||||
onSubmit: (values: CommentFormValues) => void
|
callback: (e: string) => void
|
||||||
) => {
|
) => {
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 500));
|
||||||
|
callback('wrong');
|
||||||
|
};
|
||||||
|
|
||||||
|
export const useCommentFormFormik = (values: CommentFormValues) => {
|
||||||
const { current: initialValues } = useRef(values);
|
const { current: initialValues } = useRef(values);
|
||||||
|
|
||||||
|
const onSuccess = useCallback(
|
||||||
|
({ resetForm, setStatus, setSubmitting }: FormikHelpers<CommentFormValues>) => (e: string) => {
|
||||||
|
setSubmitting(false);
|
||||||
|
|
||||||
|
if (e) {
|
||||||
|
setStatus(e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (resetForm) {
|
||||||
|
resetForm();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[]
|
||||||
|
);
|
||||||
|
|
||||||
|
const onSubmit = useCallback(
|
||||||
|
(values: CommentFormValues, helpers: FormikHelpers<CommentFormValues>) => {
|
||||||
|
helpers.setSubmitting(true);
|
||||||
|
submitComment(0, values, onSuccess(helpers));
|
||||||
|
},
|
||||||
|
[values, onSuccess]
|
||||||
|
);
|
||||||
|
|
||||||
const formik = useFormik({
|
const formik = useFormik({
|
||||||
initialValues,
|
initialValues,
|
||||||
validationSchema,
|
validationSchema,
|
||||||
onSubmit,
|
onSubmit,
|
||||||
|
initialStatus: '',
|
||||||
});
|
});
|
||||||
|
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
return { formik };
|
||||||
|
|
||||||
return { formik, isLoading };
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useCommentFormContext = () => useFormikContext<CommentFormValues>();
|
export const useCommentFormContext = () => useFormikContext<CommentFormValues>();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue