Skip to content

Commit 8d8e76e

Browse files
committed
feat: use http service
1 parent 66edac3 commit 8d8e76e

File tree

4 files changed

+54
-31
lines changed

4 files changed

+54
-31
lines changed

frontend/src/components/LocationModal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import GlobalState from "../GlobalState";
1515
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';
18-
import {UserRequest, UserResponse} from "./LoginModal";
1918
import styles from './modal.module.scss';
19+
import { UserRequest, UserResponse } from "../service/dtos";
2020

2121
interface PostCodeLocation {
2222
Message: string;

frontend/src/components/LoginModal.tsx

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,8 @@ import { UserDataState } from "../GlobalState";
1717
import { useState, ChangeEventHandler, FormEvent, BaseSyntheticEvent } from "react";
1818
import { Box, TextField, Button, Tab, Tabs, Dialog, DialogContent } from '@mui/material';
1919
import { useNavigate } from "react-router";
20-
//import { Token } from "@mui/icons-material";
21-
22-
export interface UserRequest {
23-
Username: string;
24-
Password: string;
25-
Latitude?: number;
26-
Longitude?: number;
27-
SearchRadius?: number;
28-
PostCode?: number;
29-
TargetDiesel?: string;
30-
TargetE10?: string;
31-
TargetE5?: string;
32-
}
33-
34-
export interface UserResponse {
35-
Token?: string;
36-
Message?: string;
37-
PostCode?: number;
38-
Uuid?: string;
39-
Longitude?: number;
40-
Latitude?: number;
41-
SearchRadius?: number;
42-
TargetDiesel?: number;
43-
TargetE5?: number;
44-
TargetE10?: number;
45-
}
20+
import { UserRequest, UserResponse } from "../service/dtos";
21+
import { postLogin, postSignin } from "../service/http-client";
4622

4723
interface MsgData {
4824
jwtToken?: string;
@@ -111,8 +87,7 @@ const LoginModal = () => {
11187
};
11288
setResponseMsg('');
11389
controller = new AbortController();
114-
const httpResponse = activeTab === 0 ? await fetch('/appuser/login', requestOptions) : await fetch('/appuser/signin', requestOptions);
115-
const userResponse = await httpResponse.json() as UserResponse;
90+
const userResponse = activeTab === 0 ? await postLogin(userName, password1, controller) : await postSignin(userName, password1, controller);
11691
controller = null;
11792
//console.log(userResponse);
11893
if (!userResponse?.Message && !!userResponse?.Token && userResponse.Token?.length > 10 && !!userResponse?.Uuid && userResponse.Uuid?.length > 10) {

frontend/src/service/dtos.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,28 @@ export interface TimeSlot {
114114
e10: number;
115115
diesel: number;
116116
}
117+
118+
export interface UserRequest {
119+
Username: string;
120+
Password: string;
121+
Latitude?: number;
122+
Longitude?: number;
123+
SearchRadius?: number;
124+
PostCode?: number;
125+
TargetDiesel?: string;
126+
TargetE10?: string;
127+
TargetE5?: string;
128+
}
129+
130+
export interface UserResponse {
131+
Token?: string;
132+
Message?: string;
133+
PostCode?: number;
134+
Uuid?: string;
135+
Longitude?: number;
136+
Latitude?: number;
137+
SearchRadius?: number;
138+
TargetDiesel?: number;
139+
TargetE5?: number;
140+
TargetE10?: number;
141+
}

frontend/src/service/http-client.ts

Lines changed: 25 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 } from "./dtos";
2+
import { GasPriceAvgs, GasStation, Notification, TimeSlotResponse, UserRequest, UserResponse } from "./dtos";
33

44
const fetchGasStations = async function(jwtToken: string, controller: AbortController | null, globalUserDataState: UserDataState): Promise<GasStation[]> {
55
const requestOptions2 = {
@@ -44,7 +44,30 @@ const requestOptions2 = {
4444
return myResult;
4545
}
4646

47+
const postLogin = async function(userName: string, password1: string, controller: AbortController | null): Promise<UserResponse> {
48+
const requestOptions = loginSigninOptions(userName, password1, controller);
49+
const result = await fetch('/appuser/login', requestOptions);
50+
return result.json() as Promise<UserResponse>;
51+
}
52+
53+
const postSignin = async function(userName: string, password1: string, controller: AbortController | null): Promise<UserResponse> {
54+
const requestOptions = loginSigninOptions(userName, password1, controller);
55+
const result = await fetch('/appuser/signin', requestOptions);
56+
return result.json() as Promise<UserResponse>;
57+
}
58+
59+
const loginSigninOptions = (userName: string, password1: string, controller: AbortController | null) => {
60+
return {
61+
method: 'POST',
62+
headers: { 'Content-Type': 'application/json' },
63+
body: JSON.stringify({ Username: userName, Password: password1 } as UserRequest),
64+
signal: controller?.signal
65+
};
66+
};
67+
4768
export { fetchGasStations };
4869
export { fetchPriceAvgs };
4970
export { fetchUserNotifications };
50-
export { fetchTimeSlots };
71+
export { fetchTimeSlots };
72+
export { postLogin };
73+
export { postSignin };

0 commit comments

Comments
 (0)