From 6b69213bc3cb2fc8911ea209e09be50e245fdd76 Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Wed, 12 Jan 2022 21:58:22 -0500 Subject: [PATCH] fix(striker-ui): draft file list and upload/edit form --- striker-ui/components/Files/FileInfo.tsx | 96 ++++++++++++++++++++++++ striker-ui/components/Files/FileList.tsx | 67 +++++++++++++++++ striker-ui/components/Files/Files.tsx | 44 ++++------- 3 files changed, 179 insertions(+), 28 deletions(-) create mode 100644 striker-ui/components/Files/FileInfo.tsx create mode 100644 striker-ui/components/Files/FileList.tsx diff --git a/striker-ui/components/Files/FileInfo.tsx b/striker-ui/components/Files/FileInfo.tsx new file mode 100644 index 00000000..bb989f35 --- /dev/null +++ b/striker-ui/components/Files/FileInfo.tsx @@ -0,0 +1,96 @@ +import { useEffect, useRef } from 'react'; +import { + Box, + Button, + Checkbox, + FormControlLabel, + Input, + InputLabel, + MenuItem, + Select, + TextField, +} from '@mui/material'; +import { BodyText } from '../Text'; + +type FileInfoProps = { + anvilList: { + anvilName: string; + anvilDescription: string; + anvilUUID: string; + }[]; + isShowSelectFileOnStart?: boolean; +}; + +const FILE_TYPE_LIST = { + iso: 'ISO (optical disc)', + other: 'Other file type', + script: 'Script (program)', +}; + +// Used to solve react/require-default-params AND ensure linting works within the function component. +const FILE_INFO_DEFAULT_PROPS = { isShowSelectFileOnStart: false }; + +const FileInfo = ({ + anvilList, + isShowSelectFileOnStart = FILE_INFO_DEFAULT_PROPS.isShowSelectFileOnStart, +}: FileInfoProps): JSX.Element => { + const selectFileRef = useRef(); + + const openFilePicker = () => { + selectFileRef.current?.click(); + }; + + useEffect(() => { + if (isShowSelectFileOnStart) { + openFilePicker(); + } + }, [isShowSelectFileOnStart]); + + return ( +
+ + + + + + + + + + {anvilList.map(({ anvilName, anvilDescription, anvilUUID }) => { + return ( + } + key={anvilUUID} + label={`${anvilName}: ${anvilDescription}`} + value={`${anvilUUID}-sync`} + /> + ); + })} + +
+ ); +}; + +FileInfo.defaultProps = FILE_INFO_DEFAULT_PROPS; + +export default FileInfo; diff --git a/striker-ui/components/Files/FileList.tsx b/striker-ui/components/Files/FileList.tsx new file mode 100644 index 00000000..583dce0d --- /dev/null +++ b/striker-ui/components/Files/FileList.tsx @@ -0,0 +1,67 @@ +import { Box, Divider, List, ListItem } from '@mui/material'; +import * as prettyBytes from 'pretty-bytes'; + +import { DIVIDER } from '../../lib/consts/DEFAULT_THEME'; + +import { BodyText } from '../Text'; + +const FileList = ({ list = [] }: { list: string[][] }): JSX.Element => { + return ( + + {list.map((file) => { + const fileUUID: string = file[0]; + const fileName: string = file[1]; + const fileSize: string = prettyBytes.default(parseInt(file[2], 10), { + binary: true, + }); + const fileType: string = file[3]; + const fileChecksum: string = file[4]; + + return ( + + + + + + + + + + + + + + + + ); + })} + + ); +}; + +export default FileList; diff --git a/striker-ui/components/Files/Files.tsx b/striker-ui/components/Files/Files.tsx index 792ad45c..8209a8fa 100644 --- a/striker-ui/components/Files/Files.tsx +++ b/striker-ui/components/Files/Files.tsx @@ -1,5 +1,5 @@ -import { useRef } from 'react'; -import { Box, IconButton, Input, InputLabel } from '@mui/material'; +import { useState } from 'react'; +import { Box, IconButton } from '@mui/material'; import AddIcon from '@mui/icons-material/Add'; import { styled } from '@mui/material/styles'; @@ -9,6 +9,8 @@ import { Panel } from '../Panels'; import PeriodicFetch from '../../lib/fetchers/periodicFetch'; import Spinner from '../Spinner'; import { HeaderText } from '../Text'; +import FileInfo from './FileInfo'; +import FileList from './FileList'; const PREFIX = 'Files'; @@ -26,15 +28,14 @@ const StyledDiv = styled('div')(() => ({ })); const Files = (): JSX.Element => { - const addFileInputRef = useRef(); + const [isShowUploadForm, setIsShowUploadForm] = useState(false); - const { data, isLoading } = PeriodicFetch( + const { data: fileList, isLoading } = PeriodicFetch( `${process.env.NEXT_PUBLIC_API_URL?.replace('/cgi-bin', '/api')}/files`, ); - // Let the icon button trigger the invisible input element. const onAddFileButtonClick = () => { - addFileInputRef.current?.click(); + setIsShowUploadForm(!isShowUploadForm); }; return ( @@ -45,31 +46,18 @@ const Files = (): JSX.Element => { -
- - - - - - -
+ + +
- {isLoading ? ( - - ) : ( - - {data} - + {isShowUploadForm && ( + )} + {isLoading ? : } );