1
1
import { yupResolver } from '@hookform/resolvers/yup' ;
2
- import { Button , DatePicker , Heading , Loader , Radio , RadioGroup , TextField , useDatepicker } from '@navikt/ds-react'
2
+ import { Button , DatePicker , Heading , Loader , Radio , RadioGroup , TextField , useDatepicker } from '@navikt/ds-react'
3
3
import { useRouter } from 'next/router'
4
4
import { useContext , useState } from 'react'
5
5
import { Controller , useForm } from 'react-hook-form'
6
- import AsyncSelect from 'react-select/async'
7
6
import * as yup from 'yup'
8
7
import { DatasetQuery } from '../../../lib/schema/datasetQuery'
9
8
import { UserState } from '../../../lib/context'
10
9
import ErrorMessage from '../../lib/error' ;
11
10
import { PollyInput , SubjectType } from '../../../lib/rest/access' ;
12
11
import { useSearchPolly } from '../../../lib/rest/polly' ;
13
- import { set } from 'lodash ' ;
12
+ import Select from 'react-select ' ;
14
13
15
14
const tomorrow = ( ) => {
16
15
const date = new Date ( )
@@ -19,11 +18,11 @@ const tomorrow = () => {
19
18
}
20
19
21
20
const currentDate = ( currentDate : any ) => {
22
- if ( typeof currentDate === 'string' ) return new Date ( currentDate )
23
- else if ( currentDate instanceof Date ) {
24
- return currentDate
25
- }
26
- return undefined
21
+ if ( typeof currentDate === 'string' ) return new Date ( currentDate )
22
+ else if ( currentDate instanceof Date ) {
23
+ return currentDate
24
+ }
25
+ return undefined
27
26
}
28
27
29
28
const schema = yup
@@ -47,10 +46,10 @@ const schema = yup
47
46
. nullable ( )
48
47
. when ( 'accessType' , {
49
48
is : 'until' ,
50
- then : ( ) => yup . date ( ) . required ( 'Du må angi en utløpsdato for tilgang' ) ,
51
- otherwise : ( ) => yup . date ( ) . nullable ( )
49
+ then : ( ) => yup . date ( ) . required ( 'Du må angi en utløpsdato for tilgang' ) ,
50
+ otherwise : ( ) => yup . date ( ) . nullable ( )
52
51
} )
53
- } )
52
+ } )
54
53
. required ( )
55
54
56
55
export type AccessRequestFormInput = {
@@ -89,12 +88,10 @@ const AccessRequestFormV2 = ({
89
88
error,
90
89
} : AccessRequestFormProps ) => {
91
90
const [ searchText , setSearchText ] = useState ( '' )
92
- const [ polly , setPolly ] = useState < PollyInput | undefined | null > ( null )
91
+ const [ polly , setPolly ] = useState < PollyInput | undefined | null > ( accessRequest ?. polly )
93
92
const [ submitted , setSubmitted ] = useState ( false )
94
- const [ loadOptionsCallBack , setLoadOptionsCallBack ] = useState < Function | null > ( null )
95
93
const router = useRouter ( )
96
94
const userInfo = useContext ( UserState )
97
-
98
95
const {
99
96
register,
100
97
handleSubmit,
@@ -105,7 +102,7 @@ const AccessRequestFormV2 = ({
105
102
} = useForm ( {
106
103
resolver : yupResolver ( schema ) ,
107
104
defaultValues : {
108
- subject : accessRequest ?. subject ? accessRequest . subject : userInfo ?. email ? userInfo . email : "" ,
105
+ subject : accessRequest ?. subject ? accessRequest . subject : userInfo ?. email ? userInfo . email : "" ,
109
106
subjectType : accessRequest ?. subjectType
110
107
? accessRequest . subjectType
111
108
: SubjectType . User ,
@@ -132,8 +129,8 @@ const AccessRequestFormV2 = ({
132
129
datasetID : dataset . id ,
133
130
subject : data . subject ,
134
131
subjectType : data . subjectType ,
135
- polly : polly ?? undefined ,
136
- expires : data . accessType === 'until' ? data . expires ? new Date ( data . expires ) : undefined : undefined ,
132
+ polly : polly ?? undefined ,
133
+ expires : data . accessType === 'until' ? data . expires ? new Date ( data . expires ) : undefined : undefined ,
137
134
}
138
135
onSubmit ( accessRequest )
139
136
}
@@ -143,29 +140,16 @@ const AccessRequestFormV2 = ({
143
140
label : string
144
141
}
145
142
146
- console . log ( searchResult )
147
- loadOptionsCallBack ?.(
148
- searchResult
149
- ? searchResult . map ( ( el ) => {
150
- return { value : el . externalID , label : el . name }
151
- } )
152
- : [ ]
153
- )
154
-
155
- const loadOptions = (
156
- input : string ,
157
- callback : ( options : Option [ ] ) => void
158
- ) => {
159
- console . log ( input )
160
- setSearchText ( input )
161
- setLoadOptionsCallBack ( ( ) => callback )
143
+ const getOptionsForSelect = ( ) => {
144
+ const optionsBySearch = searchResult ? searchResult . map ( ( el ) => {
145
+ return { value : el . externalID , label : el . name }
146
+ } ) : [ ]
147
+ return optionsBySearch
162
148
}
163
149
164
- const onInputChange = ( newOption : Option | null ) => {
165
- newOption != null
166
- ? searchResult &&
167
- setPolly ( searchResult . find ( ( e ) => e . externalID == newOption . value ) )
168
- : setPolly ( null )
150
+
151
+ const setPollyIfMatches = ( input : string ) => {
152
+ setPolly ( input && searchResult ? searchResult . find ( ( e ) => e . externalID === input ) : null )
169
153
}
170
154
171
155
return (
@@ -221,11 +205,11 @@ const AccessRequestFormV2 = ({
221
205
>
222
206
< Radio value = "until" > Til dato</ Radio >
223
207
< DatePicker { ...datepickerProps } >
224
- < DatePicker . Input
225
- { ...inputProps }
226
- label = ""
227
- disabled = { field . value === 'eternal' }
228
- error = { errors ?. expires ?. message ?. toString ( ) }
208
+ < DatePicker . Input
209
+ { ...inputProps }
210
+ label = ""
211
+ disabled = { field . value === 'eternal' }
212
+ error = { errors ?. expires ?. message ?. toString ( ) }
229
213
/>
230
214
</ DatePicker >
231
215
< Radio value = "eternal" > For alltid</ Radio >
@@ -236,24 +220,27 @@ const AccessRequestFormV2 = ({
236
220
< label className = "navds-label" >
237
221
Velg behandling fra behandlingskatalogen
238
222
</ label >
239
- < AsyncSelect
240
- className = "pt-2"
241
- classNamePrefix = "select"
242
- isClearable
243
- placeholder = "Skriv inn navnet på behandlingen"
244
- noOptionsMessage = { ( { inputValue } ) =>
245
- inputValue ? 'Finner ikke behandling' : null
246
- }
247
- loadingMessage = { ( ) => 'Søker etter behandling...' }
248
- loadOptions = { loadOptions }
223
+ < Select
224
+ className = "basic-single"
249
225
isLoading = { loading }
250
- onChange = { onInputChange }
251
- menuIsOpen = { true }
226
+ isClearable = { false }
227
+ isRtl = { false }
228
+ isSearchable = { true }
229
+ name = "color"
230
+ defaultValue = { polly ? { value : polly . externalID , label : polly . name } : undefined }
231
+ options = { getOptionsForSelect ( ) }
232
+ onInputChange = { ( t : string ) => {
233
+ setSearchText ( t )
234
+ } }
235
+ onChange = { ( e : any ) => {
236
+ setPollyIfMatches ( e . value )
237
+ } }
252
238
/>
239
+
253
240
</ div >
254
241
</ div >
255
- { error && < ErrorMessage error = { error } /> }
256
- { submitted && ! error && < div > Vennligst vent...< Loader size = "small" /> </ div > }
242
+ { error && < ErrorMessage error = { error } /> }
243
+ { submitted && ! error && < div > Vennligst vent...< Loader size = "small" /> </ div > }
257
244
< div className = "flex flex-row gap-4 grow items-end pb-8" >
258
245
< Button
259
246
type = "button"
0 commit comments