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 { isAutheticated } from "src/auth"; import swal from "sweetalert"; const Products = () => { const token = isAutheticated(); const navigate = useNavigate(); const [loading, setLoading] = useState(false); const [success, setSuccess] = useState(true); const [productsData, setProductsData] = useState([]); const [categories, setCategories] = useState([]); const nameRef = useRef(); const categoryRef = useRef(); const FeatureProductRef = useRef(); const [currentPage, setCurrentPage] = useState(1); const [itemPerPage, setItemPerPage] = useState(10); const [totalData, setTotalData] = useState(0); // const { // edit, // add, // delete: deletepermission, // } = checkPermission("Product Master"); const getProductsData = async () => { setLoading(true); await axios .get(`/api/product/getAll/admin/`, { headers: { Authorization: `Bearer ${token}`, }, params: { page: currentPage, show: itemPerPage, name: nameRef.current.value, category: categoryRef.current.value, FeatureProduct: FeatureProductRef.current.value, }, }) .then((res) => { // console.log(res.data); setProductsData(res.data?.products); setTotalData(res.data?.total_data); setLoading(false); }) .catch((err) => { const msg = err?.response?.data?.message || "Something went wrong!"; swal({ title: err, text: msg, icon: "error", button: "Retry", dangerMode: true, }); setLoading(false); }); setLoading(false); }; const getCatagories = () => { axios .get(`/api/category/getCategories`, { headers: { "Access-Control-Allow-Origin": "*", Authorization: `Bearer ${token}`, }, }) .then((res) => { // console.log(res?.data?.categories); setCategories(res?.data?.categories); }); }; const [currencyDetails, setCurrencyDetails] = useState(null); const getCurrency = async () => { try { const response = await axios.get("/api/currency/getall", { // headers: { // Authorization: `Bearer ${token}`, // }, }); if (response.status === 200) { setCurrencyDetails(response?.data?.currency[0]); } } catch (error) { console.log(error); } }; useEffect(() => { getCatagories(); getCurrency(); }, []); useEffect(() => { getProductsData(); }, [success, itemPerPage, currentPage]); 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) => { let msg = err?.response?.data?.message ? err?.response?.data?.message : "Something went wrong!"; swal({ title: "Warning", text: msg, icon: "error", button: "Retry", dangerMode: true, }); }); } }); }; const handleFeaturedProduct = (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/feature_product/status/${id}`, { headers: { "Access-Control-Allow-Origin": "*", Authorization: `Bearer ${token}`, }, }) .then((res) => { swal({ title: "Changed", text: " Feature Product status changed successfully!", icon: "success", button: "ok", }); setSuccess((prev) => !prev); }) .catch((err) => { let msg = err?.response?.data?.msg ? err?.response?.data?.msg : "Something went wrong!"; swal({ title: "Warning", text: msg, icon: "warning", button: "ok", dangerMode: true, }); }); } }); }; 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: "Changed", 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, }); }); } }); }; // console.log("currencyDetails", currencyDetails); return (
Products
{loading ? ( ) : productsData?.length > 0 ? ( productsData?.map((product, i) => { return ( ); }) ) : ( !loading && productsData?.length === 0 && ( ) )}
Image Product Category Feature Product Price Status Added On Actions
Loading...
{product?.image && product?.image?.length !== 0 ? ( <> preview ) : (

No

image

uploaded!

)}
{product.name} {product.category?.categoryName !== "" ? product.category?.categoryName : "Category Not selected "} {currencyDetails?.CurrencySymbol} {product?.price} {new Date(product.createdAt).toLocaleString( "en-IN", { weekday: "short", month: "short", day: "numeric", year: "numeric", hour: "numeric", minute: "numeric", hour12: true, } )}
No Product Available...
Showing {currentPage * itemPerPage - itemPerPage + 1} to{" "} {Math.min(currentPage * itemPerPage, totalData)} of{" "} {totalData} entries
  • setCurrentPage((prev) => prev - 1)} disabled={loading} > Previous
  • {!(currentPage - 1 < 1) && (
  • setCurrentPage((prev) => prev - 1) } disabled={loading} > {currentPage - 1}
  • )}
  • {currentPage}
  • {!( (currentPage + 1) * itemPerPage - itemPerPage > totalData - 1 ) && (
  • { setCurrentPage((prev) => prev + 1); }} disabled={loading} > {currentPage + 1}
  • )}
  • totalData - 1 ) ? "paginate_button page-item next" : "paginate_button page-item next disabled" } > setCurrentPage((prev) => prev + 1)} disabled={loading} > Next
); }; export default Products;