import {DateTime} from 'luxon' import Head from 'next/head' import redirect from '~/utils/redirectGetInitialProps' import OrderSummary from '~/components/orderSummary' OrderDetails.getInitialProps = async function({ctx, user}) { const {axios, query: {orderNum}} = ctx; if(!user) return redirect(ctx, 302, '/login') if(!user.email_confirmed) return redirect(ctx, 302, '/account/email/confirm') const {data: orders} = await axios.get(`/api/orders`) const order = orders.find(({number}) => number === parseInt(orderNum, 10)) return {order} } export default function OrderDetails({order}) { const lastTransaction = getLastTransaction(order) return ( <> Order #{order.number} | Society of Socks

Order #{order.number}: Details

Purchased {DateTime.fromISO(lastTransaction.completion_time).toFormat('LLLL dd, h:mm a')}

) } function getLastTransaction(order){ return order.transactions.sort(sortTransactions)[0] } function sortTransactions({completion_time: a}, {completion_time: b}){ const timeA = DateTime.fromISO(a) const timeB = DateTime.fromISO(b) return timeB.diff(timeA).as('seconds') }