From a3d83b19fd3e83b50eeb6ef5912ed087c3b34c9b Mon Sep 17 00:00:00 2001 From: mohamed Date: Tue, 28 Mar 2023 23:03:25 +0200 Subject: [PATCH] avancement --- R-Dash/package-lock.json | 14 ++--- R-Dash/package.json | 2 +- R-Dash/redux/Constants.ts | 4 +- R-Dash/redux/actions/sessions.ts | 10 ++-- R-Dash/redux/actions/teams.ts | 6 +-- R-Dash/redux/actions/users.ts | 2 +- R-Dash/screens/NewTrack.tsx | 92 ++++++++++++++++++++++++++++++-- 7 files changed, 108 insertions(+), 22 deletions(-) diff --git a/R-Dash/package-lock.json b/R-Dash/package-lock.json index c7f4d34..3596061 100644 --- a/R-Dash/package-lock.json +++ b/R-Dash/package-lock.json @@ -20,7 +20,7 @@ "jsonwebtoken": "^9.0.0", "react": "18.2.0", "react-native": "0.71.3", - "react-native-document-picker": "^8.1.4", + "react-native-document-picker": "^8.2.0", "react-native-gesture-handler": "^2.9.0", "react-native-maps": "1.3.2", "react-native-safe-area-context": "^4.5.0", @@ -12210,9 +12210,9 @@ } }, "node_modules/react-native-document-picker": { - "version": "8.1.4", - "resolved": "https://registry.npmjs.org/react-native-document-picker/-/react-native-document-picker-8.1.4.tgz", - "integrity": "sha512-1GJ6jKTxmoT3jyQQjH6cBXZDLLAjCe+f3JE//bfm1+CFa12ziM4wkv8o6sVzbqQWbFr98Mvnx9VKptmUNTXVWQ==", + "version": "8.2.0", + "resolved": "https://registry.npmjs.org/react-native-document-picker/-/react-native-document-picker-8.2.0.tgz", + "integrity": "sha512-S/n4DIHWC0DTrtJnj2WZZleIfN4C2QjPMUpe8l/URt2Gc3/95dxkyfp4Dz4d7aZq5yEVFkdAvXv0nyWkM5w4MA==", "dependencies": { "invariant": "^2.2.4" }, @@ -23871,9 +23871,9 @@ } }, "react-native-document-picker": { - "version": "8.1.4", - "resolved": "https://registry.npmjs.org/react-native-document-picker/-/react-native-document-picker-8.1.4.tgz", - "integrity": "sha512-1GJ6jKTxmoT3jyQQjH6cBXZDLLAjCe+f3JE//bfm1+CFa12ziM4wkv8o6sVzbqQWbFr98Mvnx9VKptmUNTXVWQ==", + "version": "8.2.0", + "resolved": "https://registry.npmjs.org/react-native-document-picker/-/react-native-document-picker-8.2.0.tgz", + "integrity": "sha512-S/n4DIHWC0DTrtJnj2WZZleIfN4C2QjPMUpe8l/URt2Gc3/95dxkyfp4Dz4d7aZq5yEVFkdAvXv0nyWkM5w4MA==", "requires": { "invariant": "^2.2.4" } diff --git a/R-Dash/package.json b/R-Dash/package.json index 3d0c40f..18c1090 100644 --- a/R-Dash/package.json +++ b/R-Dash/package.json @@ -21,7 +21,7 @@ "jsonwebtoken": "^9.0.0", "react": "18.2.0", "react-native": "0.71.3", - "react-native-document-picker": "^8.1.4", + "react-native-document-picker": "^8.2.0", "react-native-gesture-handler": "^2.9.0", "react-native-maps": "1.3.2", "react-native-safe-area-context": "^4.5.0", diff --git a/R-Dash/redux/Constants.ts b/R-Dash/redux/Constants.ts index c64d810..d287ec8 100644 --- a/R-Dash/redux/Constants.ts +++ b/R-Dash/redux/Constants.ts @@ -2,4 +2,6 @@ export const FETCH_USERS = 'FETCH_USERS'; export const FETCH_TEAMS = 'FETCH_TEAMS'; export const FETCH_SESSIONS = 'FETCH_SESSIONS'; export const ADD_TEAM = 'ADD_TEAM'; -export const ADD_FILE = 'ADD_FILE'; \ No newline at end of file +export const ADD_FILE = 'ADD_FILE'; +//export const server_link = "https://codefirst.iut.uca.fr/containers/enzojolys-r-dash_container"; +export const server_link = "https://r-dash.azurewebsites.net"; \ No newline at end of file diff --git a/R-Dash/redux/actions/sessions.ts b/R-Dash/redux/actions/sessions.ts index 2c3c147..430ede2 100644 --- a/R-Dash/redux/actions/sessions.ts +++ b/R-Dash/redux/actions/sessions.ts @@ -1,9 +1,10 @@ +import { Alert } from "react-native"; import { Geocalisation } from "../../core/Geocalisation"; import { Lap } from "../../core/Lap"; import { Point } from "../../core/Point"; import { Session } from "../../core/Session"; import { User } from "../../core/User"; -import { FETCH_SESSIONS } from "../Constants"; +import { FETCH_SESSIONS, server_link } from "../Constants"; export const setSessionsList = (sessionsList: Session[]) => { return { @@ -34,7 +35,7 @@ export const addXlsFile = (file: File, pseudoPilote: string, email: string, pass const formData = new FormData(); formData.append('file', file); const response = await fetch( - `https://r-dash.azurewebsites.net/File?pseudoPilote=${pseudoPilote}&Email=${email}&password=${password}&nameSession=${nameSession}&nameCircuit=${nameCircuit}&typeSession=${typeSession}`, + server_link+`/File?pseudoPilote=${pseudoPilote}&Email=${email}&password=${password}&nameSession=${nameSession}&nameCircuit=${nameCircuit}&typeSession=${typeSession}`, { method: 'POST', body: formData @@ -44,14 +45,14 @@ export const addXlsFile = (file: File, pseudoPilote: string, email: string, pass return data; } catch (error) { console.log('Error - POST FILE', error); + Alert.alert('Error', 'An error occured while adding a session. (server might be down)'); } } }; - export const getSessionsList = () => { return async dispatch => { try { - const sessionsPromise = await fetch('https://r-dash.azurewebsites.net/FullSession'); + const sessionsPromise = await fetch(server_link+'/FullSession'); const sessionsListJson = await sessionsPromise.json(); const sessionsList: Session[] = sessionsListJson.map(elt => { const laps: Lap[] = elt["tours"].map(lap => { @@ -66,6 +67,7 @@ export const getSessionsList = () => { dispatch(setSessionsList(sessionsList)); } catch (error) { console.log('Error -- GET SESSIONS', error); + Alert.alert('Error', 'An error occured while getting sessions. (server might be down)'); //dispatch(fetchDataRejected(error)) } } diff --git a/R-Dash/redux/actions/teams.ts b/R-Dash/redux/actions/teams.ts index 1b6d351..d3a5db3 100644 --- a/R-Dash/redux/actions/teams.ts +++ b/R-Dash/redux/actions/teams.ts @@ -1,5 +1,5 @@ import { Team } from "../../core/Team"; -import { FETCH_TEAMS, ADD_TEAM } from "../Constants"; +import { FETCH_TEAMS, ADD_TEAM, server_link } from "../Constants"; export const setTeamsList = (teamsList: Team[]) => { return { @@ -11,7 +11,7 @@ export const setTeamsList = (teamsList: Team[]) => { export const addNewTeam = (newTeam: Team) => { return async dispatch => { try { - const response = await fetch('https://r-dash.azurewebsites.net/Ecuries?' + "Email=test@gmail.com" + "&password=test123" + "&pseudoPilote=test_PILOTE", { + const response = await fetch(server_link + '/Ecuries?' + "Email=test@gmail.com" + "&password=test123" + "&pseudoPilote=test_PILOTE", { method: 'POST', headers: { 'Content-Type': 'application/json' @@ -33,7 +33,7 @@ export const addNewTeam = (newTeam: Team) => { export const getTeamsList = () => { return async dispatch => { try { - const teamsPromise = await fetch('https://r-dash.azurewebsites.net/Ecuries'); + const teamsPromise = await fetch(server_link+'/Ecuries'); const teamsListJson = await teamsPromise.json(); const teamsList: Team[] = teamsListJson.map(elt => new Team(elt["name"], elt["owner"], elt["users"], elt["logo"])); dispatch(setTeamsList(teamsList)); diff --git a/R-Dash/redux/actions/users.ts b/R-Dash/redux/actions/users.ts index 32f89b7..c1b0599 100644 --- a/R-Dash/redux/actions/users.ts +++ b/R-Dash/redux/actions/users.ts @@ -16,7 +16,7 @@ export const setUsersList = (usersList: User[]) => { export const getUsersList = (team: Team) => { return async dispatch => { try { - const usersPromise = await fetch('https://codefirst.iut.uca.fr/containers/enzojolys-r-dash_container/Pilotes/'+team); + const usersPromise = await fetch(server_link+'/Pilotes/'+team); const usersListJson = await usersPromise.json(); const dto: DtoUserEcurie = usersListJson.map(elt => new DtoUserEcurie(elt["owner"], elt["members"], elt["waitingMember"])); const usersList: User[] = [] diff --git a/R-Dash/screens/NewTrack.tsx b/R-Dash/screens/NewTrack.tsx index de1519c..1703a87 100644 --- a/R-Dash/screens/NewTrack.tsx +++ b/R-Dash/screens/NewTrack.tsx @@ -1,11 +1,10 @@ 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 * as DocumentPicker from 'expo-document-picker'; import TopBar from '../components/TopBar'; import { addXlsFile } from '../redux/actions/sessions'; import { useDispatch } from 'react-redux'; - +import * as DocumentPicker from 'expo-document-picker'; export default function NewTrack(props: { navigation: any }) { const { navigation } = props; const dispatch = useDispatch(); @@ -28,20 +27,103 @@ export default function NewTrack(props: { navigation: any }) { console.log(err); } }; + + // 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 { - await dispatch(addXlsFile(pickedDocument.uri, 'test_PILOTE', 'test@gmail.com', 'test123', sessionName, trackName, 'Training')); + const file = new File([await pickedDocument.uri], pickedDocument.name, { type: pickedDocument.type }); + const url = 'https://r-dash.azurewebsites.net/File?pseudoPilote=test_PILOTE&nameSession=weeke&nameCircuit=test_CIRCUIT&typeSession=Training'; + const formData = new FormData(); + formData.append('file', file); + // formData.append('pseudoPilote', 'test_PILOTE'); + // formData.append('nameSession', sessionName); + // formData.append('nameCircuit', 'test_CIRCUIT'); + // formData.append('typeSession', 'Training'); + + const response = await fetch(url, { + method: 'POST', + body: file, + headers: { + Accept: '*/*', + 'Content-Type': 'multipart/form-data', + }, + }); + + if (!response.ok) { + throw new Error('Failed to upload file'); + } + + const data = await response.json(); + console.log('API response:', data); navigation.goBack(); } catch (error) { console.log('Error - POST FILE', error); } }; - + + return (