Skip to content

Commit d841050

Browse files
committed
Fiks undefined janzz feil
1 parent 6e42abe commit d841050

File tree

1 file changed

+18
-19
lines changed
  • src/stilling/stilling/edit/om-stillingen/janzz

1 file changed

+18
-19
lines changed

src/stilling/stilling/edit/om-stillingen/janzz/Janzz.tsx

+18-19
Original file line numberDiff line numberDiff line change
@@ -12,49 +12,50 @@ type Props = {
1212
tittel: string;
1313
};
1414

15+
interface Suggestion {
16+
label: string;
17+
konseptId: number;
18+
}
19+
1520
const Janzz: FunctionComponent<Props> = ({ tittel }) => {
1621
const dispatch = useDispatch();
1722
const yrkestittelError = useSelector((state: State) => state.adValidation.errors.yrkestittel);
1823

1924
const [input, setInput] = useState<string>(tittel);
20-
const [initialLoad, setInitialLoad] = useState<boolean>(true);
2125

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

24-
// Keep track of whether the user has modified the title
28+
const typedSuggestions = suggestions as Suggestion[] | undefined;
29+
2530
const isUserEditing = useRef(false);
2631

27-
// Update input when tittel prop changes (e.g., when loading a different position)
2832
useEffect(() => {
2933
setInput(tittel);
30-
setInitialLoad(true);
3134
isUserEditing.current = false;
3235
}, [tittel]);
3336

3437
useEffect(() => {
35-
if (initialLoad) {
36-
setInitialLoad(false);
37-
return;
38-
}
39-
4038
if (!isUserEditing.current) {
4139
return;
4240
}
4341

4442
if (input.trim() === '') {
43+
// User cleared the input
4544
dispatch({ type: SET_JANZZ, payload: undefined });
4645
dispatch({ type: SET_EMPLOYMENT_JOBTITLE, jobtitle: '' });
4746
return;
4847
}
4948

50-
if (suggestions && suggestions.length > 0) {
51-
const found = suggestions.find(
52-
({ label }) => label.toLowerCase() === input.toLowerCase()
49+
// Update job title with user's input
50+
dispatch({ type: SET_EMPLOYMENT_JOBTITLE, jobtitle: input });
51+
52+
// Check if the input matches a suggestion
53+
if (typedSuggestions && typedSuggestions.length > 0) {
54+
const found = typedSuggestions.find(
55+
(suggestion) => suggestion.label.toLowerCase() === input.toLowerCase()
5356
);
5457

5558
if (found) {
56-
dispatch({ type: SET_EMPLOYMENT_JOBTITLE, jobtitle: found.label });
57-
5859
const kategori = [
5960
{
6061
id: found.konseptId,
@@ -69,13 +70,11 @@ const Janzz: FunctionComponent<Props> = ({ tittel }) => {
6970
dispatch({ type: SET_JANZZ, kategori });
7071
} else {
7172
dispatch({ type: SET_JANZZ, payload: undefined });
72-
dispatch({ type: SET_EMPLOYMENT_JOBTITLE, jobtitle: input });
7373
}
7474
} else {
7575
dispatch({ type: SET_JANZZ, payload: undefined });
76-
dispatch({ type: SET_EMPLOYMENT_JOBTITLE, jobtitle: input });
7776
}
78-
}, [input, suggestions, initialLoad, dispatch]);
77+
}, [input, typedSuggestions, dispatch]);
7978

8079
const onChange = (event: React.ChangeEvent<HTMLInputElement> | null, value?: string) => {
8180
const newValue = event?.target?.value || value || '';
@@ -102,14 +101,14 @@ const Janzz: FunctionComponent<Props> = ({ tittel }) => {
102101
? ''
103102
: input
104103
}
105-
options={suggestions ? suggestions.map((f) => f.label) : []}
104+
options={typedSuggestions ? typedSuggestions.map((s) => s.label) : []}
106105
onChange={onChange}
107106
onToggleSelected={onToggleSelected}
108107
isLoading={isLoading}
109108
error={yrkestittelError || feilmeldingTilBruker}
110109
className={css.typeahead}
111110
aria-labelledby="endre-stilling-styrk"
112-
filteredOptions={suggestions ? suggestions.map((f) => f.label) : []}
111+
filteredOptions={typedSuggestions ? typedSuggestions.map((s) => s.label) : []}
113112
/>
114113
</div>
115114
);

0 commit comments

Comments
 (0)