Passed
Push — development ( cdc56b...8e8ffd )
by Vad
18:54 queued 08:30
created

frontend/src/helpers/bike-functions.tsx   A

Complexity

Total Complexity 11
Complexity/F 0

Size

Lines of Code 141
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 110
dl 0
loc 141
rs 10
c 0
b 0
f 0
wmc 11
mnd 11
bc 11
fnc 0
bpm 0
cpm 0
noi 0
1
import axios, { AxiosError } from 'axios';
2
import { API_URL, getHeader } from '../helpers/config';
3
import { toast } from 'react-toastify';
4
import { toastOptionsError, toastOptionsSuccess } from '../helpers/config';
5
6
7
export type City = {
8
    id: string;
9
    name: string;
10
    latitude: number | null;
11
    longitude: number | null;
12
    createdAt: string;
13
    updatedAt: string; 
14
};
15
  
16
export type BikeStatus = {
17
    batteryLevel: number;
18
    city: City;
19
    createdAt: string;
20
    id: string;
21
    latitude: number;
22
    longitude: number;
23
    status: "Rented" | "Available" | "Service" | string; // Add other possible status values if needed
24
    updatedAt: string; // ISO timestamp as a string
25
};
26
27
type bikeArray = {
28
    [key: string] : any;
29
}
30
31
type RentBikeResponse = Array<{
32
    id: number;
33
    [key: string]: any; // Allow additional unknown keys
34
  }>;
35
36
export const bikePerCity = async (city: string, token: string, status = '') : Promise<any> =>
37
{
38
    let data;
39
    
40
    try {
41
            const response = await axios.get(`${API_URL}/bike/city/${city}`, getHeader(token));
42
            if (status === '')
43
            {
44
                data = response;
45
            } else {
46
                data = response.data.filter((item : BikeStatus) => item.status === status);
47
            }
48
    }
49
    catch(error: any) {
50
        toast.error(error.response.data.message, toastOptionsError)
51
52
    }
53
    console.log(data);
54
    return data;
55
}
56
57
export const allBikes = async ( token:string ) : Promise<any> =>
58
{
59
        let data;
60
        try {
61
                const response = await axios.get(`${API_URL}/bike`, getHeader(token));
62
                data = response.data;
63
        }
64
        catch(error: any) {
65
            toast.error(error.response.data.message, toastOptionsError)
66
67
        }
68
        return data;
69
}
70
71
export const rentBike = async (bikeId: string, token: string) : Promise<RentBikeResponse | any> =>
72
{
73
        let data: RentBikeResponse | any;
74
        try
75
        {
76
            const response = await axios.post(`${API_URL}/rental/bike/${bikeId}`, {}, getHeader(token));
77
78
            data = {... response.data, "statusCode": 200 };
79
80
                
81
        }
82
        catch(error: any) {
83
            toast.error(error.response.data.message,toastOptionsError)
84
            data = error.response.data;
85
        }
86
        return data;
87
}
88
89
export const returnBike = async (tripID: string | null, token: string) : Promise<any> =>
90
{
91
        let data: RentBikeResponse | any;
92
93
            try {
94
                    const response = await axios.post(`${API_URL}/rental/${tripID}/end`, {}, getHeader(token));
95
                    data = {... response.data, "statusCode": 200 };
96
            }
97
            catch(error: any) {
98
99
                toast.error(error.response.data.message, toastOptionsError)
100
                data = error.response.data;
101
            }
102
            return data;
103
}
104
105
106
export const allRentals = async (token: string): Promise<any> => {
107
    let data = {};
108
    try {
109
        let me = await axios.get(`${API_URL}/auth/me`, getHeader(token));
110
        let userId = me.data.githubId;
111
        const response = await axios.get(`${API_URL}/rental/customer/${userId}`, getHeader(token));
112
        data = response.data;
113
    } catch(error)
114
    {
115
        console.log(`No rentals found for user`);
116
    }
117
    return data;
118
}
119
120
121
export const bikeIdByFive = async (bikeIdFive: string | null, token: string): Promise<string> => {
122
    const data = await allBikes(token);
123
    const bike = data.find((item: bikeArray) => item.id.startsWith(bikeIdFive?.toLowerCase()));
124
125
    return bike.id;
126
}
127
128
export const returnAllRentals = async (token: string): Promise<any> => {
129
    try {
130
        let me = await axios.get(`${API_URL}/auth/me`, getHeader(token));
131
        let userId = me.data.githubId;
132
        const data = await axios.post(`${API_URL}/rental/${userId}/end-all-travels`,{}, getHeader(token));
133
        return data
134
    } catch(error)
135
    {  
136
        
137
        if (error instanceof AxiosError && error.response?.data?.message) {
138
            toast.error(`Rentals were not returned: ${error.response.data.message}`, toastOptionsError);
139
        }
140
    }
141
}