|
| 1 | +/* |
| 2 | + - Copyright 2022 Sven Loesekann |
| 3 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + you may not use this file except in compliance with the License. |
| 5 | + You may obtain a copy of the License at |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + Unless required by applicable law or agreed to in writing, software |
| 8 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + See the License for the specific language governing permissions and |
| 11 | + limitations under the License. |
| 12 | +*/ |
| 13 | +import * as React from 'react'; |
| 14 | +import GlobalState from "../GlobalState"; |
| 15 | +import { type UserDataState} from "../GlobalState"; |
| 16 | +import { useMemo,useEffect,useState,type FormEvent, type ChangeEvent,type SyntheticEvent } from "react"; |
| 17 | +import {Box,TextField,Button,Dialog,DialogContent, Autocomplete} from '@mui/material'; |
| 18 | +import styles from './location-modal.module.css'; |
| 19 | +import { fetchLocation, postLocationRadius } from "../service/http-client"; |
| 20 | +import { type PostCodeLocation } from "../model/location"; |
| 21 | +import { type UserRequest } from "../model/user"; |
| 22 | +import { useAtom } from "jotai"; |
| 23 | + |
| 24 | +const LocationModal = () => { |
| 25 | + let controller: AbortController | null = null; |
| 26 | + const [open, setOpen] = useState(false); |
| 27 | + const [searchRadius, setSearchRadius] = useState(0); |
| 28 | + const [longitude, setLongitude] = useState(0); |
| 29 | + const [latitude, setLatitude] = useState(0); |
| 30 | + const [postCode, setPostCode] = useState(''); |
| 31 | + const [options, setOptions] = useState([] as PostCodeLocation[]); |
| 32 | + const [globalLocationModalState, setGlobalLocationModalState] = useAtom(GlobalState.locationModalState); |
| 33 | + const [globalJwtTokenState, setGlobalJwtTokenState] = useAtom(GlobalState.jwtTokenState); |
| 34 | + const [globalUserDataState, setGlobalUserDataState] = useAtom(GlobalState.userDataState); |
| 35 | + const [globalUserNameState, setGlobalUserNameState] = useAtom(GlobalState.userNameState); |
| 36 | + |
| 37 | + useEffect(() => { |
| 38 | + if (!open) { |
| 39 | + setOptions([]); |
| 40 | + } |
| 41 | + }, [open]); |
| 42 | + |
| 43 | + const handleSubmit = async (event: FormEvent<HTMLFormElement>) => { |
| 44 | + event.preventDefault(); |
| 45 | + if(!!controller) { |
| 46 | + controller.abort(); |
| 47 | + } |
| 48 | + controller = new AbortController(); |
| 49 | + //console.log("Submit: ",event); |
| 50 | + const requestString = JSON.stringify({Username: globalUserNameState, Password: '', Latitude: latitude, Longitude: longitude, SearchRadius: searchRadius, PostCode: parseInt(postCode)} as UserRequest); |
| 51 | + const userResponse = await postLocationRadius(globalJwtTokenState, controller, requestString); |
| 52 | + controller = null; |
| 53 | + setGlobalUserDataState({Latitude: userResponse.Latitude, Longitude: userResponse.Longitude, SearchRadius: userResponse.SearchRadius, PostCode: postCode.toString() || 0, |
| 54 | + TargetDiesel: globalUserDataState.TargetDiesel, TargetE10: globalUserDataState.TargetE10, TargetE5: globalUserDataState.TargetE5} as UserDataState); |
| 55 | + setGlobalLocationModalState(false); |
| 56 | + } |
| 57 | +/* |
| 58 | + const handleClose = (event: React.FormEvent) => { |
| 59 | + setGlobalLocationModalState(false); |
| 60 | + } |
| 61 | +*/ |
| 62 | + const handleChange = async (event: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => { |
| 63 | + event.preventDefault(); |
| 64 | + if(!event?.currentTarget?.value) { |
| 65 | + setOptions([]); |
| 66 | + return; |
| 67 | + } |
| 68 | + if(!!controller) { |
| 69 | + controller.abort(); |
| 70 | + } |
| 71 | + controller = new AbortController(); |
| 72 | + const locations = await fetchLocation(globalJwtTokenState, controller, event.currentTarget.value); |
| 73 | + setOptions(!locations ? [] : locations); |
| 74 | + controller = null; |
| 75 | + //console.log(locations); |
| 76 | + } |
| 77 | + |
| 78 | + const handleSearchRadiusChange = (event: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => { |
| 79 | + //console.log(event?.currentTarget?.value); |
| 80 | + const mySearchRadius = parseFloat(event?.currentTarget?.value); |
| 81 | + setSearchRadius(Number.isNaN(mySearchRadius) ? searchRadius : mySearchRadius); |
| 82 | + } |
| 83 | + |
| 84 | + const handleOptionChange = (event: SyntheticEvent<Element, Event>, value: string) =>{ |
| 85 | + const filteredOptions = options.filter(option => option.Label === value); |
| 86 | + //console.log(filteredOptions); |
| 87 | + if(filteredOptions.length > 0) { |
| 88 | + setLongitude(filteredOptions[0].Longitude); |
| 89 | + setLatitude(filteredOptions[0].Latitude); |
| 90 | + setPostCode(formatPostCode(filteredOptions[0].PostCode)); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + const formatPostCode = (myPlz: number) => { |
| 95 | + return '00000'.substring(0, 5 - myPlz?.toString()?.length > 0 ? myPlz?.toString()?.length : 0) + myPlz.toString(); |
| 96 | + } |
| 97 | + |
| 98 | + const handleCancel = (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => { |
| 99 | + setSearchRadius(0); |
| 100 | + setLongitude(0); |
| 101 | + setLatitude(0); |
| 102 | + setGlobalLocationModalState(false); |
| 103 | + } |
| 104 | + |
| 105 | + const handleGetLocation = (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => { |
| 106 | + if(!!navigator.geolocation) { |
| 107 | + navigator.geolocation.getCurrentPosition(result => { |
| 108 | + if(!!result?.coords?.longitude && !!result?.coords?.latitude) { |
| 109 | + setLongitude(result.coords.longitude); |
| 110 | + setLatitude(result.coords.latitude); |
| 111 | + } |
| 112 | + }); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + let dialogOpen = useMemo(() => { |
| 117 | + //console.log(globalUserDataState.Longitude+' '+globalUserDataState.Latitude); |
| 118 | + setLongitude(globalUserDataState.Longitude); |
| 119 | + setLatitude(globalUserDataState.Latitude); |
| 120 | + setSearchRadius(globalUserDataState.SearchRadius); |
| 121 | + setPostCode(formatPostCode(globalUserDataState.PostCode)); |
| 122 | + return globalLocationModalState; |
| 123 | + }, [globalLocationModalState, globalUserDataState.Longitude, globalUserDataState.Latitude, globalUserDataState.SearchRadius, globalUserDataState.PostCode]); |
| 124 | + |
| 125 | + return (<Dialog open={dialogOpen} className="backDrop"> |
| 126 | + <DialogContent> |
| 127 | + <Box |
| 128 | + component="form" |
| 129 | + noValidate |
| 130 | + autoComplete="off" |
| 131 | + onSubmit={handleSubmit}> |
| 132 | + <Autocomplete |
| 133 | + open={open} |
| 134 | + onOpen={() => { |
| 135 | + setOpen(true); |
| 136 | + }} |
| 137 | + onClose={() => { |
| 138 | + setOpen(false); |
| 139 | + }} |
| 140 | + style={{ width: 300 }} |
| 141 | + onInputChange={handleOptionChange} |
| 142 | + getOptionLabel={option => option.Label} |
| 143 | + options={options} |
| 144 | + renderInput={(params) => <TextField {...params} label="Locations" onChange={handleChange} />} |
| 145 | + ></Autocomplete> |
| 146 | + <div> |
| 147 | + <h3>Longitude: {longitude}</h3> |
| 148 | + <h3>Latitude: {latitude}</h3> |
| 149 | + <h3>Postcode: {postCode}</h3> |
| 150 | + </div> |
| 151 | + <TextField |
| 152 | + autoFocus |
| 153 | + margin="dense" |
| 154 | + value={searchRadius} |
| 155 | + onChange={handleSearchRadiusChange} |
| 156 | + label="Search Radius" |
| 157 | + type="string" |
| 158 | + fullWidth |
| 159 | + variant="standard"/> |
| 160 | + <div> |
| 161 | + <Button type="submit">Ok</Button> |
| 162 | + <Button onClick={handleCancel}>Cancel</Button> |
| 163 | + <Button className={styles.toright} onClick={handleGetLocation}>Get Location</Button> |
| 164 | + </div> |
| 165 | + </Box> |
| 166 | + </DialogContent> |
| 167 | + </Dialog>); |
| 168 | +} |
| 169 | +export default LocationModal; |
0 commit comments