From b910a240a474ce3fc6f0f07b76604bd7c69d0834 Mon Sep 17 00:00:00 2001 From: Melnyk Mykhailo Date: Mon, 29 Jan 2024 13:28:35 +0200 Subject: [PATCH] fix code style and add progress bar --- src/components/pages/profile/file-input.tsx | 37 ++++++++++++++++----- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/src/components/pages/profile/file-input.tsx b/src/components/pages/profile/file-input.tsx index 9e5abca..8992d5a 100644 --- a/src/components/pages/profile/file-input.tsx +++ b/src/components/pages/profile/file-input.tsx @@ -2,7 +2,8 @@ import ImageCard from '@/components/pages/auth/image-card' import { Icons } from '@/components/ui/icons' -import { classnames } from '@/utils' +import { Progress } from '@/components/ui/progress' +import { cn } from '@/lib/utils' import { FC, FormEvent, useRef, useState } from 'react' type FileInputProps = { @@ -16,11 +17,13 @@ const FileInput: FC = ({ className }) => { const [preview, setPreview] = useState() - function clearSelectFile() { + const [progress, setProgress] = useState(0) + + const clearSelectFile = () => { setPreview(undefined) } - function handleSelectFile() { + const handleSelectFile = () => { fileInputRef.current?.click() } @@ -31,17 +34,21 @@ const FileInput: FC = ({ className }) => { fileReader.readAsDataURL(file) - // TODO: implement shadcn/ui progress component on image upload - // fileReader.onprogress = (progress) => { - // console.log('progress', progress) - // } + fileReader.onloadstart = () => { + setProgress(0) + } + + fileReader.onprogress = (progress) => { + setProgress((progress.loaded / progress.total) * 100) + } fileReader.onload = () => { setPreview(fileReader.result as string) + setTimeout(() => setProgress(0), 500) } } - function handleImageChange(event: FormEvent) { + const handleImageChange = (event: FormEvent) => { const target = event.target as HTMLElement & { files: FileList } @@ -53,7 +60,7 @@ const FileInput: FC = ({ className }) => { return (