|
| 1 | +import React, { useRef, useState } from 'react'; |
| 2 | + |
| 3 | +import { |
| 4 | + AspectRatio, |
| 5 | + AspectRatioProps, |
| 6 | + Box, |
| 7 | + BoxProps, |
| 8 | + IconButton, |
| 9 | + Image, |
| 10 | + ImageProps, |
| 11 | + Input, |
| 12 | +} from '@chakra-ui/react'; |
| 13 | +import { FiX } from 'react-icons/fi'; |
| 14 | + |
| 15 | +import { Loader } from '@/app/layout'; |
| 16 | +import { DefaultImagePlaceholder } from '@/components/ImageUpload/DefaultImagePlaceholder'; |
| 17 | +import { uploadFile } from '@/components/ImageUpload/cloudinary.service'; |
| 18 | + |
| 19 | +export type ImageUploadProps = Omit<BoxProps, 'onChange' | 'placeholder'> & |
| 20 | + Pick<AspectRatioProps, 'ratio'> & { |
| 21 | + value: string; |
| 22 | + onChange: (url: string) => void; |
| 23 | + onUploadStateChange?: (isUploading: boolean) => void; |
| 24 | + placeholder?: ImageProps['fallback']; |
| 25 | + }; |
| 26 | + |
| 27 | +export const ImageUpload: React.FC<ImageUploadProps> = ({ |
| 28 | + value, |
| 29 | + onChange, |
| 30 | + onUploadStateChange = () => undefined, |
| 31 | + placeholder = undefined, |
| 32 | + ratio = 16 / 9, |
| 33 | + ...rest |
| 34 | +}) => { |
| 35 | + const fileInputRef = useRef<HTMLInputElement>(null); |
| 36 | + const [isUploading, setIsUploading] = useState(false); |
| 37 | + |
| 38 | + const handleChange = async (e: React.ChangeEvent<HTMLInputElement>) => { |
| 39 | + const file = e.target.files?.[0]; |
| 40 | + if (!file) { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + try { |
| 45 | + setIsUploading(true); |
| 46 | + onUploadStateChange(true); |
| 47 | + |
| 48 | + const imageUrl = await uploadFile(file); |
| 49 | + console.log({ imageUrl }); |
| 50 | + onChange(imageUrl); |
| 51 | + } catch (err) { |
| 52 | + console.error(err); |
| 53 | + } finally { |
| 54 | + onUploadStateChange(false); |
| 55 | + setIsUploading(false); |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + const handleDelete = () => { |
| 60 | + onChange(''); |
| 61 | + if (fileInputRef.current) { |
| 62 | + fileInputRef.current.value = ''; |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + const getFallback = () => { |
| 67 | + // If uploading to cloudinary or loading the image from the url, display |
| 68 | + // the loader. |
| 69 | + if (value || isUploading) { |
| 70 | + return <Loader />; |
| 71 | + } |
| 72 | + |
| 73 | + return placeholder ?? <DefaultImagePlaceholder />; |
| 74 | + }; |
| 75 | + |
| 76 | + return ( |
| 77 | + <Box |
| 78 | + position="relative" |
| 79 | + border="1px" |
| 80 | + borderStyle="dashed" |
| 81 | + borderColor="gray.200" |
| 82 | + borderRadius="16px" |
| 83 | + overflow="hidden" |
| 84 | + cursor="pointer" |
| 85 | + transition="border-color 150ms ease-in-out" |
| 86 | + _hover={{ borderColor: 'gray.400' }} |
| 87 | + {...rest} |
| 88 | + > |
| 89 | + <Input |
| 90 | + ref={fileInputRef} |
| 91 | + type="file" |
| 92 | + display="none" |
| 93 | + onChange={handleChange} |
| 94 | + /> |
| 95 | + <AspectRatio ratio={ratio} onClick={() => fileInputRef.current?.click()}> |
| 96 | + <Image src={value} fallback={getFallback()} /> |
| 97 | + </AspectRatio> |
| 98 | + {!!value && ( |
| 99 | + <IconButton |
| 100 | + icon={<FiX />} |
| 101 | + position="absolute" |
| 102 | + top="0" |
| 103 | + right="0" |
| 104 | + size="lg" |
| 105 | + variant="ghost" |
| 106 | + aria-label="Remove photo" |
| 107 | + onClick={handleDelete} |
| 108 | + _active={{ bgColor: 'blackAlpha.600' }} |
| 109 | + _hover={{ bgColor: 'blackAlpha.300' }} |
| 110 | + /> |
| 111 | + )} |
| 112 | + </Box> |
| 113 | + ); |
| 114 | +}; |
0 commit comments