From 31584b4e5a3a551e2ceb15281a38677c39c46e12 Mon Sep 17 00:00:00 2001 From: Alberto Duarte Delgado Date: Mon, 30 Mar 2026 15:55:32 +0100 Subject: Offices logic --- src/features/offices/hooks/useOffice.ts | 47 +++++++++++++++++++++++++++++++ src/features/offices/hooks/useOffice.ts~ | 0 src/features/offices/hooks/useOffices.ts | 34 ++++++++++++++++++++++ src/features/offices/hooks/useOffices.ts~ | 0 4 files changed, 81 insertions(+) create mode 100644 src/features/offices/hooks/useOffice.ts create mode 100644 src/features/offices/hooks/useOffice.ts~ create mode 100644 src/features/offices/hooks/useOffices.ts create mode 100644 src/features/offices/hooks/useOffices.ts~ (limited to 'src/features/offices/hooks') diff --git a/src/features/offices/hooks/useOffice.ts b/src/features/offices/hooks/useOffice.ts new file mode 100644 index 0000000..87d47b5 --- /dev/null +++ b/src/features/offices/hooks/useOffice.ts @@ -0,0 +1,47 @@ +import { useEffect, useState } from 'react'; +import { officesService } from '@/features/offices/services/officesService'; +import { Office } from '@/features/offices/types'; + +export function useOffice(officeId?: string) { + const [office, setOffice] = useState(null); + const [loading, setLoading] = useState(true); + const [error, setError] = useState(null); + + const loadOffice = async () => { + if (!officeId) { + setError('No se indicó una oficina válida'); + setLoading(false); + return; + } + + try { + setError(null); + setLoading(true); + + const data = await officesService.getById(officeId); + + if (!data) { + setOffice(null); + setError('La oficina no existe'); + return; + } + + setOffice(data); + } catch (err: any) { + setError(err?.message ?? 'No se pudo cargar la oficina'); + } finally { + setLoading(false); + } + }; + + useEffect(() => { + loadOffice(); + }, [officeId]); + + return { + office, + loading, + error, + reload: loadOffice, + }; +} diff --git a/src/features/offices/hooks/useOffice.ts~ b/src/features/offices/hooks/useOffice.ts~ new file mode 100644 index 0000000..e69de29 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([]); + const [loading, setLoading] = useState(true); + const [error, setError] = useState(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, + }; +} diff --git a/src/features/offices/hooks/useOffices.ts~ b/src/features/offices/hooks/useOffices.ts~ new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3-54-g00ecf