From 11b28f583d295398a804efd9e4e223a1e14d2b4b Mon Sep 17 00:00:00 2001 From: Knut Anderssen Date: Wed, 18 Sep 2024 09:40:18 +0100 Subject: [PATCH] Try to simplify zfcp and dasd changes queries --- web/src/components/storage/dasd/DASDPage.tsx | 5 +- .../components/storage/zfcp/ZFCPDiskForm.tsx | 3 +- web/src/queries/storage/zfcp.ts | 118 +++++++----------- 3 files changed, 47 insertions(+), 79 deletions(-) diff --git a/web/src/components/storage/dasd/DASDPage.tsx b/web/src/components/storage/dasd/DASDPage.tsx index 1e96960c5..8488ebb27 100644 --- a/web/src/components/storage/dasd/DASDPage.tsx +++ b/web/src/components/storage/dasd/DASDPage.tsx @@ -39,7 +39,8 @@ export default function DASDPage() { - + {/** TRANSLATORS: DASD devices selection table */} + @@ -52,6 +53,6 @@ export default function DASDPage() { {_("Back to device selection")} - + ); } diff --git a/web/src/components/storage/zfcp/ZFCPDiskForm.tsx b/web/src/components/storage/zfcp/ZFCPDiskForm.tsx index c503af01b..a738ff9b7 100644 --- a/web/src/components/storage/zfcp/ZFCPDiskForm.tsx +++ b/web/src/components/storage/zfcp/ZFCPDiskForm.tsx @@ -107,7 +107,8 @@ export default function ZFCPDiskForm({ if (!formData.channel && getChannels().length > 0) select(); return ( - + // TRANSLATORS: zFCP disk activation form + {isFailed && (

{_("The zFCP disk was not activated.")}

diff --git a/web/src/queries/storage/zfcp.ts b/web/src/queries/storage/zfcp.ts index 04818fb28..d2e1a1cd8 100644 --- a/web/src/queries/storage/zfcp.ts +++ b/web/src/queries/storage/zfcp.ts @@ -93,51 +93,32 @@ const useZFCPControllersChanges = () => { React.useEffect(() => { if (!client) return; - return client.ws().onEvent((event) => { - switch (event.type) { - case "ZFCPControllerAdded": { - const device: ZFCPController = event.device; - queryClient.setQueryData( - zfcpControllersQuery.queryKey, - (prev: ZFCPController[] | undefined) => { - if (prev === undefined) return; - return [...prev, device]; - }, - ); - break; - } - case "ZFCPControllerRemoved": { - const device: ZFCPController = event.device; - const { id } = device; - queryClient.setQueryData( - zfcpControllersQuery.queryKey, - (prev: ZFCPController[] | undefined) => { - if (prev === undefined) return; - const res = prev.filter((dev) => dev.id !== id); - return res; - }, - ); - break; - } - case "ZFCPControllerChanged": { - const device: ZFCPController = event.device; - const { id } = device; - queryClient.setQueryData( - zfcpControllersQuery.queryKey, - (prev: ZFCPController[] | undefined) => { - if (prev === undefined) return; - // deep copy of original to have it immutable - const res = [...prev]; - const index = res.findIndex((dev) => dev.id === id); - res[index] = device; - return res; - }, - ); - break; - } + return client.ws().onEvent(({ type, device }) => { + if ( + !["ZFCPControllerAdded", "ZFCPControllerChanged", "ZFCPControllerRemoved"].includes(type) + ) { + return; } + queryClient.setQueryData( + zfcpControllersQuery.queryKey, + (prev: ZFCPController[] | undefined) => { + if (prev === undefined) return; + + switch (type) { + case "ZFCPControllerAdded": { + return [...prev, device]; + } + case "ZFCPControllerRemoved": { + return prev.filter((dev) => dev.id !== device.id); + } + case "ZFCPControllerChanged": { + return prev.map((d) => (d.id === device.id ? device : d)); + } + } + }, + ); }); - }); + }, [client, queryClient]); }; /** @@ -150,42 +131,27 @@ const useZFCPDisksChanges = () => { React.useEffect(() => { if (!client) return; - return client.ws().onEvent((event) => { - switch (event.type) { - case "ZFCPDiskAdded": { - const device: ZFCPDisk = event.device; - queryClient.setQueryData(zfcpDisksQuery.queryKey, (prev: ZFCPDisk[]) => { - if (prev === undefined) return; + return client.ws().onEvent(({ type, device }) => { + if (!["ZFCPDiskAdded", "ZFCPDiskChanged", "ZFCPDiskRemoved"].includes(type)) { + return; + } + queryClient.setQueryData(zfcpDisksQuery.queryKey, (prev: ZFCPDisk[] | undefined) => { + if (prev === undefined) return; + + switch (type) { + case "ZFCPDiskAdded": { return [...prev, device]; - }); - break; - } - case "ZFCPDiskRemoved": { - const device: ZFCPDisk = event.device; - const { name } = device; - queryClient.setQueryData(zfcpDisksQuery.queryKey, (prev: ZFCPDisk[]) => { - if (prev === undefined) return; - const res = prev.filter((dev) => dev.name !== name); - return res; - }); - break; + } + case "ZFCPDiskRemoved": { + return prev.filter((dev) => dev.name !== device.name); + } + case "ZFCPDiskChanged": { + return prev.map((d) => (d.name === device.name ? device : d)); + } } - case "ZFCPDiskChanged": { - const device: ZFCPDisk = event.device; - const { name } = device; - queryClient.setQueryData(zfcpDisksQuery.queryKey, (prev: ZFCPDisk[]) => { - if (prev === undefined) return; - // deep copy of original to have it immutable - const res = [...prev]; - const index = res.findIndex((dev) => dev.name === name); - res[index] = device; - return res; - }); - break; - } - } + }); }); - }); + }, [client, queryClient]); }; export {