2020-11-06 01:21:05 +00:00
|
|
|
|
/**
|
|
|
|
|
* External dependencies
|
|
|
|
|
*/
|
|
|
|
|
import { __, _n, sprintf } from '@wordpress/i18n';
|
2022-06-01 17:15:09 +00:00
|
|
|
|
import { useMemo, useContext } from '@wordpress/element';
|
2021-07-23 12:47:23 +00:00
|
|
|
|
import { useSelect } from '@wordpress/data';
|
2020-11-06 01:21:05 +00:00
|
|
|
|
import PropTypes from 'prop-types';
|
2022-02-21 02:34:25 +00:00
|
|
|
|
import interpolateComponents from '@automattic/interpolate-components';
|
2020-11-06 01:21:05 +00:00
|
|
|
|
import {
|
|
|
|
|
EmptyContent,
|
|
|
|
|
Flag,
|
|
|
|
|
H,
|
|
|
|
|
Link,
|
|
|
|
|
OrderStatus,
|
|
|
|
|
Section,
|
|
|
|
|
} from '@woocommerce/components';
|
|
|
|
|
import { getNewPath } from '@woocommerce/navigation';
|
2022-01-06 12:53:30 +00:00
|
|
|
|
import { getAdminLink } from '@woocommerce/settings';
|
2022-06-01 17:15:09 +00:00
|
|
|
|
import { ORDERS_STORE_NAME, ITEMS_STORE_NAME } from '@woocommerce/data';
|
2020-11-06 01:21:05 +00:00
|
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Internal dependencies
|
|
|
|
|
*/
|
|
|
|
|
import {
|
|
|
|
|
ActivityCard,
|
|
|
|
|
ActivityCardPlaceholder,
|
2021-12-14 16:56:42 +00:00
|
|
|
|
} from '~/activity-panel/activity-card';
|
2022-01-06 12:53:30 +00:00
|
|
|
|
import { getAdminSetting } from '~/utils/admin-settings';
|
2022-06-01 17:15:09 +00:00
|
|
|
|
import { getCountryCode } from '~/dashboard/utils';
|
|
|
|
|
import { CurrencyContext } from '~/lib/currency-context';
|
2020-11-06 01:21:05 +00:00
|
|
|
|
import './style.scss';
|
|
|
|
|
|
2021-07-23 12:47:23 +00:00
|
|
|
|
function recordOrderEvent( eventName ) {
|
|
|
|
|
recordEvent( `activity_panel_orders_${ eventName }`, {} );
|
|
|
|
|
}
|
2020-11-06 01:21:05 +00:00
|
|
|
|
|
2021-07-23 12:47:23 +00:00
|
|
|
|
const renderEmptyCard = () => {
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<ActivityCard
|
|
|
|
|
className="woocommerce-empty-activity-card"
|
|
|
|
|
title=""
|
|
|
|
|
icon=""
|
|
|
|
|
>
|
|
|
|
|
<span
|
|
|
|
|
className="woocommerce-order-empty__success-icon"
|
|
|
|
|
role="img"
|
|
|
|
|
aria-labelledby="woocommerce-order-empty-message"
|
2020-11-06 01:21:05 +00:00
|
|
|
|
>
|
2021-07-23 12:47:23 +00:00
|
|
|
|
🎉
|
|
|
|
|
</span>
|
|
|
|
|
<H id="woocommerce-order-empty-message">
|
2022-03-30 09:00:04 +00:00
|
|
|
|
{ __( 'You’ve fulfilled all your orders', 'woocommerce' ) }
|
2021-07-23 12:47:23 +00:00
|
|
|
|
</H>
|
|
|
|
|
</ActivityCard>
|
|
|
|
|
<Link
|
|
|
|
|
href={ 'edit.php?post_type=shop_order' }
|
|
|
|
|
onClick={ () => recordOrderEvent( 'orders_manage' ) }
|
|
|
|
|
className="woocommerce-layout__activity-panel-outbound-link woocommerce-layout__activity-panel-empty"
|
|
|
|
|
type="wp-admin"
|
|
|
|
|
>
|
2022-03-30 09:00:04 +00:00
|
|
|
|
{ __( 'Manage all orders', 'woocommerce' ) }
|
2021-07-23 12:47:23 +00:00
|
|
|
|
</Link>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2022-06-01 17:15:09 +00:00
|
|
|
|
function renderOrders( orders, customers, getFormattedOrderTotal ) {
|
2021-07-23 12:47:23 +00:00
|
|
|
|
if ( orders.length === 0 ) {
|
|
|
|
|
return renderEmptyCard();
|
2020-11-06 01:21:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-01 17:15:09 +00:00
|
|
|
|
const getCustomerString = ( customer ) => {
|
|
|
|
|
const { name } = customer || {};
|
2020-11-06 01:21:05 +00:00
|
|
|
|
|
2022-06-01 17:15:09 +00:00
|
|
|
|
if ( ! name ) {
|
2021-07-23 12:47:23 +00:00
|
|
|
|
return '';
|
2020-11-06 01:21:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-23 12:47:23 +00:00
|
|
|
|
return `{{customerLink}}${ name }{{/customerLink}}`;
|
|
|
|
|
};
|
2020-11-06 01:21:05 +00:00
|
|
|
|
|
2021-07-23 12:47:23 +00:00
|
|
|
|
const orderCardTitle = ( order ) => {
|
2022-06-01 17:15:09 +00:00
|
|
|
|
const {
|
|
|
|
|
id: orderId,
|
|
|
|
|
number: orderNumber,
|
|
|
|
|
customer_id: customerId,
|
|
|
|
|
} = order;
|
|
|
|
|
const customer =
|
|
|
|
|
customers.find( ( c ) => c.user_id === customerId ) || {};
|
2021-07-23 12:47:23 +00:00
|
|
|
|
let customerUrl = null;
|
2022-06-01 17:15:09 +00:00
|
|
|
|
if ( customer && customer.id ) {
|
2021-07-23 12:47:23 +00:00
|
|
|
|
customerUrl = window.wcAdminFeatures.analytics
|
2020-11-06 01:21:05 +00:00
|
|
|
|
? getNewPath( {}, '/analytics/customers', {
|
|
|
|
|
filter: 'single_customer',
|
2022-06-01 17:15:09 +00:00
|
|
|
|
customers: customer.id,
|
2020-11-06 01:21:05 +00:00
|
|
|
|
} )
|
2022-06-01 17:15:09 +00:00
|
|
|
|
: getAdminLink( 'user-edit.php?user_id=' + customer.id );
|
2021-07-23 12:47:23 +00:00
|
|
|
|
}
|
2020-11-06 01:21:05 +00:00
|
|
|
|
|
2021-07-23 12:47:23 +00:00
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
{ interpolateComponents( {
|
|
|
|
|
mixedString: sprintf(
|
|
|
|
|
__(
|
|
|
|
|
'{{orderLink}}Order #%(orderNumber)s{{/orderLink}} %(customerString)s',
|
2022-03-30 09:00:04 +00:00
|
|
|
|
'woocommerce'
|
2020-11-06 01:21:05 +00:00
|
|
|
|
),
|
2021-07-23 12:47:23 +00:00
|
|
|
|
{
|
|
|
|
|
orderNumber,
|
2022-06-01 17:15:09 +00:00
|
|
|
|
customerString: getCustomerString( customer ),
|
2021-07-23 12:47:23 +00:00
|
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
components: {
|
|
|
|
|
orderLink: (
|
|
|
|
|
<Link
|
|
|
|
|
href={ getAdminLink(
|
|
|
|
|
'post.php?action=edit&post=' + orderId
|
|
|
|
|
) }
|
|
|
|
|
onClick={ () =>
|
|
|
|
|
recordOrderEvent( 'order_number' )
|
|
|
|
|
}
|
|
|
|
|
type="wp-admin"
|
|
|
|
|
/>
|
|
|
|
|
),
|
|
|
|
|
destinationFlag:
|
|
|
|
|
customer && customer.country ? (
|
2020-11-06 01:21:05 +00:00
|
|
|
|
<Flag
|
2021-07-23 12:47:23 +00:00
|
|
|
|
code={ customer && customer.country }
|
2020-11-06 01:21:05 +00:00
|
|
|
|
round={ false }
|
|
|
|
|
/>
|
|
|
|
|
) : null,
|
2021-07-23 12:47:23 +00:00
|
|
|
|
customerLink: customerUrl ? (
|
|
|
|
|
<Link
|
|
|
|
|
href={ customerUrl }
|
|
|
|
|
onClick={ () =>
|
|
|
|
|
recordOrderEvent( 'customer_name' )
|
|
|
|
|
}
|
|
|
|
|
type="wc-admin"
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<span />
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
} ) }
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|
2020-11-06 01:21:05 +00:00
|
|
|
|
|
2021-07-23 12:47:23 +00:00
|
|
|
|
const cards = [];
|
|
|
|
|
orders.forEach( ( order ) => {
|
|
|
|
|
const {
|
|
|
|
|
date_created_gmt: dateCreatedGmt,
|
2022-06-01 17:15:09 +00:00
|
|
|
|
line_items: lineItems,
|
2021-07-23 12:47:23 +00:00
|
|
|
|
id: orderId,
|
|
|
|
|
} = order;
|
2022-06-01 17:15:09 +00:00
|
|
|
|
const productsCount = lineItems ? lineItems.length : 0;
|
2020-11-06 01:21:05 +00:00
|
|
|
|
|
2021-07-23 12:47:23 +00:00
|
|
|
|
cards.push(
|
|
|
|
|
<ActivityCard
|
|
|
|
|
key={ orderId }
|
|
|
|
|
className="woocommerce-order-activity-card"
|
|
|
|
|
title={ orderCardTitle( order ) }
|
|
|
|
|
date={ dateCreatedGmt }
|
|
|
|
|
onClick={ ( { target } ) => {
|
|
|
|
|
recordOrderEvent( 'orders_begin_fulfillment' );
|
|
|
|
|
if ( ! target.href ) {
|
|
|
|
|
window.location.href = getAdminLink(
|
|
|
|
|
`post.php?action=edit&post=${ orderId }`
|
|
|
|
|
);
|
2020-11-06 01:21:05 +00:00
|
|
|
|
}
|
2021-07-23 12:47:23 +00:00
|
|
|
|
} }
|
|
|
|
|
subtitle={
|
|
|
|
|
<div>
|
|
|
|
|
<span>
|
|
|
|
|
{ sprintf(
|
|
|
|
|
_n(
|
|
|
|
|
'%d product',
|
|
|
|
|
'%d products',
|
|
|
|
|
productsCount,
|
2022-03-30 09:00:04 +00:00
|
|
|
|
'woocommerce'
|
2021-07-23 12:47:23 +00:00
|
|
|
|
),
|
|
|
|
|
productsCount
|
|
|
|
|
) }
|
|
|
|
|
</span>
|
2022-06-01 17:15:09 +00:00
|
|
|
|
<span>
|
|
|
|
|
{ getFormattedOrderTotal(
|
|
|
|
|
order.total,
|
|
|
|
|
order.currency
|
|
|
|
|
) }
|
|
|
|
|
</span>
|
2021-07-23 12:47:23 +00:00
|
|
|
|
</div>
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
<OrderStatus
|
|
|
|
|
order={ order }
|
2022-01-06 12:53:30 +00:00
|
|
|
|
orderStatusMap={ getAdminSetting( 'orderStatuses', {} ) }
|
2021-07-23 12:47:23 +00:00
|
|
|
|
/>
|
|
|
|
|
</ActivityCard>
|
2020-11-06 01:21:05 +00:00
|
|
|
|
);
|
2021-07-23 12:47:23 +00:00
|
|
|
|
} );
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
{ cards }
|
|
|
|
|
<Link
|
|
|
|
|
href={ 'edit.php?post_type=shop_order' }
|
|
|
|
|
className="woocommerce-layout__activity-panel-outbound-link"
|
|
|
|
|
onClick={ () => recordOrderEvent( 'orders_manage' ) }
|
|
|
|
|
type="wp-admin"
|
|
|
|
|
>
|
2022-03-30 09:00:04 +00:00
|
|
|
|
{ __( 'Manage all orders', 'woocommerce' ) }
|
2021-07-23 12:47:23 +00:00
|
|
|
|
</Link>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-11-06 01:21:05 +00:00
|
|
|
|
|
2022-01-19 16:45:17 +00:00
|
|
|
|
function OrdersPanel( { unreadOrdersCount, orderStatuses } ) {
|
2021-07-23 12:47:23 +00:00
|
|
|
|
const actionableOrdersQuery = useMemo(
|
|
|
|
|
() => ( {
|
|
|
|
|
page: 1,
|
|
|
|
|
per_page: 5,
|
|
|
|
|
status: orderStatuses,
|
|
|
|
|
_fields: [
|
|
|
|
|
'id',
|
|
|
|
|
'number',
|
2022-06-01 17:15:09 +00:00
|
|
|
|
'currency',
|
2021-07-23 12:47:23 +00:00
|
|
|
|
'status',
|
2022-06-01 17:15:09 +00:00
|
|
|
|
'total',
|
2021-07-23 12:47:23 +00:00
|
|
|
|
'customer',
|
2022-06-01 17:15:09 +00:00
|
|
|
|
'line_items',
|
2021-07-23 12:47:23 +00:00
|
|
|
|
'customer_id',
|
|
|
|
|
'date_created_gmt',
|
|
|
|
|
],
|
|
|
|
|
} ),
|
|
|
|
|
[ orderStatuses ]
|
|
|
|
|
);
|
2020-11-06 01:21:05 +00:00
|
|
|
|
|
2022-06-01 17:15:09 +00:00
|
|
|
|
const currencyContext = useContext( CurrencyContext );
|
|
|
|
|
|
|
|
|
|
const getFormattedOrderTotal = ( total, countryState ) => {
|
|
|
|
|
if ( ! countryState ) {
|
|
|
|
|
return null;
|
2021-07-23 12:47:23 +00:00
|
|
|
|
}
|
2020-11-06 01:21:05 +00:00
|
|
|
|
|
2022-06-01 17:15:09 +00:00
|
|
|
|
const country = getCountryCode( countryState );
|
|
|
|
|
const { currencySymbols = {}, localeInfo = {} } = getAdminSetting(
|
|
|
|
|
'onboarding',
|
|
|
|
|
{}
|
|
|
|
|
);
|
|
|
|
|
const currency = currencyContext.getDataForCountry(
|
|
|
|
|
country,
|
|
|
|
|
localeInfo,
|
|
|
|
|
currencySymbols
|
|
|
|
|
);
|
|
|
|
|
currencyContext.setCurrency( currency );
|
|
|
|
|
return currencyContext.formatAmount( total );
|
|
|
|
|
};
|
|
|
|
|
|
2022-06-21 08:37:34 +00:00
|
|
|
|
const {
|
|
|
|
|
orders = [],
|
|
|
|
|
isRequesting,
|
|
|
|
|
isError,
|
|
|
|
|
customerItems,
|
|
|
|
|
} = useSelect( ( select ) => {
|
|
|
|
|
const { getOrders, hasFinishedResolution, getOrdersError } =
|
|
|
|
|
select( ORDERS_STORE_NAME );
|
|
|
|
|
// eslint-disable-next-line @wordpress/no-unused-vars-before-return
|
|
|
|
|
const { getItems } = select( ITEMS_STORE_NAME );
|
2022-06-01 17:15:09 +00:00
|
|
|
|
|
2022-06-21 08:37:34 +00:00
|
|
|
|
if ( ! orderStatuses.length && unreadOrdersCount === 0 ) {
|
|
|
|
|
return { isRequesting: false };
|
|
|
|
|
}
|
2022-06-01 17:15:09 +00:00
|
|
|
|
|
2022-06-21 08:37:34 +00:00
|
|
|
|
/* eslint-disable @wordpress/no-unused-vars-before-return */
|
|
|
|
|
const actionableOrders = getOrders( actionableOrdersQuery, null );
|
2021-07-23 12:47:23 +00:00
|
|
|
|
|
2022-06-21 08:37:34 +00:00
|
|
|
|
const isRequestingActionable = hasFinishedResolution( 'getOrders', [
|
|
|
|
|
actionableOrdersQuery,
|
|
|
|
|
] );
|
2021-07-23 12:47:23 +00:00
|
|
|
|
|
2022-06-21 08:37:34 +00:00
|
|
|
|
if (
|
|
|
|
|
isRequestingActionable ||
|
|
|
|
|
unreadOrdersCount === null ||
|
|
|
|
|
actionableOrders === null
|
|
|
|
|
) {
|
2021-07-23 12:47:23 +00:00
|
|
|
|
return {
|
2022-06-21 08:37:34 +00:00
|
|
|
|
isError: Boolean( getOrdersError( actionableOrdersQuery ) ),
|
|
|
|
|
isRequesting: true,
|
2021-07-23 12:47:23 +00:00
|
|
|
|
orderStatuses,
|
2020-11-06 01:21:05 +00:00
|
|
|
|
};
|
2021-07-23 12:47:23 +00:00
|
|
|
|
}
|
2022-06-21 08:37:34 +00:00
|
|
|
|
|
|
|
|
|
const customers = getItems( 'customers', {
|
|
|
|
|
users: actionableOrders
|
|
|
|
|
.map( ( order ) => order.customer_id )
|
|
|
|
|
.filter( ( id ) => id !== 0 ),
|
|
|
|
|
_fields: [ 'id', 'name', 'country', 'user_id' ],
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
orders: actionableOrders,
|
|
|
|
|
isError: Boolean( getOrdersError( actionableOrders ) ),
|
|
|
|
|
isRequesting: isRequestingActionable,
|
|
|
|
|
orderStatuses,
|
|
|
|
|
customerItems: customers,
|
|
|
|
|
};
|
|
|
|
|
} );
|
2021-07-23 12:47:23 +00:00
|
|
|
|
|
|
|
|
|
if ( isError ) {
|
|
|
|
|
if ( ! orderStatuses.length && window.wcAdminFeatures.analytics ) {
|
2020-11-06 01:21:05 +00:00
|
|
|
|
return (
|
2021-07-23 12:47:23 +00:00
|
|
|
|
<EmptyContent
|
|
|
|
|
title={ __(
|
|
|
|
|
"You currently don't have any actionable statuses. " +
|
|
|
|
|
'To display orders here, select orders that require further review in settings.',
|
2022-03-30 09:00:04 +00:00
|
|
|
|
'woocommerce'
|
2021-07-23 12:47:23 +00:00
|
|
|
|
) }
|
2022-03-30 09:00:04 +00:00
|
|
|
|
actionLabel={ __( 'Settings', 'woocommerce' ) }
|
2021-07-23 12:47:23 +00:00
|
|
|
|
actionURL={ getAdminLink(
|
|
|
|
|
'admin.php?page=wc-admin&path=/analytics/settings'
|
|
|
|
|
) }
|
|
|
|
|
/>
|
2020-11-06 01:21:05 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-23 12:47:23 +00:00
|
|
|
|
const title = __(
|
|
|
|
|
'There was an error getting your orders. Please try again.',
|
2022-03-30 09:00:04 +00:00
|
|
|
|
'woocommerce'
|
2021-07-23 12:47:23 +00:00
|
|
|
|
);
|
2022-03-30 09:00:04 +00:00
|
|
|
|
const actionLabel = __( 'Reload', 'woocommerce' );
|
2021-07-23 12:47:23 +00:00
|
|
|
|
const actionCallback = () => {
|
|
|
|
|
// @todo Add tracking for how often an error is displayed, and the reload action is clicked.
|
|
|
|
|
window.location.reload();
|
|
|
|
|
};
|
|
|
|
|
|
2020-11-06 01:21:05 +00:00
|
|
|
|
return (
|
2021-07-23 12:47:23 +00:00
|
|
|
|
<>
|
|
|
|
|
<EmptyContent
|
|
|
|
|
title={ title }
|
|
|
|
|
actionLabel={ actionLabel }
|
|
|
|
|
actionURL={ null }
|
|
|
|
|
actionCallback={ actionCallback }
|
|
|
|
|
/>
|
|
|
|
|
</>
|
2020-11-06 01:21:05 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
2022-06-01 17:15:09 +00:00
|
|
|
|
const customerList = customerItems
|
|
|
|
|
? Array.from( customerItems, ( [ , value ] ) => value )
|
|
|
|
|
: [];
|
2021-07-23 12:47:23 +00:00
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<Section>
|
|
|
|
|
{ isRequesting ? (
|
|
|
|
|
<ActivityCardPlaceholder
|
|
|
|
|
className="woocommerce-order-activity-card"
|
|
|
|
|
hasAction
|
|
|
|
|
hasDate
|
|
|
|
|
lines={ 1 }
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
2022-06-01 17:15:09 +00:00
|
|
|
|
renderOrders( orders, customerList, getFormattedOrderTotal )
|
2021-07-23 12:47:23 +00:00
|
|
|
|
) }
|
|
|
|
|
</Section>
|
|
|
|
|
</>
|
|
|
|
|
);
|
2020-11-06 01:21:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OrdersPanel.propTypes = {
|
|
|
|
|
isError: PropTypes.bool,
|
|
|
|
|
isRequesting: PropTypes.bool,
|
2022-01-19 16:45:17 +00:00
|
|
|
|
unreadOrdersCount: PropTypes.number,
|
2020-11-06 16:50:24 +00:00
|
|
|
|
orders: PropTypes.array.isRequired,
|
|
|
|
|
orderStatuses: PropTypes.array,
|
2020-11-06 01:21:05 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
OrdersPanel.defaultProps = {
|
|
|
|
|
orders: [],
|
|
|
|
|
isError: false,
|
|
|
|
|
isRequesting: false,
|
|
|
|
|
};
|
|
|
|
|
|
2021-07-23 12:47:23 +00:00
|
|
|
|
export default OrdersPanel;
|