Skip to content

Commit 1c920ac

Browse files
Update files
1 parent 0672b6b commit 1c920ac

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
# How to make an API request to an Express server when clicking on value cells within a React Pivot Table
3+
4+
A quick start project that helps you on how to make an API request to an Express server when clicking on value cells within a React Pivot Table
5+
6+
## Project prerequisites
7+
8+
Make sure that you have the latest versions of NodeJS and Visual Studio Code in your machine before starting to work on this project.
9+
10+
### How to run this application?
11+
12+
* To run this application, you need to clone the `how-to-do-api-call-to-express-server-when-clicking-value-cells-in-a-react-pivot-table` repository and then open it in Visual Studio Code.
13+
* Then Open the [Server](./Server/) file and install all the necessary packages into your server-side application using the `npm install` command and run your project using the `npm start` command
14+
* Next Open the [React](./React/) file install all the necessary react packages into your client-side application using the `npm install` command and run your project using the `npm start` command.

Server/index.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Middleware for parsing JSON and handling CORS
2+
import express from 'express';
3+
import cors from 'cors';
4+
const app = express();
5+
6+
app.use(express.json());
7+
app.use(cors());
8+
9+
// Endpoint to handle updates to the pivot table data
10+
app.use("/data", (req,res) => {
11+
try{
12+
console.log(req.body);
13+
let dataFromReact=req.body;
14+
// Delete the specific raw data of the cells from the data source if we have data in the cell.
15+
if(dataFromReact.data.value>0){
16+
dataFromReact.pivotData.splice(Object.keys(dataFromReact.data.indexObject),1);
17+
res.status(200).send(dataFromReact.pivotData);
18+
} else{
19+
// Added the raw data based on the cell combinations to the pivot table if the cell is empty.
20+
let newData={
21+
"Sold": 40,
22+
"Country": dataFromReact.data.rowHeaders,
23+
"Year": dataFromReact.data.columnHeaders
24+
};
25+
dataFromReact.pivotData.push(newData);
26+
// Send back the updated data source to the client
27+
res.status(200).send(dataFromReact.pivotData);
28+
}
29+
} catch (error) {
30+
res.status(400).send(error);
31+
}
32+
});
33+
34+
// Define the port for the server here.
35+
app.listen(5000,()=>{
36+
console.log("Server is running on port 5000");
37+
});

Server/package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "server",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"type": "module",
7+
"dependencies": {
8+
"cors": "^2.8.5",
9+
"express": "^4.18.2",
10+
"nodemon": "^2.0.21"
11+
},
12+
"devDependencies": {},
13+
"scripts": {
14+
"test": "echo \"Error: no test specified\" && exit 1",
15+
"start": "nodemon index.js"
16+
},
17+
"author": "",
18+
"license": "ISC"
19+
}

0 commit comments

Comments
 (0)