import Image from 'next/image'; import { Dialog, DialogBackdrop, DialogPanel } from '@headlessui/react'; import { toPrintDate } from 'lib/utils'; import PaymentsDetails from './payment-details'; import Price from 'components/price'; import Divider from 'components/divider'; import Markdown from 'markdown-to-jsx'; import { Order, OrderConfirmationContent } from 'lib/shopify/types'; import { FormEventHandler, useEffect, useRef, useState, useTransition } from 'react'; import { confirmOrder } from 'components/orders/actions'; import { Button, Heading, Text, Label, Skeleton, InputLabel, Input } from 'components/ui'; function OrderConfirmationDetails({ content, order }: { content: OrderConfirmationContent; order: Order; }) { return (
{content?.logo?.altText
ORDER INFORMATION:
Order number: {order.name} Email: {order.customer?.emailAddress} Date: {toPrintDate(order.processedAt)}
{order.shippingAddress!.firstName} {order.shippingAddress!.lastName} {order.shippingAddress!.address1} {order.shippingAddress!.address2 && {order.shippingAddress!.address2}} {order.shippingAddress!.city} {order.shippingAddress!.provinceCode}{' '} {order.shippingAddress!.zip} {order.shippingAddress!.country}
{order.billingAddress!.firstName} {order.billingAddress!.lastName} {order.billingAddress!.address1} {order.billingAddress!.address2 && {order.billingAddress!.address2}} {order.billingAddress!.city} {order.billingAddress!.provinceCode}{' '} {order.billingAddress!.zip} {order.billingAddress!.country}
Products {order.lineItems.map((lineItem, index) => ( ))}
{lineItem.title} {lineItem.quantity}
Subtotal
Shipping {order.shippingMethod.price.amount !== '0.0' ? ( ) : ( Free )}
Total
{content?.body || ''}
); } export default function OrderConfirmationModal({ order, isOpen, onClose }: { order: Order; isOpen: boolean; onClose: () => void; }) { const [loading, setLoading] = useState(true); const [orderConfirmationContent, setOrderConfirmationContent] = useState(); const [, startTransition] = useTransition(); const formRef = useRef(null); useEffect(() => { const fetchOrderConfirmationContent = async () => { const res = await fetch('/api/orders/confirmation'); const data = await res.json(); setOrderConfirmationContent(data); setLoading(false); }; // If the order has already been confirmed, don't fetch the content if (order.orderConfirmation) return; fetchOrderConfirmationContent(); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); const handleSubmit: FormEventHandler = (event) => { event.preventDefault(); setLoading(true); const form = formRef.current; if (!form) return; const formData = new FormData(form); startTransition(async () => { await confirmOrder({ order, content: orderConfirmationContent!, formData }); form.reset(); }); }; if (!loading && !orderConfirmationContent) return null; return (
{loading ? ( ) : ( )}
Today's date
Print your name to sign
Credit card holder's electronic signature
); }