summaryrefslogtreecommitdiffstats
path: root/src/features/offices/hooks/useOffices.ts
blob: 99f8f8e30bc1bee7ac6ecca81a2c2fb21beb4aab (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
import { useEffect, useState } from 'react';
import { officesService } from '@/features/offices/services/officesService';
import { Office } from '@/features/offices/types';
import { getErrorMessage } from '@/shared/lib/errors/getErrorMessage';

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) {
      setError(getErrorMessage(err, 'No se pudieron cargar los espacios'));
    } finally {
      setLoading(false);
    }
  };

  useEffect(() => {
    loadOffices();
  }, []);

  return {
    offices,
    loading,
    error,
    reload: loadOffices,
  };
}