diff --git a/src/components/Form/Form.tsx b/src/components/Form/Form.tsx deleted file mode 100644 index 1c0ef7946b8..00000000000 --- a/src/components/Form/Form.tsx +++ /dev/null @@ -1,171 +0,0 @@ -import isEqual from "lodash/isEqual"; -//loadish -import { useEffect, useMemo, useRef, useState } from "react"; - -import { Cancel, Submit } from "@/components/Common/ButtonV2"; -import { FieldValidator } from "@/components/Form/FieldValidators"; -import { - FormContextValue, - createFormContext, -} from "@/components/Form/FormContext"; -import { FieldChangeEvent } from "@/components/Form/FormFields/Utils"; -import { - FormDetails, - FormErrors, - FormState, - formReducer, -} from "@/components/Form/Utils"; - -import { DraftSection, useAutoSaveReducer } from "@/Utils/AutoSave"; -import * as Notification from "@/Utils/Notifications"; -import { classNames, isEmpty, omitBy } from "@/Utils/utils"; - -type Props = { - className?: string; - defaults: T; - asyncGetDefaults?: (() => Promise) | false; - validate?: (form: T) => FormErrors; - onSubmit: (form: T) => Promise | void>; - onCancel?: () => void; - noPadding?: true; - disabled?: boolean; - submitLabel?: string; - cancelLabel?: string; - onDraftRestore?: (newState: FormState) => void; - children: (props: FormContextValue) => React.ReactNode; - hideRestoreDraft?: boolean; - resetFormValsOnCancel?: boolean; - resetFormValsOnSubmit?: boolean; - hideCancelButton?: boolean; -}; - -const Form = ({ - asyncGetDefaults, - validate, - hideCancelButton = false, - ...props -}: Props) => { - const initial = { form: props.defaults, errors: {} }; - const [isLoading, setIsLoading] = useState(!!asyncGetDefaults); - const [state, dispatch] = useAutoSaveReducer(formReducer, initial); - const formVals = useRef(props.defaults); - const [isFormModified, setIsFormModified] = useState(false); - - useEffect(() => { - if (!asyncGetDefaults) return; - - asyncGetDefaults().then((form) => { - dispatch({ type: "set_form", form }); - setIsLoading(false); - }); - }, [asyncGetDefaults]); - - const handleSubmit = async (event: React.FormEvent) => { - event.preventDefault(); - event.stopPropagation(); - if (validate) { - const errors = omitBy(validate(state.form), isEmpty) as FormErrors; - if (Object.keys(errors).length) { - dispatch({ type: "set_errors", errors }); - // - if (errors.$all) { - Notification.Error({ msg: errors.$all }); - } - return; - } - } - - const errors = await props.onSubmit(state.form); - if (errors) { - dispatch({ - type: "set_errors", - errors: { ...state.errors, ...errors }, - }); - } else if (props.resetFormValsOnSubmit) { - dispatch({ type: "set_form", form: formVals.current }); - setIsFormModified(false); - } - }; - - const handleCancel = () => { - if (props.resetFormValsOnCancel) { - dispatch({ type: "set_form", form: formVals.current }); - setIsFormModified(false); - } - props.onCancel?.(); - }; - - const { Provider, Consumer } = useMemo(() => createFormContext(), []); - const disabled = isLoading || props.disabled; - - return ( -
{ - if (e.key === "Enter") { - handleSubmit(e); - } - }} - className={classNames( - "mx-auto w-full", - !props.noPadding && "px-8 py-5 md:px-16 md:py-11", - props.className, - )} - noValidate - > - ) => { - dispatch({ type: "set_state", state: newState }); - props.onDraftRestore?.(newState); - }} - formData={state.form} - hidden={props.hideRestoreDraft} - > - ) => { - return { - name, - id: name, - onChange: ({ name, value }: FieldChangeEvent) => { - const newForm = { - ...state.form, - [name]: value, - }; - setIsFormModified(!isEqual(newForm, formVals.current)); - dispatch({ - type: "set_field", - name, - value, - error: validate?.(value), - }); - }, - value: state.form[name], - error: state.errors[name], - disabled, - }; - }} - > -
- {props.children} -
-
- {!hideCancelButton && ( - - )} - -
-
-
-
- ); -}; - -export default Form;