refactor(striker-ui): repair types in Network

main
Tsu-ba-me 2 years ago
parent 673f88f6a7
commit 87fd3bdc6b
  1. 32
      striker-ui/components/Network/Network.tsx
  2. 3
      striker-ui/components/Network/processNetwork.ts

@ -1,17 +1,20 @@
import { Box, Divider, styled } from '@mui/material';
import { useContext } from 'react'; import { useContext } from 'react';
import { Box, Divider } from '@mui/material';
import { styled } from '@mui/material/styles';
import { Panel } from '../Panels';
import { HeaderText, BodyText } from '../Text';
import periodicFetch from '../../lib/fetchers/periodicFetch';
import { import {
DIVIDER, DIVIDER,
LARGE_MOBILE_BREAKPOINT, LARGE_MOBILE_BREAKPOINT,
} from '../../lib/consts/DEFAULT_THEME'; } from '../../lib/consts/DEFAULT_THEME';
import processNetworkData from './processNetwork';
import { AnvilContext } from '../AnvilContext'; import { AnvilContext } from '../AnvilContext';
import Decorator, { Colours } from '../Decorator'; import Decorator, { Colours } from '../Decorator';
import { Panel } from '../Panels';
import periodicFetch from '../../lib/fetchers/periodicFetch';
import processNetworkData from './processNetwork';
import Spinner from '../Spinner'; import Spinner from '../Spinner';
import { HeaderText, BodyText } from '../Text';
import useProtect from '../../hooks/useProtect';
import useProtectedState from '../../hooks/useProtectedState';
const PREFIX = 'Network'; const PREFIX = 'Network';
@ -68,26 +71,35 @@ const selectDecorator = (state: string): Colours => {
const Network = (): JSX.Element => { const Network = (): JSX.Element => {
const { uuid } = useContext(AnvilContext); const { uuid } = useContext(AnvilContext);
const { protect } = useProtect();
const [processed, setProcessed] = useProtectedState<
ProcessedNetwork | undefined
>(undefined, protect);
const { data, isLoading } = periodicFetch<AnvilNetwork>( const { isLoading } = periodicFetch<AnvilNetwork>(
`${process.env.NEXT_PUBLIC_API_URL}/get_networks?anvil_uuid=${uuid}`, `${process.env.NEXT_PUBLIC_API_URL}/get_networks?anvil_uuid=${uuid}`,
{
onSuccess: (data) => {
setProcessed(processNetworkData(data));
},
},
); );
const processed = processNetworkData(data);
return ( return (
<Panel> <Panel>
<StyledDiv> <StyledDiv>
<HeaderText text="Network" /> <HeaderText text="Network" />
{!isLoading ? ( {!isLoading ? (
<Box className={classes.container}> <Box className={classes.container}>
{data && {processed &&
processed.bonds.map((bond: ProcessedBond) => ( processed.bonds.map((bond: ProcessedBond) => (
<> <>
<Box <Box
className={classes.root}
display="flex" display="flex"
flexDirection="row" flexDirection="row"
width="100%" width="100%"
className={classes.root}
> >
<Box p={1} className={classes.noPaddingLeft}> <Box p={1} className={classes.noPaddingLeft}>
<Decorator colour={selectDecorator(bond.bond_state)} /> <Decorator colour={selectDecorator(bond.bond_state)} />

@ -2,7 +2,7 @@ const processNetworkData = (data: AnvilNetwork): ProcessedNetwork => {
const processedBonds: string[] = []; const processedBonds: string[] = [];
const displayBonds: ProcessedNetwork = { bonds: [] }; const displayBonds: ProcessedNetwork = { bonds: [] };
data?.hosts.forEach((host) => { data.hosts.forEach((host) => {
host.bonds.forEach((bond) => { host.bonds.forEach((bond) => {
const index = processedBonds.findIndex( const index = processedBonds.findIndex(
(processed: string) => processed === bond.bond_name, (processed: string) => processed === bond.bond_name,
@ -33,7 +33,6 @@ const processNetworkData = (data: AnvilNetwork): ProcessedNetwork => {
}); });
}); });
/* eslint-disable no-param-reassign */
displayBonds.bonds.forEach((bond) => { displayBonds.bonds.forEach((bond) => {
const hostIndex = const hostIndex =
bond.hosts[0].link.link_speed > bond.hosts[1].link.link_speed ? 1 : 0; bond.hosts[0].link.link_speed > bond.hosts[1].link.link_speed ? 1 : 0;

Loading…
Cancel
Save