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.
43 lines
1.1 KiB
JavaScript
43 lines
1.1 KiB
JavaScript
4 years ago
|
import {DateTime} from 'luxon'
|
||
|
|
||
|
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 (
|
||
|
<>
|
||
|
<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')
|
||
|
}
|