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.

38 lines
934 B
JavaScript

import {DateTime} from 'luxon'
CheckoutComplete.getInitialProps = async function({ctx: {query: {session_id}, axios}}){
const {data: orders} = await axios.get('/api/orders')
const mostRecentOrder = orders.sort(sortOrders)[0]
return {order: mostRecentOrder}
}
export default function CheckoutComplete({order}){
return (
<pre>
{JSON.stringify(order, null, 2)}
</pre>
)
}
function parsePaymentTime({payment}){
if(typeof payment.time === 'string')
payment.time = DateTime.fromISO(payment.time)
return payment.time
}
function sortTransactions(a,b){
const timeA = parsePaymentTime(a)
const timeB = parsePaymentTime(b)
return timeB.diff(timeA).as('seconds')
}
function sortOrders(a,b){
const timePaidA = parsePaymentTime(a.transactions.sort(sortTransactions)[0])
const timePaidB = parsePaymentTime(b.transactions.sort(sortTransactions)[0])
return timePaidB.diff(timePaidA).as('seconds')
}