-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into 142-vis-options-on-dashboard
- Loading branch information
Showing
7 changed files
with
168 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import React, { ChangeEvent, Dispatch, SetStateAction, useEffect, useState } from 'react'; | ||
import { MetAPIVariables } from '../../queries/metApi'; | ||
import { Button, Combobox, FormField, TextInputField, toaster } from 'evergreen-ui'; | ||
import { Address } from '../../types/Address'; | ||
import debounce from 'lodash.debounce'; | ||
|
||
type LatLonInputSetProps = { | ||
state: MetAPIVariables; | ||
setState: Dispatch<SetStateAction<MetAPIVariables>>; | ||
}; | ||
|
||
type LabeledPosition = { | ||
label: string; | ||
lat: number; | ||
lon: number; | ||
}; | ||
|
||
const LatLonFromLocationSearch: React.FC<LatLonInputSetProps> = (props) => { | ||
const [state, setState] = useState<{ lat: string; lon: string }>({ | ||
lat: String(props.state.lat), | ||
lon: String(props.state.lon), | ||
}); | ||
|
||
const [locations, setLocations] = useState<LabeledPosition[]>([]); | ||
|
||
// To avoid problems with difficult digit entry for user | ||
useEffect(() => { | ||
props.setState({ lat: Number(state.lat), lon: Number(state.lon) }); | ||
}, [state]); | ||
|
||
const update = (key: keyof typeof state) => (e: ChangeEvent<HTMLInputElement>) => | ||
setState((prevState) => ({ ...prevState, [key]: e.target.value })); | ||
|
||
const setToMyLocation = () => { | ||
navigator.geolocation.getCurrentPosition( | ||
(position) => { | ||
setState({ lat: String(position.coords.latitude), lon: String(position.coords.longitude) }); | ||
}, | ||
() => { | ||
toaster.warning('Du må tillate lokasjonstjenester for at denne tjenesten skal fungere.'); | ||
}, | ||
); | ||
}; | ||
|
||
const onInputChange = async (e: React.ChangeEvent<HTMLInputElement>): Promise<void> => { | ||
const rawLocationData = await fetch( | ||
`https://ws.geonorge.no/adresser/v1/sok?sok=${e.target.value}&treffPerSide=10`, | ||
); | ||
const locationData: { adresser: Array<Address> } = await rawLocationData.json(); | ||
const locationList = locationData.adresser.map((address) => { | ||
return { | ||
label: `${address.adressetekst} (${address.kommunenavn})`, | ||
lat: address.representasjonspunkt.lat, | ||
lon: address.representasjonspunkt.lon, | ||
}; | ||
}); | ||
setLocations(locationList); | ||
}; | ||
|
||
const selectionChanged = (address: LabeledPosition | null): void => { | ||
if (address === null) return; | ||
setState({ lat: address.lat.toString(), lon: address.lon.toString() }); | ||
}; | ||
|
||
return ( | ||
<> | ||
<FormField label="Adresse" marginBottom="24px"> | ||
<Combobox | ||
items={locations} | ||
placeholder="Adresse" | ||
onInput={debounce(onInputChange, 500)} | ||
itemToString={(item) => item?.label ?? ''} | ||
onChange={selectionChanged} | ||
/> | ||
</FormField> | ||
<TextInputField disabled={true} value={state.lat} label="Breddegrad" onChange={update('lat')} /> | ||
<TextInputField disabled={true} value={state.lon} label="Lengdegrad" onChange={update('lon')} /> | ||
<Button onClick={setToMyLocation}>Sett til min posisjon</Button> | ||
</> | ||
); | ||
}; | ||
export default LatLonFromLocationSearch; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export type Address = { | ||
adressekode: number; | ||
adressenavn: string; | ||
adressetekst: string; | ||
adressetekstutenadressetilleggsnavn: string; | ||
adressetilleggsnavn: string; | ||
bokstav: string; | ||
bruksenhetsnummer: number[]; | ||
bruksnummer: number; | ||
festenummer: number; | ||
gardsnummer: number; | ||
kommunenavn: string; | ||
kommunenummer: string; | ||
nummer: number; | ||
objtype: string; | ||
oppdateringsdato: Date; | ||
postnummer: number; | ||
poststed: string; | ||
representasjonspunkt: { | ||
epsg: string; | ||
lat: number; | ||
lon: number; | ||
}; | ||
stedfestingverifisert: boolean; | ||
undernummer: number; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters