-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusePullRequest.js
More file actions
48 lines (44 loc) · 1.42 KB
/
usePullRequest.js
File metadata and controls
48 lines (44 loc) · 1.42 KB
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
import { useState, useEffect } from "react";
import getPullRequest from "../helpers/getPullRequest";
import { useRouter } from "next/router";
import { useErrorStatus } from "./errorHandler";
import getPullRepoInfo from "../helpers/getPullRepoInfo";
import { useApiClient } from "../context/ApiClientContext";
export default function usePullRequest(repository, initialPullRequest = {}) {
const router = useRouter();
const { setErrorStatusCode } = useErrorStatus();
const [pullRequest, setPullRequest] = useState({
iid: router.query.pullRequestIid,
creator: "",
description: "",
comments: [],
reviewers: [],
assignees: [],
labels: [],
issues: [],
head: { repository: {} },
base: { repository: {} },
...initialPullRequest,
});
const [refreshIndex, setRefreshIndex] = useState(1);
const { apiClient } = useApiClient();
const refreshPullRequest = () =>
setRefreshIndex((prevIndex) => prevIndex + 1);
useEffect(() => {
async function initRepository() {
const p = await getPullRequest(
apiClient,
router.query.userId,
router.query.repositoryId,
router.query.pullRequestIid
);
if (p) {
setPullRequest(await getPullRepoInfo(apiClient, p, repository));
} else {
setErrorStatusCode(404);
}
}
initRepository();
}, [router.query, repository.id, refreshIndex]);
return { pullRequest, refreshPullRequest };
}