From 5ef61bcef404b5dd6117c06c9982e80d9e58b520 Mon Sep 17 00:00:00 2001 From: itoshi Date: Thu, 29 May 2025 16:43:25 +0300 Subject: [PATCH] fix: From device to vehicle table --- src/widgets/DevicesTable/index.tsx | 179 +++++++++++++++++++---------- 1 file changed, 120 insertions(+), 59 deletions(-) diff --git a/src/widgets/DevicesTable/index.tsx b/src/widgets/DevicesTable/index.tsx index 386b513..bbc40c9 100644 --- a/src/widgets/DevicesTable/index.tsx +++ b/src/widgets/DevicesTable/index.tsx @@ -15,7 +15,7 @@ import { } from "@shared"; import { useEffect, useState } from "react"; import { observer } from "mobx-react-lite"; -import { Button, Checkbox } from "@mui/material"; +import { Button, Checkbox, Typography } from "@mui/material"; // Import Typography for the modal message const formatDate = (dateString: string | undefined) => { if (!dateString) return "Нет данных"; @@ -48,21 +48,16 @@ function createData( return { uuid, online, lastUpdate, gps, media, connection }; } +// Keep the rows function as you provided it, without additional filters const rows = (devices: any[], vehicles: any[]) => { - return devices.map((device) => { - const { device_status } = vehicles.find( - (v) => v?.device_status?.device_uuid === device - ); - const findVehicle = vehicles.find((v) => v?.vehicle?.uuid === device); - - console.log(findVehicle); + return vehicles.map((vehicle) => { return createData( - findVehicle?.vehicle?.tail_number ?? "1243000", - device_status?.online, - device_status?.last_update, - device_status?.gps_ok, - device_status?.media_service_ok, - device_status?.is_connected + vehicle?.vehicle?.tail_number ?? "1243000", // Using tail_number as UUID, as in your original code + vehicle?.device_status?.online ?? false, + vehicle?.device_status?.last_update, + vehicle?.device_status?.gps_ok, + vehicle?.device_status?.media_service_ok, + vehicle?.device_status?.is_connected ); }); }; @@ -71,14 +66,18 @@ export const DevicesTable = observer(() => { const { devices, getDevices, - uuid, - setSelectedDevice, + // uuid, // This 'uuid' from devicesStore refers to a *single* selected device, not for batch actions. + setSelectedDevice, // Useful for individual device actions like 'Reload Status' sendSnapshotModalOpen, toggleSendSnapshotModal, } = devicesStore; const { snapshots, getSnapshots } = snapshotStore; const { vehicles, getVehicles } = vehicleStore; const [selectedDevices, setSelectedDevices] = useState([]); + + // Get the current list of rows displayed in the table + const currentRows = rows(devices, vehicles); + useEffect(() => { const fetchData = async () => { await getVehicles(); @@ -88,55 +87,98 @@ export const DevicesTable = observer(() => { fetchData(); }, []); - const handleSendSnapshot = (uuid: string) => { - setSelectedDevice(uuid); - toggleSendSnapshotModal(); - }; + // Determine if all visible devices are selected + const isAllSelected = + currentRows.length > 0 && selectedDevices.length === currentRows.length; - const handleReloadStatus = async (uuid: string) => { - setSelectedDevice(uuid); - await authInstance.post(`/devices/${uuid}/request-status`); - await getDevices(); + const handleSelectAllDevices = () => { + if (isAllSelected) { + // If all are currently selected, deselect all + setSelectedDevices([]); + } else { + // Otherwise, select all device UUIDs from the current rows + setSelectedDevices(currentRows.map((row) => row.uuid)); + } }; const handleSelectDevice = (event: React.ChangeEvent) => { + const deviceUuid = event.target.value; if (event.target.checked) { - setSelectedDevices([...selectedDevices, event.target.value]); + setSelectedDevices((prevSelected) => [...prevSelected, deviceUuid]); } else { - setSelectedDevices( - selectedDevices.filter((device) => device !== event.target.value) + setSelectedDevices((prevSelected) => + prevSelected.filter((uuid) => uuid !== deviceUuid) ); } }; - const handleSendSnapshotAction = async (uuid: string, snapshotId: string) => { - await authInstance.post(`/devices/${uuid}/force-snapshot`, { - snapshot_id: snapshotId, - }); - await getDevices(); + // This function now opens the modal for selected devices + const handleOpenSendSnapshotModal = () => { + if (selectedDevices.length > 0) { + toggleSendSnapshotModal(); + } + }; + + const handleReloadStatus = async (uuid: string) => { + setSelectedDevice(uuid); // Set the active device in store for context if needed + await authInstance.post(`/devices/${uuid}/request-status`); + await getDevices(); // Refresh devices after status request + }; + + // This function now handles sending snapshots to ALL selected devices + const handleSendSnapshotAction = async (snapshotId: string) => { + try { + for (const deviceUuid of selectedDevices) { + console.log(`Sending snapshot ${snapshotId} to device ${deviceUuid}`); + // Ensure you are using the correct API endpoint for force-snapshot + await authInstance.post(`/devices/${deviceUuid}/force-snapshot`, { + snapshot_id: snapshotId, + }); + } + // After all requests are sent + await getDevices(); // Refresh the device list to show updated status + setSelectedDevices([]); // Clear the selection + toggleSendSnapshotModal(); // Close the modal + } catch (error) { + console.error("Error sending snapshots:", error); + // You might want to show an error notification to the user here + } }; return ( <> -
-
- + + {" "} + {/* Added padding="checkbox" */} + + Бортовой номер Онлайн Последнее обновление @@ -147,56 +189,58 @@ export const DevicesTable = observer(() => { - {rows(devices, vehicles).map((row) => ( + {currentRows.map((row) => ( - + + {" "} + {/* Added padding="checkbox" */} - {row?.uuid} + {row.uuid} - {row?.online ? ( + {row.online ? ( ) : ( )} - {formatDate(row?.lastUpdate)} + {formatDate(row.lastUpdate)} - {row?.gps ? ( + {row.gps ? ( ) : ( )} - {row?.media ? ( + {row.media ? ( ) : ( )} - {row?.connection ? ( + {row.connection ? ( ) : ( )} - - @@ -206,18 +250,35 @@ export const DevicesTable = observer(() => {
-

Выбрать снапшот

+ + Выбрать снапшот для{" "} + {selectedDevices.length}{" "} + устройств +
- {snapshots && + {snapshots && snapshots.length > 0 ? ( snapshots.map((snapshot) => ( - - ))} + + )) + ) : ( + + Нет доступных снапшотов. + + )}