Merged in feat/LOY-424-Sidepeek-Past-Stays (pull request #3270)
feat(LOY-424): Load More Past Stays via Sidepeek * feat(LOY-424): Load More Past Stays via Sidepeek * chore(LOY-424): use new section header * fix(LOY-424): remove uneeded nextCursor check Approved-by: Emma Zettervall
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
import { dt } from "@scandic-hotels/common/dt"
|
||||
|
||||
import type { Stay } from "@scandic-hotels/trpc/routers/user/output"
|
||||
|
||||
export interface StaysByYear {
|
||||
year: number
|
||||
stays: Stay[]
|
||||
}
|
||||
|
||||
/**
|
||||
* Groups stays by year based on checkinDate.
|
||||
* @returns an array sorted by year in descending order (most recent first).
|
||||
*/
|
||||
export function groupStaysByYear(stays: Stay[]): StaysByYear[] {
|
||||
const groupedMap = new Map<number, Stay[]>()
|
||||
|
||||
for (const stay of stays) {
|
||||
const year = dt(stay.attributes.checkinDate).year()
|
||||
|
||||
if (!groupedMap.has(year)) {
|
||||
groupedMap.set(year, [])
|
||||
}
|
||||
groupedMap.get(year)!.push(stay)
|
||||
}
|
||||
|
||||
return Array.from(groupedMap.entries())
|
||||
.sort(([yearA], [yearB]) => yearB - yearA)
|
||||
.map(([year, stays]) => ({ year, stays }))
|
||||
}
|
||||
Reference in New Issue
Block a user