Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(app-sys): national-id-with-name fetch form more endpoints #17101

Merged
merged 18 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions libs/application/core/src/lib/fieldBuilders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,12 @@ export const buildNationalIdWithNameField = (
nameDefaultValue,
errorMessage,
minAgePerson,
searchPersons,
searchCompanies,
titleVariant,
description,
marginTop,
marginBottom,
} = data
return {
...extractCommonFields(data),
Expand All @@ -761,9 +767,15 @@ export const buildNationalIdWithNameField = (
nameDefaultValue,
errorMessage,
minAgePerson,
searchPersons,
searchCompanies,
children: undefined,
type: FieldTypes.NATIONAL_ID_WITH_NAME,
component: FieldComponents.NATIONAL_ID_WITH_NAME,
titleVariant,
description,
marginTop,
marginBottom,
}
}

Expand Down
5 changes: 5 additions & 0 deletions libs/application/types/src/lib/Fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,11 @@ export interface NationalIdWithNameField extends InputField {
nameDefaultValue?: string
errorMessage?: string
minAgePerson?: number
searchPersons?: boolean
searchCompanies?: boolean
titleVariant?: TitleVariants
marginTop?: ResponsiveProp<Space>
marginBottom?: ResponsiveProp<Space>
}

type Modify<T, R> = Omit<T, keyof R> & R
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FC, useEffect, useState } from 'react'
import { Box, GridRow, GridColumn } from '@island.is/island-ui/core'
import { GridRow, GridColumn } from '@island.is/island-ui/core'
import { useLocale } from '@island.is/localization'
import {
coreErrorMessages,
Expand All @@ -8,12 +8,16 @@ import {
} from '@island.is/application/core'
import { Application, StaticText } from '@island.is/application/types'
import { gql, useLazyQuery } from '@apollo/client'
import { IdentityInput, Query } from '@island.is/api/schema'
import {
IdentityInput,
Query,
RskCompanyInfoInput,
} from '@island.is/api/schema'
import { InputController } from '@island.is/shared/form-fields'
import { useFormContext } from 'react-hook-form'
import * as kennitala from 'kennitala'
import debounce from 'lodash/debounce'
import { IDENTITY_QUERY } from './graphql/queries'
import { COMPANY_IDENTITY_QUERY, IDENTITY_QUERY } from './graphql/queries'

interface NationalIdWithNameProps {
id: string
Expand All @@ -29,6 +33,8 @@ interface NationalIdWithNameProps {
nameDefaultValue?: string
errorMessage?: string
minAgePerson?: number
searchPersons?: boolean
searchCompanies?: boolean
}

export const NationalIdWithName: FC<
Expand All @@ -47,6 +53,8 @@ export const NationalIdWithName: FC<
nameDefaultValue,
errorMessage,
minAgePerson,
searchPersons = true,
searchCompanies = false,
}) => {
const fieldId = customId.length > 0 ? customId : id
const nameField = `${fieldId}.name`
Expand All @@ -58,6 +66,8 @@ export const NationalIdWithName: FC<
formState: { errors },
} = useFormContext()
const [nationalIdInput, setNationalIdInput] = useState('')
const [personName, setPersonName] = useState('')
const [companyName, setCompanyName] = useState('')

// get name validation errors
const nameFieldErrors = errorMessage
Expand Down Expand Up @@ -101,23 +111,73 @@ export const NationalIdWithName: FC<
{
onCompleted: (data) => {
onNameChange && onNameChange(data.identity?.name ?? '')
setValue(nameField, data.identity?.name ?? undefined)
setPersonName(data.identity?.name ?? '')
},
},
)

// query to get company name by national id
const [
getCompanyIdentity,
{
data: companyData,
loading: companyQueryLoading,
error: companyQueryError,
},
] = useLazyQuery<Query, { input: RskCompanyInfoInput }>(
gql`
${COMPANY_IDENTITY_QUERY}
`,
{
onCompleted: (companyData) => {
onNameChange &&
onNameChange(companyData.companyRegistryCompany?.name ?? '')
setCompanyName(companyData.companyRegistryCompany?.name ?? '')
},
},
)

// fetch and update name when user has entered a valid national id
useEffect(() => {
if (nationalIdInput.length === 10 && kennitala.isValid(nationalIdInput)) {
getIdentity({
variables: {
input: {
nationalId: nationalIdInput,
},
},
})
{
searchPersons &&
getIdentity({
variables: {
input: {
nationalId: nationalIdInput,
},
},
})
}

{
searchCompanies &&
getCompanyIdentity({
variables: {
input: {
nationalId: nationalIdInput,
},
},
})
}
}
}, [
nationalIdInput,
getIdentity,
getCompanyIdentity,
searchPersons,
searchCompanies,
])

