summaryrefslogtreecommitdiffstats
path: root/src/features/offices/hooks/useOffice.ts
diff options
context:
space:
mode:
authorAlberto Duarte Delgado <alberto@albertoduarte.com>2026-03-30 15:55:32 +0100
committerAlberto Duarte Delgado <alberto@albertoduarte.com>2026-03-30 15:55:32 +0100
commit31584b4e5a3a551e2ceb15281a38677c39c46e12 (patch)
treeeae47ea7351aa47f25244b42d8d11b69ee9c9d4e /src/features/offices/hooks/useOffice.ts
parent6aede83b39f1050b7528b243278a85e5623856be (diff)
Offices logic
Diffstat (limited to 'src/features/offices/hooks/useOffice.ts')
-rw-r--r--src/features/offices/hooks/useOffice.ts47
1 files changed, 47 insertions, 0 deletions
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<Office | null>(null);
+ const [loading, setLoading] = useState(true);
+ const [error, setError] = useState<string | null>(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,
+ };
+}