import { SelectContent } from "@/components/ui/select"; import { SelectValue } from "@/components/ui/select"; import { SelectTrigger } from "@/components/ui/select"; import { DialogFooter } from "@/components/ui/dialog"; import { Reward, RewardType } from "@autumn/shared"; import { Select, SelectItem } from "@/components/ui/select"; import { DialogContent, DialogTitle } from "@/components/ui/dialog"; import { useState } from "react"; import { Button } from "@/components/ui/button"; import { useCustomerContext } from "../CustomerContext"; import { getBackendErr } from "@/utils/genUtils"; import { toast } from "sonner"; import { CusService } from "@/services/customers/CusService"; import { useAxiosInstance } from "@/services/useAxiosInstance"; import { getOriginalCouponId } from "@/utils/product/couponUtils"; import { WarningBox } from "@/components/general/modal-components/WarningBox"; import { useRewardsQuery } from "@/hooks/queries/useRewardsQuery"; import { useCusQuery } from "../hooks/useCusQuery"; import { useCusReferralQuery } from "../hooks/useCusReferralQuery"; const AddCouponDialogContent = ({ setOpen, }: { setOpen: (open: boolean) => void; }) => { const { stripeCus } = useCusReferralQuery(); const { customer, refetch } = useCusQuery(); const [couponSelected, setCouponSelected] = useState(null); const [loading, setLoading] = useState(false); const axiosInstance = useAxiosInstance(); const { rewards } = useRewardsQuery(); const handleAddClicked = async () => { try { setLoading(true); await CusService.addCouponToCustomer({ axios: axiosInstance, customer_id: customer.id, coupon_id: couponSelected!.internal_id, }); setOpen(false); await refetch(); toast.success("Reward added to customer"); } catch (error) { toast.error(getBackendErr(error, "Failed to create coupon")); } finally { setLoading(false); } }; const existingDiscount = stripeCus?.discount; const getExistingCoupon = () => { if (existingDiscount) { return rewards.find( (c: Reward) => c.id === getOriginalCouponId(existingDiscount.coupon.id) ); } else { return null; } }; if (!rewards) return null; return ( Add Reward {getExistingCoupon() && ( Reward {getExistingCoupon()?.name} already applied. Adding a new one will replace the existing one. )}
); }; export default AddCouponDialogContent;