selectCouponPageComponent.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import {
  2. View,
  3. Text,
  4. Pressable,
  5. Dimensions,
  6. ImageSourcePropType,
  7. useWindowDimensions,
  8. ScrollView,
  9. ActivityIndicator,
  10. StyleSheet
  11. } from 'react-native';
  12. import { SafeAreaView } from 'react-native-safe-area-context';
  13. import { router } from 'expo-router';
  14. import { CrossLogoSvg } from '../global/SVG';
  15. import { useEffect, useState } from 'react';
  16. import { walletService } from '../../service/walletService';
  17. import { SceneMap, TabBar, TabView } from 'react-native-tab-view';
  18. import React from 'react';
  19. import { formatCouponDate } from '../../util/lib';
  20. import useCouponStore from '../../providers/coupon_store';
  21. export const SelectIndividualCouponComponent = ({
  22. title,
  23. price,
  24. detail,
  25. date,
  26. redeemCode,
  27. disabled = false
  28. }: {
  29. title: string;
  30. price: string;
  31. detail: string;
  32. date: string;
  33. redeemCode: string;
  34. disabled: boolean;
  35. }) => {
  36. const setSelectedCouponName = useCouponStore((state) => state.setSelectedCouponName);
  37. const setSelectedCouponRedeemCode = useCouponStore((state) => state.setSelectedCouponRedeemCode);
  38. const setSelectedCouponPrice = useCouponStore((state) => state.setSelectedCouponPrice);
  39. return (
  40. <Pressable
  41. disabled={disabled}
  42. onPress={() => {
  43. setSelectedCouponRedeemCode(redeemCode);
  44. setSelectedCouponName(title);
  45. setSelectedCouponPrice(price);
  46. router.push({
  47. pathname: '/paymentSummaryPage'
  48. });
  49. }}
  50. >
  51. <View className="bg-[#e7f2f8] h-[124px] rounded-xl flex-row mb-3">
  52. <View className="bg-white mx-3 my-3 w-[28%] rounded-xl">
  53. <View className="flex-row justify-center items-center pr-4 pt-4 ">
  54. <Text className="color-[#02677d] text-2xl pl-2 pr-1">$</Text>
  55. <Text className="color-[#02677d] text-3xl font-bold" adjustsFontSizeToFit={true}>
  56. {price}
  57. </Text>
  58. </View>
  59. <View className="items-center justify-center">
  60. <Text className="text-base mt-1">{title}</Text>
  61. </View>
  62. </View>
  63. {/* //dash line */}
  64. <View style={{ overflow: 'hidden' }}>
  65. <View
  66. style={{
  67. borderStyle: 'dashed',
  68. borderWidth: 1,
  69. borderColor: '#CCCCCC',
  70. margin: -1,
  71. width: 0,
  72. marginRight: 0,
  73. height: '100%'
  74. }}
  75. >
  76. <View style={{ height: 60 }}></View>
  77. </View>
  78. </View>
  79. <View className="flex-col flex-1 m-[5%] justify-center ">
  80. <Text className="text-lg">{title}</Text>
  81. <Text className="color-[#888888] text-sm">{detail}</Text>
  82. <View className="flex-row items-center ">
  83. <Text className="text-base">有效期 </Text>
  84. <Text className="text-base font-bold text-[#02677d]">{date}</Text>
  85. </View>
  86. </View>
  87. </View>
  88. </Pressable>
  89. );
  90. };
  91. export interface TabItem {
  92. imgURL: ImageSourcePropType;
  93. date: string;
  94. time: string;
  95. chargeStationName: string;
  96. chargeStationAddress: string;
  97. distance: string;
  98. }
  99. interface TabViewComponentProps {
  100. titles: string[];
  101. }
  102. export const SelectCouponTabViewComponent: React.FC<TabViewComponentProps> = ({ titles }) => {
  103. const layout = useWindowDimensions();
  104. const [loading, setLoading] = useState(false);
  105. const [coupons, setCoupons] = useState([]);
  106. const [userID, setUserID] = useState('');
  107. useEffect(() => {
  108. const fetchData = async () => {
  109. try {
  110. setLoading(true);
  111. const info = await walletService.getCustomerInfo();
  112. const coupon = await walletService.getCouponForSpecificUser(userID);
  113. setUserID(info.id);
  114. setCoupons(coupon);
  115. } catch (error) {
  116. } finally {
  117. setLoading(false);
  118. }
  119. };
  120. fetchData();
  121. }, []);
  122. //tab 1
  123. const FirstRoute = () => (
  124. <ScrollView style={{ flex: 1, backgroundColor: 'white', marginTop: 14 }}>
  125. <View className="flex-1 flex-col">
  126. {loading ? (
  127. <View className="items-center justify-center">
  128. <ActivityIndicator />
  129. </View>
  130. ) : (
  131. <View>
  132. {coupons.filter(
  133. (coupon) => coupon.is_consumed === false && new Date(coupon.expire_date) > new Date()
  134. ).length === 0 ? (
  135. <Text className="pl-4">暫時戶口沒有優惠券。</Text>
  136. ) : (
  137. coupons
  138. .filter(
  139. (coupon) =>
  140. coupon.is_consumed === false && new Date(coupon.expire_date) > new Date()
  141. )
  142. .slice(0, 2)
  143. .map((coupon, index) => (
  144. <SelectIndividualCouponComponent
  145. key={`${coupon.id}-${index}`}
  146. title={coupon.coupon.name}
  147. price={coupon.coupon.amount}
  148. detail={coupon.coupon.description}
  149. date={formatCouponDate(coupon.expire_date)}
  150. redeemCode={coupon.id}
  151. disabled={false}
  152. />
  153. ))
  154. )}
  155. </View>
  156. )}
  157. </View>
  158. </ScrollView>
  159. );
  160. //tab 2
  161. const SecondRoute = () => (
  162. <ScrollView style={{ flex: 1, backgroundColor: 'white', marginTop: 14 }}>
  163. <View className="flex-1 flex-col">
  164. {coupons
  165. .filter((coupon) => coupon.is_consumed === true || new Date(coupon.expire_date) < new Date())
  166. .slice(0, 2)
  167. .map((coupon, index) => (
  168. <SelectIndividualCouponComponent
  169. key={index}
  170. title={coupon.name}
  171. price={coupon.amount}
  172. detail={coupon.description}
  173. date={formatCouponDate(coupon.expire_date)}
  174. redeemCode={coupon.id}
  175. disabled={true}
  176. />
  177. ))}
  178. </View>
  179. </ScrollView>
  180. );
  181. const renderScene = SceneMap({
  182. firstRoute: FirstRoute,
  183. secondRoute: SecondRoute
  184. });
  185. const [routes] = React.useState([
  186. { key: 'firstRoute', title: titles[0] },
  187. { key: 'secondRoute', title: titles[1] }
  188. ]);
  189. const [index, setIndex] = React.useState(0);
  190. const renderTabBar = (props: any) => (
  191. <TabBar
  192. {...props}
  193. indicatorStyle={{
  194. backgroundColor: '#025c72'
  195. }}
  196. style={{
  197. backgroundColor: 'white',
  198. borderColor: '#DBE4E8',
  199. elevation: 0,
  200. marginHorizontal: 15,
  201. borderBottomWidth: 0.5
  202. }}
  203. />
  204. );
  205. return (
  206. <TabView
  207. navigationState={{ index, routes }}
  208. renderScene={renderScene}
  209. onIndexChange={setIndex}
  210. initialLayout={{ width: layout.width }}
  211. renderTabBar={renderTabBar}
  212. commonOptions={{
  213. label: ({ route, focused }) => (
  214. <Text
  215. style={{
  216. color: focused ? '#025c72' : '#888888',
  217. fontWeight: focused ? '900' : 'thin',
  218. fontSize: 20
  219. }}
  220. >
  221. {route.title}
  222. </Text>
  223. )
  224. }}
  225. />
  226. );
  227. };
  228. const styles = StyleSheet.create({
  229. container: { flexDirection: 'row' },
  230. image: { width: 100, height: 100, margin: 15, borderRadius: 10 },
  231. textContainer: { flexDirection: 'column', gap: 8, marginTop: 20 }
  232. });
  233. const SelectCouponPageComponent = () => {
  234. const screenHeight = Dimensions.get('window').height;
  235. return (
  236. <SafeAreaView className="flex-1 bg-white" edges={['top', 'right', 'left']}>
  237. <View style={{ minHeight: screenHeight, flex: 1 }}>
  238. <View className="mx-[5%]" style={{ marginTop: 25 }}>
  239. <Pressable
  240. onPress={() => {
  241. if (router.canGoBack()) {
  242. router.back();
  243. } else {
  244. router.replace('/accountMainPage');
  245. }
  246. }}
  247. >
  248. <CrossLogoSvg />
  249. </Pressable>
  250. <Text style={{ fontSize: 45, marginVertical: 25 }}>選擇優惠券</Text>
  251. </View>
  252. <View className="flex-1 ">
  253. <SelectCouponTabViewComponent titles={['可用優惠券', '已使用/失效']} />
  254. </View>
  255. </View>
  256. </SafeAreaView>
  257. );
  258. };
  259. export default SelectCouponPageComponent;