forked from open-sauced/app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseUserHighlightReactions.ts
49 lines (44 loc) · 1.48 KB
/
useUserHighlightReactions.ts
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
import useSWR, { Fetcher } from "swr";
import { publicApiFetcher } from "lib/utils/public-api-fetcher";
import { HighlightReactionResponse } from "./useHighlightReactions";
import useSupabaseAuth from "./useSupabaseAuth";
const useUserHighlightReactions = (id: string) => {
const { sessionToken } = useSupabaseAuth();
const { data, error, mutate } = useSWR<HighlightReactionResponse[], Error>(
id ? `user/highlights/${id}/reactions` : null,
publicApiFetcher as Fetcher<HighlightReactionResponse[], Error>
);
const addReaction = async (emojiId: string) => {
const req = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/user/highlights/${id}/reactions/${emojiId}`, {
method: "POST",
headers: {
Authorization: `Bearer ${sessionToken}`,
},
// eslint-disable-next-line no-console
}).catch((err) => console.log(err));
if (req && req.ok) {
mutate();
}
};
const deleteReaction = async (emojiId: string) => {
const req = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/user/highlights/${id}/reactions/${emojiId}`, {
method: "DELETE",
headers: {
Authorization: `Bearer ${sessionToken}`,
},
// eslint-disable-next-line no-console
}).catch((err) => console.log(err));
if (req && req.ok) {
mutate();
}
};
return {
data: data ?? [],
isError: !!error,
isLoading: !error && !data,
mutate,
addReaction,
deleteReaction,
};
};
export default useUserHighlightReactions;