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);