import React, { useState, useEffect, useRef } from 'react'; import AllCard from '../Components/AllCard'; import Pagination from "../Components/Pagination"; import axios from 'axios'; import catchError from '../utils/catchErrors'; import { Row, Form, FormControl, Button, Container } from 'react-bootstrap'; function Admin() { const [search, setSearch] = useState({ word: '' }) const [productlist, setProductlist] = useState([]) const [length, setLength] = useState(0) const [currentPage, setCurrentPage] = useState(1) const [error, setError] = useState('') const searchref = useRef(null) const per = 9; useEffect(() => { getProductlist() }, []) useEffect(() => { if (search.word == '') { getProductlist() } else { handleSearch() } }, [currentPage]) async function getProductlist() { try { setError('') setSearch({ word: '' }) const response = await axios.get(`/api/product/getproduct/all?page=${currentPage}`) setProductlist(response.data.productPiece) setLength(response.data.length) } catch (error) { catchError(error, setError) } } async function handleSearch() { try { setError('') const response = await axios.get(`/api/product/getproduct/all?product=${search.word}&page=${currentPage}`) setProductlist(response.data.productPiece) setLength(response.data.length) } catch (error) { catchError(error, setError) } } function handleChange(event) { setSearch({ word: event.target.value }) } async function handleSubmit(e) { e.preventDefault() try { setError('') if (currentPage != 1) { setCurrentPage(1) } const response = await axios.get(`/api/product/getproduct/all?product=${search.word}&page=${currentPage}`) setProductlist(response.data.productPiece) setLength(response.data.length) } catch (error) { catchError(error, setError) } finally { searchref.current.value = '' } } if (error) { alert(`${error}`) setError('') searchref.current.value = '' } return ( {productlist.map(pro => ( ))} ) } export default Admin