add withForm custom defailts and fix useMethod and query sigantures

This commit is contained in:
honzapatCZ 2025-02-04 17:10:23 +01:00
parent a60f19ff08
commit 0b49c447e0
3 changed files with 9 additions and 5 deletions

View File

@ -10,11 +10,15 @@ type FormProps<T> = {
initialData?: T; initialData?: T;
}; };
export type WithFormProps<T, P> = P & FormProps<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) { 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>, submitText: string, trMethod?: (initialData: T, props: CleanedFormProps<P>) => T) {
return function GameFormWrapper({ refresh, afterSubmit, initialData, ...props }: P) { return function GameFormWrapper({ refresh, afterSubmit, initialData, ...props }: P) {
const client = useApiClient(); const client = useApiClient();
const company = useOwner(); const company = useOwner();
if (trMethod)
initialData = trMethod(initialData, props);
const fnc = method(client, company, { ...props, initialData } as P); const fnc = method(client, company, { ...props, initialData } as P);
console.log(fnc); console.log(fnc);
@ -29,7 +33,7 @@ export function withForm<T extends object, P extends FormProps<T>>(WrappedForm:
onSubmit={(values) => { submit(values); afterSubmit && afterSubmit(values) }} onSubmit={(values) => { submit(values); afterSubmit && afterSubmit(values) }}
submitText={submitText} submitText={submitText}
> >
<WrappedForm /> <WrappedForm {...props} />
</FormikForm> </FormikForm>
); );
}; };

View File

@ -1,4 +1,4 @@
import { CancelablePromise, Error } from "src/services/api"; import { CancelablePromise, Error } from "@services/api";
export default function useMethod<T, Arg>(method: (...args: Arg) => CancelablePromise<T>, export default function useMethod<T, Arg>(method: (...args: Arg) => CancelablePromise<T>,
onDone?: (data: T, ...args: Arg) => void, onDone?: (data: T, ...args: Arg) => void,

View File

@ -1,4 +1,4 @@
import { CancelablePromise, Error } from "src/services/api"; import { CancelablePromise, Error } from "@services/api";
//default export is a function //default export is a function
/* /*