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.
79 lines
2.2 KiB
JavaScript
79 lines
2.2 KiB
JavaScript
5 years ago
|
import React from 'react'
|
||
|
import Router from 'next/router'
|
||
|
import ActionBar from '~/components/admin/actionBar'
|
||
|
import Table from '~/components/table'
|
||
|
import {DateTime} from 'luxon'
|
||
|
|
||
|
Orders.getInitialProps = async ({ctx}) => {
|
||
|
const {data: orders} = await ctx.axios.get('/api/orders/all')
|
||
|
return {orders}
|
||
|
}
|
||
|
|
||
|
export default function Orders({orders}){
|
||
|
return (
|
||
|
<>
|
||
|
<ActionBar title="Orders"/>
|
||
|
<Table
|
||
|
columns={[
|
||
|
{name: 'Purchased', extractor: getPurchaseTime},
|
||
|
{name: 'Items', extractor: getNumberItems},
|
||
|
{name: 'Price', extractor: getItemPrice},
|
||
|
{name: 'Shipping', extractor: getShippingEstimate},
|
||
|
{name: 'Amount Paid', extractor: getAmountPaid},
|
||
|
{name: '', extractor: row =>
|
||
|
<button className="buttonLink" onClick={() => Router.push(`/admin/orders/${row.id}`)}>Details</button>
|
||
|
}
|
||
|
]}
|
||
|
rows={orders.map(order => ({id: order.uuid, ...order}))}
|
||
|
/>
|
||
|
</>
|
||
|
)
|
||
|
}
|
||
|
|
||
|
function getPurchaseTime(order){
|
||
|
const mostRecentTransaction = order.transactions.sort(sortTransactions)[0]
|
||
|
const time = parsePaymentTime(mostRecentTransaction)
|
||
|
|
||
|
return time.setZone('local').toFormat('LLLL dd, h:mm a')
|
||
|
}
|
||
|
|
||
|
function getNumberItems(order){
|
||
|
return order.transactions.map(transaction =>
|
||
|
transaction.cart.items.map(item => item.count)
|
||
|
).reduce((a,b)=>(a+b))
|
||
|
}
|
||
|
|
||
|
function getItemPrice(order){
|
||
|
return formatMoney(order.transactions.map(transaction => transaction.item_total_price)
|
||
|
.reduce((a,b)=>(a+b)))
|
||
|
}
|
||
|
|
||
|
function getShippingEstimate(order){
|
||
|
return formatMoney(order.transactions.map(transaction => transaction.shipping_price)
|
||
|
.reduce((a,b)=>(a+b)))
|
||
|
}
|
||
|
|
||
|
function getAmountPaid(order){
|
||
|
return formatMoney(order.transactions.map(({payment}) => payment.value_cents)
|
||
|
.reduce((a,b)=>(a+b)))
|
||
|
}
|
||
|
|
||
|
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')
|
||
|
}
|
||
|
|
||
|
const formatMoney = money => {
|
||
|
if (money === undefined || money === null) return null;
|
||
|
|
||
|
return '$' + (money / 100).toFixed(2)
|
||
|
}
|