Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jason Kim code challenge #172

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
23 changes: 23 additions & 0 deletions septa-fare-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*
11,962 changes: 11,962 additions & 0 deletions septa-fare-calculator/package-lock.json

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions septa-fare-calculator/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "septa-fare-calculator",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.3.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Binary file added septa-fare-calculator/public/favicon.ico
Binary file not shown.
43 changes: 43 additions & 0 deletions septa-fare-calculator/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.

Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.

You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.

To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
Binary file added septa-fare-calculator/public/logo192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added septa-fare-calculator/public/logo512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions septa-fare-calculator/public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
3 changes: 3 additions & 0 deletions septa-fare-calculator/public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:
12 changes: 12 additions & 0 deletions septa-fare-calculator/src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from 'react';
import Calculator from './components/calculator';

function App() {
return (
<div className="septa-calculator-container">
<Calculator />
</div>
);
}

export default App;
8 changes: 8 additions & 0 deletions septa-fare-calculator/src/App.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { render, screen } from '@testing-library/react';
import App from './App';

test('renders learn react link', () => {
render(<App />);
const linkElement = screen.getByText(/learn react/i);
expect(linkElement).toBeInTheDocument();
});
Binary file added septa-fare-calculator/src/SEPTA.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 73 additions & 0 deletions septa-fare-calculator/src/components/calculator.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import React, {useState, useEffect} from 'react';

import Zone from './zone.jsx';
import Time from './time.jsx';
import Purchase from './purchase.jsx';
import Quantity from './quantity.jsx';
import Cost from './cost.jsx';
import SeptaLogo from '../SEPTA.png';

import '../stylesheets/reset.css';
import '../stylesheets/calculator.css';

const Calculator = () => {


//set hook variables that I need for calculating total cost
const [data, setData] = useState('');
const [zone, setZone] = useState('');
const [time, setTime] = useState('');
const [purchase, setPurchase] = useState('');
const [quantity, setQuantity] = useState('')
const [cost, setCost] = useState('');

//useEffect to load data from URL from fares.json to populate widget with live data
useEffect(() => {
const fareInfo = async () => {
await fetch("https://raw.githubusercontent.com/jasonkim0105/code-challenges-jason-kim/master/septa-fare-calculator/fares.json")
.then(res => res.json())
.then(result => setData(result))
}
fareInfo()
}, [])

/*useEffect to calculate total cost depending on which option is clicked for zone, time, purchase, and quantity. Should update automatically everytime a new option is selected. Goes into effect once zone, time, purchase, and quantity are set. Set cost with final cost number in the end.*/
useEffect(() => {
let multiplier = 1;
if (zone && time && purchase && quantity) {
for (let el of data.zones) {
if (el.name === zone) {
for (let i = 0; i < el.fares.length; i++) {
if (el.fares[i].type === time && el.fares[i].purchase === purchase) {
multiplier = el.fares[i].price / el.fares[i].trips;
}
}
}
}
}
setCost(multiplier * quantity)

//useEffect cleanup
return () => {
setCost(0)
}
}, [zone, time, purchase, quantity, cost, data])

return (
<div className="septa">
<div className="calculater-title">
<img src={SeptaLogo} alt="Pic" className="septa-logo" />
<h3>
Regional Rail Fares
</h3>
</div>
<Zone zone={zone} setZone={setZone} options={data ? data.zones : []} />
<Time time={time} setTime={setTime} options={data ? Object.entries(data.info).slice(0,3) : []} timeInfo={data ? data.info[time] : ''}/>
<Purchase purchase={purchase} setPurchase={setPurchase} options={data ? Object.entries(data.info).slice(3) : []} timeInfo={data ? data.info[time] : ''} />
<Quantity quantity={quantity} setQuantity={setQuantity} timeInfo={data ? data.info[time] : ''} />
<Cost cost={cost ? cost : ''} timeInfo={data ? data.info[time] : ''} />
</div>
)
}

export default Calculator;
26 changes: 26 additions & 0 deletions septa-fare-calculator/src/components/cost.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import '../stylesheets/cost.css'

