|
1 | 1 | import {accountIDSelector} from '@selectors/Session'; |
2 | 2 | import {useMemo} from 'react'; |
3 | | -import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; |
4 | 3 | import useOnyx from '@hooks/useOnyx'; |
5 | | -import {isTripRoom} from '@libs/ReportUtils'; |
6 | 4 | import type {ReservationData} from '@libs/TripReservationUtils'; |
7 | 5 | import {getReservationsFromTripReport} from '@libs/TripReservationUtils'; |
8 | 6 | import CONST from '@src/CONST'; |
9 | 7 | import ONYXKEYS from '@src/ONYXKEYS'; |
10 | | -import type {Report} from '@src/types/onyx'; |
11 | | -import mapOnyxCollectionItems from '@src/utils/mapOnyxCollectionItems'; |
| 8 | +import useTripRoomReports from './useTripRoomReports'; |
12 | 9 |
|
13 | 10 | type UpcomingReservation = ReservationData & { |
14 | 11 | reportID: string; |
15 | 12 | }; |
16 | 13 |
|
17 | | -const tripRoomSelector = (report: OnyxEntry<Report>): Report | undefined => { |
18 | | - if (!report || !isTripRoom(report)) { |
19 | | - return; |
20 | | - } |
21 | | - return report; |
22 | | -}; |
23 | | - |
24 | | -const allTripRoomsSelector = (reports: OnyxCollection<Report>) => mapOnyxCollectionItems(reports, tripRoomSelector); |
25 | | - |
26 | 14 | function useUpcomingTravelReservations(): UpcomingReservation[] { |
27 | | - const [tripRoomReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT, {selector: allTripRoomsSelector}); |
| 15 | + const tripRoomReports = useTripRoomReports(); |
28 | 16 | const [accountID] = useOnyx(ONYXKEYS.SESSION, {selector: accountIDSelector}); |
29 | 17 |
|
30 | 18 | return useMemo(() => { |
31 | 19 | const now = new Date(); |
32 | 20 | const windowEnd = new Date(now); |
33 | 21 | windowEnd.setDate(windowEnd.getDate() + CONST.UPCOMING_TRAVEL_WINDOW_DAYS); |
34 | 22 |
|
35 | | - const reports = Object.values(tripRoomReports ?? {}); |
36 | 23 | const upcoming: UpcomingReservation[] = []; |
37 | 24 |
|
38 | | - for (const report of reports) { |
| 25 | + for (const report of tripRoomReports) { |
39 | 26 | // Only include reservations where the current user is the traveler |
40 | | - if (!report || report.ownerAccountID !== accountID) { |
| 27 | + if (report.ownerAccountID !== accountID) { |
41 | 28 | continue; |
42 | 29 | } |
43 | 30 | const reservations = getReservationsFromTripReport(report); |
|
0 commit comments