/
useIssue.ts
47 lines (41 loc) · 1.4 KB
/
useIssue.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
import { useCallback, useEffect, useState } from "react";
import { Comment, Issue, IssueResponse } from "@/pages/api/issues/[issueNumber]";
export const useIssue = (issueNumber: string) => {
const [loading, setLoading] = useState(true);
const [issue, setIssue] = useState<Issue | undefined>();
const [comments, setComments] = useState<Comment[]>([]);
const getIssue = useCallback(async (): Promise<IssueResponse | undefined> => {
if (!issueNumber) return;
const res = await fetch(`/api/issues/${issueNumber}`);
return await res.json();
}, [issueNumber]);
const createComment = useCallback(
async (body: string) => {
const res = await fetch(`/api/issues/${issueNumber}/comments`, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({
issueId: issue?.id,
body,
}),
});
const newComment: Comment = await res.json();
setComments([...comments, newComment]);
},
[issueNumber, issue, comments]
);
useEffect(() => {
(async () => {
const res = await getIssue();
if (!res) return;
const { comments, ...issue } = res.repository.issue;
setIssue(issue);
setComments(comments.nodes);
setLoading(false);
})();
}, [getIssue]);
return { issue, comments, createComment, loading };
};