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..dbb316c8 100644 --- a/Recruit-Training/Advanced-Recruit-Training/Telemetry-Teaser/src/App.tsx +++ b/Recruit-Training/Advanced-Recruit-Training/Telemetry-Teaser/src/App.tsx @@ -1,25 +1,148 @@ +// import { parse } from "path"; +// import { stringify } from "querystring"; +import { useState } from "react"; +import { useReducer } from "react"; + import BatteryInput from "~/components/batteryInput"; import Header from "~/components/header"; import SpeedInput from "~/components/speedInput"; import WeatherInput from "~/components/weatherInput"; const App = () => { + const [visible, setVisible] = useState(false); + type InputState = { + speed: number | undefined; + battery: number | undefined; + weather: number; + }; + type Action = + | { type: "SET_BATTERY"; payload: number | undefined } + | { type: "SET_SPEED"; payload: number | undefined } + | { type: "SET_WEATHER"; payload: number }; + + const initialState: InputState = { + speed: 0, + battery: 100, + weather: 50, + }; + const calculateRange = () => { - return; + event?.preventDefault(); + setVisible(true); + }; + function BatteryValid() { + const battery = inputs["battery"]; + if (battery !== undefined && battery <= 100 && battery >= 0) { + return true; + } else { + return false; + } + } + + function SpeedValid() { + const speed = inputs["speed"]; + if (speed !== undefined && speed <= 90 && speed >= 0) { + return true; + } else { + return false; + } } + const reducer = (state: InputState, action: Action) => { + setVisible(false); + + switch (action.type) { + case "SET_BATTERY": + return { ...state, battery: action.payload }; + case "SET_SPEED": + return { ...state, speed: action.payload }; + case "SET_WEATHER": + return { ...state, weather: action.payload }; + default: + return state; + } + }; + const [inputs, dispatch] = useReducer(reducer, initialState); + return (
- - + + dispatch({ + type: "SET_SPEED", + payload: parseInt(e.target.value), + }) + } + /> + {!SpeedValid() && ( +

+ {" "} + The speed should be within the range of 0 to 90 +

+ )} + {inputs["speed"] === undefined && ( +

Speed is required

+ )} + + dispatch({ + type: "SET_BATTERY", + payload: parseInt(e.target.value), + }) + } + /> + {!BatteryValid() && ( +

+ The battery percentage should be with the range of 0 to 100 +

+ )} + {inputs["battery"] === undefined && ( +

Battery percentage is required

+ )}
- + + dispatch({ + type: "SET_WEATHER", + payload: value, + }) + } + />
+ + + {visible && + BatteryValid() && + SpeedValid() && + inputs["battery"] !== undefined && + inputs["speed"] !== undefined && ( +

+ The predicted range of the Eylsia is{" "} + {( + -( + (inputs["speed"] * inputs["speed"] * inputs["battery"]) / + 2500 + ) + + 4 * inputs["battery"] + + inputs["weather"] + ).toFixed(3)}{" "} + km +

+ )}
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..c9cf2d7a 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,10 @@ -const BatteryInput = () => { +const BatteryInput = ({ + value, + onChange, +}: { + value: number | undefined; + onChange: React.ChangeEventHandler; +}) => { return (
@@ -8,6 +14,8 @@ const BatteryInput = () => { name="battery" type="number" placeholder="Battery" + value={value !== undefined ? value : ""} + onChange={onChange} />
); 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..ad70166c 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,10 @@ -const SpeedInput = () => { +const SpeedInput = ({ + value, + onChange, +}: { + value: number | undefined; + onChange: React.ChangeEventHandler; +}) => { return ( <>
@@ -9,6 +15,8 @@ const SpeedInput = () => { name="speed" type="number" placeholder="Speed" + value={value !== undefined ? value : ""} + onChange={onChange} />
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..5fdbee8d 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,16 +1,44 @@ -const WeatherInput = () => { +// import { useState } from "react"; + +const WeatherInput = ({ + value, + onChange, +}: { + value: number; + onChange: (num: number) => void; +}) => { + const handleWeatherIncrement = () => { + onChange(value + 1); + }; + const handleWeatherDecrement = () => { + onChange(value - 1); + }; + return ( <> - Cloud + Cloud handleWeatherDecrement()} + /> onChange(Number(e.target.value))} + /> + Sun handleWeatherIncrement()} /> - Sun ); };