|
| 1 | +import { useState } from 'react' |
| 2 | + |
| 3 | +import axios from 'axios' |
| 4 | +import { setCookie } from 'cookies-next' |
| 5 | + |
| 6 | +import { |
| 7 | + useShortAnswerInput, |
| 8 | + useLongAnswerInput, |
| 9 | + useDropdownInput, |
| 10 | + useRadioInput, |
| 11 | + useGoogleForm, |
| 12 | + GoogleFormProvider, |
| 13 | +} from 'react-google-forms-hooks' |
| 14 | + |
| 15 | +import form from '../GoogleForm.json' |
| 16 | + |
| 17 | +export default function RegistrationForm({ setCurrentUser, setStateRegistration }) { |
| 18 | + const methods = useGoogleForm({ form }) |
| 19 | + const [formState, setFormState] = useState('default') |
| 20 | + const [errorMsg, setErrorMsg] = useState('') |
| 21 | + |
| 22 | + const onSubmit = async (data) => { |
| 23 | + setFormState('loading') |
| 24 | + if (!getEmailId() || !getNameId()) { |
| 25 | + setFormState('error') |
| 26 | + setErrorMsg('Form error. Contact the administrator.') |
| 27 | + return false |
| 28 | + } |
| 29 | + await methods.submitToGoogleForms(data) |
| 30 | + axios |
| 31 | + .post('/api/confirm', { email: data[getEmailId()], name: data[getNameId()] }) |
| 32 | + .then((res) => { |
| 33 | + if (res.status !== 200) { |
| 34 | + setFormState('error') |
| 35 | + setErrorMsg(res?.error || 'Error! Please try again.') |
| 36 | + } else { |
| 37 | + setFormState('default') |
| 38 | + setCookie('user', data[getEmailId()], { |
| 39 | + maxAge: 60 * 60 * 24 * 180, |
| 40 | + SameSite: true, |
| 41 | + }) |
| 42 | + setCurrentUser(data[getEmailId()]) |
| 43 | + setStateRegistration('ticket') |
| 44 | + } |
| 45 | + }) |
| 46 | + .catch((err) => { |
| 47 | + setFormState('error') |
| 48 | + setErrorMsg(err?.message ?? 'Error! Please try again.') |
| 49 | + }) |
| 50 | + } |
| 51 | + |
| 52 | + return ( |
| 53 | + <> |
| 54 | + <GoogleFormProvider {...methods}> |
| 55 | + <form onSubmit={methods.handleSubmit(onSubmit)}> |
| 56 | + <Questions /> |
| 57 | + <button |
| 58 | + type="submit" |
| 59 | + className="uppercase text-white py-2 px-4 mb-4 block mx-auto rounded-md bg-primary-600 disabled:bg-gray-400" |
| 60 | + disabled={formState === 'loading'} |
| 61 | + > |
| 62 | + {formState === 'loading' ? <>Loading</> : <>Register</>} |
| 63 | + </button> |
| 64 | + {formState === 'error' ? ( |
| 65 | + <p className="text-center text-red-600">{errorMsg}</p> |
| 66 | + ) : ( |
| 67 | + '' |
| 68 | + )} |
| 69 | + </form> |
| 70 | + </GoogleFormProvider> |
| 71 | + </> |
| 72 | + ) |
| 73 | +} |
| 74 | + |
| 75 | +const ShortAnswerInput = ({ id }) => { |
| 76 | + const { register, label, required, description } = useShortAnswerInput(id) |
| 77 | + |
| 78 | + return ( |
| 79 | + <> |
| 80 | + <label> |
| 81 | + <div className="block mb-2 font-bold">{label}</div> |
| 82 | + <div>{description}</div> |
| 83 | + <input |
| 84 | + className="p-4 w-full text-md bg-gray-100 rounded-lg placeholder:text-gray-700 focus:outline-none focus:bg-gray-200" |
| 85 | + type={label.toLocaleLowerCase() === 'email' ? 'email' : 'text'} |
| 86 | + {...register()} |
| 87 | + required={required} |
| 88 | + /> |
| 89 | + </label> |
| 90 | + </> |
| 91 | + ) |
| 92 | +} |
| 93 | + |
| 94 | +const LongAnswerInput = ({ id }) => { |
| 95 | + const { register, label, required, description } = useLongAnswerInput(id) |
| 96 | + |
| 97 | + return ( |
| 98 | + <> |
| 99 | + <label> |
| 100 | + <div className="block mb-2 font-bold">{label}</div> |
| 101 | + <div>{description}</div> |
| 102 | + <textarea |
| 103 | + className="p-4 text-gray-700 border-0 w-full text-md bg-gray-100 rounded-lg placeholder:text-gray-700 focus:outline-none focus:bg-gray-200" |
| 104 | + {...register()} |
| 105 | + required={required} |
| 106 | + /> |
| 107 | + </label> |
| 108 | + </> |
| 109 | + ) |
| 110 | +} |
| 111 | + |
| 112 | +const RadioInput = ({ id }) => { |
| 113 | + const { options, customOption, error, label, description, required } = useRadioInput(id) |
| 114 | + |
| 115 | + return ( |
| 116 | + <> |
| 117 | + <div className="block mb-2 font-bold">{label}</div> |
| 118 | + <div className="text-gray-600 text-sm">{description}</div> |
| 119 | + {options.map((o) => ( |
| 120 | + <div key={o.id} className="flex space-x-2"> |
| 121 | + <input type="radio" id={o.id} {...o.registerOption()} required={required} /> |
| 122 | + <label htmlFor={o.id}>{o.label}</label> |
| 123 | + </div> |
| 124 | + ))} |
| 125 | + {customOption && ( |
| 126 | + <div> |
| 127 | + <div className="flex space-x-2"> |
| 128 | + <input type="radio" id={customOption.id} {...customOption.registerOption()} /> |
| 129 | + <label htmlFor={customOption.id}>Your option</label> |
| 130 | + </div> |
| 131 | + <input |
| 132 | + className="p-4 block border-0 w-full text-md bg-gray-100 rounded-lg placeholder:text-gray-700 focus:outline-none focus:bg-gray-200" |
| 133 | + type="text" |
| 134 | + {...customOption.registerCustomInput()} |
| 135 | + /> |
| 136 | + </div> |
| 137 | + )} |
| 138 | + <div>{error && 'This field is required'}</div> |
| 139 | + </> |
| 140 | + ) |
| 141 | +} |
| 142 | + |
| 143 | +const DropdownInput = ({ id }) => { |
| 144 | + const { register, options, required, label, description } = useDropdownInput(id) |
| 145 | + |
| 146 | + return ( |
| 147 | + <div className="w-full"> |
| 148 | + <label className="block mb-2 font-bold">{label}</label> |
| 149 | + <div>{description}</div> |
| 150 | + <div |
| 151 | + className="appearance-none text-white inline-flex uppercase select-none font-thin relative whitespace-nowrap h-14 w-full outline-none overflow-hidden bg-white" |
| 152 | + style={{ |
| 153 | + minWidth: '160px', |
| 154 | + borderRadius: '6px', |
| 155 | + }} |
| 156 | + > |
| 157 | + <select |
| 158 | + className="appearance-none border border-black border-solid cursor-default text-black w-full p-4 rounded-lg font-bold bg-white" |
| 159 | + style={{ |
| 160 | + fontSize: '14px', |
| 161 | + fontFamily: 'inherit', |
| 162 | + wordSpacing: 'normal', |
| 163 | + transition: 'border-color 0.2s ease, background-color 0.2s ease', |
| 164 | + }} |
| 165 | + {...register()} |
| 166 | + required={required} |
| 167 | + > |
| 168 | + {options.map((o) => { |
| 169 | + return ( |
| 170 | + <option className="bg-black text-white" key={o.label} value={o.label}> |
| 171 | + {o.label} |
| 172 | + </option> |
| 173 | + ) |
| 174 | + })} |
| 175 | + </select> |
| 176 | + <div |
| 177 | + className="flex h-full right-0 pointer-events-none items-center absolute" |
| 178 | + style={{ |
| 179 | + width: '30px', |
| 180 | + transition: 'border 0.2s ease 0s', |
| 181 | + }} |
| 182 | + > |
| 183 | + <svg |
| 184 | + viewBox="0 0 24 24" |
| 185 | + width="18" |
| 186 | + height="18" |
| 187 | + stroke="black" |
| 188 | + strokeWidth="1.5" |
| 189 | + strokeLinecap="round" |
| 190 | + strokeLinejoin="round" |
| 191 | + fill="none" |
| 192 | + shapeRendering="geometricPrecision" |
| 193 | + > |
| 194 | + <path d="M6 9l6 6 6-6" /> |
| 195 | + </svg> |
| 196 | + </div> |
| 197 | + </div> |
| 198 | + </div> |
| 199 | + ) |
| 200 | +} |
| 201 | + |
| 202 | +const Questions = () => { |
| 203 | + return ( |
| 204 | + <div> |
| 205 | + {form.fields.map((field) => { |
| 206 | + const { id } = field |
| 207 | + |
| 208 | + let questionInput = null |
| 209 | + switch (field.type) { |
| 210 | + case 'RADIO': |
| 211 | + questionInput = <RadioInput id={id} /> |
| 212 | + break |
| 213 | + case 'SHORT_ANSWER': |
| 214 | + questionInput = <ShortAnswerInput id={id} /> |
| 215 | + break |
| 216 | + case 'LONG_ANSWER': |
| 217 | + questionInput = <LongAnswerInput id={id} /> |
| 218 | + break |
| 219 | + case 'DROPDOWN': |
| 220 | + questionInput = <DropdownInput id={id} /> |
| 221 | + break |
| 222 | + } |
| 223 | + |
| 224 | + if (!questionInput) { |
| 225 | + return null |
| 226 | + } |
| 227 | + |
| 228 | + return ( |
| 229 | + <div key={id} className="my-6"> |
| 230 | + {questionInput} |
| 231 | + </div> |
| 232 | + ) |
| 233 | + })} |
| 234 | + </div> |
| 235 | + ) |
| 236 | +} |
| 237 | + |
| 238 | +const getEmailId = () => { |
| 239 | + return form?.fields?.filter((res) => res.label.toLocaleLowerCase() === 'email')?.[0]?.id |
| 240 | +} |
| 241 | + |
| 242 | +const getNameId = () => { |
| 243 | + return form?.fields?.filter( |
| 244 | + (res) => res.label.toLocaleLowerCase() === 'first name' |
| 245 | + )?.[0]?.id |
| 246 | +} |
0 commit comments