Skip to content

Commit b1ca166

Browse files
committed
Fetch chart of single product data
- Edit get revenues order route in server to add product id query and edit pipline for additional request by new query if founded - fetch single product chart data fetch > then sort it by id > then map it in jsx - Fix: verifyToken for admin as change promise of condition isAdmin
1 parent 431b4ed commit b1ca166

File tree

5 files changed

+35
-10
lines changed

5 files changed

+35
-10
lines changed

admin/src/pages/ProductSinglePage.jsx

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react'
1+
import React, { useEffect, useMemo, useState } from 'react'
22

33
// import styled components for css styling...
44
import styled from 'styled-components'
@@ -13,6 +13,7 @@ import Charts from '../components/Charts'
1313
import { productsData } from '../Data/dummyData'
1414
import { AddOutlined, Publish } from '@mui/icons-material'
1515
import { useSelector } from 'react-redux'
16+
import { userRequest } from '../requestAxiosMethod'
1617

1718
// Styling...
1819
const Container = styled.div`
@@ -171,8 +172,31 @@ export default function ProductSinglePage() {
171172
// get product by it's id from url...
172173
const pageUrl = useLocation()
173174
const productId = pageUrl.pathname.split('/')[2]
175+
const [productChartInfo, setProductChartInfo] = useState([])
174176
// get the product from redux by it's _id that comes from url...
175177
const product = useSelector((state) => state.product.products.find((item) => item._id === productId))
178+
179+
const months = useMemo(
180+
() => [
181+
'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec',
182+
], []
183+
);
184+
185+
useEffect(() => {
186+
const getOrderedProductInfo = async () => {
187+
try {
188+
const response = await userRequest.get("orders/revenues?pid=" + productId )
189+
// sort response by id..
190+
const sortedResponse = response.data.sort((a,b)=> a._id - b._id)
191+
sortedResponse.map((item) => {
192+
setProductChartInfo(previous=>[...previous, {name:months[item._id-1], Sales: item.total}])
193+
})
194+
} catch (error) {
195+
196+
}
197+
}
198+
getOrderedProductInfo()
199+
}, [months, productId])
176200
console.log(product)
177201
return (
178202
<Container>
@@ -187,7 +211,7 @@ export default function ProductSinglePage() {
187211
</ProductTitleContainer>
188212
<ProductTop>
189213
<ProductTopLeft>
190-
<Charts data={productsData} title="Product Movements" secondLine="Sales" firstLine="Returns" />
214+
<Charts data={productChartInfo} title="Product Movements" secondLine="Sales" firstLine="Returns" />
191215
</ProductTopLeft>
192216
<ProductTopRight>
193217
<ProductInfoTop>

server/controllers/authenticationController.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const singin = async (req, res, next) => {
4242
// intialize user token throw user _id and user isAdmin and send jwt...
4343
const accessToken = jwt.sign({
4444
id: user._id,
45-
isAdmin: user.isAdmin
45+
isAdmin: user.isAdmin,
4646
}, process.env.JWT_SEC,
4747
{ expiresIn: '1d' }
4848
);

server/controllers/orderController.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ export const getAllOrders = async (req, res, next) => {
5656

5757
// Get Monthly Revenues...
5858
export const getMonthlyRevenues = async (req, res, next) => {
59+
// query for productId in product single page...
60+
const productId = req.query.pid;
5961
// current date...
6062
const date = new Date()
6163
// last month from current date...
@@ -65,7 +67,7 @@ export const getMonthlyRevenues = async (req, res, next) => {
6567

6668
try {
6769
const revenues = await Order.aggregate([
68-
{ $match: { createdAt: { $gte: previousTwoMonth } } },
70+
{ $match: { createdAt: { $gte: previousTwoMonth }, ...(productId && {products:{$elemMatch: {productId}}}), }, },
6971
{
7072
$project: {
7173
month: { $month: "$createdAt" },

server/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ const Connection = ()=>{
3131
// midlewares...
3232
app.use(cors());
3333
app.use(express.json()); // allowing receiving json data...
34-
app.use(cookieParser()); // using cookie parser...
3534
// using cookie parser...
3635
app.use(cookieParser())
3736
// api routes...

server/verifyToken.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ export const verifyToken = async (req, res, next) => {
1313
})
1414
}
1515

16-
export const verifyTokenAndAuthorization = (req, res, next) => {
17-
verifyToken(req, res, () => {
16+
export const verifyTokenAndAuthorization = async (req, res, next) => {
17+
await verifyToken(req, res, () => {
1818
if (req.user.id === req.params.id || req.user.isAdmin){
1919
next()
2020
} else {
@@ -23,9 +23,9 @@ export const verifyTokenAndAuthorization = (req, res, next) => {
2323
})
2424
}
2525

26-
export const verifyTokenAndAdmin = (req, res, next) => {
27-
verifyToken(req, res, () => {
28-
if (req.user.isAdmin) {
26+
export const verifyTokenAndAdmin = async (req, res, next) => {
27+
await verifyToken(req, res, async () => {
28+
if ( await req.user?.isAdmin) {
2929
next()
3030
} else {
3131
res.status(403).json("You don't have AUTHORIZATION for such action")

0 commit comments

Comments
 (0)