From 7f7f80b4f89a557c69c8e1f5dd1e81633606662a Mon Sep 17 00:00:00 2001 From: GotthardG <51994228+GotthardG@users.noreply.github.com> Date: Tue, 17 Dec 2024 17:06:16 +0100 Subject: [PATCH] Refactor OpenAPI fetcher for improved clarity and robustness Reorganized and enhanced the OpenAPI fetch logic for better maintainability and error handling. Key updates include improved environment variable validation, more detailed error messages, streamlined configuration loading, and additional safety checks for file paths and directories. Added proper logging and ensured the process flow is easy to trace. --- frontend/src/components/ShipmentForm.tsx | 45 +++++++++++++++++------- frontend/src/pages/ShipmentView.tsx | 31 +++++++++++----- 2 files changed, 54 insertions(+), 22 deletions(-) diff --git a/frontend/src/components/ShipmentForm.tsx b/frontend/src/components/ShipmentForm.tsx index cd6466b..28de26a 100644 --- a/frontend/src/components/ShipmentForm.tsx +++ b/frontend/src/components/ShipmentForm.tsx @@ -39,35 +39,54 @@ const ShipmentForm: React.FC = ({ sx = {}, onCancel, refreshS const [errorMessage, setErrorMessage] = React.useState(null); useEffect(() => { - const isTestEnv = import.meta.env.MODE === 'test'; - OpenAPI.BASE = isTestEnv - ? import.meta.env.VITE_OPENAPI_BASE_TEST - : import.meta.env.VITE_OPENAPI_BASE_DEV; + // Detect the current environment + const mode = import.meta.env.MODE; + // Consistent dynamic `OpenAPI.BASE` resolution + OpenAPI.BASE = + mode === 'test' + ? import.meta.env.VITE_OPENAPI_BASE_TEST + : mode === 'production' + ? import.meta.env.VITE_OPENAPI_BASE_PROD + : import.meta.env.VITE_OPENAPI_BASE_DEV; + + // Log warning if `OpenAPI.BASE` is unresolved + if (!OpenAPI.BASE) { + console.error('OpenAPI.BASE is not set. Falling back to a default value.'); + OpenAPI.BASE = 'https://default-url.com'; // Define fallback + } + + console.log('Environment Mode:', mode); + console.log('Resolved OpenAPI.BASE:', OpenAPI.BASE); + + // Fetch necessary data const getContacts = async () => { try { - const c: ContactPerson[] = await ContactsService.getContactsContactsGet(); - setContactPersons(c); + const fetchedContacts: ContactPerson[] = + await ContactsService.getContactsContactsGet(); + setContactPersons(fetchedContacts); } catch { - setErrorMessage('Failed to load contact persons. Please try again later.'); + setErrorMessage('Failed to load contact persons.'); } }; const getAddresses = async () => { try { - const a: Address[] = await AddressesService.getReturnAddressesAddressesGet(); - setReturnAddresses(a); + const fetchedAddresses: Address[] = + await AddressesService.getReturnAddressesAddressesGet(); + setReturnAddresses(fetchedAddresses); } catch { - setErrorMessage('Failed to load return addresses. Please try again later.'); + setErrorMessage('Failed to load return addresses.'); } }; const getProposals = async () => { try { - const p: Proposal[] = await ProposalsService.getProposalsProposalsGet(); - setProposals(p); + const fetchedProposals: Proposal[] = + await ProposalsService.getProposalsProposalsGet(); + setProposals(fetchedProposals); } catch { - setErrorMessage('Failed to load proposals. Please try again later.'); + setErrorMessage('Failed to load proposals.'); } }; diff --git a/frontend/src/pages/ShipmentView.tsx b/frontend/src/pages/ShipmentView.tsx index 354ce4c..b8b601e 100644 --- a/frontend/src/pages/ShipmentView.tsx +++ b/frontend/src/pages/ShipmentView.tsx @@ -15,16 +15,29 @@ const ShipmentView: React.FC = () => { const [isCreatingShipment, setIsCreatingShipment] = useState(false); useEffect(() => { - const isTestEnv = import.meta.env.MODE === 'test'; - const isProdEnv = import.meta.env.MODE === 'prod'; - OpenAPI.BASE = isTestEnv - ? import.meta.env.VITE_OPENAPI_BASE_TEST - : isProdEnv - ? import.meta.env.VITE_OPENAPI_BASE_PROD - : import.meta.env.VITE_OPENAPI_BASE_DEV; + // Detect the current environment + const mode = import.meta.env.MODE; - fetchAndSetShipments(); -}, []); + // Dynamically set `OpenAPI.BASE` based on the mode + OpenAPI.BASE = + mode === 'test' + ? import.meta.env.VITE_OPENAPI_BASE_TEST + : mode === 'production' + ? import.meta.env.VITE_OPENAPI_BASE_PROD + : import.meta.env.VITE_OPENAPI_BASE_DEV; + + // Log warning if `OpenAPI.BASE` is unresolved + if (!OpenAPI.BASE) { + console.error('OpenAPI.BASE is not set. Falling back to a default value.'); + OpenAPI.BASE = 'https://default-url.com'; // Use a consistent fallback + } + + // Debug for mode and resolved `BASE` + console.log('Environment Mode:', mode); + console.log('Resolved OpenAPI.BASE:', OpenAPI.BASE); + + fetchAndSetShipments(); // Fetch data using the base URL + }, []); const handleSelectShipment = (shipment: Shipment | null) => { setSelectedShipment(shipment);