41 lines
1.6 KiB
TypeScript
41 lines
1.6 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>;
|
|
type CleanedFormProps<P> = Omit<P, "refresh" | "afterSubmit" | "initialData">;
|
|
export function withForm<T extends object, P extends FormProps<T>>(WrappedForm: React.ComponentType<CleanedFormProps<P>>, method: (client: ApiClient, project: OwnerResponse, props: P) => (body: T) => CancelablePromise<any> | any, submitText: string, trMethod?: (initialData: T, props: CleanedFormProps<P>) => T) {
|
|
return function GameFormWrapper({ refresh, afterSubmit, initialData, ...props }: P) {
|
|
const client = useApiClient();
|
|
const company = useOwner();
|
|
|
|
if (trMethod)
|
|
initialData = trMethod(initialData, props);
|
|
|
|
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 {...props} />
|
|
</FormikForm>
|
|
);
|
|
};
|
|
}
|