| 1 |
import { selectDateRangeParams } from '@/features/dateRange/dateRangeSlice'; |
| 2 |
import { useGetOrdersQuery } from '@/features/orders/ordersApi'; |
| 3 |
import useIsPro from '@/lib/useIsPro'; |
| 4 |
import { fmt, formatDate, truncate } from '@/lib/utils'; |
| 5 |
import { __ } from '@wordpress/i18n'; |
| 6 |
import { useState } from 'react'; |
| 7 |
import { useSelector } from 'react-redux'; |
| 8 |
import DataTable from '../ui-blocks/DataTable'; |
| 9 |
import ProductsMoreDialog from '../ui-blocks/ProductsMoreDialog'; |
| 10 |
|
| 11 |
const COLUMNS = [ |
| 12 |
{ key: 'id', label: 'Order' }, |
| 13 |
{ key: 'campaign', label: 'Campaign' }, |
| 14 |
{ key: 'intent', label: 'Intent Type' }, |
| 15 |
{ key: 'total_spent', label: 'Spent', sortable: true }, |
| 16 |
{ key: 'products', label: 'Products' }, |
| 17 |
{ key: 'quantity', label: 'Quantity', sortable: true }, |
| 18 |
{ key: 'date', label: 'Date', sortable: true }, |
| 19 |
]; |
| 20 |
|
| 21 |
const SORT_MAP = { total_spent: 'revenue', quantity: 'quantity', date: 'date' }; |
| 22 |
|
| 23 |
const renderRow = (order) => ( |
| 24 |
<tr |
| 25 |
key={order.id} |
| 26 |
className="disco:border-b disco:border-[#f3f4f6] disco:last:border-0 disco:hover:bg-[#f9fafb] disco:transition-colors" |
| 27 |
> |
| 28 |
<td className="disco:px-4 disco:py-3 disco:font-mono disco:text-[#9ca3af]"> |
| 29 |
#{order.id} |
| 30 |
</td> |
| 31 |
<td className="disco:px-4 disco:py-3 disco:font-medium disco:text-[#2fa86d] disco:whitespace-nowrap"> |
| 32 |
{truncate( |
| 33 |
(order.campaigns ?? []).map((c) => c.name).join(', '), |
| 34 |
30 |
| 35 |
) || '—'} |
| 36 |
</td> |
| 37 |
<td className="disco:px-4 disco:py-3 disco:text-gray-500 disco:font-medium disco:whitespace-nowrap"> |
| 38 |
{order?.campaigns[0]?.intent} |
| 39 |
</td> |
| 40 |
<td className="disco:px-4 disco:py-3 disco:font-semibold disco:text-[#111827]"> |
| 41 |
{fmt(order.total_spent)} |
| 42 |
</td> |
| 43 |
<td className="disco:px-4 disco:py-3 disco:text-[#6b7280]"> |
| 44 |
<ProductsMoreDialog title="Products" items={order.products ?? []} /> |
| 45 |
</td> |
| 46 |
<td className="disco:px-4 disco:py-3 disco:font-semibold disco:text-[#111827]"> |
| 47 |
{order.quantity} |
| 48 |
</td> |
| 49 |
<td className="disco:px-4 disco:py-3 disco:text-[#9ca3af] disco:whitespace-nowrap"> |
| 50 |
{formatDate(order.date)} |
| 51 |
</td> |
| 52 |
</tr> |
| 53 |
); |
| 54 |
|
| 55 |
const CustomerOrdersTable = ({ customerId }) => { |
| 56 |
const isPro = useIsPro(); |
| 57 |
const dateParams = useSelector(selectDateRangeParams); |
| 58 |
const [sortKey, setSortKey] = useState('date'); |
| 59 |
const [sortDir, setSortDir] = useState('desc'); |
| 60 |
const [search, setSearch] = useState(''); |
| 61 |
const [page, setPage] = useState(1); |
| 62 |
|
| 63 |
const { |
| 64 |
data: res, |
| 65 |
isLoading, |
| 66 |
isFetching, |
| 67 |
} = useGetOrdersQuery({ |
| 68 |
...dateParams, |
| 69 |
customer_id: customerId, |
| 70 |
sort_by: SORT_MAP[sortKey] ?? 'revenue', |
| 71 |
order: sortDir, |
| 72 |
page, |
| 73 |
search, |
| 74 |
limit: isPro ? 20 : 10, |
| 75 |
}); |
| 76 |
|
| 77 |
const orders = res?.data ?? []; |
| 78 |
const totalPages = res?.collection?.total_pages ?? 1; |
| 79 |
const total = res?.collection?.total ?? 0; |
| 80 |
|
| 81 |
const onSort = (key) => { |
| 82 |
if (sortKey === key) setSortDir((d) => (d === 'asc' ? 'desc' : 'asc')); |
| 83 |
else { |
| 84 |
setSortKey(key); |
| 85 |
setSortDir('desc'); |
| 86 |
} |
| 87 |
setPage(1); |
| 88 |
}; |
| 89 |
|
| 90 |
return ( |
| 91 |
<DataTable |
| 92 |
columns={COLUMNS} |
| 93 |
data={orders} |
| 94 |
renderRow={renderRow} |
| 95 |
title="Recent orders from this Customer" |
| 96 |
total={total} |
| 97 |
searchPlaceholder="Search by id..." |
| 98 |
search={search} |
| 99 |
onSearchChange={setSearch} |
| 100 |
sortKey={sortKey} |
| 101 |
sortDir={sortDir} |
| 102 |
onSort={onSort} |
| 103 |
page={page} |
| 104 |
totalPages={totalPages} |
| 105 |
onPageChange={setPage} |
| 106 |
isLoading={isLoading || isFetching} |
| 107 |
locked={!isPro} |
| 108 |
lockOverlay={{ |
| 109 |
size: 'lg', |
| 110 |
label: __('Upgrade to pro', 'disco'), |
| 111 |
titleClassName: |
| 112 |
'disco:max-w-md disco:text-base disco:font-normal disco:text-[#111827] disco:leading-snug', |
| 113 |
title: ( |
| 114 |
<> |
| 115 |
<strong className="disco:font-semibold"> |
| 116 |
{__('Upgrade to Disco Pro', 'disco')} |
| 117 |
</strong>{' '} |
| 118 |
{__( |
| 119 |
'to unlock your complete campaign data — including real revenue per campaign, discount ROI, and full trend history.', |
| 120 |
'disco' |
| 121 |
)} |
| 122 |
</> |
| 123 |
), |
| 124 |
}} |
| 125 |
/> |
| 126 |
); |
| 127 |
}; |
| 128 |
|
| 129 |
export default CustomerOrdersTable; |
| 130 |
|