diff --git a/frontend/src/hooks/useShipments.tsx b/frontend/src/hooks/useShipments.tsx new file mode 100644 index 0000000..ee264d9 --- /dev/null +++ b/frontend/src/hooks/useShipments.tsx @@ -0,0 +1,37 @@ +import { useState, useEffect } from 'react'; +import { ShipmentsService, Shipment, ContactPerson } from '../../openapi'; + +const useShipments = () => { + const [shipments, setShipments] = useState([]); + const [error, setError] = useState(null); + const [defaultContactPerson, setDefaultContactPerson] = useState(); + + const fetchAndSetShipments = async () => { + try { + const shipmentsData = await ShipmentsService.fetchShipmentsShipmentsGet(); + setShipments(shipmentsData); + } catch (error) { + console.error('Failed to fetch shipments:', error); + setError('Failed to fetch shipments. Please try again later.'); + } + }; + + const fetchDefaultContactPerson = async () => { + try { + const contacts = await ShipmentsService.getShipmentContactPersonsShipmentsContactPersonsGet(); + setDefaultContactPerson(contacts[0]); + } catch (error) { + console.error('Failed to fetch contact persons:', error); + setError('Failed to load contact persons. Please try again later.'); + } + }; + + useEffect(() => { + fetchAndSetShipments(); + fetchDefaultContactPerson(); + }, []); + + return { shipments, error, defaultContactPerson, fetchAndSetShipments }; +}; + +export default useShipments; \ No newline at end of file