This project demonstrates a simple application built using React (Next.js) that interacts with a mock API to perform server-side actions such as adding products to a database.
The serverActions.tsx
file contains server-side functions that communicate with the mock API. Specifically, it includes an addProductToDatabase
function responsible for adding products to the database by sending a POST request to a specified endpoint.
-
Data Fetching: The application fetches product data from a mock API endpoint upon rendering the page.
-
Rendering Product List: Fetched product data is displayed on the page, showcasing the existing products.
-
Adding Products:
- Form Submission: Users can add new products via a form. Upon form submission, the
addProductToDatabase
function fromserverActions.tsx
is triggered. - Predefined Product Addition: There's a dedicated button ("Add Macbook Pro") triggering the addition of a predefined product using the
addProductToDatabase
function.
- Form Submission: Users can add new products via a form. Upon form submission, the
-
Server-side Interaction:
- The
addProductToDatabase
function processes the product data received from the form or predefined parameters. - It constructs a POST request with the product details and sends it to the mock API endpoint.
- The
-
Cache Management:
- Upon successfully adding a product, the function triggers cache revalidation for the "products" tag using Next.js's cache API.
- React (Next.js)
- TypeScript
- Mock API
- Next.js Cache API
To run the project:
- Clone the repository.
- Install dependencies (
npm install
). - Run the development server (
npm run dev
). - Access the application via
http://localhost:3000
.