couponDetailPage.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { View, Text, ScrollView, Pressable, Alert } from 'react-native';
  2. import React, { useContext, useEffect, useState } from 'react';
  3. import { SafeAreaView } from 'react-native-safe-area-context';
  4. import { router } from 'expo-router';
  5. import { CrossLogoSvg } from '../../../../component/global/SVG';
  6. import { AuthContext } from '../../../../context/AuthProvider';
  7. import NormalInput from '../../../../component/global/normal_input';
  8. import NormalButton from '../../../../component/global/normal_button';
  9. import { authenticationService } from '../../../../service/authService';
  10. import * as SecureStore from 'expo-secure-store';
  11. import { useLocalSearchParams } from 'expo-router';
  12. const CouponDetailPage = () => {
  13. const { user, setUser } = useContext(AuthContext);
  14. const [token, setToken] = useState<string | null>(null);
  15. const [name, setName] = useState<string | null>(null);
  16. const [isLoading, setIsLoading] = useState(false);
  17. const [error, setError] = useState<string | null>(null);
  18. const handleChangeName = async () => {
  19. if (!name) {
  20. setError('請輸入新的暱稱');
  21. return;
  22. }
  23. if (!token) {
  24. setError('未找到有效的登錄令牌,請重新登錄');
  25. return;
  26. }
  27. setError(null);
  28. setIsLoading(true);
  29. try {
  30. const success = await authenticationService.changeName(name, token);
  31. if (success) {
  32. if (user) {
  33. setUser({
  34. ...user,
  35. nickname: name
  36. });
  37. }
  38. router.replace('accountMainPage');
  39. } else {
  40. setError('更新暱稱失敗,請稍後再試');
  41. }
  42. } catch (error) {
  43. console.error('Error changing name:', error);
  44. setError('發生錯誤,請稍後再試');
  45. } finally {
  46. setIsLoading(false);
  47. }
  48. };
  49. const { couponName, couponDescription } = useLocalSearchParams();
  50. return (
  51. <SafeAreaView className="flex-1 bg-white" edges={['top', 'right', 'left']}>
  52. <ScrollView className="flex-1 mx-[5%]">
  53. <View style={{ marginTop: 25 }}>
  54. <Pressable
  55. onPress={() => {
  56. if (router.canGoBack()) {
  57. router.back();
  58. } else {
  59. router.replace('/accountMainPage');
  60. }
  61. }}
  62. >
  63. <CrossLogoSvg />
  64. </Pressable>
  65. <Text style={{ fontSize: 45, marginVertical: 25 }}>優惠券詳情</Text>
  66. <Text className="text-2xl pb-4">{couponName}</Text>
  67. <Text className="text-lg pb-8">{couponDescription}</Text>
  68. </View>
  69. <NormalButton
  70. title={
  71. <Text
  72. style={{
  73. color: 'white',
  74. fontSize: 16,
  75. fontWeight: '800'
  76. }}
  77. >
  78. 立即使用優惠券
  79. </Text>
  80. }
  81. onPress={() => {
  82. Alert.alert(
  83. '立即使用優惠券', // Title
  84. '按確認打開相機,掃描充電站上的二維碼以使用優惠券', // Message
  85. [
  86. {
  87. text: '取消',
  88. style: 'cancel'
  89. },
  90. {
  91. text: '確認',
  92. onPress: () => router.push('scanQrPage')
  93. }
  94. ]
  95. );
  96. }}
  97. extendedStyle={{ padding: 24 }}
  98. />
  99. </ScrollView>
  100. </SafeAreaView>
  101. );
  102. };
  103. export default CouponDetailPage;