import React, { useRef, useState } from "react"; import { Box, Text, Tooltip, HStack, Input, Select, Image, Menu, MenuButton, MenuList, MenuItem, Switch, Portal, useToast, } from "@chakra-ui/react"; import { Link as RouterLink } from "react-router-dom"; import { HiDotsVertical } from "react-icons/hi"; import { formatDate } from "../../Components/Functions/UTCConvertor"; import CustomAlertDialog from "../../Components/CustomAlertDialog"; import DataTable from "../DataTable/DataTable"; import Header from "../Header"; import { OPACITY_ON_LOAD } from "../../Layout/animations"; import { IMAGE_URI, TABLE_PAGINATION } from "../../Constants/Paginations"; import { CheckCircleIcon, ChevronLeftIcon, ChevronRightIcon, WarningIcon, } from "@chakra-ui/icons"; import ToastBox from "../ToastBox"; const API_URL = import.meta.env.VITE_API_BASE_URL; const BannerCommunity = ({ dataArray, deleteApi, statusUpdateApi, title, addLink, viewLink, editLink, }) => { // ====================================================[Hooks]=================================================================== const toast = useToast(); const [deleteAlert, setDeleteAlert] = useState(false); const [actionId, setActionId] = useState(null); const [actionStatus, setActionStatus] = useState(null); const [deleteIsLoading, setDeleteIsLoading] = useState(false); const [searchTerm, setSearchTerm] = useState(""); const [statusFilter, setStatusFilter] = useState("all"); // const [pageSize, setPageSize] = useState(TABLE_PAGINATION?.size); // const [currentPage, setCurrentPage] = useState(1); // const [displayRange, setDisplayRange] = useState({ // start: TABLE_PAGINATION?.page, // end: pageSize, // }); // ====================================================[Functions]=================================================================== const handleDelete = async (bannerId, status) => { if (status) { return toast({ render: () => ( ), }); } try { // Trigger the mutation setDeleteIsLoading(true); await deleteApi(bannerId) .then((response) => { // Handle the response here if (response?.data?.statusCode === 200) { setDeleteIsLoading(false); setDeleteAlert(false); toast({ render: () => ( ), }); } }) .catch((error) => { // // console.error("Error creating community:", error); setDeleteIsLoading(false); setDeleteAlert(false); }); } catch (error) { // Handle errors // // console.error("Error deleting community:", error); } }; const handleUpdateStatus = async (id, status) => { if (status) { return toast({ render: () => ( ), }); } else { try { // Trigger the mutation await statusUpdateApi({ id }) .then((response) => { if (response?.data?.statusCode === 201) { toast({ render: () => , }); } }) .catch((error) => { }); } catch (error) { // Handle errors // // console.error("Error updating community status:", error); } } }; // ====================================================[Table Filter]================================================================ const filteredData = dataArray?.data?.data?.rows?.filter((item) => { // Filter by name (case insensitive) const name = item.Heading; const searchLower = searchTerm.toLowerCase(); const nameMatches = name.toLowerCase().includes(searchLower); const status = item.status; const statusMatches = statusFilter === "all" || (statusFilter === "active" && status === true) || (statusFilter === "inactive" && status === false); return nameMatches && statusMatches; }); // ====================================================[Table Setup]================================================================ const tableHeadRow = [ "Banner image", "Heading", "Sub heading", "Button title", "Active", "Created At", ]; const extractedArray = filteredData?.map((item, index) => { return { "Banner image": ( Dan Abramov ), Heading: ( {item?.Heading} ), "Sub heading": ( {item?.sub_heading} ), "Button title": {item?.CTO_button_title}, Active: ( handleUpdateStatus(item.id, item?.status)} isChecked={item.status} // disabled={item.status} /> ), "Created At": ( {formatDate(item?.createdAt)} Edit View { setActionId(item.id); setDeleteAlert(true); setActionStatus(item.status); }} className="web-text-medium" > Delete ), }; }); // ====================================================[Pagination Setup]================================================================ const paginationPrev = () => { if (currentPage > 1) { setCurrentPage(currentPage - 1); updateDisplayRange(currentPage - 1); } }; const paginationNext = () => { const totalPages = Math.ceil(community?.data?.data?.totalItems / pageSize); if (currentPage < totalPages) { setCurrentPage(currentPage + 1); updateDisplayRange(currentPage + 1); } }; const updateDisplayRange = (page) => { const start = (page - 1) * pageSize + 1; const end = Math.min( start + pageSize - 1, community?.data?.data?.totalItems ); setDisplayRange({ start, end }); }; return (
{/* ====================================================[ Top bar ]================================================================ */} setSearchTerm(e.target.value)} /> {/* */} {/* {displayRange.start} - {displayRange.end} of{" "} {dataArray?.data?.data?.totalItems} */} {/* ====================================================[ Table ]================================================================ */} {/* ====================================================[ Alert ]================================================================ */} setDeleteAlert(false)} isOpen={deleteAlert} alertHandler={() => handleDelete(actionId, actionStatus)} message={"Are you sure you want to delete member?"} isLoading={deleteIsLoading} /> ); }; export default BannerCommunity;