|
| 1 | +import CameraIcon from '@/components/icons/CameraIcon'; |
| 2 | +import { Input } from './Styles'; |
| 3 | +import styles from '@/styles/Components.module.css'; |
| 4 | +import PlusIcon from '@/components/icons/PlusIcon'; |
| 5 | +import { |
| 6 | + useRef, |
| 7 | + ChangeEvent, |
| 8 | + Dispatch, |
| 9 | + SetStateAction, |
| 10 | + useEffect, |
| 11 | +} from 'react'; |
| 12 | +import Image from 'next/image'; |
| 13 | +import { createServerSchema, CreateServerInput } from '@/types/client/server'; |
| 14 | +import { FieldErrorsImpl, useForm, UseFormRegister } from 'react-hook-form'; |
| 15 | +import { zodResolver } from '@hookform/resolvers/zod'; |
| 16 | + |
| 17 | +export default function AddServer({ |
| 18 | + serverImage, |
| 19 | + setServerImage, |
| 20 | + register, |
| 21 | + errors, |
| 22 | + serverError, |
| 23 | + showDesc, |
| 24 | + setShowDesc, |
| 25 | +}: { |
| 26 | + serverImage: File | null; |
| 27 | + setServerImage: Dispatch<SetStateAction<File | null>>; |
| 28 | + register: UseFormRegister<{ |
| 29 | + name: string; |
| 30 | + description?: string | undefined; |
| 31 | + }>; |
| 32 | + errors: Partial< |
| 33 | + FieldErrorsImpl<{ |
| 34 | + name: string; |
| 35 | + description: string; |
| 36 | + }> |
| 37 | + >; |
| 38 | + serverError: string; |
| 39 | + showDesc: boolean; |
| 40 | + setShowDesc: Dispatch<SetStateAction<boolean>>; |
| 41 | +}) { |
| 42 | + const imageRef = useRef<HTMLInputElement | null>(null); |
| 43 | + |
| 44 | + const handleFileChange = (e: ChangeEvent<HTMLInputElement>) => { |
| 45 | + if (!e.target.files) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + setServerImage(e.target.files[0]); |
| 50 | + }; |
| 51 | + const previewImage = serverImage ? URL.createObjectURL(serverImage) : ''; |
| 52 | + |
| 53 | + return ( |
| 54 | + <form |
| 55 | + className="flex flex-col w-12 my-4 mx-6" |
| 56 | + onSubmit={(e) => e.preventDefault()} |
| 57 | + > |
| 58 | + {serverError ? ( |
| 59 | + <span className="my-2 text-red-700 text-sm font-bold"> |
| 60 | + {serverError} |
| 61 | + </span> |
| 62 | + ) : ( |
| 63 | + '' |
| 64 | + )} |
| 65 | + <div |
| 66 | + className={`${ |
| 67 | + serverImage |
| 68 | + ? 'p-4' |
| 69 | + : 'w-9 py-4 px-7 border-dashed border-2 border-grey-600' |
| 70 | + } flex items-center rounded-lg justify-center self-center relative hover:cursor-pointer`} |
| 71 | + onClick={() => imageRef?.current?.click()} |
| 72 | + > |
| 73 | + <div className="flex flex-col justify-center items-center"> |
| 74 | + {serverImage ? ( |
| 75 | + <Image alt="serverIcon" src={previewImage} width={50} height={50} /> |
| 76 | + ) : ( |
| 77 | + <> |
| 78 | + <CameraIcon /> |
| 79 | + <span className="text-sm font-semibold text-center tracking-wider"> |
| 80 | + UPLOAD |
| 81 | + </span> |
| 82 | + <span className="absolute -top-3 -right-3"> |
| 83 | + <PlusIcon color="#4abfe8" /> |
| 84 | + </span> |
| 85 | + </> |
| 86 | + )} |
| 87 | + </div> |
| 88 | + </div> |
| 89 | + |
| 90 | + <div className="flex flex-col mt-5"> |
| 91 | + <div className="font-semibold tracking-wider">Server Name</div> |
| 92 | + <input |
| 93 | + type="file" |
| 94 | + ref={imageRef} |
| 95 | + onChange={handleFileChange} |
| 96 | + className="hidden" |
| 97 | + accept="image/*" |
| 98 | + /> |
| 99 | + <input |
| 100 | + className={`${Input('bg-grey-700')} mt-2 ${styles.input}`} |
| 101 | + type="text" |
| 102 | + placeholder="Enter server name" |
| 103 | + {...register('name')} |
| 104 | + /> |
| 105 | + {errors.name && ( |
| 106 | + <p className="text-red-700 mt-2 text-sm font-bold"> |
| 107 | + {errors.name.message} |
| 108 | + </p> |
| 109 | + )} |
| 110 | + {showDesc ? ( |
| 111 | + <div className="mt-4"> |
| 112 | + <div className="font-semibold tracking-wider">Description</div> |
| 113 | + <input |
| 114 | + className={`${Input('bg-grey-700')} mt-2 ${styles.input}`} |
| 115 | + type="text" |
| 116 | + placeholder="Enter a description" |
| 117 | + {...register('description')} |
| 118 | + /> |
| 119 | + {errors.description && ( |
| 120 | + <p className="text-red-700 mt-2 text-sm font-bold"> |
| 121 | + {errors.description.message} |
| 122 | + </p> |
| 123 | + )} |
| 124 | + </div> |
| 125 | + ) : ( |
| 126 | + <div |
| 127 | + className={`${styles.description} mt-4 text-frost-600 font-bold tracking-wide hover:cursor-pointer hover:text-frost-500 underline underline-offset-2`} |
| 128 | + onClick={() => setShowDesc(true)} |
| 129 | + > |
| 130 | + Add a description{' '} |
| 131 | + </div> |
| 132 | + )} |
| 133 | + </div> |
| 134 | + </form> |
| 135 | + ); |
| 136 | +} |
0 commit comments