import React, { useState } from "react"; import GlobalModel from "../../common/GlobalModel"; import ManageBranch from "./ManageBranch"; import { useBranches, useDeleteBranch } from "../../../hooks/useServiceProject"; import { ITEMS_PER_PAGE } from "../../../utils/constants"; import { useDebounce } from "../../../utils/appUtils"; import { useParams } from "react-router-dom"; import Pagination from "../../common/Pagination"; import ConfirmModal from "../../common/ConfirmModal"; import { SpinnerLoader } from "../../common/Loader"; const ServiceBranch = () => { const { projectId } = useParams(); const [IsDeleteModalOpen, setIsDeleteModalOpen] = useState(false); const [showInactive, setShowInactive] = useState(false); const [manageState, setManageState] = useState({ IsOpen: false, branchId: null, }); const { mutate: DeleteBranch, isPending } = useDeleteBranch(); const [deletingId, setDeletingId] = useState(null); const [search, setSearch] = useState(""); const [currentPage, setCurrentPage] = useState(1); const debouncedSearch = useDebounce(search, 500); const { data, isLoading, isError, error } = useBranches( projectId, !showInactive, ITEMS_PER_PAGE - 12, currentPage, debouncedSearch ); const paginate = (page) => { if (page >= 1 && page <= (data?.totalPages ?? 1)) { setCurrentPage(page); } }; const columns = [ { key: "branchName", label: "Name", align: "text-start", getValue: (e) => e?.branchName || "N/A", }, ]; const handleDelete = (id) => { setDeletingId(id); DeleteBranch( { id, isActive: showInactive }, { onSettled: () => { setDeletingId(null); setIsDeleteModalOpen(false); }, } ); }; return ( <> {IsDeleteModalOpen && ( setIsDeleteModalOpen(false)} loading={isPending} paramData={deletingId} /> )}
{/* Header Section */}
Branchs
{/* Flex container for toggle + button */}
setShowInactive(!showInactive)} />
{columns.map((col) => ( ))} {isLoading && ( )} {isError && ( )} {!isLoading && !isError && data?.data?.length > 0 && data.data.map((branch) => ( {columns.map((col) => ( ))} ))} {!isLoading && !isError && (!data?.data || data.data.length === 0) && ( )}
{col.label} Action
{error?.message || "Error loading branches"}
{col.getValue(branch)}
    {!showInactive ? ( <>
  • setManageState({ IsOpen: true, branchId: branch.id, }) } > Modify
  • { setIsDeleteModalOpen(true); setDeletingId(branch.id); }} > Delete
  • ) : (
  • { setIsDeleteModalOpen(true); setDeletingId(branch.id); }} > Restore
  • )}
No Branch Found
{data?.data?.length > 0 && ( )}
{manageState.IsOpen && ( setManageState({ IsOpen: false, branchId: null }) } > setManageState({ IsOpen: false, branchId: null }) } /> )}
); }; export default ServiceBranch;