@@ -16,17 +16,8 @@ import {UserDataState} from "../GlobalState";
1616import { useMemo , useEffect , useState , FormEvent , ChangeEvent , SyntheticEvent } from "react" ;
1717import { Box , TextField , Button , Dialog , DialogContent , Autocomplete } from '@mui/material' ;
1818import styles from './modal.module.scss' ;
19- import { UserRequest , UserResponse } from "../service/dtos" ;
20-
21- interface PostCodeLocation {
22- Message : string ;
23- Longitude : number ;
24- Latitude : number ;
25- Label : string ;
26- PostCode : number
27- SquareKM : number ;
28- Population : number ;
29- }
19+ import { PostCodeLocation , UserRequest , UserResponse } from "../service/dtos" ;
20+ import { fetchLocation , postLocationRadius } from "../service/http-client" ;
3021
3122const LocationModal = ( ) => {
3223 let controller : AbortController | null = null ;
@@ -53,14 +44,10 @@ const LocationModal = () => {
5344 if ( ! ! controller ) {
5445 controller . abort ( ) ;
5546 }
47+ controller = new AbortController ( ) ;
5648 //console.log("Submit: ",event);
57- const requestOptions = {
58- method : 'POST' ,
59- headers : { 'Content-Type' : 'application/json' , 'Authorization' : `Bearer ${ globalJwtTokenState } ` } ,
60- body : JSON . stringify ( { Username : globalUserNameState , Password : '' , Latitude : latitude , Longitude : longitude , SearchRadius : searchRadius , PostCode : parseInt ( postCode ) } as UserRequest )
61- } ;
62- const response = await fetch ( '/appuser/locationradius' , requestOptions ) ;
63- const userResponse = response . json ( ) as UserResponse ;
49+ const requestString = JSON . stringify ( { Username : globalUserNameState , Password : '' , Latitude : latitude , Longitude : longitude , SearchRadius : searchRadius , PostCode : parseInt ( postCode ) } as UserRequest ) ;
50+ const userResponse = await postLocationRadius ( globalJwtTokenState , controller , requestString ) ;
6451 controller = null ;
6552 setGlobalUserDataState ( { Latitude : userResponse . Latitude , Longitude : userResponse . Longitude , SearchRadius : userResponse . SearchRadius , PostCode : postCode . toString ( ) || 0 ,
6653 TargetDiesel : globalUserDataState . TargetDiesel , TargetE10 : globalUserDataState . TargetE10 , TargetE5 : globalUserDataState . TargetE5 } as UserDataState ) ;
@@ -76,14 +63,14 @@ const LocationModal = () => {
7663 if ( ! event ?. currentTarget ?. value ) {
7764 setOptions ( [ ] ) ;
7865 return ;
79- }
80- const requestOptions = {
81- method : 'GET' ,
82- headers : { 'Content-Type' : 'application/json' , 'Authorization' : `Bearer ${ globalJwtTokenState } ` }
83- } ;
84- const response = await fetch ( `/appuser/location?location=${ event . currentTarget . value } ` , requestOptions ) ;
85- const locations = await response . json ( ) as PostCodeLocation [ ] ;
66+ }
67+ if ( ! ! controller ) {
68+ controller . abort ( ) ;
69+ }
70+ controller = new AbortController ( ) ;
71+ const locations = await fetchLocation ( globalJwtTokenState , controller , event . currentTarget . value ) ;
8672 setOptions ( ! locations ? [ ] : locations ) ;
73+ controller = null ;
8774 //console.log(locations);
8875 }
8976
0 commit comments