import { DataGrid, GridColDef, GridRenderCellParams } from "@mui/x-data-grid"; import { ruRU } from "@mui/x-data-grid/locales"; import { authStore, countryStore, languageStore, SearchInput } from "@shared"; import { useEffect, useState, useMemo } from "react"; import { observer } from "mobx-react-lite"; import { Trash2, Minus } from "lucide-react"; import { CreateButton, DeleteModal, LanguageSwitcher } from "@widgets"; import { Box, CircularProgress } from "@mui/material"; export const CountryListPage = observer(() => { const { countries, getCountries, deleteCountry } = countryStore; const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false); const [isBulkDeleteModalOpen, setIsBulkDeleteModalOpen] = useState(false); const [rowId, setRowId] = useState(null); const [ids, setIds] = useState([]); const [isLoading, setIsLoading] = useState(false); const [paginationModel, setPaginationModel] = useState({ page: 0, pageSize: 50, }); const { language } = languageStore; const canWriteCountries = authStore.canWrite("countries"); const [searchQuery, setSearchQuery] = useState(""); useEffect(() => { const fetchCountries = async () => { setIsLoading(true); await getCountries(language); setIsLoading(false); }; fetchCountries(); }, [language]); const columns: GridColDef[] = [ { field: "name", headerName: "Название", flex: 1, renderCell: (params: GridRenderCellParams) => { return (
{params.value ? ( params.value ) : ( )}
); }, }, ...(authStore.canWrite("countries") ? [{ field: "actions", headerName: "Действия", align: "center" as const, headerAlign: "center" as const, width: 200, sortable: false, renderCell: (params: GridRenderCellParams) => (
), }] : []), ]; const rows = useMemo(() => { const query = searchQuery.trim().toLowerCase(); return (countries[language]?.data ?? []) .filter((country) => !query || (country.name ?? "").toLowerCase().includes(query)) .map((country) => ({ id: country.code, code: country.code, name: country.name, })); }, [countries[language]?.data, searchQuery]); return ( <>

Страны

{canWriteCountries && ( )}
{canWriteCountries && ids.length > 0 && (
)} { if (Array.isArray(newSelection)) { const selectedIds = newSelection.map( (id: string | number) => Number(id) ); setIds(selectedIds); } else if ( newSelection && typeof newSelection === "object" && "ids" in newSelection ) { const idsSet = newSelection.ids as Set; const selectedIds = Array.from(idsSet).map( (id: string | number) => Number(id) ); setIds(selectedIds); } else { setIds([]); } } : undefined } slots={{ noRowsOverlay: () => ( {isLoading ? : "Нет стран"} ), }} />
{ if (!rowId) return; await deleteCountry(rowId); setRowId(null); setIsDeleteModalOpen(false); }} onCancel={() => { setRowId(null); setIsDeleteModalOpen(false); }} /> { await Promise.all(ids.map((id) => deleteCountry(id.toString()))); getCountries(language); setIsBulkDeleteModalOpen(false); setIds([]); }} onCancel={() => { setIsBulkDeleteModalOpen(false); }} /> ); });