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';
|
2019-01-30 07:22:10 +00:00
|
|
|
import { QUERY_DEFAULTS } from 'wc-api/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 = () => {
|
2019-02-06 06:41:53 +00:00
|
|
|
// @todo Add tracking for how often an error is displayed, and the reload action is clicked.
|
2018-11-30 22:13:05 +00:00
|
|
|
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" />,
|
2019-02-06 06:41:53 +00:00
|
|
|
// @todo Hook up customer name link
|
2018-11-30 20:45:44 +00:00
|
|
|
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
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-02-01 10:01:42 +00:00
|
|
|
const cards = [];
|
|
|
|
orders.forEach( ( order, id ) => {
|
|
|
|
// 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 );
|
|
|
|
|
|
|
|
const total = order.total;
|
|
|
|
const refundValue = getOrderRefundTotal( order );
|
|
|
|
const remainingTotal = getCurrencyFormatDecimal( order.total ) + refundValue;
|
|
|
|
|
|
|
|
cards.push(
|
|
|
|
<ActivityCard
|
|
|
|
key={ id }
|
|
|
|
className="woocommerce-order-activity-card"
|
|
|
|
title={ orderCardTitle( order, address ) }
|
|
|
|
date={ order.date_created }
|
|
|
|
subtitle={
|
|
|
|
<div>
|
|
|
|
<span>
|
|
|
|
{ sprintf(
|
|
|
|
_n( '%d product', '%d products', productsCount, 'wc-admin' ),
|
|
|
|
productsCount
|
|
|
|
) }
|
|
|
|
</span>
|
|
|
|
{ refundValue ? (
|
|
|
|
<span>
|
|
|
|
<s>{ formatCurrency( total, order.currency_symbol ) }</s>{' '}
|
|
|
|
{ formatCurrency( remainingTotal, order.currency_symbol ) }
|
|
|
|
</span>
|
|
|
|
) : (
|
|
|
|
<span>{ formatCurrency( total, order.currency_symbol ) }</span>
|
|
|
|
) }
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
actions={
|
|
|
|
<Button isDefault href={ getAdminLink( 'post.php?action=edit&post=' + order.id ) }>
|
|
|
|
{ __( 'Begin fulfillment' ) }
|
|
|
|
</Button>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<OrderStatus order={ order } />
|
|
|
|
</ActivityCard>
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
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>
|
2019-02-01 10:01:42 +00:00
|
|
|
{ cards }
|
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 = {
|
2019-02-01 10:01:42 +00:00
|
|
|
orders: PropTypes.instanceOf( Map ).isRequired,
|
2018-11-30 16:12:34 +00:00
|
|
|
isError: PropTypes.bool,
|
|
|
|
isRequesting: PropTypes.bool,
|
2018-06-07 16:05:22 +00:00
|
|
|
};
|
|
|
|
|
2018-08-07 20:11:30 +00:00
|
|
|
OrdersPanel.defaultProps = {
|
2019-02-01 10:01:42 +00:00
|
|
|
orders: new Map(),
|
2018-11-30 16:12:34 +00:00
|
|
|
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 => {
|
2019-02-01 10:01:42 +00:00
|
|
|
const { getItems, getItemsError, isGetItemsRequesting } = 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
|
|
|
};
|
|
|
|
|
2019-02-01 10:01:42 +00:00
|
|
|
const orders = getItems( 'orders', ordersQuery );
|
|
|
|
const isError = Boolean( getItemsError( 'orders', ordersQuery ) );
|
|
|
|
const isRequesting = isGetItemsRequesting( 'orders', ordersQuery );
|
2018-11-30 16:12:34 +00:00
|
|
|
|
|
|
|
return { orders, isError, isRequesting };
|
|
|
|
} )
|
|
|
|
)( OrdersPanel );
|