From 42168beb1b6fa06cc92a2bd8c444fe90aa61d52f Mon Sep 17 00:00:00 2001 From: horek Date: Wed, 24 Jan 2024 17:14:31 +0100 Subject: [PATCH] feat: attach coupon on activation, add interactivity with current coupon for partner url, copy of coupon code and validity from offer --- .../src/pages/dashboard/offer/online/[id].tsx | 74 +++++++++++++------ webapp/src/payload/collections/Coupon.ts | 6 -- webapp/src/payload/collections/Offer.ts | 6 ++ webapp/src/payload/payload-types.ts | 2 +- webapp/src/payload/seed/offers.ts | 9 +++ webapp/src/server/api/routers/coupon.ts | 43 ++++++++++- webapp/src/server/api/routers/offer.ts | 2 +- 7 files changed, 106 insertions(+), 36 deletions(-) diff --git a/webapp/src/pages/dashboard/offer/online/[id].tsx b/webapp/src/pages/dashboard/offer/online/[id].tsx index aae254b7..b47cd605 100644 --- a/webapp/src/pages/dashboard/offer/online/[id].tsx +++ b/webapp/src/pages/dashboard/offer/online/[id].tsx @@ -37,23 +37,32 @@ import { } from "react-icons/fi"; import { TbBuildingStore } from "react-icons/tb"; import { IconType } from "react-icons/lib"; -import { SetStateAction, useState } from "react"; +import { useState } from "react"; import { IoCloseCircleOutline } from "react-icons/io5"; import ToastComponent from "~/components/ToastComponent"; +import { OfferIncluded } from "~/server/api/routers/offer"; +import Link from "next/link"; const DrawerContentComponent = ({ - setIsCouponActive, onClose, onlyCgu, + offer, + mutateCouponToUser, }: { - setIsCouponActive: React.Dispatch>; onClose: () => void; onlyCgu?: boolean; + offer: OfferIncluded; + mutateCouponToUser: ({ offer_id }: { offer_id: number }) => void; }) => { + const validityToDate = new Date(offer.validityTo); + const cguItems: { icon: IconType; text: string; cross?: boolean }[] = [ { icon: FiGlobe, text: "Utilisable en ligne" }, { icon: TbBuildingStore, text: "À utiliser en magasin", cross: true }, - { icon: FiClock, text: "À utiliser avant le XX/XX/XX !" }, + { + icon: FiClock, + text: `À utiliser avant le ${validityToDate.toLocaleDateString()} !`, + }, { icon: FiRotateCw, text: "Utilisation illimité" }, { icon: FiTag, text: "Non cumulable" }, ]; @@ -85,11 +94,11 @@ const DrawerContentComponent = ({ @@ -100,7 +109,6 @@ export default function Dashboard() { const router = useRouter(); const { id } = router.query; - const [isCouponActive, setIsCouponActive] = useState(false); const [isOnlyCgu, setIsOnlyCgu] = useState(false); const { data: resultOffer } = api.offer.getById.useQuery( @@ -110,7 +118,20 @@ export default function Dashboard() { { enabled: id !== undefined } ); + const { data: resultCoupon, refetch: refetchCoupon } = + api.coupon.getOne.useQuery( + { + offer_id: parseInt(id as string), + }, + { enabled: id !== undefined } + ); + const { data: offer } = resultOffer || {}; + const { data: coupon } = resultCoupon || {}; + + const { mutate: mutateCouponToUser } = api.coupon.assignToUser.useMutation({ + onSuccess: () => refetchCoupon(), + }); const toast = useToast(); const theme = useTheme(); @@ -196,7 +217,7 @@ export default function Dashboard() { position="relative" borderRadius="xl" w="full" - bgColor={isCouponActive ? "white" : "cje-gray.500"} + bgColor={coupon ? "white" : "cje-gray.500"} textAlign="center" py={10} > @@ -204,11 +225,11 @@ export default function Dashboard() { fontSize="2xl" fontWeight="bold" letterSpacing={4} - sx={!isCouponActive ? { filter: "blur(4.5px)" } : {}} + sx={!coupon ? { filter: "blur(4.5px)" } : {}} > - 6FHDJFHEIDJF + {coupon?.code ? coupon.code : "6FHDJFHEIDJF"} - {!isCouponActive && ( + {!coupon && ( )} - {isCouponActive && ( + {coupon && ( Utilisable jusqu'au:{" "} - 01/01/2025 + {new Date(coupon.offer.validityTo).toLocaleDateString()} )} - {!isCouponActive ? ( + {!coupon ? ( <>