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';
|
2019-02-20 01:51:43 +00:00
|
|
|
import { getAdminLink, getNewPath } 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';
|
2019-03-22 08:48:20 +00:00
|
|
|
import { DEFAULT_ACTIONABLE_STATUSES, 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
|
|
|
|
2019-03-15 01:26:16 +00:00
|
|
|
function OrdersPanel( { orders, isRequesting, isError, orderStatuses } ) {
|
2018-11-30 22:13:05 +00:00
|
|
|
if ( isError ) {
|
2019-03-15 01:26:16 +00:00
|
|
|
if ( ! orderStatuses.length ) {
|
|
|
|
return (
|
|
|
|
<EmptyContent
|
|
|
|
title={ __(
|
|
|
|
"You currently don't have any actionable statuses. " +
|
|
|
|
'To display orders here, select orders that require further review in settings.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
actionLabel={ __( 'Settings', 'woocommerce-admin' ) }
|
|
|
|
actionURL={ getAdminLink( 'admin.php?page=wc-admin#/analytics/settings' ) }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-13 17:14:02 +00:00
|
|
|
const title = __(
|
|
|
|
'There was an error getting your orders. Please try again.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
);
|
|
|
|
const actionLabel = __( 'Reload', 'woocommerce-admin' );
|
2018-11-30 22:13:05 +00:00
|
|
|
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>
|
|
|
|
);
|
|
|
|
|
2019-02-20 01:51:43 +00:00
|
|
|
const getCustomerString = order => {
|
|
|
|
const extended_info = order.extended_info || {};
|
|
|
|
const { first_name, last_name } = extended_info.customer || {};
|
|
|
|
|
|
|
|
if ( ! first_name && ! last_name ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
const name = [ first_name, last_name ].join( ' ' );
|
|
|
|
return sprintf(
|
|
|
|
__(
|
|
|
|
/* translators: describes who placed an order, e.g. Order #123 placed by John Doe */
|
|
|
|
'placed by {{customerLink}}%(customerName)s{{/customerLink}}',
|
2019-03-13 17:14:02 +00:00
|
|
|
'woocommerce-admin'
|
2019-02-20 01:51:43 +00:00
|
|
|
),
|
|
|
|
{
|
|
|
|
customerName: name,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const orderCardTitle = order => {
|
|
|
|
const { extended_info, order_id } = order;
|
|
|
|
const { customer } = extended_info || {};
|
|
|
|
const customerUrl = customer.customer_id
|
|
|
|
? getNewPath( {}, '/analytics/customers', {
|
|
|
|
filter: 'single_customer',
|
2019-03-18 02:39:54 +00:00
|
|
|
customers: customer.customer_id,
|
2019-02-20 01:51:43 +00:00
|
|
|
} )
|
|
|
|
: null;
|
2018-11-30 20:45:44 +00:00
|
|
|
|
2018-07-16 16:28:26 +00:00
|
|
|
return (
|
2018-11-30 20:45:44 +00:00
|
|
|
<Fragment>
|
|
|
|
{ interpolateComponents( {
|
|
|
|
mixedString: sprintf(
|
|
|
|
__(
|
2019-02-20 01:51:43 +00:00
|
|
|
'Order {{orderLink}}#%(orderNumber)s{{/orderLink}} %(customerString)s {{destinationFlag/}}',
|
2019-03-13 17:14:02 +00:00
|
|
|
'woocommerce-admin'
|
2018-11-30 20:45:44 +00:00
|
|
|
),
|
|
|
|
{
|
2019-02-20 01:51:43 +00:00
|
|
|
orderNumber: order_id,
|
|
|
|
customerString: getCustomerString( order ),
|
2018-11-30 20:45:44 +00:00
|
|
|
}
|
|
|
|
),
|
|
|
|
components: {
|
2019-02-20 01:51:43 +00:00
|
|
|
orderLink: <Link href={ 'post.php?action=edit&post=' + order_id } type="wp-admin" />,
|
|
|
|
destinationFlag: customer.country ? (
|
|
|
|
<Flag code={ customer.country } round={ false } />
|
|
|
|
) : null,
|
|
|
|
customerLink: customerUrl ? <Link href={ customerUrl } type="wc-admin" /> : <span />,
|
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 = [];
|
2019-02-20 01:51:43 +00:00
|
|
|
orders.forEach( order => {
|
|
|
|
const extended_info = order.extended_info || {};
|
|
|
|
const productsCount =
|
|
|
|
extended_info && extended_info.products ? extended_info.products.length : 0;
|
2019-02-01 10:01:42 +00:00
|
|
|
|
2019-02-20 01:51:43 +00:00
|
|
|
const total = order.gross_total;
|
|
|
|
const refundValue = order.refund_total;
|
|
|
|
const remainingTotal = getCurrencyFormatDecimal( total ) + refundValue;
|
2019-02-01 10:01:42 +00:00
|
|
|
|
|
|
|
cards.push(
|
|
|
|
<ActivityCard
|
2019-02-20 01:51:43 +00:00
|
|
|
key={ order.order_id }
|
2019-02-01 10:01:42 +00:00
|
|
|
className="woocommerce-order-activity-card"
|
2019-02-20 01:51:43 +00:00
|
|
|
title={ orderCardTitle( order ) }
|
2019-04-09 07:50:00 +00:00
|
|
|
date={ order.date_created_gmt }
|
2019-02-01 10:01:42 +00:00
|
|
|
subtitle={
|
|
|
|
<div>
|
|
|
|
<span>
|
|
|
|
{ sprintf(
|
2019-03-13 17:14:02 +00:00
|
|
|
_n( '%d product', '%d products', productsCount, 'woocommerce-admin' ),
|
2019-02-01 10:01:42 +00:00
|
|
|
productsCount
|
|
|
|
) }
|
|
|
|
</span>
|
|
|
|
{ refundValue ? (
|
|
|
|
<span>
|
2019-02-20 01:51:43 +00:00
|
|
|
<s>{ formatCurrency( total ) }</s> { formatCurrency( remainingTotal ) }
|
2019-02-01 10:01:42 +00:00
|
|
|
</span>
|
|
|
|
) : (
|
2019-02-20 01:51:43 +00:00
|
|
|
<span>{ formatCurrency( total ) }</span>
|
2019-02-01 10:01:42 +00:00
|
|
|
) }
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
actions={
|
2019-02-20 01:51:43 +00:00
|
|
|
<Button isDefault href={ getAdminLink( 'post.php?action=edit&post=' + order.order_id ) }>
|
2019-02-01 10:01:42 +00:00
|
|
|
{ __( '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>
|
2019-03-13 17:14:02 +00:00
|
|
|
<ActivityHeader title={ __( 'Orders', 'woocommerce-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-20 01:51:43 +00:00
|
|
|
orders: PropTypes.array.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-20 01:51:43 +00:00
|
|
|
orders: [],
|
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(
|
2019-03-19 10:57:11 +00:00
|
|
|
withSelect( ( select, props ) => {
|
2019-02-20 01:51:43 +00:00
|
|
|
const { getReportItems, getReportItemsError, isReportItemsRequesting } = select( 'wc-api' );
|
2019-03-19 10:57:11 +00:00
|
|
|
const { isEmpty } = props;
|
2019-03-22 08:48:20 +00:00
|
|
|
const orderStatuses =
|
|
|
|
wcSettings.wcAdminSettings.woocommerce_actionable_order_statuses ||
|
|
|
|
DEFAULT_ACTIONABLE_STATUSES;
|
2019-03-19 10:57:11 +00:00
|
|
|
|
|
|
|
if ( ! orderStatuses.length ) {
|
|
|
|
return { orders: [], isError: true, isRequesting: false, orderStatuses };
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isEmpty ) {
|
|
|
|
return { orders: [], isError: false, isRequesting: false, orderStatuses };
|
|
|
|
}
|
|
|
|
|
2018-11-30 16:12:34 +00:00
|
|
|
const ordersQuery = {
|
|
|
|
page: 1,
|
|
|
|
per_page: QUERY_DEFAULTS.pageSize,
|
2019-03-06 05:14:38 +00:00
|
|
|
status_is: orderStatuses,
|
2019-02-20 01:51:43 +00:00
|
|
|
extended_info: true,
|
2018-11-30 16:12:34 +00:00
|
|
|
};
|
|
|
|
|
2019-02-20 01:51:43 +00:00
|
|
|
const orders = getReportItems( 'orders', ordersQuery ).data;
|
|
|
|
const isError = Boolean( getReportItemsError( 'orders', ordersQuery ) );
|
|
|
|
const isRequesting = isReportItemsRequesting( 'orders', ordersQuery );
|
2018-11-30 16:12:34 +00:00
|
|
|
|
2019-03-15 01:26:16 +00:00
|
|
|
return { orders, isError, isRequesting, orderStatuses };
|
2018-11-30 16:12:34 +00:00
|
|
|
} )
|
|
|
|
)( OrdersPanel );
|