displayedOnlyCouponTabView.tsx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //the size of the TabView will follow its parent-container's size.
  2. import * as React from 'react';
  3. import {
  4. View,
  5. Text,
  6. useWindowDimensions,
  7. StyleSheet,
  8. ImageSourcePropType,
  9. ScrollView,
  10. ActivityIndicator,
  11. Pressable,
  12. Alert
  13. } from 'react-native';
  14. import { TabView, SceneMap, TabBar } from 'react-native-tab-view';
  15. import { IndividualCouponComponent } from '../accountPages/walletPageComponent';
  16. import { formatCouponDate } from '../../util/lib';
  17. import { useCallback, useEffect, useRef, useState } from 'react';
  18. import { walletService } from '../../service/walletService';
  19. import { useChargingStore } from '../../providers/scan_qr_payload_store';
  20. import { chargeStationService } from '../../service/chargeStationService';
  21. import { router } from 'expo-router';
  22. import axios from 'axios';
  23. export interface TabItem {
  24. imgURL: ImageSourcePropType;
  25. date: string;
  26. time: string;
  27. chargeStationName: string;
  28. chargeStationAddress: string;
  29. distance: string;
  30. }
  31. interface TabViewComponentProps {
  32. titles: string[];
  33. }
  34. const FirstRoute = ({
  35. coupons,
  36. loading,
  37. handleCouponClick
  38. }: {
  39. coupons: any;
  40. loading: boolean;
  41. handleCouponClick: (couponName: string, couponDescription: string) => void;
  42. }) => {
  43. return (
  44. <View className="flex-1">
  45. <ScrollView
  46. style={{ flex: 1, backgroundColor: 'white', marginTop: 14 }}
  47. contentContainerStyle={{ paddingBottom: 120 }}
  48. >
  49. <View className="flex-1 flex-col">
  50. {loading ? (
  51. <View className="items-center justify-center">
  52. <ActivityIndicator />
  53. </View>
  54. ) : (
  55. <View className="">
  56. <View>
  57. {coupons.filter(
  58. (coupon: any) =>
  59. coupon.is_consumed === false && new Date(coupon.expire_date) > new Date()
  60. ).length === 0 ? (
  61. <Text className="pl-4">暫時戶口沒有優惠券。</Text>
  62. ) : (
  63. coupons
  64. .filter(
  65. (coupon: any) =>
  66. coupon.is_consumed === false &&
  67. new Date(coupon.expire_date) > new Date()
  68. )
  69. .sort(
  70. (a: any, b: any) =>
  71. new Date(a.expire_date).getTime() - new Date(b.expire_date).getTime()
  72. )
  73. .slice(0, 30)
  74. .map((coupon: any, index: any) => (
  75. <IndividualCouponComponent
  76. onCouponClick={() =>
  77. handleCouponClick(coupon.coupon.name, coupon.coupon.description)
  78. }
  79. // key={coupon.redeem_code}
  80. key={`${coupon.id}-${index}`}
  81. title={coupon.coupon.name}
  82. price={coupon.coupon.amount}
  83. detail={coupon.coupon.description}
  84. date={formatCouponDate(coupon.expire_date)}
  85. setOpacity={false}
  86. noCircle={true}
  87. redeem_code={coupon.id}
  88. />
  89. ))
  90. )}
  91. </View>
  92. </View>
  93. )}
  94. </View>
  95. </ScrollView>
  96. </View>
  97. );
  98. };
  99. const SecondRoute = ({ coupons }: { coupons: any }) => (
  100. <ScrollView style={{ flex: 1, backgroundColor: 'white', marginTop: 14 }}>
  101. <View className="flex-1 flex-col">
  102. {coupons
  103. .filter((coupon: any) => coupon.is_consumed === true || new Date(coupon.expire_date) < new Date())
  104. .slice(0, 30)
  105. .map((coupon: any, index: any) => (
  106. <IndividualCouponComponent
  107. key={`${coupon.id}-${index}`}
  108. title={coupon.coupon.name}
  109. price={coupon.coupon.amount}
  110. detail={coupon.coupon.description}
  111. date={formatCouponDate(coupon.expire_date)}
  112. setOpacity={true}
  113. noCircle={true}
  114. />
  115. ))}
  116. </View>
  117. </ScrollView>
  118. );
  119. const DisplayedOnlyCouponTabView: React.FC<TabViewComponentProps> = ({ titles }) => {
  120. const layout = useWindowDimensions();
  121. const [loading, setLoading] = useState(false);
  122. const [coupons, setCoupons] = useState([]);
  123. const [userID, setUserID] = useState('');
  124. const [modalVisible, setModalVisible] = useState(false);
  125. const [useableCoupons, setUseableCoupons] = useState([]);
  126. useEffect(() => {
  127. const fetchData = async () => {
  128. try {
  129. setLoading(true);
  130. const info = await walletService.getCustomerInfo();
  131. const coupon = await walletService.getCouponForSpecificUser(info.id);
  132. const useableConpon = coupon.filter((couponObj: any) => {
  133. const today = new Date();
  134. if (couponObj.expire_date === null) {
  135. return couponObj.is_consumed === false;
  136. }
  137. const expireDate = new Date(couponObj.expire_date);
  138. return expireDate > today && couponObj.is_consumed === false;
  139. });
  140. setUserID(info.id);
  141. setCoupons(coupon);
  142. setUseableCoupons(useableConpon);
  143. } catch (error) {
  144. console.log(error);
  145. } finally {
  146. setLoading(false);
  147. }
  148. };
  149. fetchData();
  150. }, []);
  151. const handleCouponClick = async (clickedCoupon: string, clickedCouponDescription: string) => {
  152. router.push({
  153. pathname: '/couponDetailPage',
  154. params: {
  155. couponName: clickedCoupon,
  156. couponDescription: clickedCouponDescription
  157. }
  158. });
  159. };
  160. const renderScene = useCallback(
  161. ({ route }: { route: any }) => {
  162. switch (route.key) {
  163. case 'firstRoute':
  164. return (
  165. <FirstRoute coupons={useableCoupons} loading={loading} handleCouponClick={handleCouponClick} />
  166. );
  167. case 'secondRoute':
  168. return <SecondRoute coupons={coupons} />;
  169. default:
  170. return null;
  171. }
  172. },
  173. [coupons, loading, handleCouponClick]
  174. );
  175. const [routes] = React.useState([
  176. { key: 'firstRoute', title: titles[0] },
  177. { key: 'secondRoute', title: titles[1] }
  178. ]);
  179. const [index, setIndex] = React.useState(0);
  180. const renderTabBar = (props: any) => (
  181. <TabBar
  182. {...props}
  183. renderLabel={({ route, focused }) => (
  184. <Text
  185. style={{
  186. color: focused ? '#025c72' : '#888888',
  187. fontWeight: focused ? '900' : 'thin',
  188. fontSize: 20
  189. }}
  190. >
  191. {route.title}
  192. </Text>
  193. )}
  194. indicatorStyle={{
  195. backgroundColor: '#025c72'
  196. }}
  197. style={{
  198. backgroundColor: 'white',
  199. borderColor: '#DBE4E8',
  200. elevation: 0,
  201. marginHorizontal: 15,
  202. borderBottomWidth: 0.5
  203. }}
  204. />
  205. );
  206. return (
  207. <TabView
  208. navigationState={{ index, routes }}
  209. renderScene={renderScene}
  210. onIndexChange={setIndex}
  211. initialLayout={{ width: layout.width }}
  212. renderTabBar={renderTabBar}
  213. />
  214. );
  215. };
  216. export default DisplayedOnlyCouponTabView;
  217. const styles = StyleSheet.create({
  218. container: { flexDirection: 'row' },
  219. image: { width: 100, height: 100, margin: 15, borderRadius: 10 },
  220. textContainer: { flexDirection: 'column', gap: 8, marginTop: 20 },
  221. floatingButton: {
  222. elevation: 5,
  223. shadowColor: '#000',
  224. shadowOffset: { width: 0, height: 2 },
  225. shadowOpacity: 0.25,
  226. shadowRadius: 3.84
  227. }
  228. });