Assignment 5 - Routing and API calls
First, complete assignment 4 if you haven't already. That assignment is prerequisite; it would be a poor use of time to learn about routing and API calls if you aren't already familiar with React.
In workshop 5, we covered client-side routing and added new routes to an ecommerce app
using react-router-dom
. Then, we learned about how the JavaScript engine handles
asynchronous functions, and we used fetch
to make requests to a products API.
In assignment 5, you’ll add routing and API calls to your web app.
Setup
-
You’ll begin with your web app from assignment 4. We will build off of that repository this week.
-
Navigate to your local project repository from assignment 4. Make sure you are on the
hw-4
branch (or whatever branch contains all your changes from assignment 4).- Run
git branch
to check what branch you are on. - Run
git checkout branch-name
to switch to a different branch.
- Run
-
Run
git checkout -b hw-5
to create a new branch called “hw-5” that branches off your hw-4 branch. All of the work on your project MUST be done on this branch. You should NOT modify the main branch, or the hw-4 branch. -
Run
git push -u origin hw-5
to publish this new branch on github. -
Dedicate one terminal window to running your code with npm.
- Run
npm start
in this terminal to run your app and view your changes in the browser. - If you want to stop running your code at any time, use Ctrl+C inside this terminal.
- Run
-
Dedicate another terminal window to running normal terminal commands, like git commands.
git add .
stages all of your current changes so you can commit themgit status
displays your changes, showing what’s staged and what isn’tgit commit -m “commit message here”
bundles everything on the stage into a commitgit push
publishes your commit(s) to github
-
Now you are ready! Go forth and code!
- As you progress on this assignment, continue grouping your changes into commits and pushing them to the hw-5 branch on github. All of your commits MUST be on the hw-5 branch, not the main branch or the hw-4 branch.
Core requirements
- Add 1 new page to your web app using
react-router-dom
.- This W3 schools tutorial is great.
- Retrieve and display information from the users API.
Use the
getAllUsers
function to get an array of all the users from the database. Once you have this array, you can map over it to display a card for each user.- Hint 1: store the array of users in a state variable
- Hint 2: you only want to fetch the users the first time you load the page,
instead of every time your app renders.
useEffect
can help us out here.
The users API
- An API is just a group of functions. The users API is a group of functions
we’ve built that enable you to interact with a real database of users.
- You’ll be building your own database and API in a couple weeks!
- Here are the functions we provide in this API:
getAllUsers
: gives an array of all the users in the databasegetUserByID
: gives information about a specific user in the databasecreateUser
: adds a new user to the databaseupdateUser
: updates an existing user in the databasedeleteUser
: deletes a user from the database
- A machine in Ohio is set up with the code to execute these functions.
This machine is called a server because its job is to serve information and provide other services
(such as modifying a database). In order to run functions of the users API,
you’ll need to communicate with this server.
- The scheme that the server uses to communicate is
https
. This means that you will communicate with this server by making HTTP requests. - The domain/hostname of the server is:
disc-assignment-5-users-api.onrender.com
- The scheme that the server uses to communicate is
- Below is the information you’ll need to build your requests
Function | Method | Path | Additional request information | Response |
---|---|---|---|---|
getAllUsers | GET | /api/users | none | An array of User objects. Each User object has the following fields:
|
getUserByID | GET | /api/users/:id | none | A single User object |
createUser | POST | /api/users | The body of the request must contain these fields:
| A single User object representing the user you just created |
updateUser | PUT | /api/users/:id | The body of the request must contain these fields:
| A single User object representing the user you just updated |
deleteUser | DELETE | /api/users/:id | none | none |
- For example, say I wanted to delete the user named “Bob”.
- First, I make a
GET
request to “https://disc-assignment-5-users-api.onrender.com/api/users” to get information about all the users. Take note of how I constructed this URL using the scheme + domain + path. - Then, I search through the array of users to find the id corresponding to the user named “Bob”. Let’s say Bob has id 28.
- Finally, I make a
DELETE
request to “https://disc-assignment-5-users-api.onrender.com/api/users/28” to delete the user with id 28.
- First, I make a
- Use the fetch API
to make requests to the server in your JavaScript code.
- For
createUser
, andupdateUser
, the Content-Type you need to use for your request body ismultipart/form-data
. If you are unfamiliar with how to work with this type of content, read this guide - Remember that fetch returns a promise. How do we deal with promises?
- For
- Important note: the server will go to sleep if it hasn’t received any requests in a long time.
If that happens, the next time it receives a request, it will take a long time to
complete the request because it has to un-sleep. This means that some requests might
take up to 5 minutes to complete.
- To wake the server, you can make a
GET
request to it by opening this URL in your browser: https://disc-assignment-5-users-api.onrender.com/api/users. That’s right, every time you go to a link in your browser, it’s really just making aHTTP GET
request to that url and displaying the result.
- To wake the server, you can make a
- Having trouble? Check out Ethan's user API demo.
Additional requirements:
- Add more pages to your web app.
- Use the other functions in the users API to add more features to your web app.
We recommend integrating the functions in this order:
getAllUsers
getUserByID
– should be very easy after you havegetAllUsers
workingcreateUser
updateUser
– should be very easy after you havecreateUser
workingdeleteUser
– should be very easy after you havegetUserByID
working- Each time after you delete a user, create a new user.
Deleting users deletes them for everyone else working on this assignment,
and we don’t want to be left with 0 users. This means
do not implement
deleteUser
until you havecreateUser
working.
- Each time after you delete a user, create a new user.
Deleting users deletes them for everyone else working on this assignment,
and we don’t want to be left with 0 users. This means
do not implement
- Add a loading screen or animation that shows up while the requests load.
Tips
- Read this page very carefully.
- Use
console.log
everywhere all the time to see what all your variables look like. This is especially helpful when working with complex objects, such as fetch response objects. - The first route you add, and the first API you call, will always be the hardest. Once you figure it out once, it will be much easier to do it again!
- In almost all cases, you should be using the response from an API call to set some state variable.
- If you keep your code organized, this assignment will be easier. We love helper functions.
- There is a small chance that the users API doesn't work due to an issue in our implementation. If you suspect that is the case, reach out to Ethan Pineda on discord.
Resources
- The workshop 5 slides and recordings, which you can find here.
- We have re-recorded the presentation section about concurrency & asynchronous functions to improve our explanations of those concepts.
- The workshop 4 slides and recording, which you can find here. The useEffect section may be particularly useful.
- Routing with
react-router-dom
- Lightweight tutorial by W3Schools
- How to use route parameters.
This is the
/:id
stuff you see in some routes. - More in-depth tutorial
- Deeper dive into it's implementation
- Making API requests
- Demo of the Users Api
- How HTTP requests and responses work
- Fetch API guide
- multipart/form-data guide
- An example
of making multipart/form-data requests using the
FormData
object.- This example uses the products API from workshop 5, so your code will look a little different. But for the most part, it should look very similar.
- StackOverflow
- Quora
- ChatGPT. Know that it will be obvious if you just use AI to complete the entire assignment and put in little effort.
Submission
For feedback from DISC exec
Follow these instructions if you are submitting to get feedback from DISC exec. The deadline for your submission is 1 week after the associated workshop.
- Make sure all your changes are committed and pushed onto github
(the hw-4 branch, NOT the main branch or
hw-4
branch). - On github, make a pull request from your
hw-5
branch. Do NOT merge the pull request. - Once you've created the pull request, copy its URL from the browser.
- Fill out this google form
For the Discover Program Application
If you are not submitting for DISC exec feedback, but for your Discover Program Application, follow the submission instructions in the application form.