nej-react-utils/Form/withForm.tsx
2024-12-27 23:50:13 +01:00

37 lines
1.4 KiB
TypeScript

import { FormikForm } from "@shared/nej-react-utils/Form/FormikForm";
import { useApiClient } from "@utils/NejManager/NejProvider";
import useMethod from "../macros/useMethod.macro";
import useQuery from "../macros/useQuery.macro";
import { CancelablePromise, ApiClient, OwnerResponse, useOwner } from "@services/api";
type FormProps<T> = {
refresh: () => void;
afterSubmit?: (values: T) => void;
initialData?: T;
};
export type WithFormProps<T, P> = P & FormProps<T>;
export function withForm<T extends object, P extends FormProps<T>>(WrappedForm: React.ComponentType, method: (client: ApiClient, project: OwnerResponse, props: P) => (body: T) => CancelablePromise<any>, submitText: string) {
return function GameFormWrapper({ refresh, afterSubmit, initialData, ...props }: P) {
const client = useApiClient();
const company = useOwner();
const fnc = method(client, company, { ...props, initialData } as P);
console.log(fnc);
const [submit, { }] = useMethod(
fnc,
() => refresh()
);
return (
<FormikForm
data={initialData || {} as T}
onSubmit={(values) => {submit(values); afterSubmit && afterSubmit(values)}}
submitText={submitText}
>
<WrappedForm />
</FormikForm>
);
};
}