import React, { useEffect } from 'react' import Button from '@material-ui/core/Button' import { useState } from 'react' import { Link } from 'react-router-dom' import axios from 'axios' import swal from 'sweetalert' import OverLayButton from './OverLayButton.js' import { isAutheticated } from 'src/auth.js' const Franchisees = () => { const token = isAutheticated() const [loading, setLoading] = useState(true) const [success, setSuccess] = useState(true) const [FranchiseesData, setFranchiseesData] = useState([]) const [currentPage, setCurrentPage] = useState(1) const [itemPerPage, setItemPerPage] = useState(10) const [showData, setShowData] = useState(FranchiseesData) const handleShowEntries = (e) => { setCurrentPage(1) setItemPerPage(e.target.value) } const getCategories = () => { axios .get(`/api/franchisee`, { headers: { 'Access-Control-Allow-Origin': '*', Authorization: `Bearer ${token}` }, }) .then((res) => { setFranchiseesData(res.data.data) setLoading(false) }) .catch((err) => { console.log(err) setLoading(false) }) } useEffect(() => { getCategories() }, [success]) useEffect(() => { const loadData = () => { const indexOfLastPost = currentPage * itemPerPage const indexOfFirstPost = indexOfLastPost - itemPerPage setShowData(FranchiseesData.slice(indexOfFirstPost, indexOfLastPost)) } loadData() }, [currentPage, itemPerPage, FranchiseesData]) 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/franchisee/${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 (
Franchisees
{!loading && showData.length === 0 && ( )} {loading ? ( ) : ( showData.map((franchisee, i) => { return ( ) }) )}
Franchisee Name Logo City Created On Actions
No Data Available
Loading...
{franchisee.name} Test Image {franchisee?.city?.city_name} {new Date(franchisee.createdAt).toLocaleString('en-IN', { month: '2-digit', day: 'numeric', year: 'numeric', // hour: 'numeric', // minute: 'numeric', // hour12: true, })}
Showing {currentPage * itemPerPage - itemPerPage + 1} to{' '} {Math.min(currentPage * itemPerPage, FranchiseesData.length)} of{' '} {FranchiseesData.length} entries
  • setCurrentPage((prev) => prev - 1)} > Previous
  • {!(currentPage - 1 < 1) && (
  • setCurrentPage((prev) => prev - 1)} > {currentPage - 1}
  • )}
  • {currentPage}
  • {!( (currentPage + 1) * itemPerPage - itemPerPage > FranchiseesData.length - 1 ) && (
  • { setCurrentPage((prev) => prev + 1) }} > {currentPage + 1}
  • )}
  • FranchiseesData.length - 1 ) ? 'paginate_button page-item next' : 'paginate_button page-item next disabled' } > setCurrentPage((prev) => prev + 1)} > Next
) } export default Franchisees