const Cost = ({cost, timeInfo}) => {

//if "anytime" is clicked, the cost should be multiplied by 10, as it only caclulates for one ticket at the moment
if (timeInfo === "Valid anytime") cost = cost * 10;

if (!cost) {
return (
<div className="cost-container">
</div>
)} else {
return (
<div className="cost-container">
<p>
You fare will cost
</p>
<div className="total-cost">
${cost.toFixed(2)}
</div>
</div>
)
}
}

export default Cost;
34 changes: 34 additions & 0 deletions septa-fare-calculator/src/components/purchase.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import '../stylesheets/purchase.css'

const Purchase = ({purchase, setPurchase, options, timeInfo}) => {
//if "anytime" is clicked, only the station kiosk radio button should show

if (timeInfo !== "Valid anytime") {
return (
<div className="purchase-container">
Where will you purchase the fare?
<div className="purchase-radio-buttons">
<div>
<input type="radio" checked={purchase === "advance_purchase"} name="Station Kiosk" value="advance_purchase" onChange={(e) => setPurchase(e.target.value)}/>Station Kiosk
</div>
<div>
<input type="radio" checked={purchase === "onboard_purchase"} name="Onboard" value="onboard_purchase" onChange={(e) => setPurchase(e.target.value)}/>Onboard
</div>
</div>
</div>
)
} else {
return (
<div className="purchase-container">
Where will you purchase the fare?
<div className="purchase-radio-buttons">
<div>
<input type="radio" checked={purchase === "advance_purchase"} name="Station Kiosk" value="advance_purchase" onChange={(e) => setPurchase(e.target.value)}/>Station Kiosk
</div>
</div>
</div>
)
}
}

export default Purchase;
12 changes: 12 additions & 0 deletions septa-fare-calculator/src/components/quantity.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import '../stylesheets/quantity.css';

const Quantity = ({quantity, setQuantity, timeInfo}) => {
return (
<div className="quantity-containter">
How many rides will you need?
<input type="number" className="quantity-dropdown" onChange={(e) => setQuantity(e.target.value)}></input>
</div>
)
}

export default Quantity;
26 changes: 26 additions & 0 deletions septa-fare-calculator/src/components/time.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import '../stylesheets/time.css';

const Time = ({time, setTime, options, timeInfo}) => {
//if "anytime" is clicked, change the infomation from the data to include the fact that these tickets can only be purchased in a bundle of 10
if (timeInfo === "Valid anytime") {
timeInfo = "Valid anytime. May only be advance purchases and comes in a bundle of 10 tickets."
}
return (
<div className="time-container">
When are you riding?
<select className="time-dropdown" value={time} onChange={(e) => setTime(e.target.value) || null}>
<option value="">Select Travel Time</option>
{options.map((option,i) => (
<option key={i} value={option[0]}>
{option[0]}
</option>
))}
</select>
<div className="time-info">
{timeInfo ? timeInfo : ''}
</div>
</div>
)
}

export default Time;
19 changes: 19 additions & 0 deletions septa-fare-calculator/src/components/zone.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import '../stylesheets/zone.css';

const Zone = ({zone, setZone, options}) => {
return (
<div className="zone-container">
Where are you going?
<select className="zone-dropdown" value={zone} onChange={(e) => setZone(e.target.value)} >
<option value="">Select Zone</option>
{options.map((option, i) => (
<option key={i} value={option.name}>
{option.name}
</option>
))}
</select>
</div>
)
}

export default Zone;
16 changes: 16 additions & 0 deletions septa-fare-calculator/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import reportWebVitals from './reportWebVitals';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();
13 changes: 13 additions & 0 deletions septa-fare-calculator/src/reportWebVitals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const reportWebVitals = onPerfEntry => {
if (onPerfEntry && onPerfEntry instanceof Function) {
import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => {
getCLS(onPerfEntry);
getFID(onPerfEntry);
getFCP(onPerfEntry);
getLCP(onPerfEntry);
getTTFB(onPerfEntry);
});
}
};

export default reportWebVitals;
5 changes: 5 additions & 0 deletions septa-fare-calculator/src/setupTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// jest-dom adds custom jest matchers for asserting on DOM nodes.
// allows you to do things like:
// expect(element).toHaveTextContent(/react/i)
// learn more: https://github.com/testing-library/jest-dom
import '@testing-library/jest-dom';
Loading