2023-01-16 09:45:12 +00:00
|
|
|
import useForm, { CommonUseFormResult } from "@dashboard/hooks/useForm";
|
|
|
|
import useHandleFormSubmit from "@dashboard/hooks/useHandleFormSubmit";
|
2022-02-01 09:58:06 +00:00
|
|
|
import React from "react";
|
2021-05-06 11:38:15 +00:00
|
|
|
|
|
|
|
export enum CustomerChangeActionEnum {
|
|
|
|
KEEP_ADDRESS = "keepAddress",
|
2022-06-21 09:36:55 +00:00
|
|
|
CHANGE_ADDRESS = "changeAddress",
|
2021-05-06 11:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface OrderCustomerChangeData {
|
|
|
|
changeActionOption: CustomerChangeActionEnum;
|
|
|
|
}
|
|
|
|
|
2022-02-01 09:58:06 +00:00
|
|
|
type UseOrderCustomerChangeFormResult = CommonUseFormResult<
|
|
|
|
OrderCustomerChangeData
|
|
|
|
>;
|
2021-05-06 11:38:15 +00:00
|
|
|
|
|
|
|
export interface OrderCustomerChangeFormProps {
|
|
|
|
children: (props: UseOrderCustomerChangeFormResult) => React.ReactNode;
|
|
|
|
initial?: Partial<OrderCustomerChangeData>;
|
|
|
|
onSubmit: (data: OrderCustomerChangeData) => void;
|
|
|
|
}
|
|
|
|
|
2022-02-01 09:58:06 +00:00
|
|
|
const defaultInitialFormData: OrderCustomerChangeData = {
|
2022-06-21 09:36:55 +00:00
|
|
|
changeActionOption: CustomerChangeActionEnum.KEEP_ADDRESS,
|
2022-02-01 09:58:06 +00:00
|
|
|
};
|
|
|
|
|
2021-05-06 11:38:15 +00:00
|
|
|
function useOrderCustomerChangeForm(
|
2022-02-01 09:58:06 +00:00
|
|
|
initial: Partial<OrderCustomerChangeData> = {},
|
2022-06-21 09:36:55 +00:00
|
|
|
onSubmit: (data: OrderCustomerChangeData) => void,
|
2021-05-06 11:38:15 +00:00
|
|
|
): UseOrderCustomerChangeFormResult {
|
2022-05-05 07:54:28 +00:00
|
|
|
const { handleChange, data } = useForm({
|
2021-05-06 11:38:15 +00:00
|
|
|
...initial,
|
2022-06-21 09:36:55 +00:00
|
|
|
...defaultInitialFormData,
|
2021-05-06 11:38:15 +00:00
|
|
|
});
|
|
|
|
|
2022-02-01 09:58:06 +00:00
|
|
|
const handleFormSubmit = useHandleFormSubmit({
|
2022-06-21 09:36:55 +00:00
|
|
|
onSubmit,
|
2022-02-01 09:58:06 +00:00
|
|
|
});
|
2021-05-06 11:38:15 +00:00
|
|
|
|
2022-02-01 09:58:06 +00:00
|
|
|
const handleSubmit = () => handleFormSubmit(data);
|
2021-05-06 11:38:15 +00:00
|
|
|
|
|
|
|
const submit = (event: React.FormEvent<any>) => {
|
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
2022-02-01 09:58:06 +00:00
|
|
|
return handleSubmit();
|
2021-05-06 11:38:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
change: handleChange,
|
|
|
|
submit,
|
2022-06-21 09:36:55 +00:00
|
|
|
data,
|
2021-05-06 11:38:15 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const OrderCustomerChangeForm: React.FC<OrderCustomerChangeFormProps> = ({
|
|
|
|
children,
|
|
|
|
initial,
|
2022-06-21 09:36:55 +00:00
|
|
|
onSubmit,
|
2021-05-06 11:38:15 +00:00
|
|
|
}) => {
|
2022-02-01 09:58:06 +00:00
|
|
|
const props = useOrderCustomerChangeForm(initial, onSubmit);
|
2021-05-06 11:38:15 +00:00
|
|
|
|
|
|
|
return <form onSubmit={props.submit}>{children(props)}</form>;
|
|
|
|
};
|
|
|
|
|
|
|
|
OrderCustomerChangeForm.displayName = "OrderCustomerChangeForm";
|
|
|
|
export default OrderCustomerChangeForm;
|