You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.2 KiB
JavaScript

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 (
<>
<Head>
<title>Order #{order.number} | Society of Socks</title>
</Head>
<h2>Order #{order.number}: Details</h2>
<h3>Purchased {DateTime.fromISO(lastTransaction.completion_time).toFormat('LLLL dd, h:mm a')}</h3>
<OrderSummary order={order} isAdmin={false} />
</>
)
}
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')
}