Skip to content

Commit

Permalink
Try to simplify zfcp and dasd changes queries
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator committed Sep 18, 2024
1 parent d5720ec commit 7af24f8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 78 deletions.
3 changes: 2 additions & 1 deletion web/src/components/storage/dasd/DASDPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ export default function DASDPage() {
</Page.Header>

<Page.Content>
<Page.Section aria-label="dasd-devices">
{/** TRANSLATORS: DASD devices selection table */}
<Page.Section aria-label={_("DASD devices selection table")}>
<Stack>
<DASDTable />
</Stack>
Expand Down
3 changes: 2 additions & 1 deletion web/src/components/storage/zfcp/ZFCPDiskForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ export default function ZFCPDiskForm({
if (!formData.channel && getChannels().length > 0) select();

return (
<Page.Section aria-label="zfcp-activation-form">
// TRANSLATORS: zFCP disk activation form
<Page.Section aria-label={_("zFCP Disk activation form")}>
{isFailed && (
<Alert variant="warning" isInline title={_("Something went wrong")}>
<p>{_("The zFCP disk was not activated.")}</p>
Expand Down
118 changes: 42 additions & 76 deletions web/src/queries/storage/zfcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
};

/**
Expand All @@ -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 {
Expand Down

0 comments on commit 7af24f8

Please sign in to comment.