aboutsummaryrefslogtreecommitdiff
path: root/src/hooks/APIClient.ts
blob: c81b57d00971f5ed606ef407caea521c3c5bbf72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import useSWR, { responseInterface } from 'swr';
import { get } from '../requests';
import { User, Event, Log } from '../types';

type Response<T> = responseInterface<T, Error>;

const fetcher = (endpoint: string) => get(endpoint).then(response => response.data);

export const useUser = (username: string | null): Response<User> => {
  return useSWR(
    username && `/users?username=${username}`,
    (url: string) => get(url).then(response => response.data[0])
  );
};

export const useEvents = (): Response<Event[]> => {
  return useSWR(`/events`, fetcher);
};

export const useEventLogs = (eventId: string): Response<Log[]> => {
  return useSWR(`/logs?eventId=${eventId}`, fetcher);
};