|
|
|
@ -1,144 +1,25 @@
|
|
|
|
|
import React, { useState } from 'react';
|
|
|
|
|
import { Pressable, StyleSheet, Text, View, Image, TouchableOpacity, TextInput } from 'react-native';
|
|
|
|
|
import { SafeAreaView } from 'react-native-safe-area-context';
|
|
|
|
|
import TopBar from '../components/TopBar';
|
|
|
|
|
import { addXlsFile } from '../redux/actions/sessions';
|
|
|
|
|
import { useDispatch } from 'react-redux';
|
|
|
|
|
import * as DocumentPicker from 'expo-document-picker';
|
|
|
|
|
import { uploadFiles, DocumentDirectoryPath } from 'react-native-fs';
|
|
|
|
|
import TopBar from '../components/TopBar';
|
|
|
|
|
|
|
|
|
|
export default function NewTrack(props: { navigation: any }) {
|
|
|
|
|
const { navigation } = props;
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
|
|
|
|
|
const [pickedDocument, setPickedDocument] = useState<DocumentPicker.DocumentResult | null>(null);
|
|
|
|
|
const [trackName, setTrackName] = useState('');
|
|
|
|
|
const [sessionName, setSessionName] = useState('');
|
|
|
|
|
|
|
|
|
|
const handlePickDocument = async () => {
|
|
|
|
|
try {
|
|
|
|
|
const result = await DocumentPicker.getDocumentAsync({});
|
|
|
|
|
const result = await DocumentPicker.getDocumentAsync({ type: 'excel/xls' });
|
|
|
|
|
if (result.type === 'success') {
|
|
|
|
|
setPickedDocument(result);
|
|
|
|
|
}
|
|
|
|
|
else if(result.type === 'cancel'){
|
|
|
|
|
console.log("AAA");
|
|
|
|
|
setPickedDocument(null);
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.log(err);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// var files = [
|
|
|
|
|
// {
|
|
|
|
|
// name: "file",
|
|
|
|
|
// filename: "file.jpg",
|
|
|
|
|
// filepath: pickedDocument.uri,
|
|
|
|
|
// filetype: "image/jpeg",
|
|
|
|
|
// },
|
|
|
|
|
// ];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// const handleConfirm = async () => {
|
|
|
|
|
// if (!pickedDocument || !trackName || !sessionName) {
|
|
|
|
|
// return;
|
|
|
|
|
// }
|
|
|
|
|
// const formData = new FormData();
|
|
|
|
|
// formData.append('file', {
|
|
|
|
|
// uri: pickedDocument.uri,
|
|
|
|
|
// type: pickedDocument.type,
|
|
|
|
|
// name: pickedDocument.name,
|
|
|
|
|
// });
|
|
|
|
|
// try {
|
|
|
|
|
// await dispatch(addXlsFile(formData, 'test_PILOTE', 'test@gmail.com', 'test123', sessionName, trackName, 'Training'));
|
|
|
|
|
// navigation.goBack();
|
|
|
|
|
// } catch (error) {
|
|
|
|
|
// console.log('Error - POST FILE', error);
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
// const handleConfirm = async () => {
|
|
|
|
|
// if (!pickedDocument || !trackName || !sessionName) {
|
|
|
|
|
// return;
|
|
|
|
|
// }
|
|
|
|
|
// try {
|
|
|
|
|
// const file = new File([await pickedDocument.uri], pickedDocument.name, { type: pickedDocument.type });
|
|
|
|
|
// const url = 'https://r-dash.azurewebsites.net/File?pseudoPilote=test_PILOTE&nameSession=weekend&nameCircuit=test_CIRCUIT&typeSession=Training';
|
|
|
|
|
// const options = {
|
|
|
|
|
// method: 'POST',
|
|
|
|
|
// body: file,
|
|
|
|
|
// headers: {
|
|
|
|
|
// 'Content-Type': 'application/octet-stream',
|
|
|
|
|
// },
|
|
|
|
|
// };
|
|
|
|
|
// await fetch(url, options);
|
|
|
|
|
// navigation.goBack();
|
|
|
|
|
// } catch (error) {
|
|
|
|
|
// console.log('Error - POST FILE', error);
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
// const handleConfirm = async () => {
|
|
|
|
|
// if (!pickedDocument || !trackName || !sessionName) {
|
|
|
|
|
// return;
|
|
|
|
|
// }
|
|
|
|
|
// try {
|
|
|
|
|
// const file = new File([await pickedDocument.uri], pickedDocument.name, { type: pickedDocument.type });
|
|
|
|
|
// const url = 'https://r-dash.azurewebsites.net/File?pseudoPilote=test_PILOTE&nameSession=weekend&nameCircuit=test_CIRCUIT&typeSession=Training';
|
|
|
|
|
// const options = {
|
|
|
|
|
// method: 'POST',
|
|
|
|
|
// body: file,
|
|
|
|
|
// headers: {
|
|
|
|
|
// 'Content-Type': 'application/octet-stream',
|
|
|
|
|
// },
|
|
|
|
|
// };
|
|
|
|
|
// const response = await fetch(url, options);
|
|
|
|
|
// const responseData = await response.text(); // or response.text() or response.blob() depending on the expected response type
|
|
|
|
|
// navigation.goBack();
|
|
|
|
|
// console.log(responseData);
|
|
|
|
|
// } catch (error) {
|
|
|
|
|
// console.log('Error - POST FILE', error);
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
const handleConfirm = async () => {
|
|
|
|
|
if (!pickedDocument || !trackName || !sessionName) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
//const file = new File([await pickedDocument.uri], pickedDocument.name, { type: pickedDocument.type });
|
|
|
|
|
const url = 'https://r-dash.azurewebsites.net/File?pseudoPilote=test_PILOTE&nameSession=test%20import&nameCircuit=test_CIRCUIT&typeSession=Training';
|
|
|
|
|
const formData = new FormData();
|
|
|
|
|
console.log(pickedDocument.type);
|
|
|
|
|
console.log(pickedDocument.uri);
|
|
|
|
|
formData.append('file',
|
|
|
|
|
{
|
|
|
|
|
name: pickedDocument.name,
|
|
|
|
|
type: "application/vnd.ms-excel",
|
|
|
|
|
uri : pickedDocument.uri,
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const response = await fetch(url, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
body: formData,
|
|
|
|
|
headers: {
|
|
|
|
|
'accept':'*/*',
|
|
|
|
|
'Content-Type': 'multipart/form-data',
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
const data = await response.json();
|
|
|
|
|
console.log('API response:', data);
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error( JSON.stringify(response) + 'Failed to upload file');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
navigation.goBack();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log('Error - POST FILE', error);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<SafeAreaView>
|
|
|
|
|
<View style={styles.container}>
|
|
|
|
@ -152,8 +33,7 @@ export default function NewTrack(props: { navigation: any }) {
|
|
|
|
|
<Text style={{ paddingTop: 20 }}>Track name: </Text>
|
|
|
|
|
<TextInput
|
|
|
|
|
style={styles.textInput}
|
|
|
|
|
onChangeText={setTrackName}
|
|
|
|
|
value={trackName}
|
|
|
|
|
secureTextEntry={true}
|
|
|
|
|
placeholder="Track name"
|
|
|
|
|
/>
|
|
|
|
|
</View>
|
|
|
|
@ -162,8 +42,7 @@ export default function NewTrack(props: { navigation: any }) {
|
|
|
|
|
<Text style={{ paddingTop: 20 }}>Session name: </Text>
|
|
|
|
|
<TextInput
|
|
|
|
|
style={styles.textInput}
|
|
|
|
|
onChangeText={setSessionName}
|
|
|
|
|
value={sessionName}
|
|
|
|
|
secureTextEntry={true}
|
|
|
|
|
placeholder="Session name"
|
|
|
|
|
/>
|
|
|
|
|
</View>
|
|
|
|
@ -188,7 +67,7 @@ export default function NewTrack(props: { navigation: any }) {
|
|
|
|
|
source={require('../assets/images/return.png')}
|
|
|
|
|
/>
|
|
|
|
|
</Pressable>
|
|
|
|
|
<Pressable style={styles.button} onPress={handleConfirm}>
|
|
|
|
|
<Pressable style={styles.button} onPress={() => navigation.goBack()}>
|
|
|
|
|
<Image
|
|
|
|
|
style={styles.return}
|
|
|
|
|
source={require('../assets/images/checked.png')}
|
|
|
|
|