summaryrefslogtreecommitdiffstats
path: root/src/features/reservations/hooks/useReservations.ts
blob: 86c6ad3257330356be8bef113f4862f85364f656 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { reservationsService } from "@/features/reservations/services/reservationsService";
import { Reservation } from "@/features/reservations/types";
import { useAuthSession } from "@/providers/AuthProvider";
import { getErrorMessage } from "@/shared/lib/errors/getErrorMessage";
import { useFocusEffect } from "expo-router";
import { useCallback, useEffect, useState } from "react";

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

  const [reservations, setReservations] = useState<Reservation[]>([]);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);
  const [cancelingReservationId, setCancelingReservationId] = useState<
    string | null
  >(null);
  const [checkingInReservationId, setCheckingInReservationId] = 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) {
      setError(getErrorMessage(err, "No se pudieron cargar las reservas"));
    } finally {
      setLoading(false);
    }
  };

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

  useFocusEffect(
    useCallback(() => {
      void loadReservations();
    }, [user?.uid]),
  );

  const cancelReservation = async (reservationId: string) => {
    try {
      setError(null);
      setCancelingReservationId(reservationId);

      await reservationsService.cancel(reservationId);
      await loadReservations();
    } catch (err) {
      setError(getErrorMessage(err, "No se pudo cancelar la reserva"));
    } finally {
      setCancelingReservationId(null);
    }
  };

  const checkInReservation = async (reservationId: string) => {
    try {
      setError(null);
      setCheckingInReservationId(reservationId);

      await reservationsService.checkIn(reservationId);
      await loadReservations();
    } catch (err) {
      setError(getErrorMessage(err, "No se pudo registrar la llegada"));
    } finally {
      setCheckingInReservationId(null);
    }
  };

  return {
    reservations,
    // reservas activas en orden normal para mostrar la mas proxima primero
    activeReservations: reservations.filter(
      (reservation) => reservation.status === "active",
    ),
    // historial invertido para mostrar la mas reciente primero
    historyReservations: reservations
      .filter((reservation) => reservation.status !== "active")
      .reverse(),
    loading,
    error,
    cancelingReservationId,
    checkingInReservationId,
    cancelReservation,
    checkInReservation,
    reload: loadReservations,
  };
}