From ff88ad75a33e5d28e4dd4c2a5bbfe2a5b7411eb4 Mon Sep 17 00:00:00 2001 From: Sergey Yarkov Date: Fri, 5 Nov 2021 17:39:50 +0300 Subject: [PATCH] feat: refactor layout & added file select component --- src/components/FileSelect/FileSelect.tsx | 86 +++++++++++++++++++ src/components/LoadingList/LoadingList.tsx | 12 +++ src/components/LoadingList/index.ts | 3 + src/components/Select/Select.tsx | 2 + src/helpers/index.ts | 19 ++++ .../auth/components/AuthForm/AuthForm.tsx | 13 ++- .../components/AuthForm/AuthForm.validator.ts | 2 +- .../components/CourseList/CourseList.tsx | 6 +- .../CourseList/CourseListLoading.tsx | 12 --- .../create/components/CreateCourseForm.tsx | 19 ++-- .../components/CreateCourseForm.validator.ts | 16 ++-- .../users/components/UserList/UserItem.tsx | 14 +-- .../users/components/UserList/UserList.tsx | 13 ++- src/pages/users/users.tsx | 4 +- 14 files changed, 167 insertions(+), 54 deletions(-) create mode 100644 src/components/FileSelect/FileSelect.tsx create mode 100644 src/components/LoadingList/LoadingList.tsx create mode 100644 src/components/LoadingList/index.ts delete mode 100644 src/pages/courses/components/CourseList/CourseListLoading.tsx diff --git a/src/components/FileSelect/FileSelect.tsx b/src/components/FileSelect/FileSelect.tsx new file mode 100644 index 0000000..a48c03b --- /dev/null +++ b/src/components/FileSelect/FileSelect.tsx @@ -0,0 +1,86 @@ +import React, { useState, useRef } from 'react'; +import { Flex, Box, Text } from '@chakra-ui/layout'; +import { Button } from '@chakra-ui/button'; +import { MdAttachFile } from 'react-icons/md'; +import * as helpres from '@educt/helpers'; + +/** + * Hooks + */ +import { useColorModeValue } from '@chakra-ui/color-mode'; +import { Input } from '@chakra-ui/input'; + +type FileSelectPropsType = { + onChange: (file: File) => void; + supportedFormats?: string[]; +}; + +const FileSelect: React.FC = ({ onChange, supportedFormats }) => { + const [selectedFile, setSelectedFile] = useState(undefined); + const fileInputRef = useRef(null); + + /** + * Handle the file input selection + */ + const onSelectFileHandler = () => { + if (fileInputRef.current) { + fileInputRef.current.click(); + } + }; + + const onChangeFileHandler = (e: React.ChangeEvent) => { + e.preventDefault(); + + const { files } = e.target; + + if (files && files.length !== 0) { + const file = files[0]; + + onChange(file); + setSelectedFile(file); + } + }; + + return ( + + + + + {selectedFile ? ( + + {selectedFile.name} +
+ + {helpres.transformBytes(selectedFile.size)} + +
+ ) : ( + + Choose a file... + + )} + {supportedFormats && ( + + + Supported formats: {supportedFormats.join(', ')} + + + )} +
+ + + +
+
+ ); +}; + +export default FileSelect; diff --git a/src/components/LoadingList/LoadingList.tsx b/src/components/LoadingList/LoadingList.tsx new file mode 100644 index 0000000..9aada87 --- /dev/null +++ b/src/components/LoadingList/LoadingList.tsx @@ -0,0 +1,12 @@ +import React from 'react'; +import { Box, Text } from '@chakra-ui/layout'; + +const LoadingList: React.FC = () => { + return ( + + Wait a second... + + ); +}; + +export default LoadingList; diff --git a/src/components/LoadingList/index.ts b/src/components/LoadingList/index.ts new file mode 100644 index 0000000..f67ac9a --- /dev/null +++ b/src/components/LoadingList/index.ts @@ -0,0 +1,3 @@ +import LoadingList from './LoadingList'; + +export default LoadingList; diff --git a/src/components/Select/Select.tsx b/src/components/Select/Select.tsx index 9463395..13d2ad4 100644 --- a/src/components/Select/Select.tsx +++ b/src/components/Select/Select.tsx @@ -29,6 +29,8 @@ const Select: React.FC = props => { { }; return ( - +
@@ -67,27 +67,24 @@ const AuthForm: React.FC = () => { Login } /> - + {errors.login?.message} + Password } /> - + {errors.password?.message} +