validateNumberCode()} onChange={(e) => setNumberCode(e.target.value)} />
{numberCodeValidationError && {numberCodeValidationError}}
+
+
+ element.integer64ID == airline.sortDestinationID)} options={sortDestinations} optionLabel="name" filter placeholder="Select a default sort destination for the airline's baggage" onBlur={(e) => validateSortDestination()} onChange={(e) => setSortDestination(e.value)} />
+ {sortDestinationValidationError && {sortDestinationValidationError}}
+
>
);
diff --git a/jmayer.example.aspreact.client/src/components/airline/AirlinePage.jsx b/jmayer.example.aspreact.client/src/components/airline/AirlinePage.jsx
index f72ea5e..7ecf1d1 100644
--- a/jmayer.example.aspreact.client/src/components/airline/AirlinePage.jsx
+++ b/jmayer.example.aspreact.client/src/components/airline/AirlinePage.jsx
@@ -86,6 +86,7 @@ export default function AirlinePage() {
+
diff --git a/jmayer.example.aspreact.client/src/components/flightSchedule/FlightAddEditDialog.jsx b/jmayer.example.aspreact.client/src/components/flightSchedule/FlightAddEditDialog.jsx
index c7241bb..21e7e5b 100644
--- a/jmayer.example.aspreact.client/src/components/flightSchedule/FlightAddEditDialog.jsx
+++ b/jmayer.example.aspreact.client/src/components/flightSchedule/FlightAddEditDialog.jsx
@@ -17,7 +17,7 @@ import { useSortDestinationDataLayer } from '../../datalayers/SortDestinationDat
//@param {function} props.hide Used to hide the dialog.
export default function FlightAddEditDialog({ newRecord, flight, setFlight, visible, hide }) {
const { airlines, getAirlines } = useAirlineDataLayer();
- const { addFlight, addFlightServerSideResult, addFlightSuccess, updateFlight, updateFlightServerSideResult, updateFlightSuccess } = useFlightDataLayer();
+ const { addFlight, addFlightValidationProblemDetails, addFlightSuccess, updateFlight, updateFlightValidationProblemDetails, updateFlightSuccess } = useFlightDataLayer();
const { gates, getGates } = useGateDataLayer();
const { sortDestinations, getSortDestinations } = useSortDestinationDataLayer();
const [airlineValidationError, setAirlineValidationError] = useState('');
@@ -51,15 +51,15 @@ export default function FlightAddEditDialog({ newRecord, flight, setFlight, visi
hide();
}
- //Handle displays server side errors.
- if (addFlightServerSideResult !== null) {
- processServerSideValidationResult(addFlightServerSideResult);
+ //Handle displayings server side errors.
+ if (addFlightValidationProblemDetails !== null) {
+ processValidationProblemDetails(addFlightValidationProblemDetails);
}
- else if (updateFlightServerSideResult !== null) {
- processServerSideValidationResult(updateFlightServerSideResult);
+ else if (updateFlightValidationProblemDetails !== null) {
+ processValidationProblemDetails(updateFlightValidationProblemDetails);
}
- }, [addFlightServerSideResult, addFlightSuccess, updateFlightServerSideResult, updateFlightSuccess]);
+ }, [addFlightValidationProblemDetails, addFlightSuccess, updateFlightValidationProblemDetails, updateFlightSuccess]);
//The function clears the validation and closes the dialog.
const closeDialog = () => {
@@ -82,16 +82,16 @@ export default function FlightAddEditDialog({ newRecord, flight, setFlight, visi
return airlinePass && destinationPass && gatePass && flightNumberPass && sortDestinaitonPass;
};
- //The function processes the server side validation result and sets any validation errors.
- //@param {object} serverSideValidationResult What the server found wrong with the user input.
- const processServerSideValidationResult = (serverSideValidationResult) => {
- for (const error of serverSideValidationResult.errors) {
- switch (error.propertyName) {
+ //The function processes the validation problem details returned by the server.
+ //@param {object} result The validation problem details returned by the server.
+ const processValidationProblemDetails = (details) => {
+ for (const key in details.errors) {
+ switch (key) {
case 'Destination':
- setDestinationValidationError(error.errorMessage);
+ setDestinationValidationError(details.errors[key][0]);
break;
case 'FlightNumber':
- setFlightNumberValidationError(error.errorMessage);
+ setFlightNumberValidationError(details.errors[key][0]);
break;
}
}
diff --git a/jmayer.example.aspreact.client/src/datalayers/AirlineDataLayer.jsx b/jmayer.example.aspreact.client/src/datalayers/AirlineDataLayer.jsx
index 46763a4..08aef90 100644
--- a/jmayer.example.aspreact.client/src/datalayers/AirlineDataLayer.jsx
+++ b/jmayer.example.aspreact.client/src/datalayers/AirlineDataLayer.jsx
@@ -1,6 +1,5 @@
import { useState } from 'react';
import { useError } from '../components/errorDialog/ErrorProvider.jsx';
-import { shapeValidationResult } from './DataLayerHelper.jsx';
//Defines the initial airline object.
const initialAirline = {
@@ -9,6 +8,8 @@ const initialAirline = {
iata: '',
icao: '',
numberCode: '',
+ sortDestinationID: 0,
+ sortDestinationName: '',
};
export default initialAirline;
@@ -17,10 +18,10 @@ export function useAirlineDataLayer() {
const { showError } = useError();
const [airlines, setAirlines] = useState([]);
const [addAirlineSuccess, setAddAirlineSuccess] = useState(false);
- const [addAirlineServerSideResult, setAddAirlineServerSideResult] = useState(null);
+ const [addAirlineValidationProblemDetails, setAddAirlineValidationProblemDetails] = useState(null);
const [deleteAirlineSuccess, setDeleteAirlineSuccess] = useState(false);
const [updateAirlineSuccess, setUpdateAirlineSuccess] = useState(false);
- const [updateAirlineServerSideResult, setUpdateAirlineServerSideResult] = useState(null);
+ const [updateAirlineValidationProblemDetails, setUpdateAirlineValidationProblemDetails] = useState(null);
//The function adds an airline to the server.
//@param {object} airline The airline to add.
@@ -38,8 +39,11 @@ export function useAirlineDataLayer() {
if (response.ok) {
setAddAirlineSuccess(true);
}
- else if (response.status == 400) {
- response.json().then(serverSideValidationResult => setAddAirlineServerSideResult(shapeValidationResult(serverSideValidationResult)));
+ else if (response.status === 400) {
+ response.json().then(validationProblemDetails => setAddAirlineValidationProblemDetails(validationProblemDetails));
+ }
+ else if (response.status === 500) {
+ response.json().then(problemDetails => showError(problemDetails.detail));
}
else {
showError('Failed to create the airline because of an error on the server.');
@@ -50,11 +54,11 @@ export function useAirlineDataLayer() {
//The function clears the states before an operation.
const clearStates = () => {
- setAddAirlineServerSideResult(null);
setAddAirlineSuccess(false);
+ setAddAirlineValidationProblemDetails(null);
setDeleteAirlineSuccess(false);
- setUpdateAirlineServerSideResult(null);
setUpdateAirlineSuccess(false);
+ setUpdateAirlineValidationProblemDetails(null);
};
//The function deletes an airline from the server.
@@ -69,6 +73,9 @@ export function useAirlineDataLayer() {
if (response.ok) {
setDeleteAirlineSuccess(true);
}
+ else if (response.status === 500) {
+ response.json().then(problemDetails => showError(problemDetails.detail));
+ }
else {
showError('Failed to delete the airline because of an error on the server.');
}
@@ -100,11 +107,11 @@ export function useAirlineDataLayer() {
if (response.ok) {
setUpdateAirlineSuccess(true);
}
- else if (response.status == 400) {
- response.json().then(serverSideValidationResult => setUpdateAirlineServerSideResult(shapeValidationResult(serverSideValidationResult)));
+ else if (response.status === 400) {
+ response.json().then(validationProblemDetails => setUpdateAirlineValidationProblemDetails(validationProblemDetails));
}
- else if (response.status == 409) {
- showError('The submitted data was detected to be out of date; please refresh and try again.');
+ else if (response.status == 409 || response.status === 500) {
+ response.json().then(problemDetails => showError(problemDetails.detail));
}
else {
showError('Failed to update the airline because of an error on the server.');
@@ -117,12 +124,12 @@ export function useAirlineDataLayer() {
airlines,
getAirlines,
addAirline,
- addAirlineServerSideResult,
+ addAirlineValidationProblemDetails,
addAirlineSuccess,
deleteAirline,
deleteAirlineSuccess,
updateAirline,
- updateAirlineServerSideResult,
+ updateAirlineValidationProblemDetails,
updateAirlineSuccess
};
};
\ No newline at end of file
diff --git a/jmayer.example.aspreact.client/src/datalayers/DataLayerHelper.jsx b/jmayer.example.aspreact.client/src/datalayers/DataLayerHelper.jsx
deleted file mode 100644
index dfd25d7..0000000
--- a/jmayer.example.aspreact.client/src/datalayers/DataLayerHelper.jsx
+++ /dev/null
@@ -1,25 +0,0 @@
-//The function shapes the validation result into a C# ServerSideValidationResult object. This is done
-//because the server can send a ValidationProblemDetails object instead of a ServerSideValidationResult object;
-//ASP.NET core checks data annotations and sends a ValidationProblemDetails object on failure.
-export function shapeValidationResult(result) {
- if (!Array.isArray(result.errors)) {
- let newResult = {
- errors: []
- };
-
- //errors will be a dictionary where the property name is the key and the value is a list of error messages.
- //The loop will convert the dictionary into an array of objects where each object is a property name and error message.
- for (let key in result.errors) {
- for (const errorMessage of result.errors[key]) {
- newResult.errors.push({
- propertyName: key,
- errorMessage: errorMessage
- });
- }
- }
-
- result = newResult;
- }
-
- return result;
-}
\ No newline at end of file
diff --git a/jmayer.example.aspreact.client/src/datalayers/FlightDataLayer.jsx b/jmayer.example.aspreact.client/src/datalayers/FlightDataLayer.jsx
index 8675dd4..a5cdb1a 100644
--- a/jmayer.example.aspreact.client/src/datalayers/FlightDataLayer.jsx
+++ b/jmayer.example.aspreact.client/src/datalayers/FlightDataLayer.jsx
@@ -1,6 +1,5 @@
import { useState } from 'react';
import { useError } from '../components/errorDialog/ErrorProvider.jsx';
-import { shapeValidationResult } from './DataLayerHelper.jsx';
//Defines the initial flight object.
const initialFlight = {
@@ -22,11 +21,11 @@ export default initialFlight;
export function useFlightDataLayer() {
const { showError } = useError();
const [flights, setFlights] = useState([]);
- const [addFlightServerSideResult, setAddFlightServerSideResult] = useState(null);
const [addFlightSuccess, setAddFlightSuccess] = useState(false);
+ const [addFlightValidationProblemDetails, setAddFlightValidationProblemDetails] = useState(null);
const [deleteFlightSuccess, setDeleteFlightSuccess] = useState(false);
- const [updateFlightServerSideResult, setUpdateFlightServerSideResult] = useState(null);
const [updateFlightSuccess, setUpdateFlightSuccess] = useState(false);
+ const [updateFlightValidationProblemDetails, setUpdateFlightValidationProblemDetails] = useState(null);
//The function adds a flight to the server.
//@param {object} flight The flight to add.
@@ -46,8 +45,11 @@ export function useFlightDataLayer() {
if (response.ok) {
setAddFlightSuccess(true);
}
- else if (response.status == 400) {
- response.json().then(serverSideValidationResult => setAddFlightServerSideResult(shapeValidationResult(serverSideValidationResult)));
+ else if (response.status === 400) {
+ response.json().then(validationProblemDetails => setAddFlightValidationProblemDetails(validationProblemDetails));
+ }
+ else if (response.status === 500) {
+ response.json().then(problemDetails => showError(problemDetails.detail));
}
else {
showError('Failed to create the flight because of an error on the server.');
@@ -58,11 +60,11 @@ export function useFlightDataLayer() {
//The function clears the states before an operation.
const clearStates = () => {
- setAddFlightServerSideResult(null);
setAddFlightSuccess(false);
+ setAddFlightValidationProblemDetails(null);
setDeleteFlightSuccess(false);
- setUpdateFlightServerSideResult(null);
setUpdateFlightSuccess(false);
+ setUpdateFlightValidationProblemDetails(null);
};
//The function deletes a flight from the server.
@@ -77,6 +79,9 @@ export function useFlightDataLayer() {
if (response.ok) {
setDeleteFlightSuccess(true);
}
+ else if (response.status === 500) {
+ response.json().then(problemDetails => showError(problemDetails.detail));
+ }
else {
showError('Failed to delete the flight because of an error on the server.');
}
@@ -120,11 +125,11 @@ export function useFlightDataLayer() {
if (response.ok) {
setUpdateFlightSuccess(true);
}
- else if (response.status == 400) {
- response.json().then(serverSideValidationResult => setUpdateFlightServerSideResult(shapeValidationResult(serverSideValidationResult)));
+ else if (response.status === 400) {
+ response.json().then(validationProblemDetails => setUpdateFlightValidationProblemDetails(validationProblemDetails));
}
- else if (response.status == 409) {
- showError('The submitted data was detected to be out of date; please refresh and try again.');
+ else if (response.status == 409 || response.status === 500) {
+ response.json().then(problemDetails => showError(problemDetails.detail));
}
else {
showError('Failed to update the flight because of an error on the server.');
@@ -137,12 +142,12 @@ export function useFlightDataLayer() {
flights,
getFlights,
addFlight,
- addFlightServerSideResult,
+ addFlightValidationProblemDetails,
addFlightSuccess,
deleteFlight,
deleteFlightSuccess,
updateFlight,
- updateFlightServerSideResult,
+ updateFlightValidationProblemDetails,
updateFlightSuccess
};
};
\ No newline at end of file