Skip to content

feat: pass repoName through router parameters #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function App() {
<AuthContextProvider>
<Routes>
<Route path="/login" element={<GitHubLogin />} />
<Route path="/issue" element={<IssuePage />} />
<Route path="/:repoName/issue" element={<IssuePage />} />
<Route path="/comment" element={<CommentPage />} />
</Routes>
</AuthContextProvider>
Expand Down
63 changes: 28 additions & 35 deletions src/IssuePage.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useState, useEffect } from "react";
import { useState, useEffect, useContext } from "react";
import api from "./utils/api";
import { ActionList, Box, Text, Link } from "@primer/react";
import { useParams } from "react-router-dom";
import { AuthContext } from "./context/authContext";

const IssuePage = () => {
const [apiResult, setApiResult] = useState([]);
Expand All @@ -11,29 +13,33 @@ const IssuePage = () => {
const [searchValue, setSearchValue] = useState("");
const [searchResult, setSearchResult] = useState([]);
const [isSearching, setIsSearching] = useState(false);
const { repoName } = useParams();
const { user } = useContext(AuthContext);

useEffect(() => {
const fetchData = async () => {
try {
const [issuesData, labelsData] = await Promise.all([
api.getAllIssue("JuneLin2001", "91APP_front-end-class"), //TODO:帳號跟repo要從context取
api.getAllLabelFromIssue("JuneLin2001", "91APP_front-end-class"),
]);

setApiResult(issuesData);

const uniqueAuthors = [
...new Set(issuesData.map((issue) => issue.user.login)),
];
setAuthors(uniqueAuthors);
setLabels(labelsData);
} catch (error) {
console.error("Failed to fetch data:", error);
if (user && user.reloadUserInfo && user.reloadUserInfo.screenName) {
console.log("repoName:", repoName);
console.log("user:", user.reloadUserInfo.screenName);
try {
const [issuesData, labelsData] = await Promise.all([
api.getAllIssue(user.reloadUserInfo.screenName, repoName),
api.getAllLabelFromIssue(user.reloadUserInfo.screenName, repoName),
]);

setApiResult(issuesData);

const uniqueAuthors = [...new Set(issuesData.map((issue) => issue.user.login))];
setAuthors(uniqueAuthors);
setLabels(labelsData);
} catch (error) {
console.error("Failed to fetch data:", error);
}
}
};

fetchData();
}, []);
}, [user, repoName]);

const handleAuthorChange = (e) => {
setIsSearching(false);
Expand All @@ -54,12 +60,7 @@ const IssuePage = () => {
setIsSearching(true);

try {
const searchResults = await api.getSearchIssues(
//TODO:帳號跟repo要從context取
"JuneLin2001",
"91APP_front-end-class",
searchValue
);
const searchResults = await api.getSearchIssues(user.reloadUserInfo.screenName, repoName, searchValue);
setSearchResult(searchResults);
} catch (error) {
console.error("Failed to fetch data:", error);
Expand All @@ -71,14 +72,11 @@ const IssuePage = () => {
.filter(
(issue) =>
(selectedAuthor === "all" || issue.user.login === selectedAuthor) &&
(selectedLabel === "all" ||
issue.labels.some((label) => label.name === selectedLabel))
(selectedLabel === "all" || issue.labels.some((label) => label.name === selectedLabel))
)
.filter((issue) => !issue.pull_request);

const issuesToDisplay = isSearching
? searchResult
: filteredIssues(apiResult);
const issuesToDisplay = isSearching ? searchResult : filteredIssues(apiResult);

return (
<div>
Expand All @@ -105,11 +103,7 @@ const IssuePage = () => {
</select>

<form>
<input
placeholder="is:issue is:open"
value={searchValue}
onChange={handleSearchChange}
></input>
<input placeholder="is:issue is:open" value={searchValue} onChange={handleSearchChange}></input>
<button onClick={handleSearchClick}>搜尋</button>
</form>
{/* <ul>
Expand Down Expand Up @@ -162,8 +156,7 @@ const IssuePage = () => {
</Box>
<Box mt={1}>
<Text color="fg.muted">
opened on {new Date(issue.created_at).toLocaleDateString()} by{" "}
{issue.user.login}
opened on {new Date(issue.created_at).toLocaleDateString()} by {issue.user.login}
</Text>
</Box>
</ActionList.Item>
Expand Down
22 changes: 15 additions & 7 deletions src/utils/GitHubLogin.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AuthContext } from "../context/authContext";
import api from "./api";
import { Avatar, Label, RelativeTime } from "@primer/react";
import { DataTable } from "@primer/react/experimental";
import { Link } from "@primer/react";

const GitHubLogin = () => {
const { user, githubLogin, githubLogout } = useContext(AuthContext);
Expand Down Expand Up @@ -32,16 +33,23 @@ const GitHubLogin = () => {
header: "Repository Name",
field: "name",
rowHeader: true,
},
{
header: "Topics",
field: "topics",
renderCell: (row) => {
return row.topics.map((topic, index) => {
return <Label key={index}>{topic}</Label>;
});
return (
<Link href={`/${row.name}/issue`} key={row.name}>
{row.name}
</Link>
);
},
},
// {
// header: "Topics",
// field: "topics",
// renderCell: (row) => {
// return row.topics.map((topic, index) => {
// return <Label key={index}>{topic}</Label>;
// });
// },
// },
{
header: "Private",
field: "private",
Expand Down