2023-01-16 09:45:12 +00:00
|
|
|
import { UseNavigatorResult } from "@dashboard/hooks/useNavigator";
|
|
|
|
import { BulkAction, Dialog, SingleAction } from "@dashboard/types";
|
2019-12-06 14:58:28 +00:00
|
|
|
|
|
|
|
type Url<T extends Dialog<any>> = (params: T) => string;
|
|
|
|
type CreateCloseModal<
|
|
|
|
TAction extends string,
|
|
|
|
TParams extends Dialog<TAction>
|
|
|
|
> = [(action: TAction, newParams?: TParams) => void, () => void];
|
|
|
|
|
|
|
|
function createDialogActionHandlers<
|
|
|
|
TAction extends string,
|
|
|
|
TParams extends Dialog<TAction> & BulkAction & SingleAction
|
|
|
|
>(
|
|
|
|
navigate: UseNavigatorResult,
|
|
|
|
url: Url<TParams>,
|
2022-06-21 09:36:55 +00:00
|
|
|
params: TParams,
|
2019-12-06 14:58:28 +00:00
|
|
|
): CreateCloseModal<TAction, TParams> {
|
|
|
|
const close = () =>
|
|
|
|
navigate(
|
|
|
|
url({
|
|
|
|
...params,
|
|
|
|
action: undefined,
|
|
|
|
id: undefined,
|
2022-06-21 09:36:55 +00:00
|
|
|
ids: undefined,
|
2019-12-06 14:58:28 +00:00
|
|
|
}),
|
2022-06-21 09:36:55 +00:00
|
|
|
{ replace: true },
|
2019-12-06 14:58:28 +00:00
|
|
|
);
|
|
|
|
const open = (action: TAction, newParams?: TParams) =>
|
|
|
|
navigate(
|
|
|
|
url({
|
|
|
|
...params,
|
|
|
|
...newParams,
|
2022-06-21 09:36:55 +00:00
|
|
|
action,
|
|
|
|
}),
|
2019-12-06 14:58:28 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return [open, close];
|
|
|
|
}
|
|
|
|
|
|
|
|
export default createDialogActionHandlers;
|