| 1 |
import OrdersTable from '@/components/orders/OrdersTable'; |
| 2 |
import { selectDateRangeParams } from '@/features/dateRange/dateRangeSlice'; |
| 3 |
import { useGetOrdersQuery } from '@/features/orders/ordersApi'; |
| 4 |
import useIsPro from '@/lib/useIsPro'; |
| 5 |
import { fmt } from '@/lib/utils'; |
| 6 |
import { __ } from '@wordpress/i18n'; |
| 7 |
import { useState } from 'react'; |
| 8 |
import { useSelector } from 'react-redux'; |
| 9 |
|
| 10 |
function formatDate(dateStr) { |
| 11 |
if (!dateStr) return '—'; |
| 12 |
return new Date(dateStr).toLocaleDateString('en-US', { |
| 13 |
month: 'short', |
| 14 |
day: 'numeric', |
| 15 |
year: 'numeric', |
| 16 |
}); |
| 17 |
} |
| 18 |
|
| 19 |
const SORT_MAP = { |
| 20 |
revenue: 'revenue', |
| 21 |
date: 'date', |
| 22 |
}; |
| 23 |
|
| 24 |
const Orders = () => { |
| 25 |
const dateParams = useSelector(selectDateRangeParams); |
| 26 |
const [page, setPage] = useState(1); |
| 27 |
const [sortKey, setSortKey] = useState('date'); |
| 28 |
const [sortDir, setSortDir] = useState('desc'); |
| 29 |
const [search, setSearch] = useState(''); |
| 30 |
const isPro = useIsPro(); |
| 31 |
|
| 32 |
const { |
| 33 |
data: res, |
| 34 |
isLoading, |
| 35 |
isFetching, |
| 36 |
} = useGetOrdersQuery({ |
| 37 |
...dateParams, |
| 38 |
limit: isPro ? 20 : 10, |
| 39 |
sort_by: SORT_MAP[sortKey] ?? 'revenue', |
| 40 |
order: sortDir, |
| 41 |
page, |
| 42 |
...(search ? { search } : {}), |
| 43 |
}); |
| 44 |
|
| 45 |
const orders = (res?.data ?? []).map((o) => ({ |
| 46 |
id: `#${o.id}`, |
| 47 |
customer: o.customer_name || '—', |
| 48 |
campaign: (o.campaigns ?? []).map((c) => c.name).join(', ') || '—', |
| 49 |
products: o.products ?? [], |
| 50 |
total_spent: fmt(o.total_spent), |
| 51 |
quantity: o.quantity, |
| 52 |
date: formatDate(o.date), |
| 53 |
})); |
| 54 |
|
| 55 |
const total = res?.collection?.total ?? 0; |
| 56 |
const totalPages = res?.collection?.total_pages ?? 1; |
| 57 |
const count = res?.collection?.count ?? 0; |
| 58 |
|
| 59 |
const handleSort = (key) => { |
| 60 |
if (sortKey === key) setSortDir((d) => (d === 'asc' ? 'desc' : 'asc')); |
| 61 |
else { |
| 62 |
setSortKey(key); |
| 63 |
setSortDir('desc'); |
| 64 |
} |
| 65 |
setPage(1); |
| 66 |
}; |
| 67 |
|
| 68 |
const handleSearch = (value) => { |
| 69 |
setSearch(value); |
| 70 |
setPage(1); |
| 71 |
}; |
| 72 |
|
| 73 |
return ( |
| 74 |
<div className="disco:min-h-screen disco:bg-[#f9fafb] disco:px-6 disco:py-2"> |
| 75 |
<div className="disco:mb-5"> |
| 76 |
<h1 className="disco:text-lg! disco:font-bold! disco:text-[#111827]! disco:tracking-tight"> |
| 77 |
{__('Orders', 'disco')} |
| 78 |
</h1> |
| 79 |
<p className="disco:text-xs! disco:text-[#9ca3af] disco:mt-0.5"> |
| 80 |
{__('Orders influenced by Disco campaigns', 'disco')} |
| 81 |
</p> |
| 82 |
</div> |
| 83 |
<OrdersTable |
| 84 |
orders={orders} |
| 85 |
isLoading={isLoading || isFetching} |
| 86 |
search={search} |
| 87 |
onSearchChange={handleSearch} |
| 88 |
sortKey={sortKey} |
| 89 |
sortDir={sortDir} |
| 90 |
onSort={handleSort} |
| 91 |
page={page} |
| 92 |
totalPages={totalPages} |
| 93 |
total={total} |
| 94 |
count={count} |
| 95 |
onPageChange={setPage} |
| 96 |
/> |
| 97 |
</div> |
| 98 |
); |
| 99 |
}; |
| 100 |
|
| 101 |
export default Orders; |
| 102 |
|