From c0d4e1132e0c27cd4531ed60fce7d157f98c026b Mon Sep 17 00:00:00 2001 From: Josue Date: Tue, 10 Aug 2021 12:28:05 -0400 Subject: [PATCH] refactor(front-end): add fetching preview image from the backend --- striker-ui/components/Display/Preview.tsx | 41 +++++++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/striker-ui/components/Display/Preview.tsx b/striker-ui/components/Display/Preview.tsx index 6167d7ac..00a79213 100644 --- a/striker-ui/components/Display/Preview.tsx +++ b/striker-ui/components/Display/Preview.tsx @@ -1,4 +1,4 @@ -import { Dispatch, SetStateAction } from 'react'; +import { Dispatch, SetStateAction, useEffect, useState } from 'react'; import { Box } from '@material-ui/core'; import { makeStyles } from '@material-ui/core/styles'; import IconButton from '@material-ui/core/IconButton'; @@ -10,6 +10,7 @@ import { HeaderText } from '../Text'; interface PreviewProps { setMode: Dispatch>; + uuid: string; serverName: string | string[] | undefined; } @@ -40,10 +41,35 @@ const useStyles = makeStyles(() => ({ backgroundColor: GREY, fontSize: '8em', }, + previewImage: { + width: '100%', + height: '100%', + }, })); -const Preview = ({ setMode, serverName }: PreviewProps): JSX.Element => { +const Preview = ({ setMode, uuid, serverName }: PreviewProps): JSX.Element => { const classes = useStyles(); + const [preview, setPreview] = useState(); + + useEffect(() => { + (async () => { + try { + const res = await fetch( + `${process.env.NEXT_PUBLIC_API_URL}/get_server_screenshot?server_uuid=${uuid}`, + { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + }, + }, + ); + const { screenshot } = await res.json(); + setPreview(screenshot); + } catch { + setPreview(''); + } + })(); + }, [uuid]); return ( @@ -58,7 +84,16 @@ const Preview = ({ setMode, serverName }: PreviewProps): JSX.Element => { component="span" onClick={() => setMode(false)} > - + {!preview ? ( + + ) : ( + + )}