|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { Fragment, useState, useTransition } from 'react' |
| 4 | +import { useRouter } from 'next/navigation' |
| 5 | + |
| 6 | +import Image from 'next/image' |
| 7 | +import Link from 'next/link' |
| 8 | + |
| 9 | +import { Blinker } from '@/components/ui/loading' |
| 10 | +import { formatCurrency } from '@/lib/utils' |
| 11 | + |
| 12 | +import { Dialog, Transition } from '@headlessui/react' |
| 13 | +import { XMarkIcon } from '@heroicons/react/24/outline' |
| 14 | +import { removeFromCart } from '@/lib/swell/cart' |
| 15 | +import { useSWRConfig } from 'swr' |
| 16 | + |
| 17 | +const CartSlider = ({ cart, cartIsLoading, open, setCartSliderIsOpen }) => { |
| 18 | + const router = useRouter() |
| 19 | + const { mutate } = useSWRConfig() |
| 20 | + const [isPending, startTransition] = useTransition() |
| 21 | + const [loading, setLoading] = useState(false) |
| 22 | + const isMutating = loading || isPending |
| 23 | + |
| 24 | + const removeItem = async itemId => { |
| 25 | + setLoading(true) |
| 26 | + await removeFromCart(itemId) |
| 27 | + setLoading(false) |
| 28 | + mutate('cart') |
| 29 | + startTransition(() => { |
| 30 | + router.refresh() |
| 31 | + }) |
| 32 | + } |
| 33 | + |
| 34 | + return ( |
| 35 | + <Transition.Root show={open} as={Fragment}> |
| 36 | + <Dialog as='div' className='relative z-10' onClose={setCartSliderIsOpen}> |
| 37 | + <Transition.Child |
| 38 | + as={Fragment} |
| 39 | + enter='ease-in-out duration-500' |
| 40 | + enterFrom='opacity-0' |
| 41 | + enterTo='opacity-100' |
| 42 | + leave='ease-in-out duration-500' |
| 43 | + leaveFrom='opacity-100' |
| 44 | + leaveTo='opacity-0' |
| 45 | + > |
| 46 | + <div className='fixed inset-0 bg-gray-500 bg-opacity-75 transition-opacity' /> |
| 47 | + </Transition.Child> |
| 48 | + |
| 49 | + <div className='fixed inset-0 overflow-hidden'> |
| 50 | + <div className='absolute inset-0 overflow-hidden'> |
| 51 | + <div className='pointer-events-none fixed inset-y-0 right-0 flex max-w-full pl-10'> |
| 52 | + <Transition.Child |
| 53 | + as={Fragment} |
| 54 | + enter='transform transition ease-in-out duration-500 sm:duration-700' |
| 55 | + enterFrom='translate-x-full' |
| 56 | + enterTo='translate-x-0' |
| 57 | + leave='transform transition ease-in-out duration-500 sm:duration-700' |
| 58 | + leaveFrom='translate-x-0' |
| 59 | + leaveTo='translate-x-full' |
| 60 | + > |
| 61 | + <Dialog.Panel className='pointer-events-auto w-screen max-w-md'> |
| 62 | + <div className='flex h-full flex-col overflow-y-scroll bg-white shadow-xl'> |
| 63 | + <div className='flex-1 overflow-y-auto py-6 px-4 sm:px-6'> |
| 64 | + <div className='flex items-start justify-between'> |
| 65 | + <Dialog.Title className='text-lg font-medium text-gray-900'> |
| 66 | + {' '} |
| 67 | + Shopping cart{' '} |
| 68 | + </Dialog.Title> |
| 69 | + <div className='ml-3 flex h-7 items-center'> |
| 70 | + <button |
| 71 | + type='button' |
| 72 | + className='-m-2 p-2 text-gray-400 hover:text-gray-500' |
| 73 | + onClick={() => setCartSliderIsOpen(false)} |
| 74 | + > |
| 75 | + <span className='sr-only'>Close panel</span> |
| 76 | + <XMarkIcon className='h-6 w-6' aria-hidden='true' /> |
| 77 | + </button> |
| 78 | + </div> |
| 79 | + </div> |
| 80 | + |
| 81 | + <div className='mt-8'> |
| 82 | + <div className='flow-root'> |
| 83 | + <ul |
| 84 | + role='list' |
| 85 | + className='-my-6 divide-y divide-gray-200' |
| 86 | + > |
| 87 | + {cart?.items?.length > 0 && |
| 88 | + cart.items.map(item => ( |
| 89 | + <li key={item.id} className='flex py-6'> |
| 90 | + <div className='relative h-24 w-24 flex-shrink-0 overflow-hidden rounded-md border border-gray-200'> |
| 91 | + <Image |
| 92 | + src={item.product.images[0].file.url} |
| 93 | + alt={item.product.name} |
| 94 | + className='h-full w-full object-cover object-center' |
| 95 | + layout='fill' |
| 96 | + /> |
| 97 | + </div> |
| 98 | + |
| 99 | + <div className='ml-4 flex flex-1 flex-col'> |
| 100 | + <div> |
| 101 | + <div className='flex justify-between text-base font-medium text-gray-900'> |
| 102 | + <h3> |
| 103 | + <a |
| 104 | + href={`/products/${item.product.slug}`} |
| 105 | + > |
| 106 | + {' '} |
| 107 | + {item.product.name}{' '} |
| 108 | + </a> |
| 109 | + </h3> |
| 110 | + <p className='ml-4'> |
| 111 | + {formatCurrency({ |
| 112 | + amount: item.price_total |
| 113 | + })} |
| 114 | + </p> |
| 115 | + </div> |
| 116 | + <p className='mt-1 text-sm text-gray-500'> |
| 117 | + {item.product.name} |
| 118 | + </p> |
| 119 | + </div> |
| 120 | + <div className='flex flex-1 items-end justify-between text-sm'> |
| 121 | + <p className='text-gray-500'> |
| 122 | + Qty {item.quantity} |
| 123 | + </p> |
| 124 | + |
| 125 | + <div className='flex'> |
| 126 | + <button |
| 127 | + type='button' |
| 128 | + disabled={isMutating} |
| 129 | + onClick={() => removeItem(item.id)} |
| 130 | + className='font-medium text-pink-600 hover:text-pink-500 disabled:cursor-not-allowed disabled:opacity-50' |
| 131 | + > |
| 132 | + Remove |
| 133 | + </button> |
| 134 | + </div> |
| 135 | + </div> |
| 136 | + </div> |
| 137 | + </li> |
| 138 | + ))} |
| 139 | + </ul> |
| 140 | + </div> |
| 141 | + </div> |
| 142 | + </div> |
| 143 | + |
| 144 | + <div className='border-t border-gray-200 py-6 px-4 sm:px-6'> |
| 145 | + <div className='flex justify-between text-base font-medium text-gray-900'> |
| 146 | + <p>Subtotal</p> |
| 147 | + <p> |
| 148 | + {formatCurrency({ amount: cart?.sub_total || 0 })} |
| 149 | + </p> |
| 150 | + </div> |
| 151 | + <p className='mt-0.5 text-sm text-gray-500'> |
| 152 | + Shipping and taxes calculated at checkout. |
| 153 | + </p> |
| 154 | + |
| 155 | + {cart?.checkout_url && ( |
| 156 | + <div className='mt-6'> |
| 157 | + <Link href={cart.checkout_url}> |
| 158 | + <button |
| 159 | + disabled={cartIsLoading} |
| 160 | + className='flex h-12 w-full items-center justify-center rounded-md border border-transparent bg-cyan-600 px-6 py-3 text-base font-medium text-white shadow-sm hover:bg-cyan-700 disabled:cursor-not-allowed disabled:opacity-75' |
| 161 | + > |
| 162 | + {cartIsLoading ? <Blinker /> : 'Checkout'} |
| 163 | + </button> |
| 164 | + </Link> |
| 165 | + </div> |
| 166 | + )} |
| 167 | + |
| 168 | + <div className='mt-6 flex justify-center text-center text-sm text-gray-500'> |
| 169 | + <p> |
| 170 | + or{' '} |
| 171 | + <button |
| 172 | + type='button' |
| 173 | + className='font-medium text-cyan-600 hover:text-cyan-500' |
| 174 | + onClick={() => setCartSliderIsOpen(false)} |
| 175 | + > |
| 176 | + Continue Shopping |
| 177 | + <span aria-hidden='true'> →</span> |
| 178 | + </button> |
| 179 | + </p> |
| 180 | + </div> |
| 181 | + </div> |
| 182 | + </div> |
| 183 | + </Dialog.Panel> |
| 184 | + </Transition.Child> |
| 185 | + </div> |
| 186 | + </div> |
| 187 | + </div> |
| 188 | + </Dialog> |
| 189 | + </Transition.Root> |
| 190 | + ) |
| 191 | +} |
| 192 | + |
| 193 | +export default CartSlider |
0 commit comments