summaryrefslogtreecommitdiffstats
path: root/src/features/reservations/hooks/useReservations.ts
blob: c993c89412fffc8b0435c365efacc1e4f29e6fa5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { useEffect, useState } from 'react';
import { reservationsService } from '@/features/reservations/services/reservationsService';
import { Reservation } from '@/features/reservations/types';
import { useAuthSession } from '@/providers/AuthProvider';

export function useReservations() {
  const { user } = useAuthSession();

  const [reservations, setReservations] = useState<Reservation[]>([]);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);

  const loadReservations = async () => {
    if (!user?.uid) {
      setReservations([]);
      setLoading(false);
      return;
    }

    try {
      setError(null);
      setLoading(true);

      const data = await reservationsService.getByUserId(user.uid);
      setReservations(data);
    } catch (err: any) {
      setError(err?.message ?? 'No se pudieron cargar las reservas');
    } finally {
      setLoading(false);
    }
  };

  useEffect(() => {
    loadReservations();
  }, [user?.uid]);

  return {
    reservations,
    loading,
    error,
    reload: loadReservations,
  };
}