From 8e78ed2fea3a29ea6107c8c29e46f872690b0bdb Mon Sep 17 00:00:00 2001 From: Josue Date: Tue, 1 Jun 2021 16:57:03 -0400 Subject: [PATCH] refactor: sanitize hosts data to avoid using empty nodes --- striker-ui/components/Hosts/index.tsx | 13 ++++++++----- striker-ui/lib/sanitizers/hostsSanitizer.ts | 7 +++++++ 2 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 striker-ui/lib/sanitizers/hostsSanitizer.ts diff --git a/striker-ui/components/Hosts/index.tsx b/striker-ui/components/Hosts/index.tsx index a22b2e53..9924c2d1 100644 --- a/striker-ui/components/Hosts/index.tsx +++ b/striker-ui/components/Hosts/index.tsx @@ -5,6 +5,7 @@ import AnvilHost from './AnvilHost'; import PeriodicFetch from '../../lib/fetchers/periodicFetch'; import { AnvilContext } from '../AnvilContext'; import Spinner from '../Spinner'; +import hostsSanitizer from '../../lib/sanitizers/hostsSanitizer'; const Hosts = ({ anvil }: { anvil: AnvilListItem[] }): JSX.Element => { const { uuid } = useContext(AnvilContext); @@ -13,18 +14,20 @@ const Hosts = ({ anvil }: { anvil: AnvilListItem[] }): JSX.Element => { `${process.env.NEXT_PUBLIC_API_URL}/get_status?anvil_uuid=${uuid}`, ); + const anvilIndex = anvil.findIndex((a) => a.anvil_uuid === uuid); + return ( {!isLoading ? ( <> - {anvil.findIndex((a) => a.anvil_uuid === uuid) !== -1 && data && ( + {anvilIndex !== -1 && data && ( a.anvil_uuid === uuid)].hosts - .filter((host) => host.host_uuid) - .map((host, index) => { + hosts={hostsSanitizer(anvil[anvilIndex].hosts).map( + (host, index) => { return data.hosts[index]; - })} + }, + )} /> )} diff --git a/striker-ui/lib/sanitizers/hostsSanitizer.ts b/striker-ui/lib/sanitizers/hostsSanitizer.ts new file mode 100644 index 00000000..5fbe7826 --- /dev/null +++ b/striker-ui/lib/sanitizers/hostsSanitizer.ts @@ -0,0 +1,7 @@ +const hostsSanitizer = ( + data: Array, +): Array => { + return data.filter((host) => host.host_uuid); +}; + +export default hostsSanitizer;