notificationPage.tsx 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import {
  2. View,
  3. Text,
  4. Pressable,
  5. Image,
  6. ScrollView,
  7. Alert,
  8. ImageBackground,
  9. ActivityIndicator,
  10. Dimensions
  11. } from 'react-native';
  12. import { SafeAreaView } from 'react-native-safe-area-context';
  13. import { router, useLocalSearchParams } from 'expo-router';
  14. import { CrossLogoSvg, PreviousPageBlackSvg } from '../../../../component/global/SVG';
  15. import { useEffect, useState } from 'react';
  16. import { chargeStationService } from '../../../../service/chargeStationService';
  17. import NotificationTabView from '../../../../component/global/notificationTabViewComponent';
  18. import { useTranslation } from '../../../../util/hooks/useTranslation';
  19. const NotificationPageComponent = () => {
  20. const { t } = useTranslation(); // 使用翻译钩子
  21. const screenHeight = Dimensions.get('window').height;
  22. const [reservationAfter2025, setReservationAfter2025] = useState([]);
  23. const [passingThisPromotionToBell, setPassingThisPromotionToBell] = useState([]);
  24. const fetchData = async () => {
  25. try {
  26. const results = await Promise.allSettled([
  27. chargeStationService.fetchReservationHistories(),
  28. chargeStationService.getAdvertise()
  29. ]);
  30. // Handle reservation data
  31. if (results[0].status === 'fulfilled') {
  32. const year2025 = new Date('2025-02-01T00:00:00.000Z');
  33. const reservationAfter2025 = results[0].value.filter((r: any) => {
  34. const date = new Date(r.createdAt);
  35. return date > year2025;
  36. });
  37. setReservationAfter2025(reservationAfter2025);
  38. } else if (results[0].status === 'rejected') {
  39. Alert.alert(t('notifications.error_fetching_reservations_title'), results[0].reason);
  40. }
  41. // Handle promotion data
  42. if (results[1].status === 'fulfilled') {
  43. const passingThisPromotionToBell = results[1].value.filter((p: any) => p.is_show);
  44. setPassingThisPromotionToBell(passingThisPromotionToBell);
  45. } else if (results[1].status === 'rejected') {
  46. Alert.alert(t('notifications.error_fetching_promotions_title'), results[1].reason);
  47. }
  48. } catch (error) {
  49. console.log(t('notifications.error_fetching_data'));
  50. } finally {
  51. }
  52. };
  53. useEffect(() => {
  54. fetchData();
  55. }, []);
  56. return (
  57. <SafeAreaView className="flex-1 bg-white" edges={['top', 'left', 'right']}>
  58. <View style={{ minHeight: screenHeight, flex: 1 }}>
  59. <View className="mx-[5%]" style={{ marginTop: 25 }}>
  60. <Pressable
  61. onPress={() => {
  62. if (router.canGoBack()) {
  63. router.back();
  64. } else {
  65. router.replace('/optionPage');
  66. }
  67. }}
  68. hitSlop={{ top: 20, bottom: 20, left: 20, right: 20 }}
  69. >
  70. <CrossLogoSvg />
  71. </Pressable>
  72. <Text style={{ fontSize: 45, marginVertical: 25 }}>{t('notifications.title')}</Text>
  73. </View>
  74. <View className="flex-1">
  75. <NotificationTabView
  76. titles={[t('notifications.charging_info'), t('notifications.promotions')]}
  77. reservationAfter2025={reservationAfter2025}
  78. passingThisPromotionToBell={passingThisPromotionToBell}
  79. />
  80. </View>
  81. </View>
  82. </SafeAreaView>
  83. );
  84. };
  85. export default NotificationPageComponent;