feat: schizo sorting

This commit is contained in:
2024-11-18 01:30:36 +03:00
parent 0442935bd9
commit 90b11a5250

View File

@@ -39,18 +39,7 @@ export const Board: FC<Props> = ({
return has(obj, "deals"); return has(obj, "deals");
}; };
const getDateFromDealOrGroup = (obj: GroupWithDeals | DealSummary) => {
// if is group get group with the delivery earliest date
if (isGroup(obj)) {
const dates = obj.deals.map(d => d.deliveryDate || d.createdAt).filter(Boolean);
if (dates.length === 0) return null;
return new Date(Math.min(...dates.map(d => {
return new Date(d).getTime();
})));
}
// if is deal get deal delivery date
return new Date(obj.deliveryDate || obj.createdAt);
};
const getDealGroups = (): GroupWithDeals[] => { const getDealGroups = (): GroupWithDeals[] => {
const groups = uniq<DealGroupSchema>(summaries.filter(s => s.group).map(summary => summary.group) as DealGroupSchema[]); const groups = uniq<DealGroupSchema>(summaries.filter(s => s.group).map(summary => summary.group) as DealGroupSchema[]);
if (groups.length === 0) return []; if (groups.length === 0) return [];
@@ -71,23 +60,45 @@ export const Board: FC<Props> = ({
return acc; return acc;
}, [] as { group: DealGroupSchema; deals: DealSummary[] }[]); }, [] as { group: DealGroupSchema; deals: DealSummary[] }[]);
}; };
const getDateFromDealOrGroup = (obj: GroupWithDeals | DealSummary) => {
// if is group get group with the delivery earliest date
if (isGroup(obj)) {
const dates = obj.deals.map(d => d.deliveryDate || d.createdAt).filter(Boolean);
if (dates.length === 0) return null;
return new Date(Math.min(...dates.map(d => {
return new Date(d).getTime();
})));
}
// if is deal get deal delivery date
return new Date(obj.deliveryDate || obj.createdAt);
};
const getDealsAndGroups = (): (GroupWithDeals | DealSummary)[] => { const getDealsAndGroups = (): (GroupWithDeals | DealSummary)[] => {
const groups = getDealGroups(); const groups = getDealGroups();
const deals = summaries.filter(s => !s.group); const deals = summaries.filter(s => !s.group);
const data = [...groups, ...deals]; const dealsWithDeliveryDate = deals.filter(d => d.deliveryDate).sort((a, b) => {
return data.sort((a, b) => { if (!a.deliveryDate || !b.deliveryDate) return 0;
return new Date(a.deliveryDate).getTime() - new Date(b.deliveryDate).getTime();
});
const dealsWithoutDeliveryDate = deals.filter(d => !d.deliveryDate).sort((a, b) => {
return new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime();
});
const groupsWithDeliveryDate = groups.filter(g => g.deals.some(d => d.deliveryDate));
const groupsWithoutDeliveryDate = groups.filter(g => g.deals.some(d => !d.deliveryDate));
const firstRankData = [...dealsWithDeliveryDate, ...groupsWithDeliveryDate].sort((a, b) => {
const aDate = getDateFromDealOrGroup(a); const aDate = getDateFromDealOrGroup(a);
const bDate = getDateFromDealOrGroup(b); const bDate = getDateFromDealOrGroup(b);
if (!aDate || !bDate) return 0; if (!aDate || !bDate) return 0;
return aDate.getTime() - bDate.getTime(); return aDate.getTime() - bDate.getTime();
}).map((obj, index) => { });
if (isGroup(obj)) { const secondRankData = [...dealsWithoutDeliveryDate, ...groupsWithoutDeliveryDate].sort((a, b) => {
obj.deals[0].rank = index; const aDate = getDateFromDealOrGroup(a);
return obj; const bDate = getDateFromDealOrGroup(b);
} if (!aDate || !bDate) return 0;
obj.rank = index; return aDate.getTime() - bDate.getTime();
return obj; });
}) as (GroupWithDeals | DealSummary)[]; return [...firstRankData, ...secondRankData];
}; };
const renderDeal = (deal: DealSummary) => { const renderDeal = (deal: DealSummary) => {