|
| 1 | +import React, { Component, PropTypes } from 'react'; |
| 2 | +import { Link } from 'react-router'; |
| 3 | +import { connect } from 'react-redux'; |
| 4 | +import { get, find, keyBy } from 'lodash'; |
| 5 | +import { Button } from 'reactstrap'; |
| 6 | + |
| 7 | +import { fetchList, getMap, getMany } from '../../store/api'; |
| 8 | +import { withResourceList } from '../../hocs'; |
| 9 | +import { ListHeader, ListTable } from '../UI'; |
| 10 | + |
| 11 | +const formatDate = date => (new Date(date)).toLocaleString(); |
| 12 | + |
| 13 | +export class OrderList extends Component { |
| 14 | + componentWillMount() { |
| 15 | + const {resourceList} = this.props; |
| 16 | + this.props.fetchResourceList({sort: '-orderDate', ...resourceList.params}); |
| 17 | + } |
| 18 | + |
| 19 | +render() { |
| 20 | + const { resourceList, onFilter, categories } = this.props; |
| 21 | + const columns = [ |
| 22 | + { |
| 23 | + attribute: 'order_date', |
| 24 | + header: 'Order date', |
| 25 | + minWidth: '50px', |
| 26 | + rowRender: order => formatDate(order.orderDate), |
| 27 | + }, |
| 28 | + { |
| 29 | + attribute: 'shippedDate', |
| 30 | + header: 'Shipped date', |
| 31 | + rowRender: order => formatDate(order.shippedDate), |
| 32 | + sortable: true, |
| 33 | + }, |
| 34 | + { |
| 35 | + attribute: 'shipAddress', |
| 36 | + header: 'Ship address', |
| 37 | + rowRender: order => order.shipAddress, |
| 38 | + sortable: true, |
| 39 | + }, |
| 40 | + { |
| 41 | + attribute: 'shipCity', |
| 42 | + header: 'Ship city', |
| 43 | + rowRender: order => order.shipCity, |
| 44 | + sortable: true, |
| 45 | + }, |
| 46 | + { |
| 47 | + attribute: 'shipRegion', |
| 48 | + header: 'Ship region', |
| 49 | + rowRender: order => order.shipRegion, |
| 50 | + sortable: true, |
| 51 | + }, |
| 52 | + ]; |
| 53 | + |
| 54 | + return ( |
| 55 | + <div> |
| 56 | + <Button tag={Link} to={'/orders/new'}>New Order</Button> |
| 57 | + <ListTable {...this.props} columns={columns} /> |
| 58 | + </div> |
| 59 | + ); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +export const mapStateToProps = state => ({ |
| 64 | + ordersById: getMap(state, 'orders'), |
| 65 | + orders: getMany(state, 'orders'), |
| 66 | +}); |
| 67 | + |
| 68 | +export const mapDispatchToProps = dispatch => ({ |
| 69 | + fetchOrders: () => dispatch(fetchList('orders', { page: { size: 999 } })), |
| 70 | +}); |
| 71 | + |
| 72 | +export default connect(mapStateToProps, mapDispatchToProps)( |
| 73 | + withResourceList('orders')(OrderList), |
| 74 | +); |
0 commit comments