mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 12:56:41 +07:00
99 use swr (#100)
* 99: made node use SWR * 99: fixed comments for SWR node * 99: added error toast to useNodeFormFormik.ts
This commit is contained in:
parent
832386d39a
commit
c2d1c2bfc9
35 changed files with 366 additions and 413 deletions
|
@ -1,76 +0,0 @@
|
|||
import { INode } from '~/redux/types';
|
||||
import { FileUploader } from '~/utils/hooks/useFileUploader';
|
||||
import { useCallback, useEffect, useRef } from 'react';
|
||||
import { FormikHelpers, useFormik, useFormikContext } from 'formik';
|
||||
import { object } from 'yup';
|
||||
import { useDispatch } from 'react-redux';
|
||||
import { nodeSubmitLocal } from '~/redux/node/actions';
|
||||
import { keys } from 'ramda';
|
||||
|
||||
const validationSchema = object().shape({});
|
||||
|
||||
const onSuccess = ({ resetForm, setStatus, setSubmitting, setErrors }: FormikHelpers<INode>) => (
|
||||
e?: string,
|
||||
errors?: Record<string, string>
|
||||
) => {
|
||||
setSubmitting(false);
|
||||
|
||||
if (e) {
|
||||
setStatus(e);
|
||||
return;
|
||||
}
|
||||
|
||||
if (errors && keys(errors).length) {
|
||||
setErrors(errors);
|
||||
return;
|
||||
}
|
||||
|
||||
if (resetForm) {
|
||||
resetForm();
|
||||
}
|
||||
};
|
||||
|
||||
export const useNodeFormFormik = (
|
||||
values: INode,
|
||||
uploader: FileUploader,
|
||||
stopEditing: () => void
|
||||
) => {
|
||||
const dispatch = useDispatch();
|
||||
const onSubmit = useCallback(
|
||||
(values: INode, helpers: FormikHelpers<INode>) => {
|
||||
helpers.setSubmitting(true);
|
||||
dispatch(nodeSubmitLocal(values, onSuccess(helpers)));
|
||||
},
|
||||
[dispatch]
|
||||
);
|
||||
|
||||
const { current: initialValues } = useRef(values);
|
||||
|
||||
const onReset = useCallback(() => {
|
||||
uploader.setFiles([]);
|
||||
|
||||
if (stopEditing) stopEditing();
|
||||
}, [uploader, stopEditing]);
|
||||
|
||||
const formik = useFormik<INode>({
|
||||
initialValues,
|
||||
validationSchema,
|
||||
onSubmit,
|
||||
onReset,
|
||||
initialStatus: '',
|
||||
validateOnChange: true,
|
||||
});
|
||||
|
||||
useEffect(
|
||||
() => {
|
||||
formik.setFieldValue('files', uploader.files);
|
||||
},
|
||||
// because it breaks files logic
|
||||
// eslint-disable-next-line
|
||||
[uploader.files, formik.setFieldValue]
|
||||
);
|
||||
|
||||
return formik;
|
||||
};
|
||||
|
||||
export const useNodeFormContext = () => useFormikContext<INode>();
|
Loading…
Add table
Add a link
Reference in a new issue