import { DataGrid, GridColDef, GridRenderCellParams } from "@mui/x-data-grid"; import { ruRU } from "@mui/x-data-grid/locales"; import { countryStore, languageStore } from "@shared"; import { useEffect, useState } 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; 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 ) : ( )}
); }, }, { field: "actions", headerName: "Действия", align: "center", headerAlign: "center", width: 200, sortable: false, renderCell: (params: GridRenderCellParams) => { return (
{/* */} {/* */}
); }, }, ]; const rows = countries[language]?.data.map((country) => ({ id: country.code, code: country.code, name: country.name, })); return ( <>

Страны

{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([]); } }} 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); }} /> ); });