From 5833b24b1f0b9953118b2aabe0a4571559f0ab9c Mon Sep 17 00:00:00 2001 From: pawan-dot <71133473+pawan-dot@users.noreply.github.com> Date: Wed, 25 Jan 2023 16:11:18 +0530 Subject: [PATCH] product --- src/_nav.js | 8 +- src/index.js | 2 +- src/routes.js | 8 + src/views/Products/AddProduct.js | 1 + src/views/Products/Products.js | 384 +++++++++++++++++++++++++++++++ 5 files changed, 401 insertions(+), 2 deletions(-) create mode 100644 src/views/Products/AddProduct.js create mode 100644 src/views/Products/Products.js diff --git a/src/_nav.js b/src/_nav.js index 69db563..95574c7 100644 --- a/src/_nav.js +++ b/src/_nav.js @@ -6,6 +6,7 @@ import { cilBell, cilCalculator, cilChartPie, + cilClipboard, cilCommand, cilCursor, cilDrop, @@ -38,7 +39,12 @@ const _nav = [ }, - + { + component: CNavItem, + name: 'Products', + icon: , + to: '/products', + }, { component: CNavItem, name: 'Temples', diff --git a/src/index.js b/src/index.js index 62a8094..bc52aee 100644 --- a/src/index.js +++ b/src/index.js @@ -12,7 +12,7 @@ import axios from 'axios' const setupAxios = () => { axios.defaults.baseURL = 'https://atpapi.checkapp.one' - //axios.defaults.baseURL = 'http://localhost:5000' + // axios.defaults.baseURL = 'http://localhost:5000' axios.defaults.headers = { 'Cache-Control': 'no-cache,no-store', 'Pragma': 'no-cache', diff --git a/src/routes.js b/src/routes.js index fa3f33a..c8cae34 100644 --- a/src/routes.js +++ b/src/routes.js @@ -26,6 +26,7 @@ import Login from './views/pages/login/Login' import Temples from './views/Temples/Temples' import AddTemple from './views/Temples/AddTemple' import EditTemple from './views/Temples/EditTemple' +import Products from './views/Products/Products' const routes = [ @@ -33,6 +34,13 @@ const routes = [ { path: '/change_password', name: 'Change Password', element: Change_Password }, { path: '/profile/edit', name: 'Edit Profile', element: EditProfile }, // { path: '/profile', name: 'Profile', element: Profile }, + + + //Product + { path: '/products', name: 'products', element: Products }, + { path: '/product/add', name: 'Add products', element: AddTemple }, + { path: '/products/edit/:id', name: 'Edit products', element: EditTemple }, + //Temple { path: '/temples', name: 'Temples', element: Temples }, { path: '/temple/add', name: 'Add Temple', element: AddTemple }, diff --git a/src/views/Products/AddProduct.js b/src/views/Products/AddProduct.js new file mode 100644 index 0000000..0b04d79 --- /dev/null +++ b/src/views/Products/AddProduct.js @@ -0,0 +1 @@ +rafce diff --git a/src/views/Products/Products.js b/src/views/Products/Products.js new file mode 100644 index 0000000..441f730 --- /dev/null +++ b/src/views/Products/Products.js @@ -0,0 +1,384 @@ +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' + +const Products = () => { + const token = isAutheticated() + const navigate = useNavigate() + const [loading, setLoading] = useState(true) + const [success, setSuccess] = useState(true) + const [productsData, setProductsData] = 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`, { + headers: { + Authorization: `Bearer ${token}`, + }, + }) + .then((res) => { + setProductsData(res.data?.data) + setLoading(false) + }) + .catch((err) => { + 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/${id}`, { + headers: { + 'Access-Control-Allow-Origin': '*', + Authorization: `Bearer ${token}`, + }, + }) + .then((res) => { + setSuccess((prev) => !prev) + }) + .catch((err) => { + swal({ + title: 'Warning', + text: 'Something went wrong!', + icon: 'error', + button: 'Retry', + dangerMode: true, + }) + }) + } + }) + } + + return ( +
+
+
+
+
+
+
+ Products +
+ +
+ +
+
+
+
+ +
+
+
+
+
+
+
+ +
+
+
+ +
+ + + + + + + + + + + + + {!loading && showData.length === 0 && ( + + + + )} + {loading ? ( + + + + ) : ( + showData.map((product, i) => { + return ( + + + + + + + + + ) + }) + )} + +
Product NameCategoryPreviewMaster PriceAdded OnActions
+
No Data Available
+
+ Loading... +
{product.name}{product.category?.name} + {product?.images && ( + <> + preview + + )} + ₹ {product.master_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