Skip to content

Commit 9007131

Browse files
committed
fix: build
1 parent 8d8e76e commit 9007131

File tree

5 files changed

+51
-33
lines changed

5 files changed

+51
-33
lines changed

frontend/src/components/LocationModal.tsx

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,8 @@ import {UserDataState} from "../GlobalState";
1616
import { useMemo,useEffect,useState,FormEvent,ChangeEvent,SyntheticEvent } from "react";
1717
import {Box,TextField,Button,Dialog,DialogContent, Autocomplete} from '@mui/material';
1818
import 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

3122
const 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

frontend/src/components/LoginModal.tsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,6 @@ const LoginModal = () => {
8080
if(!!controller) {
8181
controller.abort();
8282
}
83-
const requestOptions = {
84-
method: 'POST',
85-
headers: { 'Content-Type': 'application/json' },
86-
body: JSON.stringify({ Username: userName, Password: password1 } as UserRequest)
87-
};
8883
setResponseMsg('');
8984
controller = new AbortController();
9085
const userResponse = activeTab === 0 ? await postLogin(userName, password1, controller) : await postSignin(userName, password1, controller);

frontend/src/components/TargetPriceModal.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import { useRecoilState, useRecoilValue } from "recoil";
1414
import GlobalState from "../GlobalState";
1515
import { Box, TextField, Button, Dialog, DialogContent } from '@mui/material';
1616
import { useState, useMemo, ChangeEventHandler, FormEvent } from "react";
17-
import { UserRequest, UserResponse } from "./LoginModal";
17+
import { UserRequest, UserResponse } from "../service/dtos";
18+
1819

1920
const TargetPriceModal = () => {
2021
let controller: AbortController | null = null;

frontend/src/service/dtos.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,13 @@ export interface UserResponse {
139139
TargetE5?: number;
140140
TargetE10?: number;
141141
}
142+
143+
export interface PostCodeLocation {
144+
Message: string;
145+
Longitude: number;
146+
Latitude: number;
147+
Label: string;
148+
PostCode: number
149+
SquareKM: number;
150+
Population: number;
151+
}

frontend/src/service/http-client.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { UserDataState } from "../GlobalState";
2-
import { GasPriceAvgs, GasStation, Notification, TimeSlotResponse, UserRequest, UserResponse } from "./dtos";
2+
import { GasPriceAvgs, GasStation, Notification, PostCodeLocation, TimeSlotResponse, UserRequest, UserResponse } from "./dtos";
33

44
const fetchGasStations = async function(jwtToken: string, controller: AbortController | null, globalUserDataState: UserDataState): Promise<GasStation[]> {
55
const requestOptions2 = {
@@ -65,9 +65,34 @@ const loginSigninOptions = (userName: string, password1: string, controller: Abo
6565
};
6666
};
6767

68+
const postLocationRadius = async function(jwtToken: string, controller: AbortController | null, requestString: string): Promise<UserResponse> {
69+
const requestOptions = {
70+
method: 'POST',
71+
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${jwtToken}`},
72+
body: requestString,
73+
signal: controller?.signal
74+
};
75+
const response = await fetch('/appuser/locationradius', requestOptions);
76+
const userResponse = response.json() as UserResponse;
77+
return userResponse;
78+
}
79+
80+
const fetchLocation = async function(jwtToken: string, controller: AbortController | null, location: string): Promise<PostCodeLocation[]> {
81+
const requestOptions = {
82+
method: 'GET',
83+
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${jwtToken}` },
84+
signal: controller?.signal
85+
};
86+
const response = await fetch(`/appuser/location?location=${location}`, requestOptions);
87+
const locations = response.json() as Promise<PostCodeLocation[]>;
88+
return locations;
89+
}
90+
6891
export { fetchGasStations };
6992
export { fetchPriceAvgs };
7093
export { fetchUserNotifications };
7194
export { fetchTimeSlots };
7295
export { postLogin };
73-
export { postSignin };
96+
export { postSignin };
97+
export { postLocationRadius };
98+
export { fetchLocation };

0 commit comments

Comments
 (0)