@@ -17,62 +17,70 @@ import { type PostCodeLocation } from "../model/location";
1717import { type TimeSlotResponse } from "../model/time-slot-response" ;
1818import { type UserRequest , type UserResponse } from "../model/user" ;
1919import { type Notification } from "../model/notification" ;
20+ import type { NavigateFunction } from "react-router" ;
2021
2122const apiPrefix = '/api' ;
2223
23- const fetchGasStations = async function ( jwtToken : string , controller : AbortController | null , globalUserDataState : UserDataState ) : Promise < GasStation [ ] > {
24+ async function handleResponse < T > ( response : Response , navigate : NavigateFunction ) : Promise < T > {
25+ if ( ! response . ok ) {
26+ const error = await response . text ( ) ;
27+ if ( navigate ) navigate ( '/' ) ;
28+ throw new Error ( error || `HTTP error! status: ${ response . status } ` ) ;
29+ }
30+ return response . json ( ) ;
31+ }
32+
33+ const fetchGasStations = async function ( navigate : NavigateFunction , jwtToken : string , controller : AbortController | null , globalUserDataState : UserDataState ) : Promise < GasStation [ ] > {
2434 const requestOptions2 = {
2535 method : 'POST' ,
2636 headers : { 'Content-Type' : 'application/json' , 'Authorization' : `Bearer ${ jwtToken } ` } ,
2737 body : JSON . stringify ( { Longitude : globalUserDataState . Longitude , Latitude : globalUserDataState . Latitude , Radius : globalUserDataState . SearchRadius } ) ,
2838 signal : controller ?. signal
2939 }
3040 const result = await fetch ( `${ apiPrefix } /gasstation/search/location` , requestOptions2 ) ;
31- const myResult = result . json ( ) as Promise < GasStation [ ] > ;
32- return myResult ;
41+ return handleResponse < GasStation [ ] > ( result , navigate ) ;
3342} ;
3443
35- const fetchPriceAvgs = async function ( jwtToken : string , controller : AbortController | null , myPostcode : string ) : Promise < GasPriceAvgs > {
44+ const fetchPriceAvgs = async function ( navigate : NavigateFunction , jwtToken : string , controller : AbortController | null , myPostcode : string ) : Promise < GasPriceAvgs > {
3645 const requestOptions3 = {
3746 method : 'GET' ,
3847 headers : { 'Content-Type' : 'application/json' , 'Authorization' : `Bearer ${ jwtToken } ` } ,
3948 signal : controller ?. signal
4049 }
4150 const result = await fetch ( `${ apiPrefix } /gasprice/avgs/${ myPostcode } ` , requestOptions3 ) ;
42- return result . json ( ) as Promise < GasPriceAvgs > ;
51+ return handleResponse < GasPriceAvgs > ( result , navigate ) ;
4352}
4453
45- const fetchUserNotifications = async function ( jwtToken : string , controller : AbortController | null , globalUserUuidState : string ) : Promise < Notification [ ] > {
54+ const fetchUserNotifications = async function ( navigate : NavigateFunction , jwtToken : string , controller : AbortController | null , globalUserUuidState : string ) : Promise < Notification [ ] > {
4655 const requestOptions1 = {
4756 method : 'GET' ,
4857 headers : { 'Content-Type' : 'application/json' , 'Authorization' : `Bearer ${ jwtToken } ` } ,
4958 signal : controller ?. signal
5059 }
5160 const result = await fetch ( `${ apiPrefix } /usernotification/current/${ globalUserUuidState } ` , requestOptions1 ) ;
52- return result . json ( ) as Promise < Notification [ ] > ;
61+ return handleResponse < Notification [ ] > ( result , navigate ) ;
5362}
5463
55- const fetchTimeSlots = async function ( jwtToken : string , controller : AbortController | null , myPostcode : string ) : Promise < TimeSlotResponse [ ] > {
64+ const fetchTimeSlots = async function ( navigate : NavigateFunction , jwtToken : string , controller : AbortController | null , myPostcode : string ) : Promise < TimeSlotResponse [ ] > {
5665 const requestOptions2 = {
5766 method : 'GET' ,
5867 headers : { 'Content-Type' : 'application/json' , 'Authorization' : `Bearer ${ jwtToken } ` } ,
5968 signal : controller ?. signal
6069 }
6170 const result = await fetch ( `${ apiPrefix } /postcode/countytimeslots/${ myPostcode } ` , requestOptions2 ) ;
62- const myResult = result . json ( ) as Promise < TimeSlotResponse [ ] > ;
63- return myResult ;
71+ return handleResponse < TimeSlotResponse [ ] > ( result , navigate ) ;
6472}
6573
6674const postLogin = async function ( userName : string , password1 : string , controller : AbortController | null ) : Promise < UserResponse > {
6775 const requestOptions = loginSigninOptions ( userName , password1 , controller ) ;
6876 const result = await fetch ( `${ apiPrefix } /appuser/login` , requestOptions ) ;
69- return result . json ( ) as Promise < UserResponse > ;
77+ return handleResponse < UserResponse > ( result , null as unknown as NavigateFunction ) ;
7078}
7179
7280const postSignin = async function ( userName : string , password1 : string , controller : AbortController | null ) : Promise < UserResponse > {
7381 const requestOptions = loginSigninOptions ( userName , password1 , controller ) ;
7482 const result = await fetch ( `${ apiPrefix } /appuser/signin` , requestOptions ) ;
75- return result . json ( ) as Promise < UserResponse > ;
83+ return handleResponse < UserResponse > ( result , null as unknown as NavigateFunction ) ;
7684}
7785
7886const loginSigninOptions = ( userName : string , password1 : string , controller : AbortController | null ) => {
@@ -84,39 +92,36 @@ const loginSigninOptions = (userName: string, password1: string, controller: Abo
8492 } ;
8593} ;
8694
87- const postLocationRadius = async function ( jwtToken : string , controller : AbortController | null , requestString : string ) : Promise < UserResponse > {
95+ const postLocationRadius = async function ( navigate : NavigateFunction , jwtToken : string , controller : AbortController | null , requestString : string ) : Promise < UserResponse > {
8896 const requestOptions = {
8997 method : 'POST' ,
9098 headers : { 'Content-Type' : 'application/json' , 'Authorization' : `Bearer ${ jwtToken } ` } ,
9199 body : requestString ,
92100 signal : controller ?. signal
93101 } ;
94102 const response = await fetch ( `${ apiPrefix } /appuser/locationradius` , requestOptions ) ;
95- const userResponse = response . json ( ) as UserResponse ;
96- return userResponse ;
103+ return handleResponse < UserResponse > ( response , navigate ) ;
97104}
98105
99- const fetchLocation = async function ( jwtToken : string , controller : AbortController | null , location : string ) : Promise < PostCodeLocation [ ] > {
106+ const fetchLocation = async function ( navigate : NavigateFunction , jwtToken : string , controller : AbortController | null , location : string ) : Promise < PostCodeLocation [ ] > {
100107 const requestOptions = {
101108 method : 'GET' ,
102109 headers : { 'Content-Type' : 'application/json' , 'Authorization' : `Bearer ${ jwtToken } ` } ,
103110 signal : controller ?. signal
104111 } ;
105112 const response = await fetch ( `${ apiPrefix } /appuser/location?location=${ location } ` , requestOptions ) ;
106- const locations = response . json ( ) as Promise < PostCodeLocation [ ] > ;
107- return locations ;
113+ return handleResponse < PostCodeLocation [ ] > ( response , navigate ) ;
108114}
109115
110- const postTargetPrices = async function ( jwtToken : string , controller : AbortController | null , requestString : string ) : Promise < UserResponse > {
116+ const postTargetPrices = async function ( navigate : NavigateFunction , jwtToken : string , controller : AbortController | null , requestString : string ) : Promise < UserResponse > {
111117 const requestOptions = {
112118 method : 'POST' ,
113119 headers : { 'Content-Type' : 'application/json' , 'Authorization' : `Bearer ${ jwtToken } ` } ,
114120 body : requestString ,
115121 signal : controller ?. signal
116122 } ;
117123 const response = await fetch ( `${ apiPrefix } /appuser/targetprices` , requestOptions ) ;
118- const result = response . json ( ) as Promise < UserResponse > ;
119- return result ;
124+ return handleResponse < UserResponse > ( response , navigate ) ;
120125}
121126
122127export { fetchGasStations } ;
0 commit comments