bookingTabViewComponent.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import React, { useState, useCallback, useEffect } from 'react';
  2. import { ActivityIndicator, View, RefreshControl } from 'react-native';
  3. import { useQuery, QueryClient, QueryClientProvider } from 'react-query';
  4. import TabViewComponent, { TabItem } from './tabView';
  5. import { chargeStationService } from '../../service/chargeStationService';
  6. const queryClient = new QueryClient();
  7. interface BookingTabViewComponentProps {
  8. titles: string[];
  9. }
  10. //helper function to fetch reservations and stations
  11. const findStationByConnectorId = (allStations, targetConnectorId) => {
  12. return allStations.find((station) =>
  13. station.snapshot.EquipmentInfos?.some((equipment) =>
  14. equipment.ConnectorInfos?.some((connector) => connector.ConnectorID === targetConnectorId)
  15. )
  16. );
  17. };
  18. const fetchReservationsAndStations = async () => {
  19. const [reservationResponse, allStationsResponse] = await Promise.all([
  20. chargeStationService.fetchReservationHistories(),
  21. chargeStationService.fetchAllChargeStations()
  22. ]);
  23. return { reservations: reservationResponse, stations: allStationsResponse };
  24. };
  25. const processReservations = (reservations, allStations, isFuture): TabItem[] => {
  26. const now = new Date();
  27. const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
  28. const todayReservations = reservations.filter((reservation) => {
  29. const reservationDate = new Date(reservation.book_time);
  30. return reservationDate >= today && reservationDate < new Date(today.getTime() + 24 * 60 * 60 * 1000);
  31. });
  32. console.log("Today's reservations:", todayReservations);
  33. return reservations
  34. .filter((reservation) =>
  35. isFuture ? new Date(reservation.end_time) > now : new Date(reservation.end_time) < now
  36. )
  37. .sort((a, b) =>
  38. isFuture ? new Date(a.end_time) - new Date(b.end_time) : new Date(b.end_time) - new Date(a.end_time)
  39. )
  40. .slice(0, 10)
  41. .map((reservation) => {
  42. const snapshot = JSON.parse(reservation.snapshot);
  43. let stationInfo;
  44. if (snapshot.stationID) {
  45. stationInfo = allStations.find((station) => station.id === snapshot.stationID);
  46. } else if (snapshot.connector) {
  47. stationInfo = findStationByConnectorId(allStations, snapshot.connector);
  48. }
  49. const endDate = new Date(reservation.book_time);
  50. return {
  51. imgURL: require('../../assets/dummyStationPicture.png'),
  52. date: `${endDate.getMonth() + 1}月${endDate.getDate()}`,
  53. time: endDate.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit', hour12: false }),
  54. chargeStationName: stationInfo ? stationInfo.snapshot.StationName : 'Unknown Station',
  55. chargeStationAddress: stationInfo ? stationInfo.snapshot.Address : 'Unknown Address',
  56. stationLng: stationInfo ? stationInfo.snapshot.StationLng : '',
  57. stationLat: stationInfo ? stationInfo.snapshot.StationLat : '',
  58. distance: ''
  59. };
  60. // .map((reservation) => {
  61. // const snapshot = JSON.parse(reservation.snapshot);
  62. // const stationInfo = allStations.find((station) => station.id === snapshot.stationID);
  63. // const endDate = new Date(reservation.book_time);
  64. // return {
  65. // imgURL: require('../../assets/dummyStationPicture.png'),
  66. // date: `${endDate.getMonth() + 1}月${endDate.getDate()}`,
  67. // time: endDate.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit', hour12: false }),
  68. // chargeStationName: stationInfo ? stationInfo.snapshot.StationName : 'Unknown Station',
  69. // chargeStationAddress: stationInfo ? stationInfo.snapshot.Address : 'Unknown Address',
  70. // stationLng: stationInfo ? stationInfo.snapshot.StationLng : '',
  71. // stationLat: stationInfo ? stationInfo.snapshot.StationLat : '',
  72. // distance: ''
  73. //};
  74. });
  75. };
  76. const BookingTabViewComponentInner: React.FC<BookingTabViewComponentProps> = ({ titles }) => {
  77. const { data, isLoading, error } = useQuery('reservationsAndStations', fetchReservationsAndStations, {
  78. staleTime: 0,
  79. cacheTime: 0,
  80. refetchOnMount: true,
  81. refetchOnWindowFocus: true
  82. });
  83. if (isLoading) {
  84. return (
  85. <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
  86. <ActivityIndicator size="large" color="#34657b" />
  87. </View>
  88. );
  89. }
  90. if (error) {
  91. console.error('Error fetching data:', error);
  92. return null;
  93. }
  94. if (!data) {
  95. return null;
  96. }
  97. const tabItems = processReservations(data.reservations, data.stations, true);
  98. console.log('tabItem', tabItems);
  99. const completedReservationTabItems = processReservations(data.reservations, data.stations, false);
  100. return (
  101. <TabViewComponent
  102. titles={titles}
  103. tabItems={tabItems}
  104. completedReservationTabItems={completedReservationTabItems}
  105. isLoading={false}
  106. />
  107. );
  108. };
  109. const BookingTabViewComponent: React.FC<BookingTabViewComponentProps> = (props) => (
  110. <QueryClientProvider client={queryClient}>
  111. <BookingTabViewComponentInner {...props} />
  112. </QueryClientProvider>
  113. );
  114. export default BookingTabViewComponent;