CartModal.tsx 14 KB

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