Skip to content

Commit

Permalink
Bruk onblur til validering av janzz
Browse files Browse the repository at this point in the history
  • Loading branch information
joarau committed Nov 6, 2024
1 parent a57e9cb commit e836677
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/stilling/stilling/edit/om-stillingen/janzz/Janzz.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// components/Janzz.tsx

import React, { FunctionComponent, useState } from 'react';
import css from './Janzz.module.css';
import { SET_EMPLOYMENT_JOBTITLE, SET_JANZZ } from '../../../adDataReducer';
Expand All @@ -19,11 +17,13 @@ const Janzz: FunctionComponent<Props> = ({ tittel }) => {
const yrkestittelError = useSelector((state: State) => state.adValidation.errors.yrkestittel);

const [input, setInput] = useState<string>(tittel);
const [hasValidSelection, setHasValidSelection] = useState<boolean>(true);

const { data: suggestions, isLoading, error } = useHentJanzzYrker(input);

const onChange = (event: React.ChangeEvent<HTMLInputElement> | null, value?: string) => {
setInput(event?.target?.value || value || '');
setHasValidSelection(false); // Reset valid selection status on change
};

const onToggleSelected = (option: string, isSelected: boolean) => {
Expand All @@ -45,13 +45,20 @@ const Janzz: FunctionComponent<Props> = ({ tittel }) => {
];
dispatch({ type: SET_JANZZ, kategori });
setInput(capitalizeEmployerName(found.label) || '');
setHasValidSelection(true); // Mark selection as valid
}
}
};

const onBlur = () => {
// If no valid selection is made and input is not empty, show an error message
if (!hasValidSelection && input) {
dispatch({ type: SET_JANZZ, kategori: undefined });
}
};

const feilmeldingTilBruker = error ? error.message : undefined;

// TODO: Fjern default tekster fra backend, slik at vi slipper å filterere de bort her
return (
<div>
<UnsafeCombobox
Expand All @@ -65,6 +72,7 @@ const Janzz: FunctionComponent<Props> = ({ tittel }) => {
options={suggestions ? suggestions.map((f) => f.label) : []}
onChange={onChange}
onToggleSelected={onToggleSelected}
onBlur={onBlur}
isLoading={isLoading}
error={yrkestittelError || feilmeldingTilBruker}
className={css.typeahead}
Expand Down

0 comments on commit e836677

Please sign in to comment.