Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,82 @@
import { useReducer } from "react";

import { Action, State } from "~/types/types";

import BatteryInput from "~/components/batteryInput";
import CalculateButton from "~/components/calculateButton";
import Header from "~/components/header";
import SpeedInput from "~/components/speedInput";
import WeatherInput from "~/components/weatherInput";

function reducer(state: State, action: Action): State {
switch (action.type) {
case "SET_SPEED_INPUT":
return { ...state, speedInput: action.payload };
case "SET_BATTERY_INPUT":
return { ...state, batteryInput: action.payload };
case "SET_WEATHER_INPUT":
return { ...state, weatherInput: action.payload };
case "SET_CALCULATED_RANGE":
return { ...state, calculatedRange: action.payload };
case "SET_IS_CLICKED":
return { ...state, isClicked: action.payload };
default:
return state;
}
}

const App = () => {
const calculateRange = () => {
return;
const [state, dispatch] = useReducer<React.Reducer<State, Action>>(reducer, {
speedInput: "",
batteryInput: "",
weatherInput: 50,
calculatedRange: { message: "", colour: "" },
isClicked: false,
});

const calculateRange = (
speed: string,
battery: string,
weather: number,
) => {
if (speed == "" ) {
return { message: "Speed is required", colour: "text-red-500" };
} else if (0 > parseInt(speed) || parseInt(speed) > 90) {
return {
message: "The speed should be within the range of 0 to 90",
colour: "text-red-500",
};
} else if (battery == "" ) {
return {
message: "Battery Percentage is required",
colour: "text-red-500",
};
} else if (0 > parseInt(battery) || parseInt(battery) > 100) {
return {
message:
"The battery percentage should be within the range of 0 to 100",
colour: "text-red-500",
};
} else {
const range = -((parseInt(speed) * parseInt(speed) * parseInt(battery)) / 2500) + 4 * parseInt(battery) + weather;
return {
message: `The predicted range of the Eylsia is ${range.toString()} km.`,
colour: "white",
};
}
};

function handleClick() {
const result = calculateRange(
state.speedInput,
state.batteryInput,
state.weatherInput,
);
dispatch({
type: "SET_CALCULATED_RANGE",
payload: result,
});
dispatch({ type: "SET_IS_CLICKED", payload: true });
}

return (
Expand All @@ -14,11 +85,18 @@ const App = () => {
<Header />
<form name="simulator" className="flex w-full flex-col items-center">
<div className="mb-4 flex w-full flex-col items-center gap-y-4">
<SpeedInput />
<BatteryInput />
<SpeedInput value={state.speedInput} dispatch={dispatch} />
<BatteryInput value={state.batteryInput} dispatch={dispatch} />
</div>
<div className="flex w-full flex-row justify-center gap-4">
<WeatherInput />
<WeatherInput value={state.weatherInput} dispatch={dispatch} />
</div>
<div className="flex flex-col justify-center">
<CalculateButton
value={state.calculatedRange}
handleClick={handleClick}
isClicked={state.isClicked}
/>
</div>
</form>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
const BatteryInput = () => {
import { Action } from "~/types/types";

interface Props {
value: string;
dispatch: React.Dispatch<Action>;
}

const BatteryInput = ({value, dispatch}: Props) => {
const handleLetters = (e: any) => {
if (["e", "E", "+", "-", "."].includes(e.key)) {
e.preventDefault();
}
};
const handleInput = (e: any) => {
dispatch({type:'SET_BATTERY_INPUT', payload: e.target.value});
if (e.target.value) {
dispatch({type:'SET_IS_CLICKED', payload: false})
};
}

return (
<div className="flex w-full flex-col items-center gap-2">
<label>Battery Percentage (%):</label>
Expand All @@ -8,6 +27,9 @@ const BatteryInput = () => {
name="battery"
type="number"
placeholder="Battery"
onChange={handleInput}
value = {value}
onKeyDown = {handleLetters}
/>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import DisplayText from "../displayMessage";
interface Props {
value: { message: string; colour: string };
handleClick: any;
isClicked: boolean;
}
const CalculateButton = ({ value, handleClick, isClicked }: Props) => {
return (
<div className={`flex-col justify-center text-center`}>
<button
type="button"
onClick={handleClick}
className="m-2 rounded-lg bg-sky-600 p-4"
>
Calculate
</button>
{isClicked && <DisplayText calculatedRange={value} />}
</div>
);
};

export default CalculateButton;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
interface CalculatedRange {
calculatedRange: { message: string; colour: string };
}

const DisplayText = ({ calculatedRange }: CalculatedRange) => {
return (
<div className={`p-4 ${calculatedRange.colour}`}>
{calculatedRange.message}
</div>
);
};

export default DisplayText;
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
const SpeedInput = () => {
import { Action } from "~/types/types";

interface Props {
value: string;
dispatch: React.Dispatch<Action>;
}

const SpeedInput = ({ value, dispatch }: Props) => {
const handleLetters = (e: any) => {
if (["e", "E", "+", "-", "."].includes(e.key)) {
e.preventDefault();
}
};
const handleInput = (e: any) => {
dispatch({ type: "SET_SPEED_INPUT", payload: e.target.value });
if (e.target.value) {
dispatch({ type: "SET_IS_CLICKED", payload: false });
}
};

return (
<>
<div className="flex w-full flex-col items-center gap-2">
Expand All @@ -9,6 +28,9 @@ const SpeedInput = () => {
name="speed"
type="number"
placeholder="Speed"
onChange={handleInput}
value={value}
onKeyDown={handleLetters}
/>
</div>
</>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
const WeatherInput = () => {
import { Action } from "~/types/types";

interface Props {
value: any;
dispatch: React.Dispatch<Action>;
}

const WeatherInput = ({ value, dispatch }: Props) => {
const handleInput = (e: any) => {
dispatch({ type: "SET_WEATHER_INPUT", payload: e.target.value });
if (e.target.value) {
dispatch({ type: "SET_IS_CLICKED", payload: false });
}
};

return (
<>
<img src="/Cloud.png" height="66px" width="66px" alt="Cloud" />
Expand All @@ -8,7 +22,9 @@ const WeatherInput = () => {
type="range"
min="0"
max="100"
value="50"
defaultValue="50"
onChange={handleInput}
value={value}
/>
<img src="/Sun.png" height="66px" width="66px" alt="Sun" />
</>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export interface State {
speedInput: string ;
batteryInput: string;
weatherInput: number;
calculatedRange: {message: string; colour: string};
isClicked: boolean;
}

export interface Action {
type: "SET_SPEED_INPUT" | "SET_BATTERY_INPUT" | "SET_WEATHER_INPUT" | "SET_CALCULATED_RANGE" | "SET_IS_CLICKED";
payload: any;
}
7 changes: 7 additions & 0 deletions Recruit-Training/Basic-Telemetry-Training/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": ["prettier"],
"plugins": ["prettier"],
"rules": {
"prettier/prettier": ["error"],
},
}
24 changes: 24 additions & 0 deletions Recruit-Training/Basic-Telemetry-Training/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
8 changes: 8 additions & 0 deletions Recruit-Training/Basic-Telemetry-Training/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# React + Vite

This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.

Currently, two official plugins are available:

- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh
23 changes: 23 additions & 0 deletions Recruit-Training/Basic-Telemetry-Training/calculator/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
70 changes: 70 additions & 0 deletions Recruit-Training/Basic-Telemetry-Training/calculator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Getting Started with Create React App

This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).

## Available Scripts

In the project directory, you can run:

### `npm start`

Runs the app in the development mode.\
Open [http://localhost:3000](http://localhost:3000) to view it in your browser.

The page will reload when you make changes.\
You may also see any lint errors in the console.

### `npm test`

Launches the test runner in the interactive watch mode.\
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.

### `npm run build`

Builds the app for production to the `build` folder.\
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.\
Your app is ready to be deployed!

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.

### `npm run eject`

**Note: this is a one-way operation. Once you `eject`, you can't go back!**

If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own.

You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it.

## Learn More

You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).

To learn React, check out the [React documentation](https://reactjs.org/).

### Code Splitting

This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)

### Analyzing the Bundle Size

This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)

### Making a Progressive Web App

This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)

### Advanced Configuration

This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)

### Deployment

This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)

### `npm run build` fails to minify

This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
Loading