|
| 1 | +import { Button, Flex, H1, Panel, StatefulTable, Text } from '@bigcommerce/big-design'; |
| 2 | +import { useRouter } from 'next/router'; |
| 3 | + |
| 4 | +import ErrorMessage from '../../../components/error'; |
| 5 | +import Loading from '../../../components/loading'; |
| 6 | +import { useShippingAndProductsInfo } from '../../../lib/hooks'; |
| 7 | +import { BillingAddress, OrderProduct, ShippingAndProductsInfo } from '../../../types'; |
| 8 | + |
| 9 | +const InternalOrderPage = (order: ShippingAndProductsInfo) => { |
| 10 | + const { shipping_addresses = [], products = [] } = order |
| 11 | + const items = shipping_addresses.map(address => { |
| 12 | + const addressProducts = products.filter(({ order_address_id }) => order_address_id === address.id); |
| 13 | + |
| 14 | + return { address, addressProducts } |
| 15 | + }); |
| 16 | + |
| 17 | + const Address = (address: BillingAddress) => ( |
| 18 | + <> |
| 19 | + <Text margin='none'>{address.first_name} {address.last_name}</Text> |
| 20 | + <Text margin='none'>{address.street_1} {address.street_2}</Text> |
| 21 | + <Text margin='none'>{address.city}, {address.state} {address.zip}</Text> |
| 22 | + </> |
| 23 | + ); |
| 24 | + |
| 25 | + const renderOrderProducts = (addressProducts: OrderProduct[]) => ( |
| 26 | + <> |
| 27 | + {addressProducts.map(product => <Text key={product.id}>{product.name}</Text>)} |
| 28 | + </> |
| 29 | + ); |
| 30 | + |
| 31 | + const renderOrderProductsQuantities = (addressProducts: OrderProduct[]) => ( |
| 32 | + <> |
| 33 | + {addressProducts.map(product => <Text key={product.id}>{product.quantity}</Text>)} |
| 34 | + </> |
| 35 | + ); |
| 36 | + |
| 37 | + return ( |
| 38 | + <> |
| 39 | + <Flex justifyContent="space-between" marginBottom="medium"> |
| 40 | + <H1>Order details</H1> |
| 41 | + <Button>Create shipment</Button> |
| 42 | + </Flex> |
| 43 | + <Panel> |
| 44 | + <StatefulTable |
| 45 | + columns={[ |
| 46 | + { header: 'Ship to', hash: 'address', render: ({ address }) => <Address {...address} /> }, |
| 47 | + { |
| 48 | + header: 'Products', |
| 49 | + hash: 'addressProducts', |
| 50 | + render: ({ addressProducts }) => renderOrderProducts(addressProducts), |
| 51 | + }, |
| 52 | + { |
| 53 | + header: 'Quantity', |
| 54 | + hash: 'quantity', |
| 55 | + render: ({ addressProducts }) => renderOrderProductsQuantities(addressProducts), |
| 56 | + }, |
| 57 | + ]} |
| 58 | + items={items} |
| 59 | + /> |
| 60 | + </Panel> |
| 61 | + </> |
| 62 | + ); |
| 63 | +}; |
| 64 | + |
| 65 | +const OrderPage = () => { |
| 66 | + const router = useRouter(); |
| 67 | + const { orderId } = router.query; |
| 68 | + const { isLoading, order, error }= useShippingAndProductsInfo(parseInt(`${orderId}`, 10)); |
| 69 | + |
| 70 | + if (isLoading) return <Loading />; |
| 71 | + |
| 72 | + if (error) return <ErrorMessage error={error} />; |
| 73 | + |
| 74 | + return <InternalOrderPage {...order} />; |
| 75 | +}; |
| 76 | + |
| 77 | +export default OrderPage; |
0 commit comments