bookingTabViewComponent.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import React, { useState, useCallback, useEffect } from 'react';
  2. import { ActivityIndicator, View, RefreshControl } from 'react-native';
  3. import { useQuery, QueryClient, QueryClientProvider } from '@tanstack/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. // console.log('reservations', reservations);r
  28. return reservations
  29. .filter((reservation) =>
  30. isFuture ? new Date(reservation.end_time) > now : new Date(reservation.end_time) <= now
  31. )
  32. .sort((a, b) =>
  33. isFuture ? new Date(a.end_time) - new Date(b.end_time) : new Date(b.end_time) - new Date(a.end_time)
  34. )
  35. .slice(0, 33)
  36. .map((reservation) => {
  37. const snapshot = JSON.parse(reservation.snapshot);
  38. console.log(
  39. 'reservationreservationreservationreservationreservationreservationreservationreservationreservationreservationreservation',
  40. snapshot.current_price
  41. );
  42. let stationInfo;
  43. // console.log(
  44. // 'snapshotsnapshotsnapshotsnapshotsnapshotsnapshotsnapshotsnapshotsnapshotsnapshotsnapshotsnapshotsnapshotsnapshotsnapshotsnapshotsnapshot',
  45. // snapshot
  46. // );
  47. if (snapshot.stationID) {
  48. stationInfo = allStations.find((station) => station.id === snapshot.stationID);
  49. } else if (snapshot.connector) {
  50. stationInfo = findStationByConnectorId(allStations, snapshot.connector);
  51. }
  52. //this is book_time, not end_time
  53. const endDate = new Date(reservation.book_time);
  54. //this is actual_end_time for record
  55. const actualEndDate = new Date(reservation.actual_end_time);
  56. return {
  57. imgURL: require('../../assets/dummyStationPicture.png'),
  58. date: `${endDate.getMonth() + 1}月${endDate.getDate()}`,
  59. time: endDate.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit', hour12: false }),
  60. actual_end_time: actualEndDate.toLocaleTimeString('zh-CN', {
  61. hour: '2-digit',
  62. minute: '2-digit',
  63. hour12: false
  64. }),
  65. chargeStationName: stationInfo ? stationInfo.snapshot.StationName : 'Unknown Station',
  66. chargeStationAddress: stationInfo ? stationInfo.snapshot.Address : 'Unknown Address',
  67. stationLng: stationInfo ? stationInfo.snapshot.StationLng : '',
  68. stationLat: stationInfo ? stationInfo.snapshot.StationLat : '',
  69. distance: '',
  70. format_order_id: reservation.format_order_id,
  71. actual_total_power: reservation.actual_total_power,
  72. total_fee: reservation.total_fee,
  73. withdraw_fee: reservation.withdraw_fee,
  74. actual_fee: reservation.total_fee - reservation.withdraw_fee,
  75. current_price: snapshot.current_price,
  76. total_power: reservation.total_power,
  77. };
  78. // .map((reservation) => {
  79. // const snapshot = JSON.parse(reservation.snapshot);
  80. // const stationInfo = allStations.find((station) => station.id === snapshot.stationID);
  81. // const endDate = new Date(reservation.book_time);
  82. // return {
  83. // imgURL: require('../../assets/dummyStationPicture.png'),
  84. // date: `${endDate.getMonth() + 1}月${endDate.getDate()}`,
  85. // time: endDate.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit', hour12: false }),
  86. // chargeStationName: stationInfo ? stationInfo.snapshot.StationName : 'Unknown Station',
  87. // chargeStationAddress: stationInfo ? stationInfo.snapshot.Address : 'Unknown Address',
  88. // stationLng: stationInfo ? stationInfo.snapshot.StationLng : '',
  89. // stationLat: stationInfo ? stationInfo.snapshot.StationLat : '',
  90. // distance: ''
  91. //};
  92. });
  93. };
  94. const BookingTabViewComponentInner: React.FC<BookingTabViewComponentProps> = ({ titles }) => {
  95. const { data, isLoading, error } = useQuery('reservationsAndStations', fetchReservationsAndStations, {
  96. staleTime: 0,
  97. cacheTime: 0,
  98. refetchOnMount: true,
  99. refetchOnWindowFocus: true
  100. });
  101. if (isLoading) {
  102. return (
  103. <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
  104. <ActivityIndicator size="large" color="#34657b" />
  105. </View>
  106. );
  107. }
  108. if (error) {
  109. console.error('Error fetching data:', error);
  110. return null;
  111. }
  112. if (!data) {
  113. return null;
  114. }
  115. const tabItems = processReservations(data.reservations, data.stations, true);
  116. const completedReservationTabItems = processReservations(data.reservations, data.stations, false);
  117. console.log('completedReservationTabItems', completedReservationTabItems);
  118. // console.log('completedReservationTabItems', completedReservationTabItems);
  119. return (
  120. <TabViewComponent
  121. titles={titles}
  122. tabItems={tabItems}
  123. completedReservationTabItems={completedReservationTabItems}
  124. isLoading={false}
  125. />
  126. );
  127. };
  128. const BookingTabViewComponent: React.FC<BookingTabViewComponentProps> = (props) => (
  129. <QueryClientProvider client={queryClient}>
  130. <BookingTabViewComponentInner {...props} />
  131. </QueryClientProvider>
  132. );
  133. export default BookingTabViewComponent;