import React from 'react' import state_lookup from '../state_lookup' import saveAs from 'browser-saveas' import csvStringify from 'csv-stringify' export default class OrderList extends React.Component { constructor(props) { super() this.downloadCsv = this.downloadCsv.bind(this) } downloadCsv() { const orders = this.parseOrders().map((order) => { return [ order.order_id, order.order_date, order.order_checkout_type, order.order_ship_name, order.order_company, order.order_address1, order.order_address2, order.order_city, state_lookup(order.order_state), order.order_zip, order.order_country !== 'United States' ? '' : order.order_country, order.order_email, ] }) csvStringify(orders, (err, csv) => { const blob = new Blob([csv], {type : 'text/plain;charset=utf-8'}) saveAs(blob, this.props.product.product_name.replace(/\s+/g,"") +'.csv') }) } parseOrders() { return this.props.orders.filter( (order) => { // also filter where order.order_status == 3 (paid in full) ? // filter weird blank orders if found return !! order.order_ship_name }).map( (order, i) => { const name = order.order_ship_name .toUpperCase() .replace(/(Sr|Jr|I|II|III|IV)\.?$/i, "") .split(" ") .reverse()[0] return [ name, order ] }).sort((a,b) => { return a[0] < b[0] ? -1 : a[0] === b[0] ? 0 : 1 }).map((pair, i) => { return pair[1] }) } render() { if (! this.props.product) { return (
) } if (! this.props.orders) { return (