aboutsummaryrefslogtreecommitdiff
path: root/src/hooks/APIClient.ts
blob: 2322af448cf59151547dd45058c0110ed690e4d9 (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
import useSWR from 'swr';
import { get } from '../requests';


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

const arrayOptions = {
  initialData: [],
  revalidateOnMount: true
};

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

export const useProfile = (id: string) => {
  return useSWR(id && `/profiles/${id}`, fetcher, arrayOptions);
};

export const useFeed = () => {
  return useSWR('/feed', fetcher, { ...arrayOptions, revalidateOnFocus: false });
};

export const useFeedback = () => {
  return useSWR('/feedback', fetcher, arrayOptions);
};