Welcome to today's class on building a User Management System using React and MSW (Mock Service Worker)! In this class, we'll be learning how to make API requests using fetch()
in React and how to mock those requests using MSW. By the end of this workshop, you'll have built a fully functional User Management System that fetches and manages user data from a mock API.
Before getting started, make sure you have the following installed on your computer:
- Node.js (https://nodejs.org/)
- Git (https://git-scm.com/)
-
Clone the Repository: Clone this repository to your local machine using the following command:
git clone [email protected]:Migracode-Barcelona/react-user-management-starter.git user-management-system
-
Install Dependencies: Navigate into the project directory and install the necessary dependencies using the following command:
cd user-management-system npm install
-
Start the Development Server: Start the development server to view the app in your browser:
npm start
-
Mock Service Worker (MSW): MSW is already configured in this project to mock API requests. You'll find the MSW handlers in the
src/mocks/handlers.js
file.
- Clone the repository to your local machine.
- Install the necessary dependencies using
npm install
. - Start the development server using
npm start
.
- Take a moment to explore the project structure and familiarize yourself with the files and folders.
Objective: Implement the User Management System to fetch and manage user data from a mock API.
-
Display Users:
- Open the
src/App.jsx
file. - Implement logic to fetch the list of users from the mock API using
fetch()
and display them in the app. - Display each user's details such as ID and username in a user-friendly format.
- Open the
-
Add New User:
- Implement functionality to add a new user to the system.
- Create a form or input field where users can enter the username of the new user.
- Use
fetch()
to send a POST request to the mock API with the username of the new user. - Update the user list to include the newly added user.
-
Handle Errors:
- Implement error handling to display meaningful error messages if there are any issues with fetching or adding users.
- Test different error scenarios (e.g., network errors, server errors) to ensure error handling is working correctly.
-
User Interface Design:
- Design the user interface to make it visually appealing and user-friendly.
- Use CSS or a UI library (e.g., Bootstrap, Material-UI) to style the components and layout.
-
Testing:
- Test the User Management System to ensure it works as expected in different scenarios (e.g., fetching users, adding users, error handling).
- Verify that the user interface looks and functions correctly.
- Break down the implementation into smaller tasks and tackle them one at a time.
- Refer to React documentation and online resources for guidance on implementing specific features or handling common tasks.
- Feel free to customize the user interface and functionality based on your preferences and requirements.
- Open the
src/mocks/handlers.js
file and explore the provided MSW handlers. - Test different scenarios (success, error) by enabling/disabling the handlers as needed.
- Test your app to ensure it works as expected in different scenarios (e.g., successful response, error response).
- Reflect on your learnings from today's workshop.
- Discuss any challenges faced and strategies for overcoming them.
- Share your experience with the class.