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.
This commit is contained in:
parent
2581d7cedc
commit
7f7f80b4f8
@ -39,35 +39,54 @@ const ShipmentForm: React.FC<ShipmentFormProps> = ({ sx = {}, onCancel, refreshS
|
||||
const [errorMessage, setErrorMessage] = React.useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
const isTestEnv = import.meta.env.MODE === 'test';
|
||||
OpenAPI.BASE = isTestEnv
|
||||
// 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.');
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -15,16 +15,29 @@ const ShipmentView: React.FC<ShipmentViewProps> = () => {
|
||||
const [isCreatingShipment, setIsCreatingShipment] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const isTestEnv = import.meta.env.MODE === 'test';
|
||||
const isProdEnv = import.meta.env.MODE === 'prod';
|
||||
OpenAPI.BASE = isTestEnv
|
||||
// Detect the current environment
|
||||
const mode = import.meta.env.MODE;
|
||||
|
||||
// Dynamically set `OpenAPI.BASE` based on the mode
|
||||
OpenAPI.BASE =
|
||||
mode === 'test'
|
||||
? import.meta.env.VITE_OPENAPI_BASE_TEST
|
||||
: isProdEnv
|
||||
: mode === 'production'
|
||||
? import.meta.env.VITE_OPENAPI_BASE_PROD
|
||||
: import.meta.env.VITE_OPENAPI_BASE_DEV;
|
||||
|
||||
fetchAndSetShipments();
|
||||
}, []);
|
||||
// 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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user