|
| 1 | +import React, { useCallback } from 'react'; |
| 2 | +import { Controller, useController, useFormContext } from 'react-hook-form'; |
| 3 | +import useStore from '../store'; |
| 4 | +import Spinner from './Spinner'; |
| 5 | + |
| 6 | +type FileUpLoaderProps = { |
| 7 | + name: string; |
| 8 | +}; |
| 9 | +const FileUpLoader: React.FC<FileUpLoaderProps> = ({ name }) => { |
| 10 | + const { |
| 11 | + control, |
| 12 | + formState: { errors }, |
| 13 | + } = useFormContext(); |
| 14 | + const { field } = useController({ name, control }); |
| 15 | + const store = useStore(); |
| 16 | + |
| 17 | + const onFileDrop = useCallback( |
| 18 | + async (e: React.SyntheticEvent<EventTarget>) => { |
| 19 | + const target = e.target as HTMLInputElement; |
| 20 | + if (!target.files) return; |
| 21 | + const newFile = Object.values(target.files).map((file: File) => file); |
| 22 | + const formData = new FormData(); |
| 23 | + formData.append('file', newFile[0]); |
| 24 | + formData.append('upload_preset', 'trpc-api'); |
| 25 | + |
| 26 | + store.setUploadingImage(true); |
| 27 | + const data = await fetch( |
| 28 | + 'https://api.cloudinary.com/v1_1/Codevo/image/upload', |
| 29 | + { |
| 30 | + method: 'POST', |
| 31 | + body: formData, |
| 32 | + } |
| 33 | + ) |
| 34 | + .then((res) => { |
| 35 | + store.setUploadingImage(false); |
| 36 | + |
| 37 | + return res.json(); |
| 38 | + }) |
| 39 | + .catch((err) => { |
| 40 | + store.setUploadingImage(false); |
| 41 | + console.log(err); |
| 42 | + }); |
| 43 | + |
| 44 | + if (data.secure_url) { |
| 45 | + field.onChange(data.secure_url); |
| 46 | + } |
| 47 | + }, |
| 48 | + |
| 49 | + [field, store] |
| 50 | + ); |
| 51 | + |
| 52 | + return ( |
| 53 | + <Controller |
| 54 | + name={name} |
| 55 | + defaultValue='' |
| 56 | + control={control} |
| 57 | + render={({ field: { name, onBlur, ref } }) => ( |
| 58 | + <> |
| 59 | + <div className='mb-2 flex justify-between items-center'> |
| 60 | + <div> |
| 61 | + <span className='block mb-2'>Choose profile photo</span> |
| 62 | + <input |
| 63 | + className='block text-sm mb-2 text-slate-500 file:mr-4 file:py-2 file:px-4 file:rounded-full file:border-0 file:text-sm file:font-semibold file:bg-violet-50 file:text-violet-700 hover:file:bg-violet-100' |
| 64 | + type='file' |
| 65 | + name={name} |
| 66 | + onBlur={onBlur} |
| 67 | + ref={ref} |
| 68 | + onChange={onFileDrop} |
| 69 | + multiple={false} |
| 70 | + accept='image/jpg, image/png, image/jpeg' |
| 71 | + /> |
| 72 | + </div> |
| 73 | + <div> |
| 74 | + {store.uploadingImage && <Spinner color='text-yellow-400' />} |
| 75 | + </div> |
| 76 | + </div> |
| 77 | + <p |
| 78 | + className={`text-red-500 text-xs italic mb-2 ${ |
| 79 | + errors[name] ? 'visible' : 'invisible' |
| 80 | + }`} |
| 81 | + > |
| 82 | + {errors[name] && (errors[name]?.message as string)} |
| 83 | + </p> |
| 84 | + </> |
| 85 | + )} |
| 86 | + /> |
| 87 | + ); |
| 88 | +}; |
| 89 | + |
| 90 | +export default FileUpLoader; |
0 commit comments