import {redirect, useLoaderData} from 'react-router'; import type {Route} from './+types/account.orders.$id'; import {Money, Image} from '@shopify/hydrogen'; import type { OrderLineItemFullFragment, OrderQuery, } from 'customer-accountapi.generated'; import {CUSTOMER_ORDER_QUERY} from '~/graphql/customer-account/CustomerOrderQuery'; export const meta: Route.MetaFunction = ({data}) => { return [{title: `Order ${data?.order?.name}`}]; }; export async function loader({params, context}: Route.LoaderArgs) { const {customerAccount} = context; if (!params.id) { return redirect('/account/orders'); } const orderId = atob(params.id); const {data, errors}: {data: OrderQuery; errors?: Array<{message: string}>} = await customerAccount.query(CUSTOMER_ORDER_QUERY, { variables: { orderId, language: customerAccount.i18n.language, }, }); if (errors?.length || !data?.order) { throw new Error('Order not found'); } const {order} = data; // Extract line items directly from nodes array const lineItems = order.lineItems.nodes; // Extract discount applications directly from nodes array const discountApplications = order.discountApplications.nodes; // Get fulfillment status from first fulfillment node const fulfillmentStatus = order.fulfillments.nodes[0]?.status ?? 'N/A'; // Get first discount value with proper type checking const firstDiscount = discountApplications[0]?.value; // Type guard for MoneyV2 discount const discountValue = firstDiscount?.__typename === 'MoneyV2' ? (firstDiscount as Extract< typeof firstDiscount, {__typename: 'MoneyV2'} >) : null; // Type guard for percentage discount const discountPercentage = firstDiscount?.__typename === 'PricingPercentageValue' ? ( firstDiscount as Extract< typeof firstDiscount, {__typename: 'PricingPercentageValue'} > ).percentage : null; return { order, lineItems, discountValue, discountPercentage, fulfillmentStatus, }; } export default function OrderRoute() { const { order, lineItems, discountValue, discountPercentage, fulfillmentStatus, } = useLoaderData(); return (

Order {order.name}

Placed on {new Date(order.processedAt!).toDateString()}

{order.confirmationNumber && (

Confirmation: {order.confirmationNumber}

)}
{lineItems.map((lineItem, lineItemIndex) => ( // eslint-disable-next-line react/no-array-index-key ))} {((discountValue && discountValue.amount) || discountPercentage) && ( )}
Product Price Quantity Total

Discounts

Discounts

{discountPercentage ? ( -{discountPercentage}% OFF ) : ( discountValue && )}

Subtotal

Subtotal

Tax

Tax

Total

Total

Shipping Address

{order?.shippingAddress ? (

{order.shippingAddress.name}

{order.shippingAddress.formatted ? (

{order.shippingAddress.formatted}

) : ( '' )} {order.shippingAddress.formattedArea ? (

{order.shippingAddress.formattedArea}

) : ( '' )}
) : (

No shipping address defined

)}

Status

{fulfillmentStatus}


View Order Status →

); } function OrderLineRow({lineItem}: {lineItem: OrderLineItemFullFragment}) { return (
{lineItem?.image && (
)}

{lineItem.title}

{lineItem.variantTitle}
{lineItem.quantity} ); }