|
| 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 { type UserDataState } from "../GlobalState"; |
| 14 | +import { type GasPriceAvgs } from "../model/gas-price-avg"; |
| 15 | +import { type GasStation } from "../model/gas-station"; |
| 16 | +import { type PostCodeLocation } from "../model/location"; |
| 17 | +import { type TimeSlotResponse } from "../model/time-slot-response"; |
| 18 | +import { type UserRequest, type UserResponse } from "../model/user"; |
| 19 | +import { type Notification } from "../model/notification"; |
| 20 | + |
| 21 | +const fetchGasStations = async function (jwtToken: string, controller: AbortController | null, globalUserDataState: UserDataState): Promise<GasStation[]> { |
| 22 | + const requestOptions2 = { |
| 23 | + method: 'POST', |
| 24 | + headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${jwtToken}` }, |
| 25 | + body: JSON.stringify({ Longitude: globalUserDataState.Longitude, Latitude: globalUserDataState.Latitude, Radius: globalUserDataState.SearchRadius }), |
| 26 | + signal: controller?.signal |
| 27 | + } |
| 28 | + const result = await fetch('/gasstation/search/location', requestOptions2); |
| 29 | + const myResult = result.json() as Promise<GasStation[]>; |
| 30 | + return myResult; |
| 31 | +}; |
| 32 | + |
| 33 | +const fetchPriceAvgs = async function (jwtToken: string, controller: AbortController | null, myPostcode: string): Promise<GasPriceAvgs> { |
| 34 | + const requestOptions3 = { |
| 35 | + method: 'GET', |
| 36 | + headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${jwtToken}` }, |
| 37 | + signal: controller?.signal |
| 38 | + } |
| 39 | + const result = await fetch(`/gasprice/avgs/${myPostcode}`, requestOptions3); |
| 40 | + return result.json() as Promise<GasPriceAvgs>; |
| 41 | +} |
| 42 | + |
| 43 | +const fetchUserNotifications = async function (jwtToken: string, controller: AbortController | null, globalUserUuidState: string): Promise<Notification[]> { |
| 44 | + const requestOptions1 = { |
| 45 | + method: 'GET', |
| 46 | + headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${jwtToken}` }, |
| 47 | + signal: controller?.signal |
| 48 | + } |
| 49 | + const result = await fetch(`/usernotification/current/${globalUserUuidState}`, requestOptions1); |
| 50 | + return result.json() as Promise<Notification[]>; |
| 51 | +} |
| 52 | + |
| 53 | +const fetchTimeSlots = async function (jwtToken: string, controller: AbortController | null, myPostcode: string): Promise<TimeSlotResponse[]> { |
| 54 | + const requestOptions2 = { |
| 55 | + method: 'GET', |
| 56 | + headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${jwtToken}` }, |
| 57 | + signal: controller?.signal |
| 58 | + } |
| 59 | + const result = await fetch(`/postcode/countytimeslots/${myPostcode}`, requestOptions2); |
| 60 | + const myResult = result.json() as Promise<TimeSlotResponse[]>; |
| 61 | + return myResult; |
| 62 | +} |
| 63 | + |
| 64 | +const postLogin = async function (userName: string, password1: string, controller: AbortController | null): Promise<UserResponse> { |
| 65 | + const requestOptions = loginSigninOptions(userName, password1, controller); |
| 66 | + const result = await fetch('/appuser/login', requestOptions); |
| 67 | + return result.json() as Promise<UserResponse>; |
| 68 | +} |
| 69 | + |
| 70 | +const postSignin = async function (userName: string, password1: string, controller: AbortController | null): Promise<UserResponse> { |
| 71 | + const requestOptions = loginSigninOptions(userName, password1, controller); |
| 72 | + const result = await fetch('/appuser/signin', requestOptions); |
| 73 | + return result.json() as Promise<UserResponse>; |
| 74 | +} |
| 75 | + |
| 76 | +const loginSigninOptions = (userName: string, password1: string, controller: AbortController | null) => { |
| 77 | + return { |
| 78 | + method: 'POST', |
| 79 | + headers: { 'Content-Type': 'application/json' }, |
| 80 | + body: JSON.stringify({ Username: userName, Password: password1 } as UserRequest), |
| 81 | + signal: controller?.signal |
| 82 | + }; |
| 83 | +}; |
| 84 | + |
| 85 | +const postLocationRadius = async function (jwtToken: string, controller: AbortController | null, requestString: string): Promise<UserResponse> { |
| 86 | + const requestOptions = { |
| 87 | + method: 'POST', |
| 88 | + headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${jwtToken}` }, |
| 89 | + body: requestString, |
| 90 | + signal: controller?.signal |
| 91 | + }; |
| 92 | + const response = await fetch('/appuser/locationradius', requestOptions); |
| 93 | + const userResponse = response.json() as UserResponse; |
| 94 | + return userResponse; |
| 95 | +} |
| 96 | + |
| 97 | +const fetchLocation = async function (jwtToken: string, controller: AbortController | null, location: string): Promise<PostCodeLocation[]> { |
| 98 | + const requestOptions = { |
| 99 | + method: 'GET', |
| 100 | + headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${jwtToken}` }, |
| 101 | + signal: controller?.signal |
| 102 | + }; |
| 103 | + const response = await fetch(`/appuser/location?location=${location}`, requestOptions); |
| 104 | + const locations = response.json() as Promise<PostCodeLocation[]>; |
| 105 | + return locations; |
| 106 | +} |
| 107 | + |
| 108 | +const postTargetPrices = async function (jwtToken: string, controller: AbortController | null, requestString: string): Promise<UserResponse> { |
| 109 | + const requestOptions = { |
| 110 | + method: 'POST', |
| 111 | + headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${jwtToken}` }, |
| 112 | + body: requestString, |
| 113 | + signal: controller?.signal |
| 114 | + }; |
| 115 | + const response = await fetch('/appuser/targetprices', requestOptions); |
| 116 | + const result = response.json() as Promise<UserResponse>; |
| 117 | + return result; |
| 118 | +} |
| 119 | + |
| 120 | +export { fetchGasStations }; |
| 121 | +export { fetchPriceAvgs }; |
| 122 | +export { fetchUserNotifications }; |
| 123 | +export { fetchTimeSlots }; |
| 124 | +export { postLogin }; |
| 125 | +export { postSignin }; |
| 126 | +export { postLocationRadius }; |
| 127 | +export { fetchLocation }; |
| 128 | +export { postTargetPrices }; |
0 commit comments