This is a Next.js project bootstrapped with create-next-app
that fetches user data from an API and displays it in a paginated table with the ability to view user details in a modal. Below are the steps for setting up and running the code:
Before you begin, ensure you have the following prerequisites installed:
- Node.js: Make sure you have Node.js installed. You can download it from nodejs.org.
-
Clone the Repository
Clone the repository containing the code to your local machine.
git clone https://github.com/RahymCharyyev/usersTable.git cd usersTable
-
Install Dependencies
Install the project dependencies using yarn.
yarn
-
Environment Variables
Create an environment variable file (
.env.local
) in the project root directory and define theNEXT_PUBLIC_API_BASE_URL
variable. This variable should point to the base URL of the API you are using to fetch user data. For example:NEXT_PUBLIC_API_BASE_URL= https://reqres.in
-
Start the Development Server
Start the development server to run the React application.
yarn dev
This will start the application on
http://localhost:3000
. -
Access the Application
Open a web browser and navigate to
http://localhost:3000
to access the application. -
Deployed Version You can find a deployed version of this project on Vercel. It's accessible at the following URL: https://users-table-ten.vercel.app/
Deployed Version on Vercel.
-
Project Structure
The codebase is structured into components and modules. Key components and modules include:
Pagination
: A reusable pagination component for navigating between pages of user data.UserList
: The main component responsible for fetching user data, displaying it in a table, and handling pagination.TableHead
andTableBody
: Components for rendering the table headers and table rows, respectively.UserInfo
andUserModal
: Components for displaying user details and a user details modal.fetchUsers
: A function for making API requests to fetch user data.
-
Routing and Pagination
The code uses the Next.js
useRouter
hook from Pages Router for routing and handles pagination by modifying the URL query parameterpage
. -
API Integration
The
fetchUsers
function uses Axios to make GET requests to an API endpoint for fetching user data. The API base URL is defined as an environment variable. -
Component Styling
The code uses CSS classes and styles for component styling, and it also uses tailwind.css for responsive design and utility classes.
-
Error Handling
The code handles API request errors and displays appropriate error messages when data cannot be fetched.
-
Modal for User Details
User details can be viewed by clicking on a user row in the table, which opens a modal with detailed user information.
-
Pagination Logic
Pagination logic calculates the total number of pages based on the number of users fetched from the API. It ensures that users can navigate through the pages of user data.