Skip to content

Commit 21fc7e3

Browse files
committed
Bytt til combobox
1 parent 7748ac0 commit 21fc7e3

File tree

2 files changed

+30
-54
lines changed

2 files changed

+30
-54
lines changed

src/stilling/stilling/edit/om-stillingen/OmStillingen.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const OmStillingen = ({ stilling, erFormidling }: Props) => {
3434

3535
return (
3636
<>
37-
<Janzz tittel={stilling.title} categoryList={stilling.categoryList} />
37+
<Janzz tittel={stilling.title} />
3838
{!erFormidling && (
3939
<div>
4040
<Skjemalabel påkrevd inputId="endre-stilling-annonsetekst">
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
import React, { FunctionComponent, useEffect, useState } from 'react';
22
import css from './Janzz.module.css';
3-
import Typeahead, { Suggestion } from '../../../../common/typeahead/Typeahead';
43
import { ikkeLastet, Nettressurs, Nettstatus } from 'felles/nettressurs';
54
import { fetchJanzzYrker, JanzzStilling } from '../../../../api/api';
65
import capitalizeEmployerName from '../../endre-arbeidsgiver/capitalizeEmployerName';
76
import { SET_EMPLOYMENT_JOBTITLE, SET_JANZZ } from '../../../adDataReducer';
8-
import { StyrkCategory } from 'felles/domene/stilling/Stilling';
97
import { useDispatch, useSelector } from 'react-redux';
10-
import Skjemalabel from '../../skjemaetikett/Skjemalabel';
118
import { State } from '../../../../redux/store';
9+
import { UNSAFE_Combobox as UnsafeCombobox } from '@navikt/ds-react';
10+
import Skjemalabel from '../../skjemaetikett/Skjemalabel';
1211

1312
type Props = {
14-
categoryList: StyrkCategory[];
1513
tittel: string;
1614
};
1715

18-
const Janzz: FunctionComponent<Props> = ({ categoryList, tittel }) => {
16+
const Janzz: FunctionComponent<Props> = ({ tittel }) => {
1917
const dispatch = useDispatch();
2018
const yrkestittelError = useSelector((state: State) => state.adValidation.errors.yrkestittel);
2119

@@ -28,10 +26,8 @@ const Janzz: FunctionComponent<Props> = ({ categoryList, tittel }) => {
2826
kind: Nettstatus.LasterInn,
2927
});
3028

31-
let response: JanzzStilling[] | null = null;
32-
3329
try {
34-
response = await fetchJanzzYrker(typeahead);
30+
const response = await fetchJanzzYrker(typeahead);
3531

3632
setSuggestions({
3733
kind: Nettstatus.Suksess,
@@ -47,22 +43,19 @@ const Janzz: FunctionComponent<Props> = ({ categoryList, tittel }) => {
4743

4844
if (input.length > 2) {
4945
hentJanzzYrker(input);
46+
} else {
47+
setSuggestions(ikkeLastet());
5048
}
5149
}, [input]);
5250

53-
const onChange = (value: string | undefined) => {
54-
if (value) {
55-
setInput(value);
56-
} else {
57-
setInput('');
58-
}
51+
const onChange = (event: React.ChangeEvent<HTMLInputElement> | null, value?: string) => {
52+
setInput(value || '');
5953
};
6054

61-
const onForslagValgt = (valgtForslag: Suggestion) => {
62-
console.log('valgtforslag', valgtForslag);
63-
if (suggestions.kind === Nettstatus.Suksess) {
64-
if (valgtForslag) {
65-
const found = finnJanzzStilling(suggestions.data, valgtForslag.value);
55+
const onToggleSelected = (option: string, isSelected: boolean, isCustomOption: boolean) => {
56+
if (isSelected) {
57+
if (suggestions.kind === Nettstatus.Suksess) {
58+
const found = finnJanzzStilling(suggestions.data, option);
6659

6760
if (found) {
6861
dispatch({ type: SET_EMPLOYMENT_JOBTITLE, jobtitle: found.label });
@@ -76,64 +69,47 @@ const Janzz: FunctionComponent<Props> = ({ categoryList, tittel }) => {
7669
parentId: null,
7770
},
7871
];
79-
console.log('kategori', kategori);
8072
dispatch({ type: SET_JANZZ, kategori });
73+
setInput(capitalizeEmployerName(found.label) || '');
8174
} else {
8275
dispatch({ type: SET_JANZZ, undefined });
8376
}
84-
setInput(capitalizeEmployerName(found ? found.label : null) || '');
85-
} else {
86-
dispatch({ type: SET_JANZZ, undefined });
8777
}
78+
} else {
79+
dispatch({ type: SET_JANZZ, undefined });
8880
}
8981
};
9082

91-
//const feilmeldingTilBruker = suggestions.kind === Nettstatus.Feil && suggestions.error.message;
83+
const feilmeldingTilBruker = suggestions.kind === Nettstatus.Feil && suggestions.error.message;
9284

9385
return (
9486
<div>
95-
<Skjemalabel påkrevd>Yrkestittel som vises på stillingen</Skjemalabel>
96-
<Typeahead
97-
label=""
87+
<UnsafeCombobox
88+
label={<Skjemalabel påkrevd>Yrkestittel som vises på stillingen</Skjemalabel>}
9889
value={input === 'Stilling uten valgt jobbtittel' ? '' : input}
99-
onSelect={onForslagValgt}
90+
options={konverterTilComboboxOptions(suggestions)}
10091
onChange={onChange}
101-
onBlur={onChange}
102-
suggestions={konverterTilTypeaheadFormat(suggestions)}
103-
error={yrkestittelError}
92+
onToggleSelected={onToggleSelected}
93+
isLoading={suggestions.kind === Nettstatus.LasterInn}
94+
error={yrkestittelError || feilmeldingTilBruker || undefined}
10495
className={css.typeahead}
10596
aria-labelledby="endre-stilling-styrk"
10697
/>
10798
</div>
10899
);
109100
};
110-
/* <UNSAFE_Combobox
111-
label="Yrkestittel som vises på stillingen"
112-
value={input === 'Stilling uten valgt jobbtittel' ? '' : input}
113-
options={}
114-
//onSelect={onForslagValgt}
115-
//onChange={onChange}
116-
//onBlur={onChange}
117-
//suggestions={konverterTilTypeaheadFormat(suggestions)}
118-
error={feilmeldingTilBruker || undefined}
119-
className={css.typeahead}
120-
aria-labelledby="endre-stilling-styrk"
121-
/>*/
122101

123-
const finnJanzzStilling = (suggestions: JanzzStilling[], navn: string) =>
124-
suggestions.find(
125-
(forslag: JanzzStilling) => forslag.label.toLowerCase() === navn.toLowerCase()
126-
);
127-
128-
const konverterTilTypeaheadFormat = (suggestions: Nettressurs<JanzzStilling[]>) => {
102+
const konverterTilComboboxOptions = (suggestions: Nettressurs<JanzzStilling[]>): string[] => {
129103
if (suggestions.kind === Nettstatus.Suksess) {
130-
return suggestions.data.map((f) => ({
131-
value: f.label,
132-
label: f.label,
133-
}));
104+
return suggestions.data.map((f) => f.label);
134105
} else {
135106
return [];
136107
}
137108
};
138109

110+
const finnJanzzStilling = (suggestions: JanzzStilling[], navn: string) =>
111+
suggestions.find(
112+
(forslag: JanzzStilling) => forslag.label.toLowerCase() === navn.toLowerCase()
113+
);
114+
139115
export default Janzz;

0 commit comments

Comments
 (0)