uberUploadPageComponent.tsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. // const fileInfo = await FileSystem.getInfoAsync(fileUri);
  30. // if (fileInfo.size > MAX_FILE_SIZE) {
  31. // Alert.alert('錯誤', '檔案大小不得超過3MB');
  32. // return;
  33. // }
  34. setImage(fileUri);
  35. }
  36. };
  37. const uploadImage = async () => {
  38. if (!image) {
  39. Alert.alert('錯誤', '請先選擇一張圖片');
  40. return;
  41. }
  42. setIsLoading(true);
  43. try {
  44. const fileInfo = await FileSystem.getInfoAsync(image);
  45. if (!fileInfo.exists) {
  46. Alert.alert('錯誤', '無法讀取所選圖片');
  47. return;
  48. }
  49. const userInfo = await authenticationService.getUserInfo();
  50. if (!userInfo || !userInfo.data || !userInfo.data.id) {
  51. Alert.alert('錯誤', '無法取得使用者資訊');
  52. return;
  53. }
  54. const userId = userInfo?.data.id;
  55. const timestamp = new Date().getTime();
  56. const fileExtension = image.split('.').pop()?.toLowerCase() || 'jpg';
  57. const customFileName = `uber_${userId}_${timestamp}.${fileExtension}`;
  58. const result = await authenticationService.uploadUberImage(image, customFileName);
  59. if (result) {
  60. Alert.alert('成功', '證明上傳成功');
  61. router.push('uberUploadCompletePage');
  62. } else {
  63. Alert.alert('錯誤', result.message || '上傳失敗,請稍後再試');
  64. }
  65. } catch (error) {
  66. console.error('Error uploading image:', error);
  67. console.error('Status:', error.response?.status);
  68. console.error('Headers:', error.response?.headers);
  69. Alert.alert('錯誤', '上傳時發生錯誤,請稍後再試');
  70. throw error;
  71. } finally {
  72. setIsLoading(false);
  73. }
  74. };
  75. return (
  76. <SafeAreaView className="flex-1 bg-white" edges={['top', 'right', 'left']}>
  77. <ScrollView style={{ flex: 1 }} className="mx-[5%]" showsVerticalScrollIndicator={false}>
  78. <View style={{ marginTop: 25 }}>
  79. <Pressable
  80. onPress={() => {
  81. if (router.canGoBack()) {
  82. router.back();
  83. } else {
  84. router.replace('/accountMainPage');
  85. }
  86. }}
  87. >
  88. <PreviousPageBlackSvg />
  89. </Pressable>
  90. <Text style={{ fontSize: 45, marginVertical: 25 }}>上傳證明</Text>
  91. <View className="space-y-2">
  92. <Text style={{ fontSize: 22 }}>請上傳Uber司機的證明圖片</Text>
  93. <Text style={{ fontSize: 18 }}>• 司機名稱</Text>
  94. <Text style={{ fontSize: 18 }}>• 車牌</Text>
  95. <Text style={{ fontSize: 18 }}>• 車輛品牌及型號</Text>
  96. </View>
  97. <View className="mt-5 ">
  98. <Text style={{ fontSize: 22, marginBottom: 9 }}>上傳範例</Text>
  99. <View className="flex-row w-full justify-between">
  100. <Image
  101. source={require('../../assets/uberUploadSample1.png')}
  102. resizeMode="contain"
  103. className="flex-1 h-auto"
  104. style={{ aspectRatio: 1 }}
  105. />
  106. <Image
  107. source={require('../../assets/uberUploadSample2.png')}
  108. resizeMode="contain"
  109. className="flex-1 h-auto"
  110. style={{ aspectRatio: 1 }}
  111. />
  112. </View>
  113. {image ? (
  114. <Pressable onPress={pickImage} disabled={true}>
  115. <View
  116. style={{ borderWidth: 1 }}
  117. className="border-[#EEEEEE] rounded-2xl items-center justify-center p-4 "
  118. >
  119. <Text className="text-xl text-[#02677d] text-center ">上傳成功</Text>
  120. </View>
  121. </Pressable>
  122. ) : (
  123. <Pressable onPress={pickImage}>
  124. <View
  125. style={{ borderWidth: 1 }}
  126. className="border-[#EEEEEE] rounded-2xl items-center justify-center p-4 "
  127. >
  128. <Text style={{ fontSize: 48 }} className="text-[#02677d] text-center">
  129. +
  130. </Text>
  131. <Text className="text-xl text-[#02677d] mb-6 text-center ">上傳圖片</Text>
  132. </View>
  133. </Pressable>
  134. )}
  135. <View className="mt-4">
  136. <NormalButton
  137. title={
  138. isLoading ? (
  139. <ActivityIndicator color="white" />
  140. ) : (
  141. <Text className="text-xl text-white">提交</Text>
  142. )
  143. }
  144. onPress={uploadImage}
  145. disabled={isLoading}
  146. />
  147. </View>
  148. </View>
  149. </View>
  150. </ScrollView>
  151. </SafeAreaView>
  152. );
  153. };
  154. export default UberUploadPageComponent;