Skip to content
This repository was archived by the owner on Nov 1, 2024. It is now read-only.

Commit 11c578e

Browse files
committed
fix select options
1 parent f084402 commit 11c578e

File tree

1 file changed

+44
-57
lines changed

1 file changed

+44
-57
lines changed

components/dataproducts/accessRequest/accessRequestForm.tsx

+44-57
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
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'
33
import { useRouter } from 'next/router'
44
import { useContext, useState } from 'react'
55
import { Controller, useForm } from 'react-hook-form'
6-
import AsyncSelect from 'react-select/async'
76
import * as yup from 'yup'
87
import { DatasetQuery } from '../../../lib/schema/datasetQuery'
98
import { UserState } from '../../../lib/context'
109
import ErrorMessage from '../../lib/error';
1110
import { PollyInput, SubjectType } from '../../../lib/rest/access';
1211
import { useSearchPolly } from '../../../lib/rest/polly';
13-
import { set } from 'lodash';
12+
import Select from 'react-select';
1413

1514
const tomorrow = () => {
1615
const date = new Date()
@@ -19,11 +18,11 @@ const tomorrow = () => {
1918
}
2019

2120
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
2726
}
2827

2928
const schema = yup
@@ -47,10 +46,10 @@ const schema = yup
4746
.nullable()
4847
.when('accessType', {
4948
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()
5251
})
53-
})
52+
})
5453
.required()
5554

5655
export type AccessRequestFormInput = {
@@ -89,12 +88,10 @@ const AccessRequestFormV2 = ({
8988
error,
9089
}: AccessRequestFormProps) => {
9190
const [searchText, setSearchText] = useState('')
92-
const [polly, setPolly] = useState<PollyInput | undefined | null>(null)
91+
const [polly, setPolly] = useState<PollyInput | undefined | null>(accessRequest?.polly)
9392
const [submitted, setSubmitted] = useState(false)
94-
const [loadOptionsCallBack, setLoadOptionsCallBack] = useState<Function | null>(null)
9593
const router = useRouter()
9694
const userInfo = useContext(UserState)
97-
9895
const {
9996
register,
10097
handleSubmit,
@@ -105,7 +102,7 @@ const AccessRequestFormV2 = ({
105102
} = useForm({
106103
resolver: yupResolver(schema),
107104
defaultValues: {
108-
subject: accessRequest?.subject ? accessRequest.subject : userInfo?.email ? userInfo.email : "",
105+
subject: accessRequest?.subject ? accessRequest.subject : userInfo?.email ? userInfo.email : "",
109106
subjectType: accessRequest?.subjectType
110107
? accessRequest.subjectType
111108
: SubjectType.User,
@@ -132,8 +129,8 @@ const AccessRequestFormV2 = ({
132129
datasetID: dataset.id,
133130
subject: data.subject,
134131
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,
137134
}
138135
onSubmit(accessRequest)
139136
}
@@ -143,29 +140,16 @@ const AccessRequestFormV2 = ({
143140
label: string
144141
}
145142

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
162148
}
163149

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)
169153
}
170154

171155
return (
@@ -221,11 +205,11 @@ const AccessRequestFormV2 = ({
221205
>
222206
<Radio value="until">Til dato</Radio>
223207
<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()}
229213
/>
230214
</DatePicker>
231215
<Radio value="eternal">For alltid</Radio>
@@ -236,24 +220,27 @@ const AccessRequestFormV2 = ({
236220
<label className="navds-label">
237221
Velg behandling fra behandlingskatalogen
238222
</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"
249225
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+
}}
252238
/>
239+
253240
</div>
254241
</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>}
257244
<div className="flex flex-row gap-4 grow items-end pb-8">
258245
<Button
259246
type="button"

0 commit comments

Comments
 (0)