@@ -12,49 +12,50 @@ type Props = {
12
12
tittel : string ;
13
13
} ;
14
14
15
+ interface Suggestion {
16
+ label : string ;
17
+ konseptId : number ;
18
+ }
19
+
15
20
const Janzz : FunctionComponent < Props > = ( { tittel } ) => {
16
21
const dispatch = useDispatch ( ) ;
17
22
const yrkestittelError = useSelector ( ( state : State ) => state . adValidation . errors . yrkestittel ) ;
18
23
19
24
const [ input , setInput ] = useState < string > ( tittel ) ;
20
- const [ initialLoad , setInitialLoad ] = useState < boolean > ( true ) ;
21
25
22
26
const { data : suggestions , isLoading, error } = useHentJanzzYrker ( input ) ;
23
27
24
- // Keep track of whether the user has modified the title
28
+ const typedSuggestions = suggestions as Suggestion [ ] | undefined ;
29
+
25
30
const isUserEditing = useRef ( false ) ;
26
31
27
- // Update input when tittel prop changes (e.g., when loading a different position)
28
32
useEffect ( ( ) => {
29
33
setInput ( tittel ) ;
30
- setInitialLoad ( true ) ;
31
34
isUserEditing . current = false ;
32
35
} , [ tittel ] ) ;
33
36
34
37
useEffect ( ( ) => {
35
- if ( initialLoad ) {
36
- setInitialLoad ( false ) ;
37
- return ;
38
- }
39
-
40
38
if ( ! isUserEditing . current ) {
41
39
return ;
42
40
}
43
41
44
42
if ( input . trim ( ) === '' ) {
43
+ // User cleared the input
45
44
dispatch ( { type : SET_JANZZ , payload : undefined } ) ;
46
45
dispatch ( { type : SET_EMPLOYMENT_JOBTITLE , jobtitle : '' } ) ;
47
46
return ;
48
47
}
49
48
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 ( )
53
56
) ;
54
57
55
58
if ( found ) {
56
- dispatch ( { type : SET_EMPLOYMENT_JOBTITLE , jobtitle : found . label } ) ;
57
-
58
59
const kategori = [
59
60
{
60
61
id : found . konseptId ,
@@ -69,13 +70,11 @@ const Janzz: FunctionComponent<Props> = ({ tittel }) => {
69
70
dispatch ( { type : SET_JANZZ , kategori } ) ;
70
71
} else {
71
72
dispatch ( { type : SET_JANZZ , payload : undefined } ) ;
72
- dispatch ( { type : SET_EMPLOYMENT_JOBTITLE , jobtitle : input } ) ;
73
73
}
74
74
} else {
75
75
dispatch ( { type : SET_JANZZ , payload : undefined } ) ;
76
- dispatch ( { type : SET_EMPLOYMENT_JOBTITLE , jobtitle : input } ) ;
77
76
}
78
- } , [ input , suggestions , initialLoad , dispatch ] ) ;
77
+ } , [ input , typedSuggestions , dispatch ] ) ;
79
78
80
79
const onChange = ( event : React . ChangeEvent < HTMLInputElement > | null , value ?: string ) => {
81
80
const newValue = event ?. target ?. value || value || '' ;
@@ -102,14 +101,14 @@ const Janzz: FunctionComponent<Props> = ({ tittel }) => {
102
101
? ''
103
102
: input
104
103
}
105
- options = { suggestions ? suggestions . map ( ( f ) => f . label ) : [ ] }
104
+ options = { typedSuggestions ? typedSuggestions . map ( ( s ) => s . label ) : [ ] }
106
105
onChange = { onChange }
107
106
onToggleSelected = { onToggleSelected }
108
107
isLoading = { isLoading }
109
108
error = { yrkestittelError || feilmeldingTilBruker }
110
109
className = { css . typeahead }
111
110
aria-labelledby = "endre-stilling-styrk"
112
- filteredOptions = { suggestions ? suggestions . map ( ( f ) => f . label ) : [ ] }
111
+ filteredOptions = { typedSuggestions ? typedSuggestions . map ( ( s ) => s . label ) : [ ] }
113
112
/>
114
113
</ div >
115
114
) ;
0 commit comments