import Table from "@mui/material/Table"; import TableBody from "@mui/material/TableBody"; import TableCell from "@mui/material/TableCell"; import TableContainer from "@mui/material/TableContainer"; import TableHead from "@mui/material/TableHead"; import TableRow from "@mui/material/TableRow"; import Paper from "@mui/material/Paper"; import { authInstance, cityStore, languageStore, sightsStore } from "@shared"; import { useEffect, useState } from "react"; import { observer } from "mobx-react-lite"; import { Button } from "@mui/material"; import { DeleteModal, LanguageSwitcher } from "@widgets"; import { Pencil, Trash2 } from "lucide-react"; import { useNavigate } from "react-router-dom"; function createData(id: number, name: string, city: string) { return { id, name, city }; } const rows = (sights: any[], cities: any[]) => { return sights.map((sight) => { const city = cities.find((city) => city?.id === sight?.city_id); return createData(sight?.id, sight?.name, city?.name ?? "Нет данных"); }); }; export const SightsTable = observer(() => { const navigate = useNavigate(); const { language } = languageStore; const { sights, getSights } = sightsStore; const { cities, getCities } = cityStore; const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false); const [rowId, setRowId] = useState(null); useEffect(() => { const fetchData = async () => { await getSights(); await getCities(language); }; fetchData(); }, [language, getSights, getCities]); const handleDelete = async (id: number) => { await authInstance.delete(`/sight/${id}`); await getSights(); }; return ( <>
Название Город Действия {rows(sights, cities[language]?.data ?? [])?.map((row) => ( {row?.name} {row?.city}
))}
{ if (rowId) { await handleDelete(rowId); } setIsDeleteModalOpen(false); setRowId(null); }} onCancel={() => setIsDeleteModalOpen(false)} /> ); });