|
| 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 | +const UPLOAD_PRESET = "nextjs-jwt"; |
| 7 | +const CLOUDINARY_URL = "https://api.cloudinary.com/v1_1/dxq7xwg2w/image/upload"; |
| 8 | + |
| 9 | +interface IFileUpload { |
| 10 | + name: string; |
| 11 | +} |
| 12 | + |
| 13 | +const FileUpload: React.FC<IFileUpload> = ({ name }) => { |
| 14 | + const { |
| 15 | + control, |
| 16 | + formState: { errors }, |
| 17 | + } = useFormContext(); |
| 18 | + const { field } = useController({ name, control }); |
| 19 | + const store = useStore(); |
| 20 | + |
| 21 | + const onFileDrop = useCallback( |
| 22 | + async (e: React.SyntheticEvent<EventTarget>) => { |
| 23 | + const target = e.target as HTMLInputElement; |
| 24 | + if (!target.files) return; |
| 25 | + const newFile = Object.values(target.files).map((file: File) => file); |
| 26 | + |
| 27 | + const formData = new FormData(); |
| 28 | + formData.append("file", newFile[0]); |
| 29 | + formData.append("upload_preset", UPLOAD_PRESET); |
| 30 | + |
| 31 | + store.setUploadImage(true); |
| 32 | + try { |
| 33 | + const response = await fetch(CLOUDINARY_URL, { |
| 34 | + method: "post", |
| 35 | + body: formData, |
| 36 | + }); |
| 37 | + const data = await response.json(); |
| 38 | + |
| 39 | + if (data.secure_url) { |
| 40 | + field.onChange(data.secure_url); |
| 41 | + } |
| 42 | + return data; |
| 43 | + } catch (err) { |
| 44 | + console.error("error in upload image: ", err); |
| 45 | + } finally { |
| 46 | + store.setUploadImage(false); |
| 47 | + } |
| 48 | + }, |
| 49 | + [field, store] |
| 50 | + ); |
| 51 | + return ( |
| 52 | + <Controller |
| 53 | + name={name} |
| 54 | + defaultValue="" |
| 55 | + control={control} |
| 56 | + render={({ field: { name, onBlur, ref } }) => ( |
| 57 | + <> |
| 58 | + <div className="mb-2 flex justify-between items-center"> |
| 59 | + <div> |
| 60 | + <span className="block mb-2">Choose profile photo</span> |
| 61 | + <input |
| 62 | + 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" |
| 63 | + type="file" |
| 64 | + name={name} |
| 65 | + onBlur={onBlur} |
| 66 | + ref={ref} |
| 67 | + onChange={onFileDrop} |
| 68 | + multiple={false} |
| 69 | + accept="image/jpg, image/png, image/jpeg" |
| 70 | + /> |
| 71 | + </div> |
| 72 | + <div> |
| 73 | + {store.uploadingImage && <Spinner color="text-yellow-400" />} |
| 74 | + </div> |
| 75 | + </div> |
| 76 | + <p |
| 77 | + className={`text-red-500 text-xs italic mb-2 ${ |
| 78 | + errors[name] ? "visible" : "invisible" |
| 79 | + }`} |
| 80 | + > |
| 81 | + {errors[name] && (errors[name]?.message as string)} |
| 82 | + </p> |
| 83 | + </> |
| 84 | + )} |
| 85 | + /> |
| 86 | + ); |
| 87 | +}; |
| 88 | + |
| 89 | +export default FileUpload; |
0 commit comments