import Image from 'next/image'; import { Dialog, DialogBackdrop, DialogPanel, DialogTitle } 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, fetchOrderConfirmationContent } from 'components/orders/actions'; import { Button, Heading, Text, Label, Input } from 'components/ui'; import LoadingDots from 'components/loading-dots'; 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 && 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 [submitting, startTransition] = useTransition(); const formRef = useRef(null); useEffect(() => { // If the order has already been confirmed, don't fetch the content if (order.orderConfirmation) return; if (!isOpen) return; (async () => { const data = await fetchOrderConfirmationContent(); setOrderConfirmationContent(data); setLoading(false); })(); }, [isOpen, order.orderConfirmation]); const handleSubmit: FormEventHandler = (event) => { event.preventDefault(); const form = formRef.current; if (!form) return; startTransition(async () => { const formData = new FormData(form); await confirmOrder({ order, content: orderConfirmationContent!, formData }); }); }; if (!loading && !orderConfirmationContent) return null; return (
Confirm Order {loading ? ( ) : ( )}
); }