Skip to content
Snippets Groups Projects
Commit 58667462 authored by Max Frederiksen's avatar Max Frederiksen Committed by Max Frederiksen
Browse files

Host components

parent e6dfcc96
No related branches found
No related tags found
2 merge requests!612Release 5.0.0,!564CE-2550: Remove query params for pagination, tabs and accordions
import { useState, useEffect, useCallback, useContext, useMemo } from "react"; import { useState, useEffect, useCallback, useContext } from "react";
import { Container, Grid, Tabs, Tab } from "@mui/material"; import { Container, Grid, Tabs, Tab } from "@mui/material";
import { HostTable } from "../../components/host/HostTable"; import { HostTable } from "../../components/host/HostTable";
import { import {
...@@ -37,47 +37,14 @@ export function HostListView() { ...@@ -37,47 +37,14 @@ export function HostListView() {
const [urlState, setUrlState] = useUrlState( const [urlState, setUrlState] = useUrlState(
{ {
tab: "0",
rows: "20",
page: "0",
query: "" query: ""
}, },
{ navigateMode: "replace" } { navigateMode: "replace" }
); );
const [tabIndex, setTabIndex] = useState(0);
const [hostFilter, setHostFilter] = useState("ALL"); const [hostFilter, setHostFilter] = useState("ALL");
const urlPagination = useMemo( const handleTabChange = (tab) => {
() => ({
rows: deserialize(urlState.rows),
page: deserialize(urlState.page)
}),
[urlState.rows, urlState.page]
);
const setUrlPagination = useCallback(
({ rows, page }) => {
setUrlState({
rows: serialize(rows),
page: serialize(page)
});
},
[setUrlState]
);
const handleTabChange = useCallback(
(event, tab) => {
setUrlState((s) =>
serialize(s.tab) === serialize(tab)
? { tab: serialize(tab) }
: { tab: serialize(tab), page: "0" }
);
changeTab(tab);
},
[setUrlState]
);
const changeTab = (tab) => {
if (tab === 0) { if (tab === 0) {
setHostFilter("ALL"); setHostFilter("ALL");
} else if (tab === 1) { } else if (tab === 1) {
...@@ -85,20 +52,15 @@ export function HostListView() { ...@@ -85,20 +52,15 @@ export function HostListView() {
} else if (tab === 2) { } else if (tab === 2) {
setHostFilter("NO_IOCS"); setHostFilter("NO_IOCS");
} }
setTabIndex(tab);
}; };
useEffect(() => {
if (urlState.tab) {
changeTab(deserialize(urlState.tab));
}
}, [urlState]);
const rowsPerPage = [20, 50]; const rowsPerPage = [20, 50];
const { pagination, setPagination } = usePagination({ const { pagination, setPagination } = usePagination({
rowsPerPageOptions: rowsPerPage, rowsPerPageOptions: rowsPerPage,
initLimit: urlPagination.rows ?? rowsPerPage[0], initLimit: rowsPerPage[0],
initPage: urlPagination.page ?? 0 initPage: 0
}); });
// update pagination whenever search result total pages change // update pagination whenever search result total pages change
...@@ -106,17 +68,6 @@ export function HostListView() { ...@@ -106,17 +68,6 @@ export function HostListView() {
setPagination({ totalCount: hosts?.totalCount ?? 0 }); setPagination({ totalCount: hosts?.totalCount ?? 0 });
}, [setPagination, hosts?.totalCount]); }, [setPagination, hosts?.totalCount]);
// whenever url state changes, update pagination
useEffect(() => {
setPagination({ ...urlPagination });
}, [setPagination, urlPagination]);
// whenever table pagination internally changes (user clicks next page etc)
// update the row params
useEffect(() => {
setUrlPagination(pagination);
}, [pagination, setUrlPagination]);
// Request new search results whenever search or pagination changes // Request new search results whenever search or pagination changes
useEffect(() => { useEffect(() => {
let requestParams = initRequestParams(pagination); let requestParams = initRequestParams(pagination);
...@@ -174,8 +125,8 @@ export function HostListView() { ...@@ -174,8 +125,8 @@ export function HostListView() {
> >
<Grid item> <Grid item>
<Tabs <Tabs
value={deserialize(urlState.tab)} value={tabIndex}
onChange={handleTabChange} onChange={(_, tab) => handleTabChange(tab)}
> >
<Tab label="All" /> <Tab label="All" />
<Tab label="Only hosts with IOCs" /> <Tab label="Only hosts with IOCs" />
......
import { useEffect, useContext } from "react"; import { useEffect, useContext, useState } from "react";
import useUrlState from "@ahooksjs/use-url-state";
import { Box, IconButton, Typography, Stack } from "@mui/material"; import { Box, IconButton, Typography, Stack } from "@mui/material";
import ArrowBackIcon from "@mui/icons-material/ArrowBack"; import ArrowBackIcon from "@mui/icons-material/ArrowBack";
import { import {
...@@ -13,10 +12,6 @@ import { LokiPanel } from "../../../components/common/Loki/LokiPanel"; ...@@ -13,10 +12,6 @@ import { LokiPanel } from "../../../components/common/Loki/LokiPanel";
import { useNavigate } from "react-router-dom"; import { useNavigate } from "react-router-dom";
import { applicationTitle } from "../../../components/common/Helper"; import { applicationTitle } from "../../../components/common/Helper";
import AccessControl from "../../../components/auth/AccessControl"; import AccessControl from "../../../components/auth/AccessControl";
import {
serialize,
deserialize
} from "../../../components/common/URLState/URLState";
import { HostDetailsTable } from "./HostDetailsTable"; import { HostDetailsTable } from "./HostDetailsTable";
import { HostStatus } from "../../../components/host/HostStatus"; import { HostStatus } from "../../../components/host/HostStatus";
import { HostJobsSection } from "./HostJobsSection"; import { HostJobsSection } from "./HostJobsSection";
...@@ -25,6 +20,10 @@ import env from "../../../config/env"; ...@@ -25,6 +20,10 @@ import env from "../../../config/env";
export function HostDetailsView({ hostId, host, alert }) { export function HostDetailsView({ hostId, host, alert }) {
const { setTitle } = useContext(GlobalAppBarContext); const { setTitle } = useContext(GlobalAppBarContext);
const [accordionState, setAccordionState] = useState({
detailsOpen: false,
logStreamOpen: false
});
useEffect(() => { useEffect(() => {
if (host && host.name) { if (host && host.name) {
...@@ -32,18 +31,6 @@ export function HostDetailsView({ hostId, host, alert }) { ...@@ -32,18 +31,6 @@ export function HostDetailsView({ hostId, host, alert }) {
} }
}, [host, setTitle]); }, [host, setTitle]);
const [urlState, setUrlState] = useUrlState(
{
iocs_rows: 20,
iocs_page: 0,
details_open: false,
log_stream_open: false,
job_log_open: false,
job_log_rows: 20,
job_log_page: 0
},
{ navigateMode: "replace" }
);
const navigate = useNavigate(); const navigate = useNavigate();
return ( return (
...@@ -95,20 +82,9 @@ export function HostDetailsView({ hostId, host, alert }) { ...@@ -95,20 +82,9 @@ export function HostDetailsView({ hostId, host, alert }) {
</Box> </Box>
</Stack> </Stack>
<Stack gap={2}> <Stack gap={2}>
<HostIocSection <HostIocSection hostId={hostId} />
hostId={hostId}
rows={deserialize(urlState.iocs_rows)}
page={deserialize(urlState.iocs_page)}
onUrlStateChange={(params) => setUrlState(params)}
/>
</Stack> </Stack>
<HostJobsSection <HostJobsSection hostId={hostId} />
hostId={hostId}
rows={deserialize(urlState.job_log_rows)}
page={deserialize(urlState.job_log_page)}
expanded={deserialize(urlState.job_log_open)}
onUrlStateChange={(params) => setUrlState(params)}
/>
<AccessControl <AccessControl
allowedRoles={["DeploymentToolAdmin", "DeploymentToolIntegrator"]} allowedRoles={["DeploymentToolAdmin", "DeploymentToolIntegrator"]}
...@@ -123,9 +99,9 @@ export function HostDetailsView({ hostId, host, alert }) { ...@@ -123,9 +99,9 @@ export function HostDetailsView({ hostId, host, alert }) {
Host details Host details
</Typography> </Typography>
} }
expanded={deserialize(urlState.details_open)} expanded={accordionState.detailsOpen}
onChange={(_, expanded) => onChange={(_, expanded) =>
setUrlState({ details_open: serialize(expanded) }) setAccordionState({ detailsOpen: expanded })
} }
> >
<HostDetailsTable host={host} /> <HostDetailsTable host={host} />
...@@ -140,9 +116,9 @@ export function HostDetailsView({ hostId, host, alert }) { ...@@ -140,9 +116,9 @@ export function HostDetailsView({ hostId, host, alert }) {
Host log stream Host log stream
</Typography> </Typography>
} }
expanded={deserialize(urlState.log_stream_open)} expanded={accordionState.logStreamOpen}
onChange={(_, expanded) => onChange={(_, expanded) =>
setUrlState({ log_stream_open: serialize(expanded) }) setAccordionState({ logStreamOpen: expanded })
} }
> >
<LokiPanel <LokiPanel
......
import { useEffect, useContext, useCallback } from "react"; import { useEffect, useContext, useCallback } from "react";
import IOCTable from "../../../components/IOC/IOCTable"; import IOCTable from "../../../components/IOC/IOCTable";
import { string, number, func } from "prop-types"; import { string } from "prop-types";
import { apiContext } from "../../../api/DeployApi"; import { apiContext } from "../../../api/DeployApi";
import { Typography } from "@mui/material"; import { Typography } from "@mui/material";
import { useAPIMethod, usePagination } from "@ess-ics/ce-ui-common"; import { useAPIMethod, usePagination } from "@ess-ics/ce-ui-common";
import { initRequestParams } from "../../../components/common/Helper"; import { initRequestParams } from "../../../components/common/Helper";
import { serialize } from "../../../components/common/URLState/URLState";
const propTypes = { const propTypes = {
hostId: string, hostId: string
row: number,
page: number,
onUrlStateChange: func
}; };
export const HostIocSection = ({ hostId, rows, page, onUrlStateChange }) => { const rowsPerPage = [20, 50];
export const HostIocSection = ({ hostId }) => {
const client = useContext(apiContext); const client = useContext(apiContext);
const { pagination, setPagination, setTotalCount } = usePagination({ const { pagination, setPagination, setTotalCount } = usePagination({
initLimit: rows, initLimit: rowsPerPage[0],
initPage: page initPage: 0
}); });
const { const {
...@@ -36,19 +35,15 @@ export const HostIocSection = ({ hostId, rows, page, onUrlStateChange }) => { ...@@ -36,19 +35,15 @@ export const HostIocSection = ({ hostId, rows, page, onUrlStateChange }) => {
(params) => { (params) => {
setPagination(params); setPagination(params);
abortGetIocs(); abortGetIocs();
onUrlStateChange({
iocs_rows: serialize(params.limit),
iocs_page: serialize(params.page)
});
}, },
[setPagination, abortGetIocs, onUrlStateChange] [setPagination, abortGetIocs]
); );
const getIocs = useCallback(() => { const getIocs = useCallback(() => {
let requestParams = initRequestParams({ page, rows }, null); let requestParams = initRequestParams(pagination, null);
requestParams.host_id = hostId; requestParams.host_id = hostId;
callgetIocs(requestParams); callgetIocs(requestParams);
}, [callgetIocs, hostId, page, rows]); }, [callgetIocs, hostId, pagination]);
// update pagination whenever search result total pages change // update pagination whenever search result total pages change
useEffect(() => { useEffect(() => {
...@@ -61,7 +56,7 @@ export const HostIocSection = ({ hostId, rows, page, onUrlStateChange }) => { ...@@ -61,7 +56,7 @@ export const HostIocSection = ({ hostId, rows, page, onUrlStateChange }) => {
return () => { return () => {
abortGetIocs(); abortGetIocs();
}; };
}, [rows, page, abortGetIocs, getIocs]); }, [abortGetIocs, getIocs]);
return ( return (
<> <>
......
import { useContext, useEffect, useMemo, useCallback } from "react"; import { useContext, useEffect, useMemo, useCallback, useState } from "react";
import { string, number, boolean, func } from "prop-types"; import { string } from "prop-types";
import { serialize } from "../../../components/common/URLState/URLState";
import { getErrorMessage } from "../../../components/common/Helper"; import { getErrorMessage } from "../../../components/common/Helper";
import { apiContext } from "../../../api/DeployApi"; import { apiContext } from "../../../api/DeployApi";
import { import {
...@@ -12,29 +11,20 @@ import { JobTable } from "../../../components/Job"; ...@@ -12,29 +11,20 @@ import { JobTable } from "../../../components/Job";
import { Alert, Typography } from "@mui/material"; import { Alert, Typography } from "@mui/material";
const propTypes = { const propTypes = {
hostId: string.isRequired, hostId: string.isRequired
rows: number,
page: number,
expanded: boolean,
onUrlStateChange: func
}; };
const rowsPerPage = [20, 50]; const rowsPerPage = [20, 50];
export const HostJobsSection = ({ export const HostJobsSection = ({ hostId }) => {
hostId,
rows,
page,
expanded,
onUrlStateChange
}) => {
const client = useContext(apiContext); const client = useContext(apiContext);
const { pagination, setPagination } = usePagination({ const { pagination, setPagination } = usePagination({
rowsPerPageOptions: rowsPerPage, rowsPerPageOptions: rowsPerPage,
initLimit: rows, initLimit: rowsPerPage[0],
initPage: page initPage: 0
}); });
const [expanded, setExpanded] = useState(false);
const { const {
value: hostLog, value: hostLog,
...@@ -56,12 +46,8 @@ export const HostJobsSection = ({ ...@@ -56,12 +46,8 @@ export const HostJobsSection = ({
(params) => { (params) => {
setPagination(params); setPagination(params);
abortGetHostLog(); abortGetHostLog();
onUrlStateChange({
job_log_rows: serialize(params.limit),
job_log_page: serialize(params.page)
});
}, },
[setPagination, abortGetHostLog, onUrlStateChange] [setPagination, abortGetHostLog]
); );
useEffect(() => { useEffect(() => {
...@@ -82,6 +68,7 @@ export const HostJobsSection = ({ ...@@ -82,6 +68,7 @@ export const HostJobsSection = ({
return ( return (
<SimpleAccordion <SimpleAccordion
expanded={expanded}
summary={ summary={
<Typography <Typography
variant="h3" variant="h3"
...@@ -90,9 +77,7 @@ export const HostJobsSection = ({ ...@@ -90,9 +77,7 @@ export const HostJobsSection = ({
Job log Job log
</Typography> </Typography>
} }
onChange={(_, isExpanded) => onChange={(_, expanded) => setExpanded(expanded)}
onUrlStateChange({ job_log_open: serialize(isExpanded) })
}
> >
{error ? <Alert severity="error">{getErrorMessage(error)}</Alert> : null} {error ? <Alert severity="error">{getErrorMessage(error)}</Alert> : null}
{hostLog ? ( {hostLog ? (
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment