uberUploadPageComponent.tsx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import { View, Text, Pressable, Image, ScrollView, Alert, ActivityIndicator } from 'react-native';
  2. import { SafeAreaView } from 'react-native-safe-area-context';
  3. import { router } from 'expo-router';
  4. import { PreviousPageBlackSvg } from '../global/SVG';
  5. import NormalButton from '../global/normal_button';
  6. import { useEffect, useState } from 'react';
  7. import * as ImagePicker from 'expo-image-picker';
  8. import * as SecureStore from 'expo-secure-store';
  9. import * as FileSystem from 'expo-file-system';
  10. import { authenticationService } from '../../service/authService';
  11. const UberUploadPageComponent = () => {
  12. const [image, setImage] = useState('');
  13. const [isLoading, setIsLoading] = useState(false);
  14. const MAX_FILE_SIZE = 3 * 1024 * 1024;
  15. const pickImage = async () => {
  16. let result = await ImagePicker.launchImageLibraryAsync({
  17. mediaTypes: ImagePicker.MediaTypeOptions.Images,
  18. allowsEditing: true,
  19. aspect: [4, 3],
  20. quality: 1
  21. });
  22. if (!result.canceled) {
  23. const fileUri = result.assets[0].uri;
  24. const fileExtension = fileUri.split('.').pop().toLowerCase();
  25. if (!['jpg', 'jpeg', 'png'].includes(fileExtension)) {
  26. Alert.alert('錯誤', '請選擇 JPG, JPEG 或 PNG 格式的圖片');
  27. return;
  28. }
  29. setImage(fileUri);
  30. }
  31. };
  32. const uploadImage = async () => {
  33. if (!image) {
  34. Alert.alert('錯誤', '請先選擇一張圖片');
  35. return;
  36. }
  37. setIsLoading(true);
  38. try {
  39. const fileInfo = await FileSystem.getInfoAsync(image);
  40. if (!fileInfo.exists) {
  41. Alert.alert('錯誤', '無法讀取所選圖片');
  42. return;
  43. }
  44. const userInfo = await authenticationService.getUserInfo();
  45. if (!userInfo || !userInfo.data || !userInfo.data.id) {
  46. Alert.alert('錯誤', '無法取得使用者資訊');
  47. return;
  48. }
  49. const userId = userInfo?.data.id;
  50. const timestamp = new Date().getTime();
  51. const fileExtension = image.split('.').pop()?.toLowerCase() || 'jpg';
  52. const customFileName = `uber_${userId}_${timestamp}.${fileExtension}`;
  53. const result = await authenticationService.uploadUberImage(image, customFileName);
  54. if (result) {
  55. Alert.alert('成功', '證明上傳成功');
  56. router.push('uberUploadCompletePage');
  57. } else {
  58. Alert.alert('錯誤', result.message || '上傳失敗,請稍後再試');
  59. }
  60. } catch (error) {
  61. console.error('Error uploading image:', error);
  62. console.error('Status:', error.response?.status);
  63. console.error('Headers:', error.response?.headers);
  64. Alert.alert('錯誤', '上傳時發生錯誤,請稍後再試');
  65. throw error;
  66. } finally {
  67. setIsLoading(false);
  68. }
  69. };
  70. return (
  71. <SafeAreaView className="flex-1 bg-white" edges={['top', 'right', 'left']}>
  72. <ScrollView style={{ flex: 1 }} className="mx-[5%]" showsVerticalScrollIndicator={false}>
  73. <View style={{ marginTop: 25 }}>
  74. <Pressable
  75. onPress={() => {
  76. if (router.canGoBack()) {
  77. router.back();
  78. } else {
  79. router.replace('/accountMainPage');
  80. }
  81. }}
  82. >
  83. <PreviousPageBlackSvg />
  84. </Pressable>
  85. <Text style={{ fontSize: 45, marginVertical: 25 }}>上傳證明</Text>
  86. <View className="space-y-2">
  87. <Text style={{ fontSize: 22 }}>請上傳Uber司機的證明圖片</Text>
  88. <Text style={{ fontSize: 18 }}>• 司機名稱</Text>
  89. <Text style={{ fontSize: 18 }}>• 車牌</Text>
  90. <Text style={{ fontSize: 18 }}>• 車輛品牌及型號</Text>
  91. </View>
  92. <View className="mt-5 ">
  93. <Text style={{ fontSize: 22, marginBottom: 9 }}>上傳範例</Text>
  94. <View className="flex-row w-full justify-between">
  95. <Image
  96. source={require('../../assets/uberUploadSample111.png')}
  97. resizeMode="contain"
  98. className="flex-1 h-auto"
  99. style={{ aspectRatio: 1 }}
  100. />
  101. <Image
  102. source={require('../../assets/uberUploadSample222.png')}
  103. resizeMode="contain"
  104. className="flex-1 h-auto"
  105. style={{ aspectRatio: 1 }}
  106. />
  107. </View>
  108. {image ? (
  109. <Pressable onPress={pickImage} disabled={true}>
  110. <View
  111. style={{ borderWidth: 1 }}
  112. className="border-[#EEEEEE] rounded-2xl items-center justify-center p-4 "
  113. >
  114. <Text className="text-xl text-[#02677d] text-center ">上傳成功</Text>
  115. </View>
  116. </Pressable>
  117. ) : (
  118. <Pressable onPress={pickImage}>
  119. <View
  120. style={{ borderWidth: 1 }}
  121. className="border-[#EEEEEE] rounded-2xl items-center justify-center p-4 "
  122. >
  123. <Text style={{ fontSize: 48 }} className="text-[#02677d] text-center">
  124. +
  125. </Text>
  126. <Text className="text-xl text-[#02677d] mb-6 text-center ">上傳圖片</Text>
  127. </View>
  128. </Pressable>
  129. )}
  130. <View className="mt-4">
  131. <NormalButton
  132. title={
  133. isLoading ? (
  134. <ActivityIndicator color="white" />
  135. ) : (
  136. <Text className="text-xl text-white">提交</Text>
  137. )
  138. }
  139. onPress={uploadImage}
  140. disabled={isLoading}
  141. />
  142. </View>
  143. </View>
  144. </View>
  145. </ScrollView>
  146. </SafeAreaView>
  147. );
  148. };
  149. export default UberUploadPageComponent;