-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlightDataLayer.jsx
More file actions
159 lines (144 loc) · 5.83 KB
/
Copy pathFlightDataLayer.jsx
File metadata and controls
159 lines (144 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { useState } from 'react';
import { useError } from '../components/errorDialog/ErrorProvider.jsx';
//Defines the initial flight object.
const initialFlight = {
airlineID: 0,
codeShares: [],
description: '',
departTime: new Date(new Date().setHours(0, 0, 0, 0)),
destination: '',
flightNumber: '',
gateID: 0,
gateName: '',
name: '',
sortDestinationID: 0,
sortDestinationName: '',
};
export default initialFlight;
//The function returns a hook to interact with the server for the flights.
export function useFlightDataLayer() {
const { showError } = useError();
const [flights, setFlights] = useState([]);
const [addFlightSuccess, setAddFlightSuccess] = useState(false);
const [addFlightValidationProblemDetails, setAddFlightValidationProblemDetails] = useState(null);
const [deleteFlightSuccess, setDeleteFlightSuccess] = useState(false);
const [updateFlightSuccess, setUpdateFlightSuccess] = useState(false);
const [updateFlightValidationProblemDetails, setUpdateFlightValidationProblemDetails] = useState(null);
//Constants for the status codes returned by the server.
const BadRequestCode = 400;
const NotFoundCode = 404;
const ConflictCode = 409;
const InternalServerError = 500;
//The function adds a flight to the server.
//@param {object} flight The flight to add.
const addFlight = (flight) => {
clearStates();
let prepFlight = prepFlightObject(flight);
fetch('api/Flight', {
method: 'POST',
body: JSON.stringify(prepFlight),
headers: {
"Content-Type": "application/json",
},
})
.then(response => {
if (response.ok) {
setAddFlightSuccess(true);
}
else if (response.status === BadRequestCode) {
response.json().then(validationProblemDetails => setAddFlightValidationProblemDetails(validationProblemDetails));
}
else if (response.status === InternalServerError) {
response.json().then(problemDetails => showError(problemDetails.detail));
}
else {
showError('Failed to create the flight because of an error on the server.');
}
})
.catch(error => showError('Failed to communicate with the server.'));
};
//The function clears the states before an operation.
const clearStates = () => {
setAddFlightSuccess(false);
setAddFlightValidationProblemDetails(null);
setDeleteFlightSuccess(false);
setUpdateFlightSuccess(false);
setUpdateFlightValidationProblemDetails(null);
};
//The function deletes a flight from the server.
//@param {object} flight The flight to delete.
const deleteFlight = (flight) => {
clearStates();
fetch('/api/Flight/' + flight.integer64ID, {
method: 'DELETE'
})
.then(response => {
if (response.ok) {
setDeleteFlightSuccess(true);
}
else if (response.status === NotFoundCode || response.status === InternalServerError) {
response.json().then(problemDetails => showError(problemDetails.detail));
}
else {
showError('Failed to delete the flight because of an error on the server.');
}
})
.catch(error => showError('Failed to communicate with the server.'));
};
//The function retrieves the flights from the server.
const getFlights = () => {
fetch('api/Flight/All')
.then(response => response.json())
.then(json => setFlights(json))
.catch(error => showError('Failed to communicate with the server.'));
};
//The function prepares the flight object before being sent to the server.
const prepFlightObject = (flight) => {
const options = { hour12: false, hour: '2-digit', minute: '2-digit', second: '2-digit' };
return {
...flight,
//Only send the time to the server because the property on the server side is a C# TimeSpan.
departTime: flight.departTime.toLocaleTimeString('en-US', options)
};
};
//The function updates a flight to the server.
//@param {object} flight The flight to update.
const updateFlight = (flight) => {
clearStates();
let prepFlight = prepFlightObject(flight);
fetch('api/Flight', {
method: 'PUT',
body: JSON.stringify(prepFlight),
headers: {
"Content-Type": "application/json",
},
})
.then(response => {
if (response.ok) {
setUpdateFlightSuccess(true);
}
else if (response.status === BadRequestCode) {
response.json().then(validationProblemDetails => setUpdateFlightValidationProblemDetails(validationProblemDetails));
}
else if (response.status === NotFoundCode || response.status == ConflictCode || response.status === InternalServerError) {
response.json().then(problemDetails => showError(problemDetails.detail));
}
else {
showError('Failed to update the flight because of an error on the server.');
}
})
.catch(error => showError('Failed to communicate with the server.'));
};
return {
flights,
getFlights,
addFlight,
addFlightValidationProblemDetails,
addFlightSuccess,
deleteFlight,
deleteFlightSuccess,
updateFlight,
updateFlightValidationProblemDetails,
updateFlightSuccess
};
};