diff --git a/react-calculator/.gitignore b/react-calculator/.gitignore new file mode 100644 index 000000000..4d29575de --- /dev/null +++ b/react-calculator/.gitignore @@ -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* diff --git a/react-calculator/README.md b/react-calculator/README.md new file mode 100644 index 000000000..e629de7ee --- /dev/null +++ b/react-calculator/README.md @@ -0,0 +1,52 @@ +# SEPTA Rail Fare Calculator App + +This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). + +## Install + +``` +npm install +``` + +or + +``` +yarn +``` + +## 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. diff --git a/react-calculator/package.json b/react-calculator/package.json new file mode 100644 index 000000000..902acb52d --- /dev/null +++ b/react-calculator/package.json @@ -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" + ] + } +} diff --git a/react-calculator/public/fares.json b/react-calculator/public/fares.json new file mode 100644 index 000000000..994098a28 --- /dev/null +++ b/react-calculator/public/fares.json @@ -0,0 +1,155 @@ +{ + "info": { + "anytime": "Valid anytime", + "weekday": "Valid Monday through Friday, 4:00 a.m. - 7:00 p.m. On trains arriving or departing 30th Street Station, Suburban and Jefferson Station", + "evening_weekend": "Valid weekdays after 7:00 p.m.; all day Saturday, Sunday and major holidays. On trains arriving or departing 30th Street Station, Suburban and Jefferson Station", + "advance_purchase": "Tickets available for purchase at all SEPTA offices.", + "onboard_purchase": "Tickets available for purchase from a train conductor aboard SEPTA regional rail trains." + }, + "zones": [{ + "name": "CCP/1", + "zone": 1, + "fares": [{ + "type": "weekday", + "purchase": "advance_purchase", + "trips": 1, + "price": 4.75 + }, { + "type": "weekday", + "purchase": "onboard_purchase", + "trips": 1, + "price": 6.00 + }, { + "type": "evening_weekend", + "purchase": "advance_purchase", + "trips": 1, + "price": 3.75 + }, { + "type": "evening_weekend", + "purchase": "onboard_purchase", + "trips": 1, + "price": 5.00 + }, { + "type": "anytime", + "purchase": "advance_purchase", + "trips": 10, + "price": 38.00 + }] + }, { + "name": "Zone 2", + "zone": 2, + "fares": [{ + "type": "weekday", + "purchase": "advance_purchase", + "trips": 1, + "price": 4.75 + }, { + "type": "weekday", + "purchase": "onboard_purchase", + "trips": 1, + "price": 6.00 + }, { + "type": "evening_weekend", + "purchase": "advance_purchase", + "trips": 1, + "price": 3.75 + }, { + "type": "evening_weekend", + "purchase": "onboard_purchase", + "trips": 1, + "price": 5.00 + }, { + "type": "anytime", + "purchase": "advance_purchase", + "trips": 10, + "price": 45.00 + }] + }, { + "name": "Zone 3", + "zone": 3, + "fares": [{ + "type": "weekday", + "purchase": "advance_purchase", + "trips": 1, + "price": 5.75 + }, { + "type": "weekday", + "purchase": "onboard_purchase", + "trips": 1, + "price": 7.00 + }, { + "type": "evening_weekend", + "purchase": "advance_purchase", + "trips": 1, + "price": 5.00 + }, { + "type": "evening_weekend", + "purchase": "onboard_purchase", + "trips": 1, + "price": 7.00 + }, { + "type": "anytime", + "purchase": "advance_purchase", + "trips": 10, + "price": 54.50 + }] + }, { + "name": "Zone 4", + "zone": 4, + "fares": [{ + "type": "weekday", + "purchase": "advance_purchase", + "trips": 1, + "price": 6.50 + }, { + "type": "weekday", + "purchase": "onboard_purchase", + "trips": 1, + "price": 8.00 + }, { + "type": "evening_weekend", + "purchase": "advance_purchase", + "trips": 1, + "price": 5.00 + }, { + "type": "evening_weekend", + "purchase": "onboard_purchase", + "trips": 1, + "price": 7.00 + }, { + "type": "anytime", + "purchase": "advance_purchase", + "trips": 10, + "price": 62.50 + }] + }, { + "name": "NJ", + "zone": 5, + "fares": [{ + "type": "weekday", + "purchase": "advance_purchase", + "trips": 1, + "price": 9.00 + }, { + "type": "weekday", + "purchase": "onboard_purchase", + "trips": 1, + "price": 10.00 + }, { + "type": "evening_weekend", + "purchase": "advance_purchase", + "trips": 1, + "price": 9.00 + }, { + "type": "evening_weekend", + "purchase": "onboard_purchase", + "trips": 1, + "price": 10.00 + }, { + "type": "anytime", + "purchase": "advance_purchase", + "trips": 10, + "price": 80.00 + }] + }] +} \ No newline at end of file diff --git a/react-calculator/public/favicon.ico b/react-calculator/public/favicon.ico new file mode 100644 index 000000000..a11777cc4 Binary files /dev/null and b/react-calculator/public/favicon.ico differ diff --git a/react-calculator/public/index.html b/react-calculator/public/index.html new file mode 100644 index 000000000..49eaf2785 --- /dev/null +++ b/react-calculator/public/index.html @@ -0,0 +1,43 @@ + + + + + + + + + + + + + SEPTA Regional Rail Fare Calculator + + + +
+ + + diff --git a/react-calculator/public/logo192.png b/react-calculator/public/logo192.png new file mode 100644 index 000000000..fc44b0a37 Binary files /dev/null and b/react-calculator/public/logo192.png differ diff --git a/react-calculator/public/logo512.png b/react-calculator/public/logo512.png new file mode 100644 index 000000000..a4e47a654 Binary files /dev/null and b/react-calculator/public/logo512.png differ diff --git a/react-calculator/public/manifest.json b/react-calculator/public/manifest.json new file mode 100644 index 000000000..080d6c77a --- /dev/null +++ b/react-calculator/public/manifest.json @@ -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" +} diff --git a/react-calculator/public/robots.txt b/react-calculator/public/robots.txt new file mode 100644 index 000000000..e9e57dc4d --- /dev/null +++ b/react-calculator/public/robots.txt @@ -0,0 +1,3 @@ +# https://www.robotstxt.org/robotstxt.html +User-agent: * +Disallow: diff --git a/react-calculator/src/App.css b/react-calculator/src/App.css new file mode 100644 index 000000000..316fb32ac --- /dev/null +++ b/react-calculator/src/App.css @@ -0,0 +1,15 @@ +.App { + text-align: center; + width: calc(100% - 60px); + padding: 30px; + display: flex; + justify-content: center; + align-items: center; +} + +@media screen and (max-width: 450px) { + .App { + padding: 0; + width: 100%; + } +} diff --git a/react-calculator/src/App.js b/react-calculator/src/App.js new file mode 100644 index 000000000..18b58d3f2 --- /dev/null +++ b/react-calculator/src/App.js @@ -0,0 +1,12 @@ +import './App.css'; +import FareCalculator from "./components/FareCalculator"; + +function App() { + return ( +
+ +
+ ); +} + +export default App; diff --git a/react-calculator/src/App.test.js b/react-calculator/src/App.test.js new file mode 100644 index 000000000..1f03afeec --- /dev/null +++ b/react-calculator/src/App.test.js @@ -0,0 +1,8 @@ +import { render, screen } from '@testing-library/react'; +import App from './App'; + +test('renders learn react link', () => { + render(); + const linkElement = screen.getByText(/learn react/i); + expect(linkElement).toBeInTheDocument(); +}); diff --git a/react-calculator/src/components/FareCalculator/index.jsx b/react-calculator/src/components/FareCalculator/index.jsx new file mode 100644 index 000000000..16185564c --- /dev/null +++ b/react-calculator/src/components/FareCalculator/index.jsx @@ -0,0 +1,161 @@ +import "./styles.css" +import {useEffect, useState} from "react"; + +const minRides = 1 // Min number of trips +const maxRides = 10 // Max number of trips +const titles = { + 'advance_purchase': 'Station Kiosk', + 'onboard_purchase': 'Onboard', + 'weekday': 'Weekdays', + 'evening_weekend': 'Weekend/Evening', + 'anytime': 'Anytime', +} + +export default function FareCalculator() { + const [data, setData] = useState() // Loaded zones data + const [loading, setLoading] = useState(true) // Loading data indicator + const [zone, setZone] = useState() // Current selected zone + const [type, setType] = useState() // Current selected type + const [purchase, setPurchase] = useState() // Current selected purchase + const [rides, setRides] = useState(minRides) // Current number of trips + const [fare, setFare] = useState({}) // Current fare + const [price, setPrice] = useState(0) // Current price + + // Load data + useEffect(() => { + fetch('fares.json') + .then((res) => res.json()) + .then((data) =>{ + setData(data) + setLoading(false) + }) + }, []) + + // Set zone after loading data + useEffect(() => { + if (data?.zones) { + setZone(data?.zones[0]) + } + }, [data]) + + // Set default type and purchase after zone changed + useEffect(() => { + if (zone?.fares?.length > 0) { + setType(getTypes()[0]) + setPurchase(getPurchases()[0]) + } + }, [zone]) + + // Calculate price after any parameter changed + useEffect(() => { + if (zone?.fares?.length > 0) { + const fare = zone?.fares?.find(item => + item.type === type + && item.purchase === purchase + ) + + setFare(fare || {}) + setPrice(rides > fare?.trips ? rides * fare.price : fare?.price || 0) + } + }, [zone, type, purchase, rides]) + + // Select type handler + const selectType = (event) => { + setType(event.target.value) + } + + // Select zone handler + const selectZone = (event) => { + const name = parseInt(event.target.value) + const zone = data?.zones?.find(item => item.zone === name) + + setZone(zone) + } + + // Change number of the trips handler + const changeRides = (event) => { + const value = parseInt(event.target.value) + if (value < minRides) { + setRides(minRides) + } else if (value > maxRides) { + setRides(maxRides) + } else { + setRides(value) + } + } + + // Select purchase handler + const changePurchase = (event) => { + const value = event.target.value + setPurchase(value) + } + + // Get all types of current zone + const getTypes = () => { + return zone?.fares?.map(item => item.type).filter((value, index, self) => self.indexOf(value) === index) + } + + // Get all purchases of current zone + const getPurchases = () => { + return zone?.fares?.map(item => item.purchase).filter((value, index, self) => self.indexOf(value) === index) + } + + return ( +
+
Regional Rail Fares
+ + {!loading && data ? <> + {/* Zone control */} +
+
Where are going?
+ +
+ + {/* Types control */} +
+
When are you riding?
+ +
{type && data?.info[type]}
+
+ + {/* Purchase radio control */} +
+
Where will you purchase the fare?
+ +
+ {getPurchases()?.map(key => ( +
+ + +
+ ))} +
+ +
{purchase && data?.info[purchase]}
+
+ + {/* Trips number control */} +
+
How many rides will you need?
+ +
+ + {/* Price */} +
+
Your fare will cost
+
{price ? '$' + parseFloat(`${price || 0}`).toFixed(2) : ''}
+
+ :
Loading...
} +
+ ) +} diff --git a/react-calculator/src/components/FareCalculator/styles.css b/react-calculator/src/components/FareCalculator/styles.css new file mode 100644 index 000000000..be6ac090f --- /dev/null +++ b/react-calculator/src/components/FareCalculator/styles.css @@ -0,0 +1,93 @@ +input, select { + font-size: 1rem; + padding: 8px 16px; + border-radius: 4px; + border: 2px solid #ddd; +} + +input { + padding-right: 3px; +} + +.container { + font-family: Arial, sans-serif; + display: block; + border: 3px solid #ddd; + width: 400px; + max-width: 100%; + overflow: hidden; + color: #444; +} + +.header, .footer { + width: calc(100% - 32px); + border: 0; + color: white; + font-weight: 600; + font-size: 1.25rem; + padding: 8px 16px; + background: #666; + display: flex; + justify-content: center; + align-items: center; +} + +.form-control { + padding: 16px; + width: calc(100% - 32px); + border-bottom: 2px solid #e5e5e5; +} + +.form-control.no-border { + border: none; +} + +.subtitle { + font-weight: 600; + padding-bottom: 8px; +} + +.footer { + font-size: 1rem; + padding: 16px; + flex-direction: column; +} + +.price { + font-weight: bold; + font-size: 2.5rem; + line-height: 1; +} + +.helper-text { + font-size: 0.9rem; + color: #777; + text-align: center; + margin-top: 8px; +} + +.radio-container { + margin: auto; + width: fit-content; + text-align: left; +} + +.width-30 { + width: 30%; +} +.align-center { + text-align: center; +} +.loading { + height: 300px; + min-height: 300px; + display: flex; + justify-content: center; + align-items: center; +} + +@media screen and (max-width: 450px) { + .container { + width: 100%; + } +} diff --git a/react-calculator/src/index.css b/react-calculator/src/index.css new file mode 100644 index 000000000..ec2585e8c --- /dev/null +++ b/react-calculator/src/index.css @@ -0,0 +1,13 @@ +body { + margin: 0; + font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', + 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', + sans-serif; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; +} + +code { + font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', + monospace; +} diff --git a/react-calculator/src/index.js b/react-calculator/src/index.js new file mode 100644 index 000000000..d563c0fb1 --- /dev/null +++ b/react-calculator/src/index.js @@ -0,0 +1,17 @@ +import React from 'react'; +import ReactDOM from 'react-dom/client'; +import './index.css'; +import App from './App'; +import reportWebVitals from './reportWebVitals'; + +const root = ReactDOM.createRoot(document.getElementById('root')); +root.render( + + + +); + +// 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(); diff --git a/react-calculator/src/reportWebVitals.js b/react-calculator/src/reportWebVitals.js new file mode 100644 index 000000000..5253d3ad9 --- /dev/null +++ b/react-calculator/src/reportWebVitals.js @@ -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; diff --git a/react-calculator/src/setupTests.js b/react-calculator/src/setupTests.js new file mode 100644 index 000000000..8f2609b7b --- /dev/null +++ b/react-calculator/src/setupTests.js @@ -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';