From f7143bdc9db41991663e2d5228c1732f723c1781 Mon Sep 17 00:00:00 2001
From: pawan-dot <71133473+pawan-dot@users.noreply.github.com>
Date: Thu, 18 Apr 2024 17:48:54 +0530
Subject: [PATCH] change product and razorpay
---
src/index.js | 2 +-
src/views/Products/Products.js | 1340 ++++++++++++++++++++++----------
src/views/orders/ViewOrders.js | 3 +-
3 files changed, 921 insertions(+), 424 deletions(-)
diff --git a/src/index.js b/src/index.js
index 466639c..3e4516a 100644
--- a/src/index.js
+++ b/src/index.js
@@ -14,7 +14,7 @@ import { cibGmail } from "@coreui/icons";
import { createRoot } from "react-dom/client";
const setupAxios = () => {
- //axios.defaults.baseURL = "http://localhost:5000";
+ // axios.defaults.baseURL = "http://localhost:5000";
axios.defaults.baseURL = "https://api.smellika.com";
axios.defaults.headers = {
diff --git a/src/views/Products/Products.js b/src/views/Products/Products.js
index f7f11c1..4cb6d68 100644
--- a/src/views/Products/Products.js
+++ b/src/views/Products/Products.js
@@ -1,57 +1,782 @@
-import React, { useState, useEffect } from "react";
+// import React, { useState, useEffect } from "react";
+// import { Link } from "react-router-dom";
+// import Button from "@material-ui/core/Button";
+// import { useNavigate } from "react-router-dom";
+// import axios from "axios";
+// import { isAutheticated } from "src/auth";
+// import swal from "sweetalert";
+// import {
+// Box,
+// FormControl,
+// IconButton,
+// InputLabel,
+// MenuItem,
+// Select,
+// TextField,
+// } from "@mui/material";
+// import SearchIcon from "@mui/icons-material/Search";
+// import Fuse from "fuse.js";
+// import { Typography } from "@material-ui/core";
+// const Products = () => {
+// const token = isAutheticated();
+// const [query, setQuery] = useState("");
+// const navigate = useNavigate();
+// const [loading, setLoading] = useState(true);
+// const [success, setSuccess] = useState(true);
+// const [productsData, setProductsData] = useState([]);
+// const [filterData, setFilterData] = useState([]);
+// const [queryData, setQueryData] = useState([]);
+
+// const [currentPage, setCurrentPage] = useState(1);
+// const [itemPerPage, setItemPerPage] = useState(10);
+// const [showData, setShowData] = useState(productsData);
+
+// const handleShowEntries = (e) => {
+// setCurrentPage(1);
+// setItemPerPage(e.target.value);
+// };
+
+// const getProductsData = async () => {
+// axios
+// .get(`/api/product/getAll/`, {
+// headers: {
+// Authorization: `Bearer ${token}`,
+// },
+// })
+// .then((res) => {
+// setProductsData(res.data?.product);
+// setLoading(false);
+// })
+// .catch((error) => {
+// const msg = error?.response?.data?.message || "Something went wrong!";
+
+// swal({
+// title: error,
+// text: msg,
+// icon: "error",
+// button: "Retry",
+// dangerMode: true,
+// });
+// setLoading(false);
+// });
+// };
+
+// useEffect(() => {
+// getProductsData();
+// }, [success]);
+
+// useEffect(() => {
+// const loadData = () => {
+// const indexOfLastPost = currentPage * itemPerPage;
+// const indexOfFirstPost = indexOfLastPost - itemPerPage;
+// setShowData(productsData.slice(indexOfFirstPost, indexOfLastPost));
+// };
+// loadData();
+// }, [currentPage, itemPerPage, productsData]);
+
+// const handleDelete = (id) => {
+// swal({
+// title: "Are you sure?",
+// icon: "error",
+// buttons: {
+// Yes: { text: "Yes", value: true },
+// Cancel: { text: "Cancel", value: "cancel" },
+// },
+// }).then((value) => {
+// if (value === true) {
+// axios
+// .delete(`/api/product/delete/${id}`, {
+// headers: {
+// "Access-Control-Allow-Origin": "*",
+// Authorization: `Bearer ${token}`,
+// },
+// })
+// .then((res) => {
+// swal({
+// title: "Deleted",
+// text: "Product Deleted successfully!",
+// icon: "success",
+// button: "ok",
+// });
+// setSuccess((prev) => !prev);
+// })
+// .catch((err) => {
+// const msg = err?.response?.data?.message || "Something went wrong!";
+
+// swal({
+// title: "Warning",
+// text: msg,
+// icon: "error",
+// button: "Retry",
+// dangerMode: true,
+// });
+// });
+// }
+// });
+// };
+// const [filterCategory, setFilterCategory] = useState("");
+
+// const handleSearchClick = (query) => {
+// const option = {
+// isCaseSensitive: true,
+// includeScore: false,
+// shouldSort: true,
+// includeMatches: false,
+// findAllMatches: false,
+// minMatchCharLength: 1,
+// location: 0,
+// threshold: 0.6,
+// distance: 100,
+// useExtendedSearch: true,
+// ignoreLocation: false,
+// ignoreFieldNorm: false,
+// fieldNormWeight: 1,
+// keys: ["name"],
+// };
+
+// const fuse = new Fuse(productsData, option);
+// const result = fuse.search(query);
+
+// const searchedResult = result.map((result) => result.item);
+// console.log(searchedResult);
+// setQueryData(searchedResult);
+// };
+// useEffect(() => {
+// if (query !== "") {
+// setFilterCategory("");
+// }
+// setTimeout(() => handleSearchClick(query), 100);
+// }, [query]);
+// const uniqueCategoryNames = [
+// ...new Set(showData?.map((product) => product?.category?.categoryName)),
+// ];
+// useEffect(() => {
+// setTimeout(() => {
+// if (filterCategory !== "") {
+// const filteredProducts = productsData.filter(
+// (product) => product.category?.categoryName === filterCategory
+// );
+
+// setFilterData(filteredProducts);
+// } else {
+// // If no category is selected, show all products
+// setShowData(productsData);
+// // setFilterData(filteredProducts);
+// }
+// }, 100);
+// }, [filterCategory, productsData]);
+// return (
+//
+//
+//
+//
+//
+//
+//
+// Products
+//
+
+//
+//
+//
+//
+//
+//
+
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+
+//
+//
+//
+//
+// Image |
+// Product |
+// Category |
+
+// Price |
+// Added On |
+// Actions |
+//
+//
+//
+// {!loading && showData.length === 0 && (
+//
+//
+// No Data Available
+// |
+//
+// )}
+// {loading ? (
+//
+//
+// Loading...
+// |
+//
+// ) : query === "" && filterCategory == "" ? (
+// showData.map((product, i) => {
+// return (
+//
+//
+// {product.image &&
+// product.image.map((i, j) => (
+//
+// ))}
+// |
+// {product.name} |
+//
+// {product.category?.categoryName !== ""
+// ? product.category?.categoryName
+// : "Category Not selected "}
+// |
+//
+// ₹
+// {product?.total_amount
+// ? product?.total_amount
+// : product?.variants[0]?.price}
+// |
+//
+// {new Date(product.createdAt).toLocaleString(
+// "en-IN",
+// {
+// weekday: "short",
+// month: "short",
+// day: "numeric",
+// year: "numeric",
+// hour: "numeric",
+// minute: "numeric",
+// hour12: true,
+// }
+// )}
+// |
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+// |
+//
+// );
+// })
+// ) : query !== "" ? (
+// queryData.map((product, i) => {
+// return (
+//
+//
+// {product.image &&
+// product.image.map((i, j) => (
+//
+// ))}
+// |
+// {product.name} |
+//
+// {product.category !== ""
+// ? product.category?.categoryName
+// : "Category Not selected "}
+// |
+// ₹{product.price} |
+//
+// {new Date(product.createdAt).toLocaleString(
+// "en-IN",
+// {
+// weekday: "short",
+// month: "short",
+// day: "numeric",
+// year: "numeric",
+// hour: "numeric",
+// minute: "numeric",
+// hour12: true,
+// }
+// )}
+// |
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+// |
+//
+// );
+// })
+// ) : (
+// query == "" &&
+// filterData.map((product, i) => {
+// return (
+//
+//
+// {product.image &&
+// product.image.map((i, j) => (
+//
+// ))}
+// |
+// {product.name} |
+//
+// {product.category?.categoryName}
+// |
+// ₹{product.price} |
+//
+// {new Date(product.createdAt).toLocaleString(
+// "en-IN",
+// {
+// weekday: "short",
+// month: "short",
+// day: "numeric",
+// year: "numeric",
+// hour: "numeric",
+// minute: "numeric",
+// hour12: true,
+// }
+// )}
+// |
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+// |
+//
+// );
+// })
+// )}
+//
+//
+//
+
+//
+//
+//
+// Showing {currentPage * itemPerPage - itemPerPage + 1} to{" "}
+// {Math.min(
+// currentPage * itemPerPage,
+// productsData.length
+// )}{" "}
+// of {productsData.length} entries
+//
+//
+
+//
+//
+//
+// -
+// setCurrentPage((prev) => prev - 1)}
+// >
+// Previous
+//
+//
+
+// {!(currentPage - 1 < 1) && (
+// -
+//
+// setCurrentPage((prev) => prev - 1)
+// }
+// >
+// {currentPage - 1}
+//
+//
+// )}
+
+// -
+//
+// {currentPage}
+//
+//
+
+// {!(
+// (currentPage + 1) * itemPerPage - itemPerPage >
+// productsData.length - 1
+// ) && (
+// -
+// {
+// setCurrentPage((prev) => prev + 1);
+// }}
+// >
+// {currentPage + 1}
+//
+//
+// )}
+
+// -
+// productsData.length - 1
+// )
+// ? "paginate_button page-item next"
+// : "paginate_button page-item next disabled"
+// }
+// >
+// setCurrentPage((prev) => prev + 1)}
+// >
+// Next
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+// );
+// };
+
+// export default Products;
+
+import React, { useState, useEffect, useRef } from "react";
import { Link } from "react-router-dom";
+import axios from "axios";
import Button from "@material-ui/core/Button";
import { useNavigate } from "react-router-dom";
-import axios from "axios";
import { isAutheticated } from "src/auth";
import swal from "sweetalert";
-import {
- Box,
- FormControl,
- IconButton,
- InputLabel,
- MenuItem,
- Select,
- TextField,
-} from "@mui/material";
-import SearchIcon from "@mui/icons-material/Search";
-import Fuse from "fuse.js";
-import { Typography } from "@material-ui/core";
const Products = () => {
const token = isAutheticated();
- const [query, setQuery] = useState("");
const navigate = useNavigate();
- const [loading, setLoading] = useState(true);
+ const [loading, setLoading] = useState(false);
const [success, setSuccess] = useState(true);
const [productsData, setProductsData] = useState([]);
- const [filterData, setFilterData] = useState([]);
- const [queryData, setQueryData] = useState([]);
+ const [categories, setCategories] = useState([]);
+
+ const nameRef = useRef();
+ const categoryRef = useRef();
const [currentPage, setCurrentPage] = useState(1);
const [itemPerPage, setItemPerPage] = useState(10);
- const [showData, setShowData] = useState(productsData);
-
- const handleShowEntries = (e) => {
- setCurrentPage(1);
- setItemPerPage(e.target.value);
- };
-
+ const [totalData, setTotalData] = useState(0);
+ // const {
+ // edit,
+ // add,
+ // delete: deletepermission,
+ // } = checkPermission("Product Master");
const getProductsData = async () => {
+ setLoading(true);
+
axios
- .get(`/api/product/getAll/`, {
+ .get(`/api/product/getAll/admin/`, {
headers: {
Authorization: `Bearer ${token}`,
},
+ params: {
+ page: currentPage,
+ show: itemPerPage,
+ name: nameRef.current.value,
+ category: categoryRef.current.value,
+ },
})
.then((res) => {
+ console.log("res.data?.data", res.data?.product);
setProductsData(res.data?.product);
+ setTotalData(res.data?.total_data);
setLoading(false);
})
- .catch((error) => {
- const msg = error?.response?.data?.message || "Something went wrong!";
-
+ .catch((err) => {
+ const msg = err?.response?.data?.message || "Something went wrong!";
swal({
- title: error,
+ title: err,
text: msg,
icon: "error",
button: "Retry",
@@ -59,20 +784,30 @@ const Products = () => {
});
setLoading(false);
});
+
+ setLoading(false);
+ };
+
+ const getCatagories = () => {
+ axios
+ .get(`/api/category/getCategories`, {
+ headers: {
+ "Access-Control-Allow-Origin": "*",
+ Authorization: `Bearer ${token}`,
+ },
+ })
+ .then((res) => {
+ setCategories(res?.data?.categories);
+ });
};
useEffect(() => {
- getProductsData();
- }, [success]);
+ getCatagories();
+ }, []);
useEffect(() => {
- const loadData = () => {
- const indexOfLastPost = currentPage * itemPerPage;
- const indexOfFirstPost = indexOfLastPost - itemPerPage;
- setShowData(productsData.slice(indexOfFirstPost, indexOfLastPost));
- };
- loadData();
- }, [currentPage, itemPerPage, productsData]);
+ getProductsData();
+ }, [success, itemPerPage, currentPage]);
const handleDelete = (id) => {
swal({
@@ -101,11 +836,9 @@ const Products = () => {
setSuccess((prev) => !prev);
})
.catch((err) => {
- const msg = err?.response?.data?.message || "Something went wrong!";
-
swal({
title: "Warning",
- text: msg,
+ text: "Something went wrong!",
icon: "error",
button: "Retry",
dangerMode: true,
@@ -114,57 +847,44 @@ const Products = () => {
}
});
};
- const [filterCategory, setFilterCategory] = useState("");
-
- const handleSearchClick = (query) => {
- const option = {
- isCaseSensitive: true,
- includeScore: false,
- shouldSort: true,
- includeMatches: false,
- findAllMatches: false,
- minMatchCharLength: 1,
- location: 0,
- threshold: 0.6,
- distance: 100,
- useExtendedSearch: true,
- ignoreLocation: false,
- ignoreFieldNorm: false,
- fieldNormWeight: 1,
- keys: ["name"],
- };
-
- const fuse = new Fuse(productsData, option);
- const result = fuse.search(query);
-
- const searchedResult = result.map((result) => result.item);
- console.log(searchedResult);
- setQueryData(searchedResult);
- };
- useEffect(() => {
- if (query !== "") {
- setFilterCategory("");
- }
- setTimeout(() => handleSearchClick(query), 100);
- }, [query]);
- const uniqueCategoryNames = [
- ...new Set(showData?.map((product) => product?.category?.categoryName)),
- ];
- useEffect(() => {
- setTimeout(() => {
- if (filterCategory !== "") {
- const filteredProducts = productsData.filter(
- (product) => product.category?.categoryName === filterCategory
- );
-
- setFilterData(filteredProducts);
- } else {
- // If no category is selected, show all products
- setShowData(productsData);
- // setFilterData(filteredProducts);
+ const handleStatus = (id) => {
+ swal({
+ title: "Are you sure?",
+ icon: "warning",
+ buttons: {
+ Yes: { text: "Yes", value: true },
+ Cancel: { text: "Cancel", value: "cancel" },
+ },
+ }).then((value) => {
+ if (value === true) {
+ axios
+ .patch(`/api/product/admin/status/${id}`, {
+ headers: {
+ "Access-Control-Allow-Origin": "*",
+ Authorization: `Bearer ${token}`,
+ },
+ })
+ .then((res) => {
+ swal({
+ title: "Chenged",
+ text: "Product status changed successfully!",
+ icon: "success",
+ button: "ok",
+ });
+ setSuccess((prev) => !prev);
+ })
+ .catch((err) => {
+ swal({
+ title: "Warning",
+ text: "Something went wrong!",
+ icon: "error",
+ button: "Retry",
+ dangerMode: true,
+ });
+ });
}
- }, 100);
- }, [filterCategory, productsData]);
+ });
+ };
return (
@@ -182,7 +902,6 @@ const Products = () => {
Products
-