From c9eb7b0908b0640ded182b89a1afc41212cc5a1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B2=D0=BE=D0=B2=20=D0=90=D1=80=D1=82?= =?UTF-8?q?=D0=B5=D0=BC?= Date: Mon, 6 Apr 2026 02:46:06 +0300 Subject: [PATCH] Refactor EmbeddedActiveEquipmentPage to use custom data fetching and remove Authenticated wrapper from App component --- client/src/App.tsx | 17 ++-- .../src/pages/EmbeddedActiveEquipmentPage.tsx | 78 +++++++++++++++++-- 2 files changed, 77 insertions(+), 18 deletions(-) diff --git a/client/src/App.tsx b/client/src/App.tsx index d5427c9..012f516 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -1,5 +1,4 @@ -import { Admin, Authenticated, CustomRoutes, Resource } from 'react-admin'; -import { Route } from 'react-router-dom'; +import { Admin, Resource } from 'react-admin'; import { authProvider } from './auth/authProvider'; import { dataProvider } from './dataProvider'; import { EmbeddedActiveEquipmentPage } from './pages/EmbeddedActiveEquipmentPage'; @@ -13,18 +12,12 @@ import { EquipmentStatusChangeList } from './resources/equipment-status-change/E import { EquipmentStatusChangeShow } from './resources/equipment-status-change/EquipmentStatusChangeShow'; function App() { + if (window.location.pathname === '/embedded/equipment-active') { + return ; + } + return ( - - - - - } - /> - ('equipment', { - pagination: { page: 1, perPage: 100 }, - sort: { field: 'name', order: 'ASC' }, - filter: { status: ['Active'] }, - }); + const [data, setData] = useState([]); + const [total, setTotal] = useState(null); + const [isPending, setIsPending] = useState(true); + const [error, setError] = useState(null); + + useEffect(() => { + let cancelled = false; + + const load = async () => { + setIsPending(true); + setError(null); + + try { + await ensureFreshToken(); + + const token = getAccessToken(); + const headers = new Headers({ + Accept: 'application/json', + }); + + if (token) { + headers.set('Authorization', `Bearer ${token}`); + } + + const query = new URLSearchParams({ + _start: '0', + _end: '100', + _sort: 'name', + _order: 'ASC', + }); + query.append('status', 'Active'); + + const response = await fetch(`${env.apiUrl}/equipment?${query.toString()}`, { + headers, + }); + + if (!response.ok) { + const requestError = new Error(`Request failed with status ${response.status}`) as Error & { + status?: number; + }; + requestError.status = response.status; + throw requestError; + } + + const payload = (await response.json()) as EquipmentRecord[]; + const contentRange = response.headers.get('Content-Range'); + const parsedTotal = contentRange?.match(/\/(\d+)$/)?.[1]; + + if (!cancelled) { + setData(payload); + setTotal(parsedTotal ? Number(parsedTotal) : payload.length); + } + } catch (caughtError) { + if (!cancelled) { + setError(caughtError instanceof Error ? caughtError : new Error('Unknown error')); + } + } finally { + if (!cancelled) { + setIsPending(false); + } + } + }; + + void load(); + + return () => { + cancelled = true; + }; + }, []); return (