notificationTabViewComponent.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. //the size of the TabView will follow its parent-container's size.
  2. import * as React from 'react';
  3. import { View, Text, useWindowDimensions, StyleSheet, ScrollView, ActivityIndicator, Pressable } from 'react-native';
  4. import { TabView, TabBar } from 'react-native-tab-view';
  5. import { formatToChineseDateTime } from '../../util/lib';
  6. import { useCallback, useEffect, useState } from 'react';
  7. import { notificationStorage } from '../notificationStorage';
  8. import { router, useFocusEffect } from 'expo-router';
  9. export interface TabItem {
  10. title: string;
  11. description: string;
  12. date: string;
  13. }
  14. interface TabViewComponentProps {
  15. titles: string[];
  16. reservationAfter2025: any;
  17. passingThisPromotionToBell: any;
  18. }
  19. const NotificationRow = ({
  20. promotionObj,
  21. index,
  22. viewedNotifications
  23. }: {
  24. promotionObj: any;
  25. index: number;
  26. viewedNotifications: any;
  27. }) => {
  28. const isViewed = viewedNotifications.some(
  29. (notification: any) => notification.id === promotionObj.id && notification.type === 'promotion'
  30. );
  31. const handlePress = async () => {
  32. // Mark promotion as viewed
  33. await notificationStorage.markAsViewed(promotionObj.id, 'promotion');
  34. router.push({
  35. pathname: '/notificationDetailPage',
  36. params: { promotion: JSON.stringify(promotionObj) }
  37. });
  38. };
  39. return (
  40. <Pressable
  41. onPress={handlePress}
  42. style={{ opacity: isViewed ? 0.5 : 1 }}
  43. className={`flex flex-col w-full bg-white space-y-2 p-2 ${index % 2 === 0 ? 'bg-[#e7f2f8]' : 'bg-white'}`}
  44. >
  45. <View className="flex flex-row justify-between">
  46. <Text className="font-[500] text-base">{promotionObj.title}</Text>
  47. <Text>{formatToChineseDateTime(promotionObj.createdAt)}</Text>
  48. </View>
  49. <View>
  50. <Text numberOfLines={2} ellipsizeMode="tail" className="text-sm">
  51. {promotionObj.text}
  52. </Text>
  53. </View>
  54. </Pressable>
  55. );
  56. };
  57. const ReservationRow = ({
  58. reservationObj,
  59. index,
  60. viewedNotifications
  61. }: {
  62. reservationObj: any;
  63. index: number;
  64. viewedNotifications: any;
  65. }) => {
  66. const isViewed = viewedNotifications.some(
  67. (notification: any) => notification.id === reservationObj.id && notification.type === 'reservation'
  68. );
  69. const handlePress = async () => {
  70. await notificationStorage.markAsViewed(reservationObj.id, 'reservation');
  71. if (reservationObj.status.id === '7') {
  72. router.push({ pathname: '/chargingPage' });
  73. } else {
  74. router.push({ pathname: '/chargingDetailPage', params: { promotion: JSON.stringify(reservationObj) } });
  75. }
  76. };
  77. const title =
  78. reservationObj.status.id === '8' || reservationObj.status.id === '13'
  79. ? '充電完成'
  80. : reservationObj.status.id === '7'
  81. ? '充電進行中'
  82. : '';
  83. const text =
  84. reservationObj.status.id === '8' || reservationObj.status.id === '13'
  85. ? '親愛的用戶,您的愛車已充滿電完成!請盡快駛離充電站,以便其他車輛使用。感謝您的配合!'
  86. : reservationObj.status.id === '7'
  87. ? `您的車輛正在充電中,當時充電百分比為${reservationObj.Soc}%。詳情請按我進入充電頁面查看。`
  88. : '';
  89. return (
  90. <Pressable
  91. onPress={handlePress}
  92. style={{ opacity: isViewed ? 0.5 : 1 }}
  93. className={`flex flex-col w-full bg-white space-y-2 p-2 ${index % 2 === 0 ? 'bg-[#e7f2f8]' : 'bg-white'}`}
  94. >
  95. <View className="flex flex-row justify-between">
  96. <Text className={`font-[400] text-base ${reservationObj.status.id === '7' ? 'font-[700]' : ''}`}>
  97. {title}
  98. </Text>
  99. <Text>{formatToChineseDateTime(reservationObj.createdAt)}</Text>
  100. </View>
  101. <View>
  102. <Text numberOfLines={2} ellipsizeMode="tail" className="text-sm">
  103. {text}
  104. </Text>
  105. </View>
  106. </Pressable>
  107. );
  108. };
  109. const FirstRoute = ({ loading, reservationAfter2025 }: { loading: boolean; reservationAfter2025: any }) => {
  110. const [viewedNotifications, setViewedNotifications] = useState([]);
  111. useFocusEffect(
  112. useCallback(() => {
  113. const getViewedNotifications = async () => {
  114. const notifications = await notificationStorage.getViewedNotifications();
  115. setViewedNotifications(notifications);
  116. };
  117. getViewedNotifications();
  118. }, [])
  119. );
  120. return (
  121. <View className="flex-1">
  122. <ScrollView
  123. style={{ flex: 1, backgroundColor: 'white', marginTop: 14 }}
  124. contentContainerStyle={{ paddingBottom: 120 }}
  125. >
  126. <View className="flex-1 flex-col">
  127. {loading ? (
  128. <View className="items-center justify-center">
  129. <ActivityIndicator />
  130. </View>
  131. ) : (
  132. <View className="">
  133. <View>
  134. {reservationAfter2025.length === 0 ? (
  135. <Text className="pl-4">暫時沒有充電資訊。</Text>
  136. ) : (
  137. reservationAfter2025
  138. .sort(
  139. (a: any, b: any) =>
  140. new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()
  141. )
  142. .filter(
  143. (reservationObj: any) =>
  144. reservationObj.status.id === '7' ||
  145. reservationObj.status.id === '8' ||
  146. reservationObj.status.id === '13'
  147. )
  148. .slice(0, 30)
  149. .map((reservationObj: any, index: any) => (
  150. <ReservationRow
  151. reservationObj={reservationObj}
  152. index={index}
  153. key={reservationObj.id}
  154. viewedNotifications={viewedNotifications}
  155. />
  156. ))
  157. )}
  158. </View>
  159. </View>
  160. )}
  161. </View>
  162. </ScrollView>
  163. </View>
  164. );
  165. };
  166. const SecondRoute = ({ promotions, loading }: { promotions: TabItem[]; loading: boolean }) => {
  167. const [viewedNotifications, setViewedNotifications] = useState([]);
  168. useFocusEffect(
  169. useCallback(() => {
  170. const getViewedNotifications = async () => {
  171. const notifications = await notificationStorage.getViewedNotifications();
  172. setViewedNotifications(notifications);
  173. };
  174. getViewedNotifications();
  175. }, [])
  176. );
  177. return (
  178. <ScrollView
  179. style={{ flex: 1, backgroundColor: 'white', marginTop: 14 }}
  180. contentContainerStyle={{ paddingBottom: 120 }}
  181. >
  182. <View className="flex-1 flex-col">
  183. {loading ? (
  184. <View className="items-center justify-center">
  185. <ActivityIndicator />
  186. </View>
  187. ) : (
  188. <View className="">
  189. <View>
  190. {promotions.length === 0 ? (
  191. <Text className="pl-4">暫時沒有通知消息。</Text>
  192. ) : (
  193. promotions
  194. .sort(
  195. (a: any, b: any) =>
  196. new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()
  197. )
  198. .slice(0, 30)
  199. .map((promotionObj: any, index: any) => (
  200. <NotificationRow
  201. promotionObj={promotionObj}
  202. index={index}
  203. key={promotionObj.id}
  204. viewedNotifications={viewedNotifications}
  205. />
  206. ))
  207. )}
  208. </View>
  209. </View>
  210. )}
  211. </View>
  212. </ScrollView>
  213. );
  214. };
  215. const NotificationTabView: React.FC<TabViewComponentProps> = ({
  216. titles,
  217. reservationAfter2025,
  218. passingThisPromotionToBell
  219. }) => {
  220. const layout = useWindowDimensions();
  221. const [loading, setLoading] = useState(false);
  222. //fetch promotion data
  223. // useEffect(() => {
  224. // const fetchMainPromotion = async () => {
  225. // try {
  226. // setLoading(true);
  227. // const response = await chargeStationService.getAdvertise();
  228. // if (response) {
  229. // setMainPromotion(response);
  230. // }
  231. // } catch (error) {
  232. // console.log('Error fetching promotion:', error);
  233. // } finally {
  234. // setLoading(false);
  235. // }
  236. // };
  237. // fetchMainPromotion();
  238. // }, []);
  239. // const handleCouponClick = async (clickedCoupon: string) => {
  240. // Alert.alert(
  241. // '立即使用優惠券', // Title
  242. // '按確認打開相機,掃描充電站上的二維碼以使用優惠券', // Message
  243. // [
  244. // {
  245. // text: '取消',
  246. // style: 'cancel'
  247. // },
  248. // {
  249. // text: '確認',
  250. // onPress: () => router.push('scanQrPage')
  251. // }
  252. // ]
  253. // );
  254. // };
  255. const renderScene = useCallback(
  256. ({ route }: { route: any }) => {
  257. switch (route.key) {
  258. case 'firstRoute':
  259. return <FirstRoute loading={loading} reservationAfter2025={reservationAfter2025} />;
  260. case 'secondRoute':
  261. return <SecondRoute promotions={passingThisPromotionToBell} loading={loading} />;
  262. default:
  263. return null;
  264. }
  265. },
  266. [loading]
  267. );
  268. const [routes] = React.useState([
  269. { key: 'firstRoute', title: titles[0] },
  270. { key: 'secondRoute', title: titles[1] }
  271. ]);
  272. const [index, setIndex] = React.useState(0);
  273. const renderTabBar = (props: any) => (
  274. <TabBar
  275. {...props}
  276. renderLabel={({ route, focused }) => (
  277. <Text
  278. style={{
  279. color: focused ? '#025c72' : '#888888',
  280. fontWeight: focused ? '900' : 'thin',
  281. fontSize: 20
  282. }}
  283. >
  284. {route.title}
  285. </Text>
  286. )}
  287. indicatorStyle={{
  288. backgroundColor: '#025c72'
  289. }}
  290. style={{
  291. backgroundColor: 'white',
  292. borderColor: '#DBE4E8',
  293. elevation: 0,
  294. marginHorizontal: 15,
  295. borderBottomWidth: 0.5
  296. }}
  297. />
  298. );
  299. return (
  300. <TabView
  301. navigationState={{ index, routes }}
  302. renderScene={renderScene}
  303. onIndexChange={setIndex}
  304. initialLayout={{ width: layout.width }}
  305. renderTabBar={renderTabBar}
  306. />
  307. );
  308. };
  309. export default NotificationTabView;
  310. const styles = StyleSheet.create({
  311. container: { flexDirection: 'row' },
  312. image: { width: 100, height: 100, margin: 15, borderRadius: 10 },
  313. textContainer: { flexDirection: 'column', gap: 8, marginTop: 20 },
  314. floatingButton: {
  315. elevation: 5,
  316. shadowColor: '#000',
  317. shadowOffset: { width: 0, height: 2 },
  318. shadowOpacity: 0.25,
  319. shadowRadius: 3.84
  320. }
  321. });