aboutsummaryrefslogtreecommitdiff
path: root/src/hooks/APIClient.ts
blob: cf5d247da1a00bd52045e2feee91d1f8df775f93 (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
import useSWR, { responseInterface } from 'swr';
import { User, Poll, Feedback } from 'which-types';
import { get } from '../requests';

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 useProfile = (id: string): Response<Poll[]> => {
  return useSWR(id && `/profiles/${id}`, fetcher);
};

export const useFeed = (): Response<Poll[]> => {
  return useSWR('/feed', fetcher, { revalidateOnFocus: false });
};

export const useFeedback = (): Response<Feedback[]> => {
  return useSWR('/feedback', fetcher);
};