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 INIT_STATUS = { indexOfFirst: 0, indexOfLast: 10 } const [search, setSearch] = useState({ word: '' }) const [productlist, setProductlist] = useState([]) const [status, setStatus] = useState(INIT_STATUS) const [currentPage, setCurrentPage] = useState(1) const [error, setError] = useState('') const searchref = useRef(null) const per = 10; useEffect(() => { getProductlist() }, []) useEffect(() => { setStatus({ indexOfFirst: (currentPage - 1) * per, indexOfLast: currentPage * per }) }, [currentPage]) function currentPosts(items) { let currentPosts = 0; currentPosts = items.slice(status.indexOfFirst, status.indexOfLast); return currentPosts } async function getProductlist() { try { setError('') const response = await axios.get(`/api/product/getproduct/all`) setProductlist(response.data) setCurrentPage(1) } catch (error) { catchError(error, setError) } } function handleChange(event) { setSearch({ word: event.target.value }) } async function handleSubmit(e) { e.preventDefault() try { setError('') const response = await axios.get(`/api/product/getproduct/all?product=${search.word}`) setProductlist(response.data) setCurrentPage(1) } catch (error) { catchError(error, setError) } finally { searchref.current.value = '' } } if (error) { alert(`${error}`) setError('') searchref.current.value = '' } return ( {currentPosts(productlist).map(pro => ( ))} ) } export default Admin