Skip to content

Commit 95af184

Browse files
author
WebDeveloperGuide
committed
Products > Export Products in CSV file functionality added.
1 parent 74ebd41 commit 95af184

File tree

5 files changed

+69
-5
lines changed

5 files changed

+69
-5
lines changed

web_admin/package-lock.json

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web_admin/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"axios": "^0.26.0",
1010
"jquery": "^3.6.0",
1111
"react": "^17.0.2",
12+
"react-csv": "^2.2.2",
1213
"react-dom": "^17.0.2",
1314
"react-paginate": "^8.1.2",
1415
"react-redux": "^7.2.6",

web_admin/src/App.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,9 @@ textarea.form-control, select.form-control{
6565

6666
.form-control{
6767
height: calc(2.25rem + 4px);
68+
}
69+
70+
.download-csv{
71+
font-size: 24px;
72+
margin-top: 5px;
6873
}

web_admin/src/components/pages/product/Products.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {useEffect, useState} from 'react';
1+
import {useEffect, useState, useRef} from 'react';
22
import {Link} from 'react-router-dom';
33
import Product from './Product';
44
import Header from '../../Header';
@@ -7,13 +7,25 @@ import Footer from '../../Footer';
77
import { useDispatch, useSelector } from "react-redux";
88
import { listProducts } from "../../../redux/actions/ProductActions";
99
import ReactPaginate from 'react-paginate';
10+
import { CSVLink } from "react-csv";
11+
import axios from 'axios';
1012
import './product.css';
1113

14+
const headers = [
15+
{ label: "Name", key: "title" },
16+
{ label: "Description", key: "description" },
17+
{ label: "Image", key: "image" },
18+
{ label: "Price", key: "price" },
19+
{ label: "Stock", key: "stock" }
20+
];
21+
1222
const Products = () => {
1323
const dispatch = useDispatch();
1424
const [currentPage, setCurrentPage] = useState(0);
1525
const [searchTerm, setSearchTerm] = useState('');
16-
26+
const [csvData, setCsvData] = useState([]);
27+
const myRefBtn= useRef(null);
28+
1729
const productList = useSelector((state) => state.productList);
1830
const { loading, error, products, numOfPages, sortBy, searchText } = productList;
1931

@@ -44,7 +56,14 @@ const Products = () => {
4456
setCurrentPage(0);
4557
dispatch(listProducts(pageNum,productsPerPage, sortByValue, searchText));
4658
}
47-
59+
60+
const getCsvProducts = async () => {
61+
const responseData = await axios.get(`/products/all`);
62+
const data = responseData.data;
63+
setCsvData(data.data);
64+
myRefBtn.current.link.click();
65+
}
66+
4867

4968
return(
5069
<>
@@ -61,7 +80,7 @@ const Products = () => {
6180
<h4 className="card-title">Products</h4>
6281
<div className="row">
6382
<div className="col-md-12">
64-
<div className="form-group row">
83+
<div className="form-group row">
6584
<div className="offset-md-2 col-sm-4">
6685
<input type="text" placeholder="Search" className="form-control"
6786
name="search" onChange={(e) => setSearchTerm(e.target.value)}/>
@@ -76,7 +95,18 @@ const Products = () => {
7695
<div className="col-sm-2">
7796
<Link to="/product/add" className="btn btn-outline-primary btn-fw float-right">
7897
Add Product
79-
</Link>
98+
</Link>
99+
</div>
100+
<div className="col-sm-1">
101+
<i className="fa fa-download download-csv" onClick={getCsvProducts} title="Download CSV"/>
102+
<CSVLink
103+
data={csvData}
104+
headers={headers}
105+
className="d-none"
106+
ref={myRefBtn}
107+
filename={"Product-Data.csv"}
108+
>
109+
</CSVLink>
80110
</div>
81111
</div>
82112
</div>

web_services/routes/product.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,23 @@ router.get("/", async (req,res)=>{
8080
}
8181
})
8282

83+
//Get All Products
84+
router.get("/all", async (req,res)=>{
85+
try{
86+
const totalProducts = await Product.countDocuments({});
87+
const productData = await Product.find({}).sort({_id: 1});
88+
89+
if(productData){
90+
res.status(200).json({success:1,message:"", data:productData});
91+
}else{
92+
res.status(200).json({success:0,message:"No Data Found!"})
93+
}
94+
95+
}catch(err){
96+
res.status(500).json({status:0,message:err.message})
97+
}
98+
})
99+
83100
//Get Single Product
84101
router.get("/find/:id", async (req,res)=>{
85102

0 commit comments

Comments
 (0)