From d7b4fbf535b2e5a71d84a10d8320dd2220afe9a5 Mon Sep 17 00:00:00 2001 From: Fedor Katurov Date: Fri, 26 Feb 2021 17:22:26 +0700 Subject: [PATCH] #34 added isLoading and error handling to useCommentFormFormik --- .../comment/LocalCommentForm/index.tsx | 16 +++++-- src/utils/hooks/useCommentFormFormik.ts | 47 +++++++++++++++---- 2 files changed, 50 insertions(+), 13 deletions(-) diff --git a/src/components/comment/LocalCommentForm/index.tsx b/src/components/comment/LocalCommentForm/index.tsx index f4fb7b2c..a2bb90db 100644 --- a/src/components/comment/LocalCommentForm/index.tsx +++ b/src/components/comment/LocalCommentForm/index.tsx @@ -2,6 +2,7 @@ import React, { FC, useState } from 'react'; import { CommentFormValues, useCommentFormFormik } from '~/utils/hooks/useCommentFormFormik'; import { FormikProvider } from 'formik'; import { LocalCommentFormTextarea } from '~/components/comment/LocalCommentFormTextarea'; +import { Button } from '~/components/input/Button'; interface IProps {} @@ -13,12 +14,19 @@ const initialValues: CommentFormValues = { const LocalCommentForm: FC = () => { const [textarea, setTextarea] = useState(); - const { formik, isLoading } = useCommentFormFormik(initialValues, console.log); + const { formik } = useCommentFormFormik(initialValues); return ( - - - +
+ + + {formik.isSubmitting &&
LOADING
} + {!!formik.status &&
error: {formik.status}
} + +
+
); }; diff --git a/src/utils/hooks/useCommentFormFormik.ts b/src/utils/hooks/useCommentFormFormik.ts index 98c1b3b6..9cc1302a 100644 --- a/src/utils/hooks/useCommentFormFormik.ts +++ b/src/utils/hooks/useCommentFormFormik.ts @@ -1,7 +1,7 @@ -import { IFile } from '~/redux/types'; -import { useRef, useState } from 'react'; -import { useFormik, useFormikContext } from 'formik'; -import { object, string, array } from 'yup'; +import { IComment, IFile } from '~/redux/types'; +import { useCallback, useRef } from 'react'; +import { FormikHelpers, useFormik, useFormikContext } from 'formik'; +import { array, object, string } from 'yup'; export interface CommentFormValues { text: string; @@ -15,21 +15,50 @@ const validationSchema = object().shape({ songs: array(), }); -export const useCommentFormFormik = ( +const submitComment = async ( + id: IComment['id'], 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 onSuccess = useCallback( + ({ resetForm, setStatus, setSubmitting }: FormikHelpers) => (e: string) => { + setSubmitting(false); + + if (e) { + setStatus(e); + return; + } + + if (resetForm) { + resetForm(); + } + }, + [] + ); + + const onSubmit = useCallback( + (values: CommentFormValues, helpers: FormikHelpers) => { + helpers.setSubmitting(true); + submitComment(0, values, onSuccess(helpers)); + }, + [values, onSuccess] + ); + const formik = useFormik({ initialValues, validationSchema, onSubmit, + initialStatus: '', }); - const [isLoading, setIsLoading] = useState(false); - - return { formik, isLoading }; + return { formik }; }; export const useCommentFormContext = () => useFormikContext();