couponDetailPage.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // app/(auth)/(tabs)/(account)/couponDetailPage.tsx
  2. import { View, Text, ScrollView, Pressable, Alert } from 'react-native';
  3. import React, { useContext, useEffect, useState } from 'react';
  4. import { SafeAreaView } from 'react-native-safe-area-context';
  5. import { router } from 'expo-router';
  6. import { CrossLogoSvg } from '../../../../component/global/SVG';
  7. import { AuthContext } from '../../../../context/AuthProvider';
  8. import NormalInput from '../../../../component/global/normal_input';
  9. import NormalButton from '../../../../component/global/normal_button';
  10. import { authenticationService } from '../../../../service/authService';
  11. import * as SecureStore from 'expo-secure-store';
  12. import { useLocalSearchParams } from 'expo-router';
  13. import { useTranslation } from '../../../../util/hooks/useTranslation';
  14. const CouponDetailPage = () => {
  15. const { t } = useTranslation();
  16. const { user, setUser } = useContext(AuthContext);
  17. const [token, setToken] = useState<string | null>(null);
  18. const [name, setName] = useState<string | null>(null);
  19. const [isLoading, setIsLoading] = useState(false);
  20. const [error, setError] = useState<string | null>(null);
  21. const handleChangeName = async () => {
  22. if (!name) {
  23. setError(t('coupon_detail.enter_name_error'));
  24. return;
  25. }
  26. if (!token) {
  27. setError(t('coupon_detail.no_token_error'));
  28. return;
  29. }
  30. setError(null);
  31. setIsLoading(true);
  32. try {
  33. const success = await authenticationService.changeName(name, token);
  34. if (success) {
  35. if (user) {
  36. setUser({
  37. ...user,
  38. nickname: name
  39. });
  40. }
  41. router.replace('accountMainPage');
  42. } else {
  43. setError(t('coupon_detail.update_failed'));
  44. }
  45. } catch (error) {
  46. console.error('Error changing name:', error);
  47. setError(t('coupon_detail.general_error'));
  48. } finally {
  49. setIsLoading(false);
  50. }
  51. };
  52. const { couponName, couponDescription } = useLocalSearchParams();
  53. return (
  54. <SafeAreaView className="flex-1 bg-white" edges={['top', 'right', 'left']}>
  55. <ScrollView className="flex-1 mx-[5%]">
  56. <View style={{ marginTop: 25 }}>
  57. <Pressable
  58. onPress={() => {
  59. if (router.canGoBack()) {
  60. router.back();
  61. } else {
  62. router.replace('/accountMainPage');
  63. }
  64. }}
  65. >
  66. <CrossLogoSvg />
  67. </Pressable>
  68. <Text style={{ fontSize: 45, marginVertical: 25 }}>{t('coupon_detail.title')}</Text>
  69. <Text className="text-2xl pb-4">{couponName}</Text>
  70. <Text className="text-lg pb-8">{couponDescription}</Text>
  71. </View>
  72. <NormalButton
  73. title={
  74. <Text
  75. style={{
  76. color: 'white',
  77. fontSize: 16,
  78. fontWeight: '800'
  79. }}
  80. >
  81. {t('coupon_detail.use_coupon')}
  82. </Text>
  83. }
  84. onPress={() => {
  85. Alert.alert(
  86. t('coupon_detail.use_coupon'), // Title
  87. t('coupon_detail.scan_qr_message'), // Message
  88. [
  89. {
  90. text: t('common.cancel'),
  91. style: 'cancel'
  92. },
  93. {
  94. text: t('common.confirm'),
  95. onPress: () => router.push('scanQrPage')
  96. }
  97. ]
  98. );
  99. }}
  100. extendedStyle={{ padding: 24 }}
  101. />
  102. </ScrollView>
  103. </SafeAreaView>
  104. );
  105. };
  106. export default CouponDetailPage;