summaryrefslogtreecommitdiffstats
path: root/src/features/offices
diff options
context:
space:
mode:
Diffstat (limited to 'src/features/offices')
-rw-r--r--src/features/offices/hooks/useOffice.ts47
-rw-r--r--src/features/offices/hooks/useOffice.ts~0
-rw-r--r--src/features/offices/hooks/useOffices.ts34
-rw-r--r--src/features/offices/hooks/useOffices.ts~0
-rw-r--r--src/features/offices/services/officesService.ts30
-rw-r--r--src/features/offices/services/officesService.ts~0
-rw-r--r--src/features/offices/types.ts7
-rw-r--r--src/features/offices/types.ts~0
8 files changed, 118 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,
+ };
+}
diff --git a/src/features/offices/hooks/useOffice.ts~ b/src/features/offices/hooks/useOffice.ts~
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/features/offices/hooks/useOffice.ts~
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,
+ };
+}
diff --git a/src/features/offices/hooks/useOffices.ts~ b/src/features/offices/hooks/useOffices.ts~
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/features/offices/hooks/useOffices.ts~
diff --git a/src/features/offices/services/officesService.ts b/src/features/offices/services/officesService.ts
new file mode 100644
index 0000000..72cf081
--- /dev/null
+++ b/src/features/offices/services/officesService.ts
@@ -0,0 +1,30 @@
+import { collection, doc, getDoc, getDocs } from 'firebase/firestore';
+import { db } from '@/shared/lib/firebase/config';
+import { Office } from '@/features/offices/types';
+
+function mapOffice(id: string, data: any): Office {
+ return {
+ id,
+ name: data.name ?? '',
+ location: data.location ?? '',
+ capacity: data.capacity ?? 0,
+ description: data.description ?? '',
+ };
+}
+
+export const officesService = {
+ async getAll(): Promise<Office[]> {
+ const snapshot = await getDocs(collection(db, 'offices'));
+ return snapshot.docs.map((item) => mapOffice(item.id, item.data()));
+ },
+
+ async getById(id: string): Promise<Office | null> {
+ const snapshot = await getDoc(doc(db, 'offices', id));
+
+ if (!snapshot.exists()) {
+ return null;
+ }
+
+ return mapOffice(snapshot.id, snapshot.data());
+ },
+};
diff --git a/src/features/offices/services/officesService.ts~ b/src/features/offices/services/officesService.ts~
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/features/offices/services/officesService.ts~
diff --git a/src/features/offices/types.ts b/src/features/offices/types.ts
new file mode 100644
index 0000000..9f9d039
--- /dev/null
+++ b/src/features/offices/types.ts
@@ -0,0 +1,7 @@
+export type Office = {
+ id: string;
+ name: string;
+ location: string;
+ capacity: number;
+ description?: string;
+};
diff --git a/src/features/offices/types.ts~ b/src/features/offices/types.ts~
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/features/offices/types.ts~