couponTabView.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. } from 'react-native';
  11. import { TabView, SceneMap, TabBar } from 'react-native-tab-view';
  12. import { IndividualCouponComponent } from '../accountPages/walletPageComponent';
  13. import { formatCouponDate } from '../../util/lib';
  14. import { useEffect, useState } from 'react';
  15. import { walletService } from '../../service/walletService';
  16. export interface TabItem {
  17. imgURL: ImageSourcePropType;
  18. date: string;
  19. time: string;
  20. chargeStationName: string;
  21. chargeStationAddress: string;
  22. distance: string;
  23. }
  24. interface TabViewComponentProps {
  25. titles: string[];
  26. }
  27. const CouponTabViewComponent: React.FC<TabViewComponentProps> = ({
  28. titles
  29. }) => {
  30. const [coupons, setCoupons] = useState([]);
  31. const layout = useWindowDimensions();
  32. useEffect(() => {
  33. try {
  34. const fetchCoupon = async () => {
  35. const coupon = await walletService.getCoupon();
  36. setCoupons(coupon);
  37. };
  38. fetchCoupon();
  39. } catch (error) {
  40. console.log(error);
  41. }
  42. }, []);
  43. //tab 1
  44. const FirstRoute = () => (
  45. <ScrollView
  46. style={{ flex: 1, backgroundColor: 'white', marginTop: 14 }}
  47. >
  48. <View className="flex-1 flex-col">
  49. {coupons
  50. .filter(
  51. (coupon) =>
  52. coupon.is_consumned === false &&
  53. new Date(coupon.expire_date) > new Date()
  54. )
  55. .map((coupon, index) => (
  56. <IndividualCouponComponent
  57. key={index}
  58. title={coupon.name}
  59. price={coupon.amount}
  60. detail={coupon.description}
  61. date={formatCouponDate(coupon.expire_date)}
  62. />
  63. ))}
  64. </View>
  65. </ScrollView>
  66. );
  67. //tab 2
  68. const SecondRoute = () => (
  69. <ScrollView
  70. style={{ flex: 1, backgroundColor: 'white', marginTop: 14 }}
  71. >
  72. <View className="flex-1 flex-col">
  73. {coupons
  74. .filter(
  75. (coupon) =>
  76. coupon.is_consumned ||
  77. new Date(coupon.expire_date) < new Date()
  78. )
  79. .map((coupon, index) => (
  80. <IndividualCouponComponent
  81. key={index}
  82. title={coupon.name}
  83. price={coupon.amount}
  84. detail={coupon.description}
  85. date={formatCouponDate(coupon.expire_date)}
  86. />
  87. ))}
  88. </View>
  89. </ScrollView>
  90. );
  91. const renderScene = SceneMap({
  92. firstRoute: FirstRoute,
  93. secondRoute: SecondRoute
  94. });
  95. const [routes] = React.useState([
  96. { key: 'firstRoute', title: titles[0] },
  97. { key: 'secondRoute', title: titles[1] }
  98. ]);
  99. const [index, setIndex] = React.useState(0);
  100. const renderTabBar = (props: any) => (
  101. <TabBar
  102. {...props}
  103. renderLabel={({ route, focused }) => (
  104. <Text
  105. style={{
  106. color: focused ? '#025c72' : '#888888',
  107. fontWeight: focused ? '900' : 'thin',
  108. fontSize: 20
  109. }}
  110. >
  111. {route.title}
  112. </Text>
  113. )}
  114. indicatorStyle={{
  115. backgroundColor: '#025c72'
  116. }}
  117. style={{
  118. backgroundColor: 'white',
  119. borderColor: '#DBE4E8',
  120. elevation: 0,
  121. marginHorizontal: 15,
  122. borderBottomWidth: 0.5
  123. }}
  124. />
  125. );
  126. return (
  127. <TabView
  128. navigationState={{ index, routes }}
  129. renderScene={renderScene}
  130. onIndexChange={setIndex}
  131. initialLayout={{ width: layout.width }}
  132. renderTabBar={renderTabBar}
  133. />
  134. );
  135. };
  136. export default CouponTabViewComponent;
  137. const styles = StyleSheet.create({
  138. container: { flexDirection: 'row' },
  139. image: { width: 100, height: 100, margin: 15, borderRadius: 10 },
  140. textContainer: { flexDirection: 'column', gap: 8, marginTop: 20 }
  141. });