bookingTabViewComponent.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. return reservations
  28. .filter((reservation) =>
  29. isFuture ? new Date(reservation.end_time) > now : new Date(reservation.end_time) < now
  30. )
  31. .sort((a, b) =>
  32. isFuture ? new Date(a.end_time) - new Date(b.end_time) : new Date(b.end_time) - new Date(a.end_time)
  33. )
  34. .slice(0, 10)
  35. .map((reservation) => {
  36. const snapshot = JSON.parse(reservation.snapshot);
  37. let stationInfo;
  38. if (snapshot.stationID) {
  39. stationInfo = allStations.find((station) => station.id === snapshot.stationID);
  40. } else if (snapshot.connector) {
  41. stationInfo = findStationByConnectorId(allStations, snapshot.connector);
  42. }
  43. const endDate = new Date(reservation.book_time);
  44. return {
  45. imgURL: require('../../assets/dummyStationPicture.png'),
  46. date: `${endDate.getMonth() + 1}月${endDate.getDate()}`,
  47. time: endDate.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit', hour12: false }),
  48. chargeStationName: stationInfo ? stationInfo.snapshot.StationName : 'Unknown Station',
  49. chargeStationAddress: stationInfo ? stationInfo.snapshot.Address : 'Unknown Address',
  50. stationLng: stationInfo ? stationInfo.snapshot.StationLng : '',
  51. stationLat: stationInfo ? stationInfo.snapshot.StationLat : '',
  52. distance: ''
  53. };
  54. // .map((reservation) => {
  55. // const snapshot = JSON.parse(reservation.snapshot);
  56. // const stationInfo = allStations.find((station) => station.id === snapshot.stationID);
  57. // const endDate = new Date(reservation.book_time);
  58. // return {
  59. // imgURL: require('../../assets/dummyStationPicture.png'),
  60. // date: `${endDate.getMonth() + 1}月${endDate.getDate()}`,
  61. // time: endDate.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit', hour12: false }),
  62. // chargeStationName: stationInfo ? stationInfo.snapshot.StationName : 'Unknown Station',
  63. // chargeStationAddress: stationInfo ? stationInfo.snapshot.Address : 'Unknown Address',
  64. // stationLng: stationInfo ? stationInfo.snapshot.StationLng : '',
  65. // stationLat: stationInfo ? stationInfo.snapshot.StationLat : '',
  66. // distance: ''
  67. //};
  68. });
  69. };
  70. const BookingTabViewComponentInner: React.FC<BookingTabViewComponentProps> = ({ titles }) => {
  71. const { data, isLoading, error } = useQuery('reservationsAndStations', fetchReservationsAndStations, {
  72. staleTime: 0,
  73. cacheTime: 0,
  74. refetchOnMount: true,
  75. refetchOnWindowFocus: true
  76. });
  77. if (isLoading) {
  78. return (
  79. <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
  80. <ActivityIndicator size="large" color="#34657b" />
  81. </View>
  82. );
  83. }
  84. if (error) {
  85. console.error('Error fetching data:', error);
  86. return null;
  87. }
  88. if (!data) {
  89. return null;
  90. }
  91. const tabItems = processReservations(data.reservations, data.stations, true);
  92. console.log(tabItems);
  93. const completedReservationTabItems = processReservations(data.reservations, data.stations, false);
  94. return (
  95. <TabViewComponent
  96. titles={titles}
  97. tabItems={tabItems}
  98. completedReservationTabItems={completedReservationTabItems}
  99. isLoading={false}
  100. />
  101. );
  102. };
  103. const BookingTabViewComponent: React.FC<BookingTabViewComponentProps> = (props) => (
  104. <QueryClientProvider client={queryClient}>
  105. <BookingTabViewComponentInner {...props} />
  106. </QueryClientProvider>
  107. );
  108. export default BookingTabViewComponent;