So, this is my first time posting here, so don't go too hard on me 😉, but I've been coding for a while but never worked on any fun challenges in the past. Well, there's a first for everything, I guess! I finally worked through a complete code challenge that I received from a recruiter and since I'm done with it, I figured I would share it with you.
Below, you'll find the objective, requirements, tech stack, and some details about the contents of the project, but to be completely transparent, this was taken from my GitHub readme file.
I welcome any feedback and hope you like the demo project.
Full source is available for review and forking, you wish.
Develop a Single Page App (SPA) that shows a simple dashboard that displays four tiles, each displaying a single statistic for a dataset that is retrieved via a REST API call. The user should be able to request new data to be loaded and to see the statistics in the tiles update to reflect the new dataset.
- The app shall display four statistics tiles: Mean, Median, Standard Deviation, and Mode.
- The app shall present a button which, when clicked, will cause a new dataset to be loaded(replacing the current dataset)and the dashboard display to be updated to reflect the newly loaded data.
- The app shall present an input field that will accept a number and a submit button which, when clicked, will cause the number to be added to the currently loaded dataset and the dashboard tiles to update to reflect the new dataset state.
- Mean - https://en.wikipedia.org/wiki/Arithmetic_mean
- Median – https://en.wikipedia.org/wiki/Median
- Standard Deviation – https://en.wikipedia.org/wiki/Standard_deviation
- Mode – https://en.wikipedia.org/wiki/Mode_(statistics)
|Tile||Default||After adding 42|
|Tile||Default||After adding 42|
- React Hooks
- Custom React Hooks
- Styled Components
- Framer Motion
- Unit Tests
- React Testing Library
- End to End Tests
# clone this project. If you are forking it first, make sure to use your # own GitHub username in place of mine ;) git clone https://github.com/briang123/reactjs-stats-code-challenge.git
Once you clone the project, you will want to make sure you install all dependencies to get up and running.
This command runs both the server and the client concurrently in development mode. The server is listening on http://localhost:5000 (Note: You will not be able to pull this up in a web browser directly) and the React app runs on http://localhost:3000
If you want to run the code as-if you're in production, then update the local .env file with
npm run start
Open http://localhost:5000/connection in a web browser to test your Rest API. The connection endpoint (http://localhost:5000/connection) will show you some brief documentation and version information for the endpoints.
npm run server
Launches the test runner in the interactive watch mode. See the section about running tests for more information.
npm run test
Launches the Cypress test runner in the interactive mode.
npm run cypress
Ensure that the React app is running on http://localhost:3000 before running this command as Cypress will attempt to access it to do its thing. This command will run the test I created this and output to the terminal.
npm run cypress:e2e
Before deploying to your own server, make sure to build a production version of your application.
npm run build
Make sure to download and install the Heroku CLI and create an account for yourself.
We are using the Heroku Buildpack for create-react-app. More information on that can be found at https://github.com/mars/create-react-app-buildpack
Before executing the steps below, make sure you do a
git commit on your code changes and that you have built your project using
npm run build.
cd $APP_NAME heroku create $APP_NAME --buildpack mars/create-react-app git push heroku master heroku open
A quick look at the top-level directories you'll see in this project.
. ├── cypress ├──── integration ├────── dashboard ├── public ├── server ├──── controllers ├──── db ├──── routes ├──── services ├──── utils ├──── index.js ├── src ├──── __tests__ ├──── common ├──── components ├──── docs ├──── hooks └──── theme
cypress - In the cypress > integration > dashboard directory, I created end-to-end tests for this project
public - React will build and deploy to this folder while in development
server - In lieu of creating a separate Node.js server that provides the Rest API for this challenge, I took the direction of creating a Node.js server proxy to simplify and expedite the process. If you would like to learn more about this you can read this article.
For the project folder on the server, I structured it so that there is a clear separation of concerns/layers in the application. Basically, we have our
routes, which forward requests to our
controllers (handles requests), which make calls to the
services layer (business logic), which make calls to the
db where methods are created that make calls to the database or external API's. There is also a separate utils directory that contains any common/shared functionality.
src - This is the React app that will make the Rest API requests to the server via a custom React hook (useDataFetch) to obtain the dataset needed to calculate the statistical data to display on the dashboard.
I created a few additional React hooks to help with this task (useStatistics and useNumArray). The implementation details for the calcuations exist in the NumArray class, which are called directly from calculateMedian, calculateMean, calculateStdDeviation, and calculateMode.
Our components are structured so that there is a separation of concerns, as well, but there is room for additional separation.
App - Container for the Header and Body components
Body - Container for the Dashboard, Form, and DataReload components.
Header - Container for the logo, title, and description
Dashboard - Container for the Tile component where there is one for each statistic.
Form - Container for the input (NumberInput component and useNumberInput custom React hook) and submit button (Button).
DataReload - Container for the two buttons (Button) that, when clicked, will trigger a Rest API call to the server to get the data.
Styled Components and, at times, co-mingling with
Framer Motion to bring the animation you will see. For CSS, I took a very basic mobile-first approach to the layout where I only have a couple of breakpoints to change the styles but using Flexbox to style the page.
Thanks for allowing me to share and welcome any feedback. I hope this helps others who might be starting out.