1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-25 04:46:40 +07:00

#34 made submitting comment form

This commit is contained in:
Fedor Katurov 2021-02-27 18:11:38 +07:00
parent 051b199d5d
commit dad416e6e2
6 changed files with 97 additions and 65 deletions

View file

@ -3,22 +3,14 @@ import { useCallback, useRef } from 'react';
import { FormikHelpers, useFormik, useFormikContext } from 'formik';
import { array, object, string } from 'yup';
import { FileUploader } from '~/utils/hooks/fileUploader';
import { useDispatch } from 'react-redux';
import { nodePostLocalComment } from '~/redux/node/actions';
const validationSchema = object().shape({
text: string(),
files: array(),
});
const submitComment = (
id: INode['id'],
values: IComment,
isBefore: boolean,
callback: (e?: string) => void
) => {
console.log('Submitting', id, values);
new Promise(resolve => setTimeout(resolve, 500)).then(() => callback());
};
const onSuccess = ({ resetForm, setStatus, setSubmitting }: FormikHelpers<IComment>) => (
e: string
) => {
@ -38,25 +30,26 @@ export const useCommentFormFormik = (
values: IComment,
nodeId: INode['id'],
uploader: FileUploader,
stopEditing?: () => void,
isBefore: boolean = false
stopEditing?: () => void
) => {
const dispatch = useDispatch();
const { current: initialValues } = useRef(values);
const onSubmit = useCallback(
(values: IComment, helpers: FormikHelpers<IComment>) => {
helpers.setSubmitting(true);
submitComment(
nodeId,
{
...values,
files: uploader.files,
},
isBefore,
onSuccess(helpers)
dispatch(
nodePostLocalComment(
nodeId,
{
...values,
files: uploader.files,
},
onSuccess(helpers)
)
);
},
[isBefore, nodeId, uploader.files]
[dispatch, nodeId, uploader.files]
);
const onReset = useCallback(() => {