useEffect(() => {
const nameInAnswers = getValueViaPath(application.answers, nameField)
if (personName && nameInAnswers !== personName) {
setValue(nameField, personName)
} else if (companyName && nameInAnswers !== companyName) {
setValue(nameField, companyName)
}
}, [nationalIdInput, getIdentity])
}, [personName, companyName, setValue, nameField])
jonnigs marked this conversation as resolved.
Show resolved Hide resolved

return (
<GridRow>
Expand All @@ -138,7 +198,7 @@ export const NationalIdWithName: FC<
onNationalIdChange &&
onNationalIdChange(v.target.value.replace(/\W/g, ''))
})}
loading={queryLoading}
loading={searchPersons ? queryLoading : companyQueryLoading}
error={nationalIdFieldErrors}
disabled={disabled}
/>
Expand All @@ -154,12 +214,23 @@ export const NationalIdWithName: FC<
}
required={required}
error={
queryError || data?.identity === null
? formatMessage(
coreErrorMessages.nationalRegistryNameNotFoundForNationalId,
)
: nameFieldErrors && !data
? nameFieldErrors
searchPersons
? queryError || data?.identity === null
? formatMessage(
coreErrorMessages.nationalRegistryNameNotFoundForNationalId,
)
: nameFieldErrors && !data
? nameFieldErrors
: undefined
: searchCompanies
? companyQueryError ||
companyData?.companyRegistryCompany === null
? formatMessage(
coreErrorMessages.nationalRegistryNameNotFoundForNationalId,
)
: nameFieldErrors && !companyData
? nameFieldErrors
: undefined
jonnigs marked this conversation as resolved.
Show resolved Hide resolved
: undefined
}
disabled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,11 @@ export const IDENTITY_QUERY = `
}
}
`

export const COMPANY_IDENTITY_QUERY = `
query CompanyIdentityQuery($input: RskCompanyInfoInput!) {
companyRegistryCompany(input: $input) {
name
}
}
`
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { buildFieldRequired } from '@island.is/application/core'
import { FC } from 'react'
import {
buildFieldRequired,
formatTextWithLocale,
} from '@island.is/application/core'
import {
FieldBaseProps,
NationalIdWithNameField,
} from '@island.is/application/types'
import { NationalIdWithName } from '@island.is/application/ui-components'
import { FC } from 'react'
import { Box, Text } from '@island.is/island-ui/core'
import { FieldDescription } from '@island.is/shared/form-fields'
import { Locale } from '@island.is/shared/types'
import { useLocale } from '@island.is/localization'

interface Props extends FieldBaseProps {
field: NationalIdWithNameField
Expand All @@ -13,20 +20,46 @@ interface Props extends FieldBaseProps {
export const NationalIdWithNameFormField: FC<
React.PropsWithChildren<Props>
> = ({ application, field }) => {
const { formatMessage, lang: locale } = useLocale()

return (
<NationalIdWithName
id={field.id}
application={application}
disabled={field.disabled}
required={buildFieldRequired(application, field.required)}
customNationalIdLabel={field.customNationalIdLabel}
customNameLabel={field.customNameLabel}
onNationalIdChange={field.onNationalIdChange}
onNameChange={field.onNameChange}
nationalIdDefaultValue={field.nationalIdDefaultValue}
nameDefaultValue={field.nameDefaultValue}
errorMessage={field.errorMessage}
minAgePerson={field.minAgePerson}
/>
<Box marginTop={field.marginTop} marginBottom={field.marginBottom}>
{field.title && (
<Text variant={field.titleVariant ?? 'h3'} marginBottom={2}>
{formatTextWithLocale(
field.title,
application,
locale as Locale,
formatMessage,
)}
</Text>
)}
{field.description && (
<FieldDescription
description={formatTextWithLocale(
field.description,
application,
locale as Locale,
formatMessage,
)}
/>
)}
<NationalIdWithName
id={field.id}
application={application}
disabled={field.disabled}
required={buildFieldRequired(application, field.required)}
customNationalIdLabel={field.customNationalIdLabel}
customNameLabel={field.customNameLabel}
onNationalIdChange={field.onNationalIdChange}
onNameChange={field.onNameChange}
nationalIdDefaultValue={field.nationalIdDefaultValue}
nameDefaultValue={field.nameDefaultValue}
errorMessage={field.errorMessage}
minAgePerson={field.minAgePerson}
searchPersons={field.searchPersons}
searchCompanies={field.searchCompanies}
/>
</Box>
)
}
Loading