handling form submission
Unanswered
astro posted this in #help-forum
astroOP
im using react-hook-form in nextjs, now, i have two buttons
and here's my onSubmit function
however i want that, if user clicks the add to cart button the above logic should be executed but if he clicks buy now button then the above logic should be executed and then the user should be redirected to /checkout
<Button
disabled={form.formState.isSubmitting}
size="sm"
>
<ShoppingBagIcon className="mr-1.5 h-3.5 w-3.5" />
Buy Now
</Button>
<Button
disabled={form.formState.isSubmitting}
size="sm"
variant="outline"
>
<ShoppingCartIcon className="mr-1.5 h-3.5 w-3.5" />
Add to Cart
</Button>and here's my onSubmit function
async function onSubmit(data: z.infer<typeof schema>) {
const variant = variants.find((v) => v.id === parseInt(data.variantId));
if (!variant) return null;
const res = await addToCart({
productId: variant.product_id,
variantId: variant.id,
price: variant.price,
});
if (res.error) {
toast({
title: res.error,
variant: "destructive",
});
return;
}
toast({
title: res.success,
});
}however i want that, if user clicks the add to cart button the above logic should be executed but if he clicks buy now button then the above logic should be executed and then the user should be redirected to /checkout