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

add datamodelbinding for surname for personlookup #2866

Merged
merged 7 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
27 changes: 14 additions & 13 deletions src/layout/PersonLookup/PersonLookupComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { useBindingValidationsForNode } from 'src/features/validation/selectors/
import { hasValidationErrors } from 'src/features/validation/utils';
import { ComponentStructureWrapper } from 'src/layout/ComponentStructureWrapper';
import classes from 'src/layout/PersonLookup/PersonLookupComponent.module.css';
import { validateName, validatePersonLookupResponse, validateSsn } from 'src/layout/PersonLookup/validation';
import { validatePersonLookupResponse, validateSsn } from 'src/layout/PersonLookup/validation';
import { useLabel } from 'src/utils/layout/useLabel';
import { useNodeItem } from 'src/utils/layout/useNodeItem';
import { httpPost } from 'src/utils/network/networking';
Expand All @@ -37,7 +37,9 @@ const personLookupQueries = {
};

export type Person = {
name: string;
firstName: string;
lastName: string;
middleName: string;
ssn: string;
};
export type PersonLookupResponse = { success: false; personDetails: null } | { success: true; personDetails: Person };
Expand Down Expand Up @@ -83,7 +85,7 @@ export function PersonLookupComponent({ node, overrideDisplay }: PropsFromGeneri
const [tempSsn, setTempSsn] = useState('');
const [tempName, setTempName] = useState('');
const [ssnErrors, setSsnErrors] = useState<string[]>();
const [nameErrors, setNameErrors] = useState<string[]>();
const [nameError, setNameError] = useState<string>();

const bindingValidations = useBindingValidationsForNode(node);
const { langAsString } = useLanguage();
Expand All @@ -95,16 +97,11 @@ export function PersonLookupComponent({ node, overrideDisplay }: PropsFromGeneri
const { data, refetch: performLookup, isFetching } = useQuery(personLookupQueries.lookup(tempSsn, tempName));

function handleValidateName(name: string) {
if (!validateName({ name })) {
const nameErrors = validateName.errors
?.filter((error) => error.instancePath === '/name')
.map((error) => error.message)
.filter((it) => it != null);

setNameErrors(nameErrors);
if (name.length < 2) {
setNameError('person_lookup.validation_error_name_too_short');
return false;
}
setNameErrors(undefined);
setNameError(undefined);
return true;
}

Expand Down Expand Up @@ -132,11 +129,15 @@ export function PersonLookupComponent({ node, overrideDisplay }: PropsFromGeneri

const { data } = await performLookup();
if (data?.person) {
setValue('person_lookup_name', data.person.name);
setValue('person_lookup_name', getFullName(data.person));
setValue('person_lookup_ssn', data.person.ssn);
}
}

function getFullName({ firstName, middleName, lastName }) {
return middleName ? `${firstName} ${middleName} ${lastName}` : `${firstName} ${lastName}`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trenger ikke endre på det nå, bare et tips i tilfelle det kan bli handy en gang du har et litt mer komplekst case enn her og du ikke har sett det før 😄
Jeg syns det ofte er nice å legge sammen tekst vha en liste. Typ
[firstName, middleName, lastName].filter(it != null).join(" ")

}

function handleClear() {
setValue('person_lookup_name', '');
setValue('person_lookup_ssn', '');
Expand Down Expand Up @@ -219,7 +220,7 @@ export function PersonLookupComponent({ node, overrideDisplay }: PropsFromGeneri
required={required}
readOnly={hasSuccessfullyFetched}
error={
(nameErrors?.length && <Lang id={nameErrors.join(' ')} />) ||
(nameError && <Lang id={nameError} />) ||
(hasValidationErrors(bindingValidations?.person_lookup_name) && (
<ComponentValidations validations={bindingValidations?.person_lookup_name} />
))
Expand Down
2 changes: 1 addition & 1 deletion src/layout/PersonLookup/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const Config = new CG.component({
new CG.prop(
'person_lookup_name',
new CG.dataModelBinding()
.setTitle('Data model binding for zip code')
.setTitle('Data model binding for the full name of a person')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

.setDescription(
'Describes the location in the data model where the component should store the name of the person to look up.',
),
Expand Down
9 changes: 0 additions & 9 deletions src/layout/PersonLookup/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,7 @@ export function checkValidSsn(ssn: string): boolean {
return k1 === calculated_k1 && k2 === calculated_k2;
}

const nameSchema: JSONSchemaType<Pick<Person, 'name'>> = {
type: 'object',
properties: {
name: { type: 'string', minLength: 2, errorMessage: 'person_lookup.validation_error_name_too_short' },
},
required: ['name'],
};

export const validateSsn = ajv.compile(ssnSchema);
export const validateName = ajv.compile(nameSchema);

const personLookupResponseSchema: JSONSchemaType<PersonLookupResponse> = {
type: 'object',
Expand Down
Loading