CartModal.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. "use client";
  2. import clsx from "clsx";
  3. import { useDisclosure } from "@heroui/use-disclosure";
  4. import { AnimatePresence, motion } from "framer-motion";
  5. import { ShoppingCartIcon } from "@heroicons/react/24/outline";
  6. import { DEFAULT_OPTION } from "@/utils/constants";
  7. import { useAppSelector } from "@/store/hooks";
  8. import OpenCart from "./OpenCart";
  9. import { Price } from "../theme/ui/Price";
  10. import CloseCart from "../common/icons/cart/CloseCart";
  11. import { DeleteItemButton } from "../common/icons/cart/DeleteItemButton";
  12. import { EditItemQuantityButton } from "../common/icons/cart/EditItemQuantityButton";
  13. import { useCartDetail } from "@utils/hooks/useCartDetail";
  14. import Image from "next/image";
  15. import { NOT_IMAGE } from "@utils/constants";
  16. import LoadingDots from "@components/common/icons/LoadingDots";
  17. import { useFormStatus } from "react-dom";
  18. import { redirectToCheckout } from "@/utils/actions";
  19. import Link from "next/link";
  20. import { createUrl, safeParse } from "@utils/helper";
  21. import { useBodyScrollLock } from "@utils/hooks/useBodyScrollLock";
  22. type MerchandiseSearchParams = {
  23. [key: string]: string;
  24. };
  25. export default function CartModal({
  26. children,
  27. className,
  28. onOpen,
  29. onClose,
  30. isOpen,
  31. }: {
  32. children?: React.ReactNode;
  33. className?: string;
  34. onOpen?: () => void;
  35. onClose?: () => void;
  36. isOpen?: boolean;
  37. }) {
  38. const {
  39. isOpen: internalIsOpen,
  40. onOpen: internalOnOpen,
  41. onClose: internalOnClose,
  42. } = useDisclosure();
  43. const isControlled = isOpen !== undefined;
  44. const finalIsOpen = isControlled ? isOpen : internalIsOpen;
  45. const finalOnOpen = isControlled ? onOpen : internalOnOpen;
  46. const finalOnClose = isControlled ? onClose : internalOnClose;
  47. const { isLoading, cartData: cartDetail } = useCartDetail();
  48. // const cartDetail = useAppSelector((state) => state.cartDetail);
  49. const cart = Array.isArray(cartDetail?.items?.edges)
  50. ? cartDetail?.items?.edges
  51. : [];
  52. useBodyScrollLock(finalIsOpen);
  53. // const handleOpenChange = (open: boolean) => {
  54. // if (!open) {
  55. // finalOnClose?.();
  56. // }
  57. // };
  58. return (
  59. <>
  60. <button
  61. type="button"
  62. aria-label="Open cart"
  63. className={clsx(
  64. className,
  65. isLoading ? "cursor-wait" : "cursor-pointer",
  66. )}
  67. disabled={isLoading}
  68. onClick={finalOnOpen}
  69. >
  70. {children ? (
  71. children
  72. ) : (
  73. <OpenCart quantity={cartDetail?.itemsQty} />
  74. )}
  75. </button>
  76. <AnimatePresence>
  77. {finalIsOpen && (
  78. <>
  79. <motion.div
  80. initial={{ opacity: 0 }}
  81. animate={{ opacity: 1 }}
  82. exit={{ opacity: 0 }}
  83. onClick={finalOnClose}
  84. className="fixed inset-0 z-40 bg-transparent lg:hidden transition-opacity"
  85. style={{ top: "68px", bottom: "64px" }}
  86. />
  87. <motion.div
  88. initial={{ x: "100%" }}
  89. animate={{ x: 0 }}
  90. exit={{ x: "100%" }}
  91. transition={{
  92. type: "spring",
  93. damping: 30,
  94. stiffness: 300,
  95. mass: 0.8,
  96. }}
  97. className="fixed right-0 z-50 flex flex-col bg-white dark:bg-black w-full max-w-[448px] border-l border-neutral-200 dark:border-neutral-800 lg:hidden"
  98. style={{
  99. top: "68px",
  100. bottom: "64px",
  101. width: "100%",
  102. maxWidth: "448px",
  103. height: "calc(var(--visual-viewport-height) - 132px)",
  104. }}
  105. >
  106. <div className="flex flex-col gap-1 p-4">
  107. <div className="flex items-center justify-between">
  108. <p
  109. className={clsx(
  110. "font-semibold",
  111. "text-xl",
  112. )}
  113. >
  114. My Cart
  115. </p>
  116. <button
  117. aria-label="Close cart"
  118. className="cursor-pointer"
  119. onClick={finalOnClose}
  120. >
  121. <CloseCart />
  122. </button>
  123. </div>
  124. </div>
  125. <div
  126. className={clsx(
  127. "flex-1 overflow-y-auto px-4 py-0 drawer-scrollbar-hidden",
  128. "!px-2",
  129. )}
  130. >
  131. {cart?.length === 0 ? (
  132. <div className="mt-20 flex w-full flex-col items-center justify-center overflow-hidden">
  133. <ShoppingCartIcon className="h-16" />
  134. <p className="mt-6 text-center text-2xl font-bold">
  135. Your cart is empty.
  136. </p>
  137. </div>
  138. ) : (
  139. <div className="flex h-full flex-col justify-between">
  140. <ul className="my-0 flex-grow overflow-auto py-0">
  141. {Array.isArray(cart) &&
  142. cart?.map((item: any, i: number) => {
  143. const merchandiseSearchParams =
  144. {} as MerchandiseSearchParams;
  145. const merchandiseUrl = createUrl(
  146. `/product/${item?.node.productUrlKey}`,
  147. new URLSearchParams(merchandiseSearchParams),
  148. );
  149. const baseImage: any = safeParse(
  150. item?.node?.baseImage,
  151. );
  152. return (
  153. <li key={i} className="flex w-full flex-col">
  154. <div
  155. className={clsx(
  156. "flex w-full flex-row justify-between py-4 px-1",
  157. "gap-1 xxs:gap-3",
  158. )}
  159. >
  160. <Link
  161. className="z-30 flex flex-row space-x-4"
  162. aria-label={`${item?.node?.name}`}
  163. href={merchandiseUrl}
  164. onClick={finalOnClose}
  165. >
  166. <div className="relative h-16 w-16 cursor-pointer overflow-hidden rounded-md border border-neutral-300 bg-neutral-300 dark:border-neutral-700 dark:bg-neutral-900 dark:hover:bg-neutral-800">
  167. <Image
  168. alt={
  169. item?.node?.baseImage ||
  170. item?.product?.name
  171. }
  172. className="h-full w-full object-cover"
  173. height={64}
  174. src={baseImage?.small_image_url || ""}
  175. width={74}
  176. onError={(e) =>
  177. (e.currentTarget.src = NOT_IMAGE)
  178. }
  179. />
  180. </div>
  181. <div className="flex flex-1 flex-col text-base">
  182. <span className="line-clamp-1 font-outfit text-base font-medium">
  183. {item?.node?.name}
  184. </span>
  185. {item.name !== DEFAULT_OPTION && (
  186. <p className="text-sm lowercase line-clamp-1 text-black dark:text-neutral-400">
  187. {item?.node?.sku}
  188. </p>
  189. )}
  190. </div>
  191. </Link>
  192. <div className="flex h-16 flex-col justify-between">
  193. <Price
  194. amount={item?.node?.price}
  195. className="flex justify-end space-y-2 text-right font-outfit text-base font-medium"
  196. currencyCode={"USD"}
  197. />
  198. <div className="flex items-center gap-x-2">
  199. <DeleteItemButton item={item} />
  200. <div className="ml-auto flex h-9 flex-row items-center rounded-full border border-neutral-200 dark:border-neutral-700">
  201. <EditItemQuantityButton
  202. item={item}
  203. type="minus"
  204. />
  205. <p className="w-6 text-center">
  206. <span className="w-full text-sm">
  207. {item?.node?.quantity}
  208. </span>
  209. </p>
  210. <EditItemQuantityButton
  211. item={item}
  212. type="plus"
  213. />
  214. </div>
  215. </div>
  216. </div>
  217. </div>
  218. </li>
  219. );
  220. })}
  221. </ul>
  222. <div className="border-0 border-t border-solid border-neutral-200 dark:border-dark-grey py-4 text-sm text-neutral-500 dark:text-neutral-400">
  223. {(cartDetail as any)?.taxAmount > 0 && (
  224. <div className="mb-3 flex items-center justify-between">
  225. <p className="text-base font-normal text-black/[60%] dark:text-white">
  226. Taxes
  227. </p>
  228. <Price
  229. amount={(cartDetail as any)?.taxAmount}
  230. className="text-right text-base font-medium text-black dark:text-white"
  231. currencyCode={"USD"}
  232. />
  233. </div>
  234. )}
  235. <div className="mb-3 flex items-center justify-between pb-1">
  236. <p className="text-base font-normal text-black/[60%] dark:text-white">
  237. Total
  238. </p>
  239. <Price
  240. amount={(cartDetail as any)?.grandTotal}
  241. className="text-right text-base font-medium text-black dark:text-white"
  242. currencyCode={"USD"}
  243. />
  244. </div>
  245. <form action={redirectToCheckout}>
  246. <CheckoutButton />
  247. </form>
  248. </div>
  249. </div>
  250. )}
  251. </div>
  252. <div className="p-4" />
  253. </motion.div>
  254. </>
  255. )}
  256. </AnimatePresence>
  257. </>
  258. );
  259. }
  260. function CheckoutButton() {
  261. const { pending } = useFormStatus();
  262. return (
  263. <>
  264. <input
  265. name="url"
  266. type="hidden"
  267. value="/checkout"
  268. />
  269. <button
  270. className={clsx(
  271. "block w-full rounded-full bg-blue-600 p-3 text-center text-sm font-medium text-white opacity-90 hover:opacity-100",
  272. pending ? "cursor-wait" : "cursor-pointer",
  273. )}
  274. disabled={pending}
  275. type="submit"
  276. >
  277. {pending ? <LoadingDots className="bg-white" /> : "Proceed to Checkout"}
  278. </button>
  279. </>
  280. );
  281. }