|
| 1 | +import { |
| 2 | + Checkbox, |
| 3 | + Container, |
| 4 | + InputSection, |
| 5 | + RadioInput, |
| 6 | + TextArea, |
| 7 | +} from '@ifrc-go/ui'; |
| 8 | +import { useTranslation } from '@ifrc-go/ui/hooks'; |
| 9 | +import { |
| 10 | + isDefined, |
| 11 | + randomString, |
| 12 | +} from '@togglecorp/fujs'; |
| 13 | +import { |
| 14 | + type ArrayError, |
| 15 | + getErrorObject, |
| 16 | + type PartialForm, |
| 17 | + type SetValueArg, |
| 18 | + useFormObject, |
| 19 | +} from '@togglecorp/toggle-form'; |
| 20 | + |
| 21 | +import useGlobalEnums from '#hooks/domain/useGlobalEnums'; |
| 22 | +import { type GoApiResponse } from '#utils/restRequest'; |
| 23 | + |
| 24 | +import { type EruType } from '../schema'; |
| 25 | + |
| 26 | +import i18n from './i18n.json'; |
| 27 | +import styles from './styles.module.css'; |
| 28 | + |
| 29 | +type GlobalEnumsResponse = GoApiResponse<'/api/v2/global-enums/'>; |
| 30 | + |
| 31 | +type ReadinessOption = NonNullable<GlobalEnumsResponse['deployments_eru_readiness_status']>[number]; |
| 32 | + |
| 33 | +function readinessKeySelector(option: ReadinessOption) { |
| 34 | + return option.key; |
| 35 | +} |
| 36 | + |
| 37 | +function readinessLabelSelector(option: ReadinessOption) { |
| 38 | + return option.value; |
| 39 | +} |
| 40 | + |
| 41 | +const defaultCollectionValue: PartialForm<EruType> = { |
| 42 | + client_id: randomString(), |
| 43 | +}; |
| 44 | + |
| 45 | +interface Props { |
| 46 | + index: number; |
| 47 | + value: PartialForm<EruType>; |
| 48 | + onChange: (value: SetValueArg<PartialForm<EruType>>, index: number) => void; |
| 49 | + title: Record<string, string> | undefined; |
| 50 | + error: ArrayError<EruType> | undefined; |
| 51 | +} |
| 52 | + |
| 53 | +function EruInputItem(props: Props) { |
| 54 | + const { |
| 55 | + index, |
| 56 | + value, |
| 57 | + onChange, |
| 58 | + title, |
| 59 | + error: errorFromProps, |
| 60 | + } = props; |
| 61 | + |
| 62 | + const strings = useTranslation(i18n); |
| 63 | + |
| 64 | + const { |
| 65 | + deployments_eru_readiness_status, |
| 66 | + } = useGlobalEnums(); |
| 67 | + |
| 68 | + const onFieldChange = useFormObject( |
| 69 | + index, |
| 70 | + onChange, |
| 71 | + defaultCollectionValue, |
| 72 | + ); |
| 73 | + const eruTypeLabel = isDefined(value.type) |
| 74 | + ? title?.[value.type] |
| 75 | + : undefined; |
| 76 | + const error = (value && value.client_id && errorFromProps) |
| 77 | + ? getErrorObject(errorFromProps?.[value.client_id]) |
| 78 | + : undefined; |
| 79 | + |
| 80 | + return ( |
| 81 | + <Container |
| 82 | + childrenContainerClassName={styles.eruTypes} |
| 83 | + heading={eruTypeLabel} |
| 84 | + > |
| 85 | + <InputSection |
| 86 | + className={styles.readinessOptions} |
| 87 | + title={strings.eruEquipmentReadiness} |
| 88 | + > |
| 89 | + <RadioInput |
| 90 | + listContainerClassName={styles.readinessList} |
| 91 | + name="equipment_readiness" |
| 92 | + value={value.equipment_readiness} |
| 93 | + onChange={onFieldChange} |
| 94 | + options={deployments_eru_readiness_status} |
| 95 | + keySelector={readinessKeySelector} |
| 96 | + labelSelector={readinessLabelSelector} |
| 97 | + error={error?.equipment_readiness} |
| 98 | + /> |
| 99 | + </InputSection> |
| 100 | + <InputSection |
| 101 | + className={styles.readinessOptions} |
| 102 | + title={strings.eruPeopleReadiness} |
| 103 | + > |
| 104 | + <RadioInput |
| 105 | + listContainerClassName={styles.readinessList} |
| 106 | + name="people_readiness" |
| 107 | + value={value.people_readiness} |
| 108 | + onChange={onFieldChange} |
| 109 | + options={deployments_eru_readiness_status} |
| 110 | + keySelector={readinessKeySelector} |
| 111 | + labelSelector={readinessLabelSelector} |
| 112 | + error={error?.people_readiness} |
| 113 | + /> |
| 114 | + </InputSection> |
| 115 | + <InputSection |
| 116 | + className={styles.readinessOptions} |
| 117 | + title={strings.eruFundingReadiness} |
| 118 | + > |
| 119 | + <RadioInput |
| 120 | + listContainerClassName={styles.readinessList} |
| 121 | + name="funding_readiness" |
| 122 | + value={value.funding_readiness} |
| 123 | + onChange={onFieldChange} |
| 124 | + options={deployments_eru_readiness_status} |
| 125 | + keySelector={readinessKeySelector} |
| 126 | + labelSelector={readinessLabelSelector} |
| 127 | + error={error?.funding_readiness} |
| 128 | + /> |
| 129 | + </InputSection> |
| 130 | + <InputSection |
| 131 | + className={styles.readinessOptions} |
| 132 | + title={strings.eruComments} |
| 133 | + > |
| 134 | + <TextArea |
| 135 | + name="comment" |
| 136 | + value={value?.comment} |
| 137 | + onChange={onFieldChange} |
| 138 | + error={error?.comment} |
| 139 | + /> |
| 140 | + <Checkbox |
| 141 | + label={strings.eruLead} |
| 142 | + name="has_capacity_to_lead" |
| 143 | + value={value.has_capacity_to_lead} |
| 144 | + onChange={onFieldChange} |
| 145 | + error={error?.has_capacity_to_lead} |
| 146 | + /> |
| 147 | + <Checkbox |
| 148 | + label={strings.eruSupport} |
| 149 | + name="has_capacity_to_support" |
| 150 | + value={value.has_capacity_to_support} |
| 151 | + onChange={onFieldChange} |
| 152 | + error={error?.has_capacity_to_support} |
| 153 | + /> |
| 154 | + </InputSection> |
| 155 | + </Container> |
| 156 | + |
| 157 | + ); |
| 158 | +} |
| 159 | + |
| 160 | +export default EruInputItem; |
0 commit comments