diff --git a/Recruit-Training/Advanced-Recruit-Training/Telemetry-Teaser/src/App.tsx b/Recruit-Training/Advanced-Recruit-Training/Telemetry-Teaser/src/App.tsx index 488bc934..4ef3dd7e 100644 --- a/Recruit-Training/Advanced-Recruit-Training/Telemetry-Teaser/src/App.tsx +++ b/Recruit-Training/Advanced-Recruit-Training/Telemetry-Teaser/src/App.tsx @@ -1,12 +1,63 @@ +import { useReducer, useState } from "react"; + import BatteryInput from "~/components/batteryInput"; import Header from "~/components/header"; +import Messages from "~/components/messages"; import SpeedInput from "~/components/speedInput"; import WeatherInput from "~/components/weatherInput"; const App = () => { + const initialState = { + speed: null, + battery: null, + weather: 50, + range: null, + }; + + const reducer = (state, action) => { + switch (action.type) { + case "setSpeed": + return { ...state, speed: action.payload, range: null }; + case "setBattery": + return { ...state, battery: action.payload, range: null }; + case "setWeather": + return { ...state, weather: action.payload, range: null }; + case "setRange": + return { ...state, range: action.payload }; + default: + throw "Unknown type error"; + } + }; + + const [state, dispatch] = useReducer(reducer, initialState); + const [messages, setMessages] = useState([]); + const calculateRange = () => { - return; - } + if (checkErrors()) return; + + // s = speed, b = battery percentage w = weather + // if s=1, b=2, w=50 then range=57.9992 + let s = state.speed, + b = state.battery, + w = state.weather; + let range = -((s * s * b) / 2500) + 4 * b + w; + dispatch({ type: "setRange", payload: range }); + }; + + const checkErrors = () => { + let messages = []; + + if (state.speed == null) messages.push("Speed is required"); + if (state.battery == null) messages.push("Battery is required"); + if (state.speed < 0 || state.speed > 90) + messages.push("The speed should be within a range of 0 and 90"); + if (state.battery < 0 || state.battery > 100) + messages.push( + "The battery percentage should be within a range of 0 and 100", + ); + setMessages(messages); + return messages.length !== 0; + }; return (
@@ -14,12 +65,30 @@ const App = () => {
- - + +
- + +
+ +
+ +
+ +
+ {state.range && + `The predicted range of the Eylsia is ${state.range} km.`}
+ {/* {messages && + messages.map((message, index) =>
  • {message}
  • )} */} + {messages.length !== 0 && }
    diff --git a/Recruit-Training/Advanced-Recruit-Training/Telemetry-Teaser/src/components/batteryInput/index.tsx b/Recruit-Training/Advanced-Recruit-Training/Telemetry-Teaser/src/components/batteryInput/index.tsx index ca4586c0..bf235148 100644 --- a/Recruit-Training/Advanced-Recruit-Training/Telemetry-Teaser/src/components/batteryInput/index.tsx +++ b/Recruit-Training/Advanced-Recruit-Training/Telemetry-Teaser/src/components/batteryInput/index.tsx @@ -1,4 +1,4 @@ -const BatteryInput = () => { +const BatteryInput = ({ dispatch }) => { return (
    @@ -8,6 +8,11 @@ const BatteryInput = () => { name="battery" type="number" placeholder="Battery" + onChange={(e) => { + const batteryValue = + e.target.value !== "" ? parseInt(e.target.value) : null; + dispatch({ type: "setBattery", payload: batteryValue }); + }} />
    ); diff --git a/Recruit-Training/Advanced-Recruit-Training/Telemetry-Teaser/src/components/messages/index.tsx b/Recruit-Training/Advanced-Recruit-Training/Telemetry-Teaser/src/components/messages/index.tsx new file mode 100644 index 00000000..e856b479 --- /dev/null +++ b/Recruit-Training/Advanced-Recruit-Training/Telemetry-Teaser/src/components/messages/index.tsx @@ -0,0 +1,14 @@ +const Messages = ({ messages }) => { + return ( +
    + Errors: + {messages.map((message, index) => ( +
  • + {message} +
  • + ))} +
    + ); +}; + +export default Messages; diff --git a/Recruit-Training/Advanced-Recruit-Training/Telemetry-Teaser/src/components/speedInput/index.tsx b/Recruit-Training/Advanced-Recruit-Training/Telemetry-Teaser/src/components/speedInput/index.tsx index aa381c14..9dc57799 100644 --- a/Recruit-Training/Advanced-Recruit-Training/Telemetry-Teaser/src/components/speedInput/index.tsx +++ b/Recruit-Training/Advanced-Recruit-Training/Telemetry-Teaser/src/components/speedInput/index.tsx @@ -1,4 +1,4 @@ -const SpeedInput = () => { +const SpeedInput = ({ dispatch }) => { return ( <>
    @@ -9,6 +9,11 @@ const SpeedInput = () => { name="speed" type="number" placeholder="Speed" + onChange={(e) => { + const speedValue = + e.target.value !== "" ? parseInt(e.target.value) : null; + dispatch({ type: "setSpeed", payload: speedValue }); + }} />
    diff --git a/Recruit-Training/Advanced-Recruit-Training/Telemetry-Teaser/src/components/weatherInput/index.tsx b/Recruit-Training/Advanced-Recruit-Training/Telemetry-Teaser/src/components/weatherInput/index.tsx index fd5ad35b..1067eb6f 100644 --- a/Recruit-Training/Advanced-Recruit-Training/Telemetry-Teaser/src/components/weatherInput/index.tsx +++ b/Recruit-Training/Advanced-Recruit-Training/Telemetry-Teaser/src/components/weatherInput/index.tsx @@ -1,4 +1,4 @@ -const WeatherInput = () => { +const WeatherInput = ({ state, dispatch }) => { return ( <> Cloud @@ -8,7 +8,12 @@ const WeatherInput = () => { type="range" min="0" max="100" - value="50" + value={state.weather} + onChange={(e) => { + const weatherValue = + e.target.value !== "" ? parseInt(e.target.value) : null; + dispatch({ type: "setWeather", payload: weatherValue }); + }} /> Sun