|
| 1 | +import { Badge, Flex, Grid, GridItem, H3, Text } from '@bigcommerce/big-design'; |
| 2 | +import { useRouter } from 'next/router'; |
| 3 | +import React from 'react'; |
| 4 | +import styled from 'styled-components'; |
| 5 | + |
| 6 | +import ErrorMessage from '../../../components/error'; |
| 7 | +import { useOrder } from '../../../lib/hooks'; |
| 8 | +import { Order } from '../../../types'; |
| 9 | + |
| 10 | +const StyledAddress = styled.address` |
| 11 | + color: ${({ theme }) => theme.colors.secondary70}; |
| 12 | + font-style: normal; |
| 13 | + line-height: ${({ theme }) => theme.lineHeight.medium}; |
| 14 | +`; |
| 15 | + |
| 16 | +const StyledDl = styled.dl` |
| 17 | + color: ${({ theme }) => theme.colors.secondary70}; |
| 18 | + display: grid; |
| 19 | + grid-template: 'dt dd' auto / 1fr auto; |
| 20 | + grid-auto-rows: auto; |
| 21 | + line-height: ${({ theme }) => theme.lineHeight.medium}; |
| 22 | + margin: 0; |
| 23 | +
|
| 24 | + dt { |
| 25 | + grid-area: 'dt'; |
| 26 | + } |
| 27 | +
|
| 28 | + dd { |
| 29 | + grid-area: 'dd'; |
| 30 | + text-align: right; |
| 31 | + } |
| 32 | +`; |
| 33 | + |
| 34 | +const InternalOrderModalPage = (order: Order) => { |
| 35 | + const { billing_address } = order; |
| 36 | + |
| 37 | + const formatCurrency = (amount: string) => |
| 38 | + new Intl.NumberFormat(order.customer_locale, { style: 'currency', currency: order.currency_code }).format(parseFloat(amount)); |
| 39 | + |
| 40 | + return ( |
| 41 | + <Grid gridColumns="repeat(auto-fill, minmax(16rem, 1fr))" gridGap="3rem"> |
| 42 | + <GridItem> |
| 43 | + <H3>Billing information</H3> |
| 44 | + <StyledAddress> |
| 45 | + <div> |
| 46 | + {billing_address.first_name} {billing_address.last_name} |
| 47 | + </div> |
| 48 | + <div>{billing_address.street_1}</div> |
| 49 | + {billing_address.street_2 && <div>{billing_address.street_2}</div>} |
| 50 | + <div> |
| 51 | + {billing_address.city}, {billing_address.state}, {billing_address.zip} |
| 52 | + </div> |
| 53 | + <div>{billing_address.country}</div> |
| 54 | + </StyledAddress> |
| 55 | + </GridItem> |
| 56 | + <GridItem> |
| 57 | + <Flex |
| 58 | + alignItems="center" |
| 59 | + flexDirection="row" |
| 60 | + flexWrap="nowrap" |
| 61 | + justifyContent="space-between" |
| 62 | + marginBottom="small" |
| 63 | + > |
| 64 | + <H3 marginBottom="none">Payment details</H3> |
| 65 | + <Badge label={order.payment_status} variant="success" /> |
| 66 | + </Flex> |
| 67 | + <StyledDl> |
| 68 | + <dt>Subtotal</dt> |
| 69 | + <dd>{formatCurrency(order.subtotal_ex_tax)}</dd> |
| 70 | + <dt>Discount</dt> |
| 71 | + <dd>-{formatCurrency(order.discount_amount)}</dd> |
| 72 | + <dt>Shipping</dt> |
| 73 | + <dd>{formatCurrency(order.shipping_cost_ex_tax)}</dd> |
| 74 | + <dt>Tax</dt> |
| 75 | + <dd>{formatCurrency(order.total_tax)}</dd> |
| 76 | + <dt> |
| 77 | + <Text as="span" bold marginBottom="none">Grand total</Text> |
| 78 | + </dt> |
| 79 | + <dd> |
| 80 | + <Text as="span" bold marginBottom="none">{formatCurrency(order.total_inc_tax)}</Text> |
| 81 | + </dd> |
| 82 | + </StyledDl> |
| 83 | + </GridItem> |
| 84 | + <GridItem> |
| 85 | + <H3>Order information</H3> |
| 86 | + <StyledDl> |
| 87 | + <dt>ID</dt> |
| 88 | + <dd>{order.id}</dd> |
| 89 | + <dt>Type</dt> |
| 90 | + <dd> |
| 91 | + <span style={{ textTransform: 'capitalize' }}>{order.order_source}</span> |
| 92 | + </dd> |
| 93 | + <dt>Status</dt> |
| 94 | + <dd>{order.status}</dd> |
| 95 | + <dt>Total items</dt> |
| 96 | + <dd> |
| 97 | + {order.items_total > 1 ? `${order.items_total} items` : `${order.items_total} item`} |
| 98 | + </dd> |
| 99 | + </StyledDl> |
| 100 | + </GridItem> |
| 101 | + </Grid> |
| 102 | + ); |
| 103 | +}; |
| 104 | + |
| 105 | +const OrderModalPage = () => { |
| 106 | + const router = useRouter(); |
| 107 | + const { orderId } = router.query; |
| 108 | + const { isLoading, order, error } = useOrder(parseInt(`${orderId}`, 10)); |
| 109 | + |
| 110 | + if (isLoading) { |
| 111 | + return null; |
| 112 | + } |
| 113 | + |
| 114 | + if (error) { |
| 115 | + return <ErrorMessage error={error} renderPanel={false} /> |
| 116 | + } |
| 117 | + |
| 118 | + return <InternalOrderModalPage {...order} />; |
| 119 | +}; |
| 120 | + |
| 121 | +export default OrderModalPage; |
0 commit comments