import Image from 'next/image';
import { Dialog, DialogBackdrop, DialogPanel } from '@headlessui/react';
import Text from 'components/ui/text';
import { Input } from 'components/ui/input';
import { InputLabel } from 'components/ui/input-label';
import Skeleton from 'components/ui/skeleton';
import { toPrintDate } from 'lib/utils';
import Label from 'components/ui/label';
import Heading from 'components/ui/heading';
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 } from 'components/ui';
function OrderConfirmationDetails({
content,
order
}: {
content: OrderConfirmationContent;
order: Order;
}) {
return (
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}
|
|
))}
Shipping
{order.shippingMethod.price.amount !== '0.0' ? (
) : (
Free
)}
{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();
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 (
);
}