displayedOnlyCouponTabView.tsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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.permission == true &&
  60. coupon.is_consumed === false &&
  61. new Date(coupon.expire_date) > new Date()
  62. ).length === 0 ? (
  63. <Text className="pl-4">暫時戶口沒有優惠券。</Text>
  64. ) : (
  65. coupons
  66. .filter(
  67. (coupon: any) =>
  68. coupon.permission == true &&
  69. coupon.is_consumed === false &&
  70. new Date(coupon.expire_date) > new Date()
  71. )
  72. .sort(
  73. (a: any, b: any) =>
  74. new Date(a.expire_date).getTime() - new Date(b.expire_date).getTime()
  75. )
  76. .slice(0, 30)
  77. .map((coupon: any, index: any) => (
  78. <IndividualCouponComponent
  79. onCouponClick={() =>
  80. handleCouponClick(coupon.coupon.name, coupon.coupon.description)
  81. }
  82. // key={coupon.redeem_code}
  83. key={`${coupon.id}-${index}`}
  84. title={coupon.coupon.name}
  85. price={coupon.coupon.amount}
  86. detail={coupon.coupon.description}
  87. date={formatCouponDate(coupon.expire_date)}
  88. setOpacity={false}
  89. noCircle={true}
  90. redeem_code={coupon.id}
  91. />
  92. ))
  93. )}
  94. </View>
  95. </View>
  96. )}
  97. </View>
  98. </ScrollView>
  99. </View>
  100. );
  101. };
  102. const SecondRoute = ({ coupons }: { coupons: any }) => (
  103. <ScrollView style={{ flex: 1, backgroundColor: 'white', marginTop: 14 }}>
  104. <View className="flex-1 flex-col">
  105. {coupons
  106. .filter((coupon: any) => coupon.is_consumed === true || new Date(coupon.expire_date) < new Date())
  107. .slice(0, 30)
  108. .map((coupon: any, index: any) => (
  109. <IndividualCouponComponent
  110. key={`${coupon.id}-${index}`}
  111. title={coupon.coupon.name}
  112. price={coupon.coupon.amount}
  113. detail={coupon.coupon.description}
  114. date={formatCouponDate(coupon.expire_date)}
  115. setOpacity={true}
  116. noCircle={true}
  117. />
  118. ))}
  119. </View>
  120. </ScrollView>
  121. );
  122. const DisplayedOnlyCouponTabView: React.FC<TabViewComponentProps> = ({ titles }) => {
  123. const layout = useWindowDimensions();
  124. const [loading, setLoading] = useState(false);
  125. const [coupons, setCoupons] = useState([]);
  126. const [userID, setUserID] = useState('');
  127. const [modalVisible, setModalVisible] = useState(false);
  128. const [useableCoupons, setUseableCoupons] = useState([]);
  129. useEffect(() => {
  130. const fetchData = async () => {
  131. try {
  132. setLoading(true);
  133. const info = await walletService.getCustomerInfo();
  134. const coupon = await walletService.getCouponForSpecificUser(info.id);
  135. const useableConpon = coupon.filter((couponObj: any) => {
  136. const today = new Date();
  137. if (couponObj.expire_date === null) {
  138. return couponObj.is_consumed === false;
  139. }
  140. const expireDate = new Date(couponObj.expire_date);
  141. return expireDate > today && couponObj.is_consumed === false;
  142. });
  143. setUserID(info.id);
  144. setCoupons(coupon);
  145. setUseableCoupons(useableConpon);
  146. } catch (error) {
  147. console.log(error);
  148. } finally {
  149. setLoading(false);
  150. }
  151. };
  152. fetchData();
  153. }, []);
  154. const handleCouponClick = async (clickedCoupon: string, clickedCouponDescription: string) => {
  155. router.push({
  156. pathname: '/couponDetailPage',
  157. params: {
  158. couponName: clickedCoupon,
  159. couponDescription: clickedCouponDescription
  160. }
  161. });
  162. };
  163. const renderScene = useCallback(
  164. ({ route }: { route: any }) => {
  165. switch (route.key) {
  166. case 'firstRoute':
  167. return (
  168. <FirstRoute coupons={useableCoupons} loading={loading} handleCouponClick={handleCouponClick} />
  169. );
  170. case 'secondRoute':
  171. return <SecondRoute coupons={coupons} />;
  172. default:
  173. return null;
  174. }
  175. },
  176. [coupons, loading, handleCouponClick]
  177. );
  178. const [routes] = React.useState([
  179. { key: 'firstRoute', title: titles[0] },
  180. { key: 'secondRoute', title: titles[1] }
  181. ]);
  182. const [index, setIndex] = React.useState(0);
  183. const renderTabBar = (props: any) => (
  184. <TabBar
  185. {...props}
  186. indicatorStyle={{
  187. backgroundColor: '#025c72'
  188. }}
  189. style={{
  190. backgroundColor: 'white',
  191. borderColor: '#DBE4E8',
  192. elevation: 0,
  193. marginHorizontal: 15,
  194. borderBottomWidth: 0.5
  195. }}
  196. />
  197. );
  198. return (
  199. <TabView
  200. navigationState={{ index, routes }}
  201. renderScene={renderScene}
  202. onIndexChange={setIndex}
  203. initialLayout={{ width: layout.width }}
  204. renderTabBar={renderTabBar}
  205. commonOptions={{
  206. label: ({ route, focused }) => (
  207. <Text
  208. style={{
  209. color: focused ? '#025c72' : '#888888',
  210. fontWeight: focused ? '900' : 'thin',
  211. fontSize: 20
  212. }}
  213. >
  214. {route.title}
  215. </Text>
  216. )
  217. }}
  218. />
  219. );
  220. };
  221. export default DisplayedOnlyCouponTabView;
  222. const styles = StyleSheet.create({
  223. container: { flexDirection: 'row' },
  224. image: { width: 100, height: 100, margin: 15, borderRadius: 10 },
  225. textContainer: { flexDirection: 'column', gap: 8, marginTop: 20 },
  226. floatingButton: {
  227. elevation: 5,
  228. shadowColor: '#000',
  229. shadowOffset: { width: 0, height: 2 },
  230. shadowOpacity: 0.25,
  231. shadowRadius: 3.84
  232. }
  233. });