|
| 1 | +import { Input } from './Styles'; |
| 2 | +import styles from '@/styles/Components.module.css'; |
| 3 | + |
| 4 | +import { Dispatch, SetStateAction, useState } from 'react'; |
| 5 | +import { |
| 6 | + Control, |
| 7 | + FieldErrorsImpl, |
| 8 | + UseFormRegister, |
| 9 | + Controller, |
| 10 | +} from 'react-hook-form'; |
| 11 | +import ChannelMessageIcon from '@/components/icons/ChannelMessageIcon'; |
| 12 | + |
| 13 | +export default function AddChannel({ |
| 14 | + register, |
| 15 | + errors, |
| 16 | + control, |
| 17 | + serverError, |
| 18 | + showDesc, |
| 19 | + setShowDesc, |
| 20 | + channelType, |
| 21 | + setChannelType, |
| 22 | +}: { |
| 23 | + register: UseFormRegister<{ |
| 24 | + description?: string | undefined; |
| 25 | + isMedia?: boolean | undefined; |
| 26 | + name: string; |
| 27 | + }>; |
| 28 | + errors: Partial< |
| 29 | + FieldErrorsImpl<{ |
| 30 | + description: string; |
| 31 | + isMedia: NonNullable<boolean | undefined>; |
| 32 | + name: string; |
| 33 | + }> |
| 34 | + >; |
| 35 | + control: Control< |
| 36 | + { |
| 37 | + description?: string | undefined; |
| 38 | + isMedia?: boolean | undefined; |
| 39 | + name: string; |
| 40 | + }, |
| 41 | + any |
| 42 | + >; |
| 43 | + serverError: string; |
| 44 | + showDesc: boolean; |
| 45 | + setShowDesc: Dispatch<SetStateAction<boolean>>; |
| 46 | + channelType: 'media' | 'text'; |
| 47 | + setChannelType: Dispatch<SetStateAction<'media' | 'text'>>; |
| 48 | +}) { |
| 49 | + return ( |
| 50 | + <form |
| 51 | + className="flex flex-col w-12 mb-1 mx-6" |
| 52 | + onSubmit={(e) => e.preventDefault()} |
| 53 | + > |
| 54 | + {serverError ? ( |
| 55 | + <span className="my-2 text-red-700 text-sm font-bold"> |
| 56 | + {serverError} |
| 57 | + </span> |
| 58 | + ) : ( |
| 59 | + '' |
| 60 | + )} |
| 61 | + |
| 62 | + <div className="flex flex-col mt-5"> |
| 63 | + <div className="font-semibold tracking-wider">Channel Name</div> |
| 64 | + <input |
| 65 | + className={`${Input('bg-grey-700')} mt-2 ${styles.input}`} |
| 66 | + type="text" |
| 67 | + placeholder="Enter channel name" |
| 68 | + {...register('name')} |
| 69 | + /> |
| 70 | + {errors.name && ( |
| 71 | + <p className="text-red-700 mt-2 text-sm font-bold"> |
| 72 | + {errors.name.message} |
| 73 | + </p> |
| 74 | + )} |
| 75 | + <div className="font-semibold tracking-wider mt-4">Channel Type</div> |
| 76 | + <div className="flex-col flex"> |
| 77 | + <Controller |
| 78 | + control={control} |
| 79 | + name="isMedia" |
| 80 | + render={({ field: { onChange, onBlur, value, ref } }) => ( |
| 81 | + <> |
| 82 | + <div |
| 83 | + className={`flex ${ |
| 84 | + channelType == 'text' ? 'bg-grey-600' : 'bg-grey-700' |
| 85 | + } rounded-2xl py-2 pl-6 items-center`} |
| 86 | + > |
| 87 | + <ChannelMessageIcon /> |
| 88 | + <label className="ml-1 flex items-center w-full justify-between hover:cursor-pointer"> |
| 89 | + Text{' '} |
| 90 | + <input |
| 91 | + className="mr-4" |
| 92 | + type="radio" |
| 93 | + onBlur={onBlur} |
| 94 | + onChange={() => onChange(false)} |
| 95 | + onClick={() => setChannelType('text')} |
| 96 | + checked={value === false} |
| 97 | + ref={ref} |
| 98 | + /> |
| 99 | + </label> |
| 100 | + </div> |
| 101 | + <div |
| 102 | + className={`flex ${ |
| 103 | + channelType == 'media' ? 'bg-grey-600' : 'bg-grey-700' |
| 104 | + } rounded-2xl py-2 pl-6 mt-2 items-center`} |
| 105 | + > |
| 106 | + <ChannelMessageIcon /> |
| 107 | + <label className="ml-1 flex items-center w-full justify-between hover:cursor-pointer"> |
| 108 | + Voice{' '} |
| 109 | + <input |
| 110 | + className="mr-4 " |
| 111 | + type="radio" |
| 112 | + onBlur={onBlur} |
| 113 | + onChange={() => onChange(true)} |
| 114 | + onClick={() => setChannelType('media')} |
| 115 | + checked={value === true} |
| 116 | + ref={ref} |
| 117 | + /> |
| 118 | + </label> |
| 119 | + </div> |
| 120 | + </> |
| 121 | + )} |
| 122 | + /> |
| 123 | + </div> |
| 124 | + {showDesc ? ( |
| 125 | + <div className="mt-4"> |
| 126 | + <div className="font-semibold tracking-wider">Description</div> |
| 127 | + <input |
| 128 | + className={`${Input('bg-grey-700')} mt-2 ${styles.input}`} |
| 129 | + type="text" |
| 130 | + placeholder="Enter a description" |
| 131 | + {...register('description')} |
| 132 | + /> |
| 133 | + {errors.description && ( |
| 134 | + <p className="text-red-700 mt-2 text-sm font-bold"> |
| 135 | + {errors.description.message} |
| 136 | + </p> |
| 137 | + )} |
| 138 | + </div> |
| 139 | + ) : ( |
| 140 | + <div |
| 141 | + className={`${styles.description} mt-4 text-frost-600 font-bold tracking-wide hover:cursor-pointer hover:text-frost-500 underline underline-offset-2`} |
| 142 | + onClick={() => setShowDesc(true)} |
| 143 | + > |
| 144 | + Add a description{' '} |
| 145 | + </div> |
| 146 | + )} |
| 147 | + </div> |
| 148 | + </form> |
| 149 | + ); |
| 150 | +} |
0 commit comments