2018-06-07 16:05:22 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __, _n, sprintf } from '@wordpress/i18n';
|
2018-08-01 19:07:17 +00:00
|
|
|
import { Button } from '@wordpress/components';
|
2018-07-23 20:14:40 +00:00
|
|
|
import { Fragment } from '@wordpress/element';
|
2018-11-30 16:12:34 +00:00
|
|
|
import { compose } from '@wordpress/compose';
|
2018-06-07 16:05:22 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2018-07-09 15:46:31 +00:00
|
|
|
import { noop } from 'lodash';
|
2018-11-30 20:45:44 +00:00
|
|
|
import interpolateComponents from 'interpolate-components';
|
2018-06-07 16:05:22 +00:00
|
|
|
|
|
|
|
/**
|
2018-10-30 18:57:48 +00:00
|
|
|
* WooCommerce dependencies
|
2018-06-07 16:05:22 +00:00
|
|
|
*/
|
2018-08-20 21:18:13 +00:00
|
|
|
import {
|
|
|
|
EllipsisMenu,
|
2018-11-30 22:13:05 +00:00
|
|
|
EmptyContent,
|
2018-08-20 21:18:13 +00:00
|
|
|
Flag,
|
2018-11-30 20:45:44 +00:00
|
|
|
Link,
|
2018-08-20 21:18:13 +00:00
|
|
|
MenuTitle,
|
|
|
|
MenuItem,
|
|
|
|
OrderStatus,
|
2018-09-21 15:19:05 +00:00
|
|
|
Section,
|
2018-08-20 21:18:13 +00:00
|
|
|
} from '@woocommerce/components';
|
2018-10-30 18:57:48 +00:00
|
|
|
import { formatCurrency, getCurrencyFormatDecimal } from '@woocommerce/currency';
|
2018-11-30 22:26:20 +00:00
|
|
|
import { getAdminLink } from '@woocommerce/navigation';
|
2018-10-30 18:57:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2018-11-30 21:47:04 +00:00
|
|
|
import { ActivityCard, ActivityCardPlaceholder } from '../activity-card';
|
2018-10-30 18:57:48 +00:00
|
|
|
import ActivityHeader from '../activity-header';
|
|
|
|
import ActivityOutboundLink from '../activity-outbound-link';
|
2018-06-07 16:05:22 +00:00
|
|
|
import { getOrderRefundTotal } from 'lib/order-values';
|
2018-11-30 16:12:34 +00:00
|
|
|
import { QUERY_DEFAULTS } from 'store/constants';
|
2018-12-03 15:59:53 +00:00
|
|
|
import withSelect from 'wc-api/with-select';
|
2018-06-07 16:05:22 +00:00
|
|
|
|
2018-11-30 22:13:05 +00:00
|
|
|
function OrdersPanel( { orders, isRequesting, isError } ) {
|
|
|
|
if ( isError ) {
|
|
|
|
const title = __( 'There was an error getting your orders. Please try again.', 'wc-admin' );
|
|
|
|
const actionLabel = __( 'Reload', 'wc-admin' );
|
|
|
|
const actionCallback = () => {
|
|
|
|
// TODO Add tracking for how often an error is displayed, and the reload action is clicked.
|
|
|
|
window.location.reload();
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<EmptyContent
|
|
|
|
title={ title }
|
|
|
|
actionLabel={ actionLabel }
|
|
|
|
actionURL={ null }
|
|
|
|
actionCallback={ actionCallback }
|
|
|
|
/>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-07-09 15:46:31 +00:00
|
|
|
const menu = (
|
|
|
|
<EllipsisMenu label="Demo Menu">
|
|
|
|
<MenuTitle>Test</MenuTitle>
|
|
|
|
<MenuItem onInvoke={ noop }>Test</MenuItem>
|
|
|
|
</EllipsisMenu>
|
|
|
|
);
|
|
|
|
|
2018-11-30 20:45:44 +00:00
|
|
|
const orderCardTitle = ( order, address ) => {
|
|
|
|
const name = `${ address.first_name } ${ address.last_name }`;
|
|
|
|
|
2018-07-16 16:28:26 +00:00
|
|
|
return (
|
2018-11-30 20:45:44 +00:00
|
|
|
<Fragment>
|
|
|
|
{ interpolateComponents( {
|
|
|
|
mixedString: sprintf(
|
|
|
|
__(
|
|
|
|
/* eslint-disable-next-line max-len */
|
|
|
|
'Order {{orderLink}}#%(orderNumber)s{{/orderLink}} placed by {{customerLink}}%(customerName)s{{/customerLink}} {{destinationFlag/}}',
|
|
|
|
'wc-admin'
|
|
|
|
),
|
|
|
|
{
|
|
|
|
orderNumber: order.number,
|
|
|
|
customerName: name,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
components: {
|
|
|
|
orderLink: <Link href={ 'post.php?action=edit&post=' + order.id } type="wp-admin" />,
|
|
|
|
// @TODO: Hook up customer name link
|
|
|
|
customerLink: <Link href={ '#' } type="wp-admin" />,
|
2019-01-22 02:18:55 +00:00
|
|
|
destinationFlag: <Flag order={ order } round={ false } />,
|
2018-11-30 20:45:44 +00:00
|
|
|
},
|
|
|
|
} ) }
|
|
|
|
</Fragment>
|
2018-07-16 16:28:26 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2018-06-07 16:05:22 +00:00
|
|
|
return (
|
2018-07-09 15:46:31 +00:00
|
|
|
<Fragment>
|
2018-07-10 12:48:06 +00:00
|
|
|
<ActivityHeader title={ __( 'Orders', 'wc-admin' ) } menu={ menu } />
|
2018-07-09 15:46:31 +00:00
|
|
|
<Section>
|
2018-11-30 16:12:34 +00:00
|
|
|
{ isRequesting ? (
|
2018-11-30 21:47:04 +00:00
|
|
|
<ActivityCardPlaceholder
|
|
|
|
className="woocommerce-order-activity-card"
|
|
|
|
hasAction
|
|
|
|
hasDate
|
|
|
|
lines={ 2 }
|
|
|
|
/>
|
2018-07-09 15:46:31 +00:00
|
|
|
) : (
|
2018-07-17 18:51:56 +00:00
|
|
|
<Fragment>
|
2018-11-30 16:12:34 +00:00
|
|
|
{ orders.map( ( order, i ) => {
|
2018-07-17 18:51:56 +00:00
|
|
|
// We want the billing address, but shipping can be used as a fallback.
|
|
|
|
const address = { ...order.shipping, ...order.billing };
|
|
|
|
const productsCount = order.line_items.reduce(
|
|
|
|
( total, line ) => total + line.quantity,
|
|
|
|
0
|
|
|
|
);
|
2018-06-07 16:05:22 +00:00
|
|
|
|
2018-07-17 18:51:56 +00:00
|
|
|
const total = order.total;
|
|
|
|
const refundValue = getOrderRefundTotal( order );
|
|
|
|
const remainingTotal = getCurrencyFormatDecimal( order.total ) + refundValue;
|
2018-06-07 16:05:22 +00:00
|
|
|
|
2018-07-23 20:14:40 +00:00
|
|
|
return (
|
|
|
|
<ActivityCard
|
|
|
|
key={ i }
|
|
|
|
className="woocommerce-order-activity-card"
|
2018-11-30 20:45:44 +00:00
|
|
|
title={ orderCardTitle( order, address ) }
|
2018-07-23 20:14:40 +00:00
|
|
|
date={ order.date_created }
|
|
|
|
subtitle={
|
|
|
|
<div>
|
2018-07-16 13:53:38 +00:00
|
|
|
<span>
|
2018-07-23 20:14:40 +00:00
|
|
|
{ sprintf(
|
|
|
|
_n( '%d product', '%d products', productsCount, 'wc-admin' ),
|
|
|
|
productsCount
|
|
|
|
) }
|
2018-07-16 13:53:38 +00:00
|
|
|
</span>
|
2018-07-23 20:14:40 +00:00
|
|
|
{ refundValue ? (
|
|
|
|
<span>
|
2019-01-17 07:04:57 +00:00
|
|
|
<s>{ formatCurrency( total, order.currency_symbol ) }</s>{' '}
|
|
|
|
{ formatCurrency( remainingTotal, order.currency_symbol ) }
|
2018-07-23 20:14:40 +00:00
|
|
|
</span>
|
|
|
|
) : (
|
2019-01-17 07:04:57 +00:00
|
|
|
<span>{ formatCurrency( total, order.currency_symbol ) }</span>
|
2018-07-23 20:14:40 +00:00
|
|
|
) }
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
actions={
|
2018-11-30 22:26:20 +00:00
|
|
|
<Button
|
|
|
|
isDefault
|
|
|
|
href={ getAdminLink( 'post.php?action=edit&post=' + order.id ) }
|
|
|
|
>
|
2018-11-30 22:31:04 +00:00
|
|
|
{ __( 'Begin fulfillment' ) }
|
2018-07-23 20:14:40 +00:00
|
|
|
</Button>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<OrderStatus order={ order } />
|
|
|
|
</ActivityCard>
|
2018-07-17 18:51:56 +00:00
|
|
|
);
|
|
|
|
} ) }
|
|
|
|
<ActivityOutboundLink href={ 'edit.php?post_type=shop_order' }>
|
|
|
|
{ __( 'Manage all orders' ) }
|
|
|
|
</ActivityOutboundLink>
|
|
|
|
</Fragment>
|
2018-07-09 15:46:31 +00:00
|
|
|
) }
|
|
|
|
</Section>
|
|
|
|
</Fragment>
|
2018-06-07 16:05:22 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-07-09 15:46:31 +00:00
|
|
|
OrdersPanel.propTypes = {
|
2018-11-30 16:12:34 +00:00
|
|
|
orders: PropTypes.array.isRequired,
|
|
|
|
isError: PropTypes.bool,
|
|
|
|
isRequesting: PropTypes.bool,
|
2018-06-07 16:05:22 +00:00
|
|
|
};
|
|
|
|
|
2018-08-07 20:11:30 +00:00
|
|
|
OrdersPanel.defaultProps = {
|
2018-11-30 16:12:34 +00:00
|
|
|
orders: [],
|
|
|
|
isError: false,
|
|
|
|
isRequesting: false,
|
2018-08-07 20:11:30 +00:00
|
|
|
};
|
|
|
|
|
2018-11-30 16:12:34 +00:00
|
|
|
export default compose(
|
|
|
|
withSelect( select => {
|
2018-12-15 12:38:54 +00:00
|
|
|
const { getOrders, getOrdersError, isGetOrdersRequesting } = select( 'wc-api' );
|
2018-11-30 16:12:34 +00:00
|
|
|
const ordersQuery = {
|
|
|
|
page: 1,
|
|
|
|
per_page: QUERY_DEFAULTS.pageSize,
|
2018-11-30 22:17:58 +00:00
|
|
|
status: 'processing',
|
2018-11-30 16:12:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const orders = getOrders( ordersQuery );
|
2018-12-15 12:38:54 +00:00
|
|
|
const isError = Boolean( getOrdersError( ordersQuery ) );
|
2018-11-30 16:12:34 +00:00
|
|
|
const isRequesting = isGetOrdersRequesting( ordersQuery );
|
|
|
|
|
|
|
|
return { orders, isError, isRequesting };
|
|
|
|
} )
|
|
|
|
)( OrdersPanel );
|