diff --git a/apps/admin-x-settings/src/components/settings/growth/offers/OffersIndex.tsx b/apps/admin-x-settings/src/components/settings/growth/offers/OffersIndex.tsx index f3a9a5b5d2..7e94314f87 100644 --- a/apps/admin-x-settings/src/components/settings/growth/offers/OffersIndex.tsx +++ b/apps/admin-x-settings/src/components/settings/growth/offers/OffersIndex.tsx @@ -132,14 +132,30 @@ export const OffersIndexModal = () => { ]; const [selectedTab, setSelectedTab] = useState('active'); const [selectedLayout, setSelectedLayout] = useState('card'); + const [sortOption, setSortOption] = useState('date-added'); + const [sortDirection, setSortDirection] = useState('desc'); const handleOfferEdit = (id:string) => { // TODO: implement updateRoute(`offers/edit/${id}`); }; + const sortedOffers = allOffers + .sort((offer1, offer2) => { + const multiplier = sortDirection === 'desc' ? -1 : 1; + switch (sortOption) { + case 'name': + return multiplier * offer1.name.localeCompare(offer2.name); + case 'redemptions': + return multiplier * (offer1.redemption_count - offer2.redemption_count); + default: + // 'date-added' or unknown option, use default sorting + return multiplier * ((offer1.created_at ? new Date(offer1.created_at).getTime() : 0) - (offer2.created_at ? new Date(offer2.created_at).getTime() : 0)); + } + }); + const cardLayoutOutput =
- {allOffers.filter(offer => offer.status === selectedTab).map((offer) => { + {sortedOffers.filter(offer => offer.status === selectedTab).map((offer) => { const offerTier = paidActiveTiers.find(tier => tier.id === offer?.tier.id); if (!offerTier) { @@ -166,14 +182,14 @@ export const OffersIndexModal = () => { const listLayoutOutput = - + - {allOffers.filter(offer => offer.status === selectedTab).map((offer) => { + {sortedOffers.filter(offer => offer.status === selectedTab).map((offer) => { const offerTier = paidActiveTiers.find(tier => tier.id === offer?.tier.id); if (!offerTier) { @@ -220,7 +236,7 @@ export const OffersIndexModal = () => {
- {allOffers.some(offer => offer.hasOwnProperty('status') && offer.status === 'archived') ? + {sortedOffers.some(offer => offer.hasOwnProperty('status') && offer.status === 'archived') ? { { - // eslint-disable-next-line no-console - console.log(`Sorting direction: ${selectedDirection}`); + const newDirection = selectedDirection === 'asc' ? 'desc' : 'asc'; + setSortDirection(newDirection); }} onSortChange={(selectedOption) => { - // eslint-disable-next-line no-console - console.log(`Sorting option: ${selectedOption}`); + setSortOption(selectedOption); }} />
{allOffers.length} {allOffers.length > 1 ? 'offers' : 'offer'}{sortedOffers.length} {sortedOffers.length > 1 ? 'offers' : 'offer'} Tier Terms Price Redemptions