|
| 1 | +import { |
| 2 | + useCallback, |
| 3 | + useMemo, |
| 4 | +} from 'react'; |
| 5 | +import { |
| 6 | + Button, |
| 7 | + Container, |
| 8 | + DateInput, |
| 9 | + InputSection, |
| 10 | + MultiSelectInput, |
| 11 | + RadioInput, |
| 12 | + TextInput, |
| 13 | +} from '@ifrc-go/ui'; |
1 | 14 | import { useTranslation } from '@ifrc-go/ui/hooks'; |
| 15 | +import { |
| 16 | + numericIdSelector, |
| 17 | + stringNameSelector, |
| 18 | +} from '@ifrc-go/ui/utils'; |
| 19 | +import { |
| 20 | + createSubmitHandler, |
| 21 | + getErrorObject, |
| 22 | + useForm, |
| 23 | +} from '@togglecorp/toggle-form'; |
2 | 24 |
|
| 25 | +import CountrySelectInput from '#components/domain/CountrySelectInput'; |
| 26 | +import DisasterTypeSelectInput from '#components/domain/DisasterTypeSelectInput'; |
| 27 | +import NationalSocietySelectInput from '#components/domain/NationalSocietySelectInput'; |
3 | 28 | import Page from '#components/Page'; |
4 | 29 |
|
| 30 | +import { |
| 31 | + type EapRegistrationInput, |
| 32 | + formSchema, |
| 33 | +} from './schema'; |
| 34 | + |
5 | 35 | import i18n from './i18n.json'; |
| 36 | +import styles from './styles.module.css'; |
| 37 | + |
| 38 | +interface Props { |
| 39 | + disabled?: boolean; |
| 40 | + eap: Partial<EapRegistrationInput>; |
| 41 | +} |
| 42 | + |
| 43 | +const eapFormOptions = [ |
| 44 | + { value: 'full', label: 'Full application' }, |
| 45 | + { value: 'simplified', label: 'Simplified application' }, |
| 46 | + { value: 'not_sure', label: 'Not Sure' }, |
| 47 | +]; |
6 | 48 |
|
7 | 49 | /** @knipignore */ |
8 | 50 | // eslint-disable-next-line import/prefer-default-export |
9 | | -export function Component() { |
| 51 | +export function Component(props: Props) { |
| 52 | + const { |
| 53 | + eap, |
| 54 | + disabled, |
| 55 | + } = props; |
| 56 | + |
10 | 57 | const strings = useTranslation(i18n); |
| 58 | + const defaultFormValue = useMemo(() => ({ |
| 59 | + national_society: eap?.national_society, |
| 60 | + country: eap?.country, |
| 61 | + disaster_type: eap?.disaster_type, |
| 62 | + eap_type: eap?.eap_type, |
| 63 | + eap_start_date: eap?.eap_start_date, |
| 64 | + eap_end_date: eap?.eap_end_date, |
| 65 | + partner_involved: eap?.partner_involved, |
| 66 | + eap_national_society_name: eap?.eap_national_society_name, |
| 67 | + eap_national_society_title: eap?.eap_national_society_title, |
| 68 | + eap_national_society_email: eap?.eap_national_society_email, |
| 69 | + eap_national_society_phone_number: eap?.eap_national_society_phone_number, |
| 70 | + eap_ifrc_name: eap?.eap_ifrc_name, |
| 71 | + eap_ifrc_title: eap?.eap_ifrc_title, |
| 72 | + eap_ifrc_email: eap?.eap_ifrc_email, |
| 73 | + eap_ifrc_phone_number: eap?.eap_ifrc_phone_number, |
| 74 | + eap_focal_point_name: eap?.eap_focal_point_name, |
| 75 | + eap_focal_point_title: eap?.eap_focal_point_title, |
| 76 | + eap_focal_point_email: eap?.eap_focal_point_email, |
| 77 | + eap_focal_point_phone_number: eap?.eap_focal_point_phone_number, |
| 78 | + }), [eap]); |
| 79 | + |
| 80 | + const { |
| 81 | + value, |
| 82 | + setFieldValue, |
| 83 | + error: formError, |
| 84 | + setError, |
| 85 | + validate, |
| 86 | + } = useForm(formSchema, { value: defaultFormValue }); |
| 87 | + |
| 88 | + const error = getErrorObject(formError); |
| 89 | + |
| 90 | + const handleCountryChange = useCallback( |
| 91 | + (val: number | undefined, name: 'country') => { |
| 92 | + setFieldValue(val, name); |
| 93 | + }, |
| 94 | + [setFieldValue], |
| 95 | + ); |
| 96 | + |
| 97 | + const eapRegistration = useCallback(() => { |
| 98 | + const handler = createSubmitHandler( |
| 99 | + validate, |
| 100 | + setError, |
| 101 | + (val) => { |
| 102 | + // FIXME: Remove this after API integration |
| 103 | + // eslint-disable-next-line no-console |
| 104 | + console.log('value', val); |
| 105 | + }, |
| 106 | + ); |
| 107 | + handler(); |
| 108 | + }, [ |
| 109 | + setError, |
| 110 | + validate, |
| 111 | + ]); |
| 112 | + |
| 113 | + const handleFormSubmit = createSubmitHandler(validate, setError, eapRegistration); |
11 | 114 |
|
12 | 115 | return ( |
13 | 116 | <Page |
| 117 | + mainSectionClassName={styles.eapRegistrationForm} |
14 | 118 | heading={strings.eapRegistrationHeading} |
15 | 119 | description={strings.eapRegistrationDescription} |
| 120 | + withBackgroundColorInMainSection |
16 | 121 | > |
17 | | - {/* TODO: Add the form */} |
18 | | - Application Details |
| 122 | + <Container |
| 123 | + heading={strings.eapApplicationDetails} |
| 124 | + childrenContainerClassName={styles.content} |
| 125 | + > |
| 126 | + <InputSection |
| 127 | + title={strings.eapNationalSociety} |
| 128 | + description={strings.eapNationalSocietyDescription} |
| 129 | + withAsteriskOnTitle |
| 130 | + > |
| 131 | + <NationalSocietySelectInput |
| 132 | + error={error?.national_society} |
| 133 | + name="national_society" |
| 134 | + onChange={setFieldValue} |
| 135 | + value={value?.national_society} |
| 136 | + disabled={disabled} |
| 137 | + /> |
| 138 | + </InputSection> |
| 139 | + <InputSection |
| 140 | + title={strings.eapCountry} |
| 141 | + description={strings.eapCountryDescription} |
| 142 | + withAsteriskOnTitle |
| 143 | + > |
| 144 | + <CountrySelectInput |
| 145 | + error={error?.country} |
| 146 | + name="country" |
| 147 | + onChange={handleCountryChange} |
| 148 | + value={value?.country} |
| 149 | + disabled={disabled} |
| 150 | + /> |
| 151 | + </InputSection> |
| 152 | + <InputSection |
| 153 | + title={strings.eapDisasterType} |
| 154 | + description={strings.eapDisasterTypeDescription} |
| 155 | + withAsteriskOnTitle |
| 156 | + > |
| 157 | + <DisasterTypeSelectInput |
| 158 | + name="disaster_type" |
| 159 | + value={value?.disaster_type} |
| 160 | + onChange={setFieldValue} |
| 161 | + error={error?.disaster_type} |
| 162 | + disabled={disabled} |
| 163 | + /> |
| 164 | + </InputSection> |
| 165 | + <InputSection |
| 166 | + title={strings.eapType} |
| 167 | + // TODO: Add link here |
| 168 | + description={strings.eapTypeDescription} |
| 169 | + withAsteriskOnTitle |
| 170 | + > |
| 171 | + <RadioInput |
| 172 | + name="eap_type" |
| 173 | + value={value?.eap_type} |
| 174 | + onChange={setFieldValue} |
| 175 | + options={eapFormOptions} |
| 176 | + keySelector={(option) => option.value} |
| 177 | + labelSelector={(option) => option.label} |
| 178 | + error={error?.eap_type} |
| 179 | + /> |
| 180 | + </InputSection> |
| 181 | + <InputSection |
| 182 | + title={strings.eapSubmission} |
| 183 | + description={strings.eapSubmissionDescription} |
| 184 | + withAsteriskOnTitle |
| 185 | + numPreferredColumns={2} |
| 186 | + > |
| 187 | + <DateInput |
| 188 | + name="eap_start_date" |
| 189 | + onChange={setFieldValue} |
| 190 | + value={value?.eap_start_date} |
| 191 | + /> |
| 192 | + <DateInput |
| 193 | + name="eap_end_date" |
| 194 | + onChange={setFieldValue} |
| 195 | + value={value?.eap_end_date} |
| 196 | + /> |
| 197 | + </InputSection> |
| 198 | + <InputSection |
| 199 | + title={strings.eapPartnersInvolved} |
| 200 | + description={strings.eapPartnersInvolvedDescription} |
| 201 | + withAsteriskOnTitle |
| 202 | + > |
| 203 | + <MultiSelectInput |
| 204 | + name="partner_involved" |
| 205 | + value={value?.partner_involved} |
| 206 | + onChange={setFieldValue} |
| 207 | + keySelector={numericIdSelector} |
| 208 | + labelSelector={stringNameSelector} |
| 209 | + options={[]} |
| 210 | + /> |
| 211 | + </InputSection> |
| 212 | + </Container> |
| 213 | + <Container |
| 214 | + heading={strings.eapContacts} |
| 215 | + > |
| 216 | + <InputSection |
| 217 | + title={strings.eapNSContact} |
| 218 | + description={strings.eapNSContactDescription} |
| 219 | + numPreferredColumns={2} |
| 220 | + > |
| 221 | + <TextInput |
| 222 | + label={strings.eapNSName} |
| 223 | + name="eap_national_society_name" |
| 224 | + value={value?.eap_national_society_name} |
| 225 | + onChange={setFieldValue} |
| 226 | + error={error?.eap_national_society_name} |
| 227 | + disabled={disabled} |
| 228 | + /> |
| 229 | + <TextInput |
| 230 | + label={strings.eapNSTitle} |
| 231 | + name="eap_national_society_title" |
| 232 | + value={value?.eap_national_society_title} |
| 233 | + onChange={setFieldValue} |
| 234 | + error={error?.eap_national_society_title} |
| 235 | + disabled={disabled} |
| 236 | + /> |
| 237 | + <TextInput |
| 238 | + label={strings.eapNSEmail} |
| 239 | + name="eap_national_society_email" |
| 240 | + value={value?.eap_national_society_email} |
| 241 | + onChange={setFieldValue} |
| 242 | + error={error?.eap_national_society_email} |
| 243 | + disabled={disabled} |
| 244 | + /> |
| 245 | + <TextInput |
| 246 | + label={strings.eapNSPhoneNumber} |
| 247 | + name="eap_national_society_phone_number" |
| 248 | + value={value?.eap_national_society_phone_number} |
| 249 | + onChange={setFieldValue} |
| 250 | + error={error?.eap_national_society_phone_number} |
| 251 | + disabled={disabled} |
| 252 | + /> |
| 253 | + </InputSection> |
| 254 | + <InputSection |
| 255 | + title={strings.eapIFRCContact} |
| 256 | + description={strings.eapIFRCContactDescription} |
| 257 | + numPreferredColumns={2} |
| 258 | + > |
| 259 | + <TextInput |
| 260 | + label={strings.eapIFRCName} |
| 261 | + name="eap_ifrc_name" |
| 262 | + value={value?.eap_ifrc_name} |
| 263 | + onChange={setFieldValue} |
| 264 | + error={error?.eap_ifrc_name} |
| 265 | + disabled={disabled} |
| 266 | + /> |
| 267 | + <TextInput |
| 268 | + label={strings.eapIFRCTitle} |
| 269 | + name="eap_ifrc_title" |
| 270 | + value={value?.eap_ifrc_title} |
| 271 | + onChange={setFieldValue} |
| 272 | + error={error?.eap_ifrc_title} |
| 273 | + disabled={disabled} |
| 274 | + /> |
| 275 | + <TextInput |
| 276 | + label={strings.eapIFRCEmail} |
| 277 | + name="eap_ifrc_email" |
| 278 | + value={value?.eap_ifrc_email} |
| 279 | + onChange={setFieldValue} |
| 280 | + error={error?.eap_ifrc_email} |
| 281 | + disabled={disabled} |
| 282 | + /> |
| 283 | + <TextInput |
| 284 | + label={strings.eapIFRCPhoneNumber} |
| 285 | + name="eap_ifrc_phone_number" |
| 286 | + value={value?.eap_ifrc_phone_number} |
| 287 | + onChange={setFieldValue} |
| 288 | + error={error?.eap_ifrc_phone_number} |
| 289 | + disabled={disabled} |
| 290 | + /> |
| 291 | + </InputSection> |
| 292 | + <InputSection |
| 293 | + title={strings.eapFocalPoint} |
| 294 | + description={strings.eapFocalPointDescription} |
| 295 | + numPreferredColumns={2} |
| 296 | + > |
| 297 | + <TextInput |
| 298 | + label={strings.eapFocalPointName} |
| 299 | + name="eap_focal_point_name" |
| 300 | + value={value?.eap_focal_point_name} |
| 301 | + onChange={setFieldValue} |
| 302 | + error={error?.eap_focal_point_name} |
| 303 | + disabled={disabled} |
| 304 | + /> |
| 305 | + <TextInput |
| 306 | + label={strings.eapFocalPointTitle} |
| 307 | + name="eap_focal_point_title" |
| 308 | + value={value?.eap_focal_point_title} |
| 309 | + onChange={setFieldValue} |
| 310 | + error={error?.eap_focal_point_title} |
| 311 | + disabled={disabled} |
| 312 | + /> |
| 313 | + <TextInput |
| 314 | + label={strings.eapFocalPointEmail} |
| 315 | + name="eap_focal_point_email" |
| 316 | + value={value?.eap_focal_point_email} |
| 317 | + onChange={setFieldValue} |
| 318 | + error={error?.eap_focal_point_email} |
| 319 | + disabled={disabled} |
| 320 | + /> |
| 321 | + <TextInput |
| 322 | + label={strings.eapFocalPointPhoneNumber} |
| 323 | + name="eap_focal_point_phone_number" |
| 324 | + value={value?.eap_focal_point_phone_number} |
| 325 | + onChange={setFieldValue} |
| 326 | + error={error?.eap_focal_point_phone_number} |
| 327 | + disabled={disabled} |
| 328 | + /> |
| 329 | + </InputSection> |
| 330 | + </Container> |
| 331 | + <div className={styles.footer}> |
| 332 | + <Button |
| 333 | + name={undefined} |
| 334 | + onClick={handleFormSubmit} |
| 335 | + type="submit" |
| 336 | + variant="secondary" |
| 337 | + disabled={disabled} |
| 338 | + > |
| 339 | + {strings.eapSubmitButton} |
| 340 | + </Button> |
| 341 | + </div> |
19 | 342 | </Page> |
20 | 343 | ); |
21 | 344 | } |
0 commit comments