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 Testimonials = () => { const token = isAutheticated(); const navigate = useNavigate(); const [loading, setLoading] = useState(true); const [success, setSuccess] = useState(true); const [TestimonialsData, setTestimonialsData] = useState([]); const [currentPage, setCurrentPage] = useState(1); const [itemPerPage, setItemPerPage] = useState(10); const [showData, setShowData] = useState(TestimonialsData); const handleShowEntries = (e) => { setCurrentPage(1); setItemPerPage(e.target.value); }; const getTestimonialsData = async () => { axios .get(`/api/testimonial/getAll/`, { headers: { Authorization: `Bearer ${token}`, }, }) .then((res) => { setTestimonialsData(res.data?.testimonial); setLoading(false); }) .catch((err) => { setLoading(false); }); }; useEffect(() => { getTestimonialsData(); }, [success]); useEffect(() => { const loadData = () => { const indexOfLastPost = currentPage * itemPerPage; const indexOfFirstPost = indexOfLastPost - itemPerPage; setShowData(TestimonialsData.slice(indexOfFirstPost, indexOfLastPost)); }; loadData(); }, [currentPage, itemPerPage, TestimonialsData]); 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/item/delete/${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 (
Testimonials
{!loading && showData.length === 0 && ( )} {loading ? ( ) : ( showData.map((item, i) => { return ( ); }) )}
Name Testimonials Date & Time Action
No Data Available
Loading...
{item.name} {item.company} {new Date(item.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, TestimonialsData.length )}{" "} of {TestimonialsData.length} entries
  • setCurrentPage((prev) => prev - 1)} > Previous
  • {!(currentPage - 1 < 1) && (
  • setCurrentPage((prev) => prev - 1) } > {currentPage - 1}
  • )}
  • {currentPage}
  • {!( (currentPage + 1) * itemPerPage - itemPerPage > TestimonialsData.length - 1 ) && (
  • { setCurrentPage((prev) => prev + 1); }} > {currentPage + 1}
  • )}
  • TestimonialsData.length - 1 ) ? "paginate_button page-item next" : "paginate_button page-item next disabled" } > setCurrentPage((prev) => prev + 1)} > Next
); }; export default Testimonials;