export function useUpdateShopForm(_args?: UseUpdateShopFormArgs) {
const { shop } = useShop();
const updateShopMutation = api.shop.update.useMutation({});
const updateShopForm = useZodForm(UpdateShopSchema, {
defaultValues: {
name: shop.name,
description: shop.description,
deliveryMethods: shop.deliveryMethods,
paymentMethods: shop.paymentMethods,
},
});
const onSubmit = useCallback(
async (input: UpdateShopSchema) => {
if (
Object.values(input.deliveryMethods).every(
(value) => value === false
) ||
Object.values(input.paymentMethods).every((value) => value === false)
) {
toast.error('At least one value must be checked.');
return;
}
const updatedShop = await updateShopMutation.mutateAsync({
shopSlug: shop.slug,
...input,
});
updateShopForm.reset({
name: updatedShop.name,
description: updatedShop.description,
deliveryMethods: updatedShop.deliveryMethods,
paymentMethods: updatedShop.paymentMethods,
});
},
[updateShopMutation, shop, updateShopForm]
);
return {
updateShopMutation,
updateShopForm,
loading: updateShopMutation.isLoading,
handleSubmit: updateShopForm.handleSubmit(onSubmit),
};
}