From ecf486f435eb85471b7f08945675340c84e0f474 Mon Sep 17 00:00:00 2001 From: mohamed Date: Thu, 23 Mar 2023 15:33:14 +0100 Subject: [PATCH] ajout redux + nouveau lien --- R-Dash/components/SessionCmp.tsx | 9 +-------- R-Dash/core/Lap.ts | 22 +++++++++++----------- R-Dash/redux/actions/sessions.ts | 12 ++++++------ R-Dash/screens/Lap.tsx | 4 ++-- R-Dash/screens/Session_browser.tsx | 22 +++++++++++++++++----- 5 files changed, 37 insertions(+), 32 deletions(-) diff --git a/R-Dash/components/SessionCmp.tsx b/R-Dash/components/SessionCmp.tsx index f8d8b98..9268de9 100644 --- a/R-Dash/components/SessionCmp.tsx +++ b/R-Dash/components/SessionCmp.tsx @@ -11,20 +11,13 @@ type SessionListItemProps = { export default function SessionListItem(props: SessionListItemProps) { const timeInSeconds = props.session.getLaps()[props.session.getLaps().length - 1].getTime(); - const timeInMs = timeInSeconds * 1000; - const date = new Date(timeInMs); - const minutes = date.getMinutes().toString().padStart(2, "0"); - const seconds = date.getSeconds().toString().padStart(2, "0"); - const milliseconds = date.getMilliseconds().toString().padStart(2, "0"); - const formattedTime = `${minutes}:${seconds}:${milliseconds}`; - return ( props.onPress(props.session)}> {props.session.getName()} {props.session.getType().toString() } - { formattedTime } + { timeInSeconds } diff --git a/R-Dash/core/Lap.ts b/R-Dash/core/Lap.ts index 430466f..e55cf8b 100644 --- a/R-Dash/core/Lap.ts +++ b/R-Dash/core/Lap.ts @@ -3,9 +3,9 @@ import { Point } from "./Point"; export class Lap { private number : number; private points: Point[]; - private time: number; + private time: string; - constructor(number: number,points: Point[], time: number) { + constructor(number: number,points: Point[], time: string) { this.number = number; this.points = points; this.time = time; @@ -31,17 +31,17 @@ export class Lap { } getFormattedTime(){ - const timeInSeconds = this.time; - const timeInMs = timeInSeconds * 1000; - const date = new Date(timeInMs); - const minutes = date.getMinutes().toString().padStart(2, "0"); - const seconds = date.getSeconds().toString().padStart(2, "0"); - const milliseconds = date.getMilliseconds().toString().padStart(2, "0"); - const formattedTime = `${minutes}:${seconds}:${milliseconds}`; - return formattedTime; + // const timeInSeconds = this.time; + // const timeInMs = timeInSeconds * 1000; + // const date = new Date(timeInMs); + // const minutes = date.getMinutes().toString().padStart(2, "0"); + // const seconds = date.getSeconds().toString().padStart(2, "0"); + // const milliseconds = date.getMilliseconds().toString().padStart(2, "0"); + // const formattedTime = `${minutes}:${seconds}:${milliseconds}`; + // return formattedTime; } - setTime(time: number) { + setTime(time: string) { this.time = time; } getAverageSpeed(){ diff --git a/R-Dash/redux/actions/sessions.ts b/R-Dash/redux/actions/sessions.ts index e7d3704..cf45b4d 100644 --- a/R-Dash/redux/actions/sessions.ts +++ b/R-Dash/redux/actions/sessions.ts @@ -34,14 +34,14 @@ export const getSessionsList = () => { const sessionsPromise = await fetch('https://r-dash.azurewebsites.net/FullSession'); const sessionsListJson = await sessionsPromise.json(); const sessionsList: Session[] = sessionsListJson.map(elt => { - const laps: Lap[] = elt.laps.map(lap => { - const points: Point[] = lap.points.map(point => { - const geo = new Geocalisation(point.geo.latitude, point.geo.longitude); - return new Point(geo, point.timer, point.distance, point.nGear, point.pBrakeF, point.aSteer, point.rPedal, point.gLong, point.gLat, point.vCar); + const laps: Lap[] = elt["tours"].map(lap => { + const points: Point[] = lap["points"].map(point => { + const geo = new Geocalisation(point["longitude"], point["latitude"]); + return new Point(geo, point["timer"] , point["distance"], point["nGear"], point["pBrakeF"], point["aSteer"], point["rPedal"], point["gLong"], point["gLat"], point["vCar"]); }); - return new Lap(lap.number, points, lap.time); + return new Lap(lap["temps"], points, lap["temps"]); }); - return new Session(elt.name, laps, elt.type); + return new Session(elt["name"], laps, elt["type"]); }); dispatch(setSessionsList(sessionsList)); } catch (error) { diff --git a/R-Dash/screens/Lap.tsx b/R-Dash/screens/Lap.tsx index 7046a99..7d0a379 100644 --- a/R-Dash/screens/Lap.tsx +++ b/R-Dash/screens/Lap.tsx @@ -82,7 +82,7 @@ export default function Lap(props: { navigation: any, route : any}) { > {/* Lap Time: - {currentLap.getFormattedTime()} + {currentLap.getTime()} diff --git a/R-Dash/screens/Session_browser.tsx b/R-Dash/screens/Session_browser.tsx index 65df065..c16d380 100644 --- a/R-Dash/screens/Session_browser.tsx +++ b/R-Dash/screens/Session_browser.tsx @@ -1,5 +1,5 @@ import { BackgroundImage, SearchBar } from "@rneui/base"; -import React, { useState } from "react"; +import React, { useEffect, useState } from "react"; import { FlatList, StyleSheet, @@ -7,9 +7,11 @@ import { View, TouchableOpacity, } from "react-native"; +import { useDispatch, useSelector } from "react-redux"; import SessionListItem from "../components/SessionCmp"; import TopBar from "../components/TopBar"; import { Session } from "../core/Session"; +import { getSessionsList } from "../redux/actions/sessions"; import { SESSIONS } from "../stub/stub"; export default function Session_browser(props: { navigation: any }) { @@ -21,13 +23,23 @@ export default function Session_browser(props: { navigation: any }) { navigation.navigate("Lap", { "session" : item }); }; - const filteredData = + + const sessions = useSelector(state => state.appReducer.sessions); + + const dispatch = useDispatch(); + useEffect(() => { + const loadTeams = async () => { + await dispatch(getSessionsList()); + }; + loadTeams(); + }, [dispatch]); + + const filteredData = search !== "" - ? SESSIONS.filter((item) => + ? sessions.filter((item) => item.getName().toLowerCase().includes(search.toLowerCase()) ) - : SESSIONS; - + : sessions; return (