Skip to content
Snippets Groups Projects
Commit 0d4999c1 authored by Tore.Brede's avatar Tore.Brede
Browse files

GREG-113: Various updates to make data sent from server appear correctly in form

parent 47a4573e
No related branches found
No related tags found
1 merge request!166GREG-113 guest date of birth
...@@ -47,9 +47,15 @@ const GuestRegisterStep = forwardRef( ...@@ -47,9 +47,15 @@ const GuestRegisterStep = forwardRef(
const { i18n, t } = useTranslation(['common']) const { i18n, t } = useTranslation(['common'])
const { nextHandler, guestData } = props const { nextHandler, guestData } = props
// For select-components it seems to be easier to tie them to a state
// and then handle the updating of the form using this, than to tie the
// components directly to the form-field using useForm
const [countryCode, setCountryCode] = useState< const [countryCode, setCountryCode] = useState<
CountryCallingCode | undefined CountryCallingCode | undefined
>(undefined) >(undefined)
const [passportNationality, setPassportNationality] = useState<
string | undefined
>(undefined)
const [idErrorState, setIdErrorState] = useState<string>('') const [idErrorState, setIdErrorState] = useState<string>('')
const submit: SubmitHandler<EnteredGuestData> = (data) => { const submit: SubmitHandler<EnteredGuestData> = (data) => {
...@@ -88,6 +94,14 @@ const GuestRegisterStep = forwardRef( ...@@ -88,6 +94,14 @@ const GuestRegisterStep = forwardRef(
} = useForm<EnteredGuestData>() } = useForm<EnteredGuestData>()
const onSubmit = handleSubmit<EnteredGuestData>(submit) const onSubmit = handleSubmit<EnteredGuestData>(submit)
const handlePassportNationalityChange = (event: SelectChangeEvent) => {
if (event.target.value) {
const passportValue = event.target.value as string
setValue('passportNationality', passportValue)
setPassportNationality(passportValue)
}
}
const handleCountryCodeChange = (event: SelectChangeEvent) => { const handleCountryCodeChange = (event: SelectChangeEvent) => {
if (event.target.value) { if (event.target.value) {
const countryCodeType = event.target.value as CountryCode const countryCodeType = event.target.value as CountryCode
...@@ -130,18 +144,38 @@ const GuestRegisterStep = forwardRef( ...@@ -130,18 +144,38 @@ const GuestRegisterStep = forwardRef(
const maxBirthDate = subYears(1)(today) const maxBirthDate = subYears(1)(today)
useEffect(() => { useEffect(() => {
// Take values coming from the server, if present, and insert them into the form // Take values coming from the server, if present, and insert them into the form.
setCountryCode(guestData.countryForCallingCode) // This effect has guestData as a dependency, so the data will be reloaded
setValue( // if guestData changes
'mobilePhoneCountry',
guestData.countryForCallingCode ? guestData.countryForCallingCode : ''
)
setValue('firstName', guestData.first_name) setValue('firstName', guestData.first_name)
setValue('lastName', guestData.last_name) setValue('lastName', guestData.last_name)
setValue( setValue(
'mobilePhone', 'mobilePhone',
guestData.mobile_phone ? guestData.mobile_phone : '' guestData.mobile_phone ? guestData.mobile_phone : ''
) )
setValue(
'nationalIdNumber',
guestData.fnr === undefined ? '' : guestData.fnr
)
setValue(
'passportNationality',
guestData.passportNationality === undefined
? ''
: guestData.passportNationality
)
setPassportNationality(guestData.passportNationality)
setValue(
'passportNumber',
guestData.passport === undefined ? '' : guestData.passport
)
setCountryCode(guestData.countryForCallingCode)
setValue(
'mobilePhoneCountry',
guestData.countryForCallingCode ? guestData.countryForCallingCode : ''
)
setValue('dateOfBirth', guestData.dateOfBirth) setValue('dateOfBirth', guestData.dateOfBirth)
}, [guestData]) }, [guestData])
...@@ -150,6 +184,7 @@ const GuestRegisterStep = forwardRef( ...@@ -150,6 +184,7 @@ const GuestRegisterStep = forwardRef(
} }
register('mobilePhoneCountry') register('mobilePhoneCountry')
register('passportNationality')
useImperativeHandle(ref, () => ({ doSubmit })) useImperativeHandle(ref, () => ({ doSubmit }))
...@@ -337,6 +372,7 @@ const GuestRegisterStep = forwardRef( ...@@ -337,6 +372,7 @@ const GuestRegisterStep = forwardRef(
rules={{ rules={{
required: true, required: true,
}} }}
defaultValue=""
render={({ field }) => ( render={({ field }) => (
<TextField <TextField
sx={{ flexGrow: 2 }} sx={{ flexGrow: 2 }}
...@@ -355,27 +391,43 @@ const GuestRegisterStep = forwardRef( ...@@ -355,27 +391,43 @@ const GuestRegisterStep = forwardRef(
AuthenticationMethod.Invite && ( AuthenticationMethod.Invite && (
<> <>
{/* The guest should fill in one of national ID number or passport number */} {/* The guest should fill in one of national ID number or passport number */}
<TextField <Controller
id="national_id_number" name="nationalIdNumber"
label={t('input.nationalIdNumber')} control={control}
error={!!errors.nationalIdNumber} defaultValue=""
helperText={ rules={{
errors.nationalIdNumber && errors.nationalIdNumber.message
}
{...register('nationalIdNumber', {
// It is not required that the national ID number be filled in, the guest may not have // It is not required that the national ID number be filled in, the guest may not have
// one, so allow empty values for the validation to pass // one, so allow empty values for the validation to pass
validate: (value) => isValidFnr(value, true), validate: (value) => isValidFnr(value, true),
})} }}
render={({ field }) => (
<TextField
id="nationalIdNumber"
label={t('input.nationalIdNumber')}
error={!!errors.nationalIdNumber}
value={field.value}
onChange={field.onChange}
helperText={
errors.nationalIdNumber &&
errors.nationalIdNumber.message
}
/>
)}
/> />
<TextField <Controller
id="passport-number-id" name="passportNumber"
data-testid="passport_number_input" control={control}
label={t('input.passportNumber')} defaultValue=""
{...register('passportNumber', { render={({ field }) => (
required: false, <TextField
})} id="passportNumber"
data-testid="passport_number_input"
value={field.value}
label={t('input.passportNumber')}
onChange={field.onChange}
/>
)}
/> />
<Select <Select
...@@ -387,9 +439,12 @@ const GuestRegisterStep = forwardRef( ...@@ -387,9 +439,12 @@ const GuestRegisterStep = forwardRef(
label={t('input.passportNationality')} label={t('input.passportNationality')}
displayEmpty displayEmpty
defaultValue="" defaultValue=""
{...register('passportNationality', { value={
required: false, passportNationality === undefined
})} ? ''
: passportNationality
}
onChange={handlePassportNationalityChange}
renderValue={(selected: any) => { renderValue={(selected: any) => {
if (!selected || selected.length === 0) { if (!selected || selected.length === 0) {
return t('input.passportNationality') return t('input.passportNationality')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment