diff options
Diffstat (limited to 'src/features/offices/hooks/useOffices.ts')
| -rw-r--r-- | src/features/offices/hooks/useOffices.ts | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/features/offices/hooks/useOffices.ts b/src/features/offices/hooks/useOffices.ts new file mode 100644 index 0000000..ff08f6c --- /dev/null +++ b/src/features/offices/hooks/useOffices.ts @@ -0,0 +1,34 @@ +import { useEffect, useState } from 'react'; +import { officesService } from '@/features/offices/services/officesService'; +import { Office } from '@/features/offices/types'; + +export function useOffices() { + const [offices, setOffices] = useState<Office[]>([]); + const [loading, setLoading] = useState(true); + const [error, setError] = useState<string | null>(null); + + const loadOffices = async () => { + try { + setError(null); + setLoading(true); + + const data = await officesService.getAll(); + setOffices(data); + } catch (err: any) { + setError(err?.message ?? 'No se pudieron cargar los espacios'); + } finally { + setLoading(false); + } + }; + + useEffect(() => { + loadOffices(); + }, []); + + return { + offices, + loading, + error, + reload: loadOffices, + }; +} |