Skip to content

Commit 8a54c07

Browse files
2 - Setting Up React Query
1 parent cb95595 commit 8a54c07

File tree

3 files changed

+83
-12
lines changed

3 files changed

+83
-12
lines changed

src/components/IssueItem.jsx

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { Link } from "react-router-dom";
2+
import { GoIssueOpened, GoIssueClosed, GoComment } from "react-icons/go";
3+
import { relativeDate } from "../helpers/relativeDate";
4+
5+
export function IssueItem({
6+
title,
7+
number,
8+
assignee,
9+
commentCount,
10+
createdBy,
11+
createdDate,
12+
labels,
13+
status,
14+
}) {
15+
return (
16+
<li>
17+
<div>
18+
{status === "done" || status === "cancelled" ? (
19+
<GoIssueClosed style={{ color: "red" }} />
20+
) : (
21+
<GoIssueOpened style={{ color: "green" }} />
22+
)}
23+
</div>
24+
<div className="issue-content">
25+
<span>
26+
<Link to={`/issue/${number}`}>{title}</Link>
27+
{labels.map((label) => (
28+
<span key={label} className={`label red`}>
29+
{label}
30+
</span>
31+
))}
32+
</span>
33+
<small>
34+
#{number} opened {relativeDate(createdDate)} by {createdBy}
35+
</small>
36+
</div>
37+
{assignee ? <div>{assignee}</div> : null}
38+
<span className="comment-count">
39+
{commentCount > 0 ? (
40+
<>
41+
<GoComment />
42+
{commentCount}
43+
</>
44+
) : null}
45+
</span>
46+
</li>
47+
);
48+
}

src/components/IssuesList.jsx

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
1-
import { Link } from "react-router-dom";
1+
import { useQuery } from "react-query";
2+
import { IssueItem } from "./IssueItem";
23

34
export default function IssuesList() {
5+
const issuesQuery = useQuery(["issues"], () =>
6+
fetch("/api/issues").then((res) => res.json())
7+
);
48
return (
59
<div>
6-
<h1>Issues List</h1>
7-
<ul>
8-
<li>
9-
<Link to="/issue/1">Issue 1</Link>
10-
</li>
11-
</ul>
10+
<h2>Issues List</h2>
11+
{issuesQuery.isLoading ? (
12+
<p>Loading...</p>
13+
) : (
14+
<ul className="issues-list">
15+
{issuesQuery.data.map((issue) => (
16+
<IssueItem
17+
key={issue.id}
18+
title={issue.title}
19+
number={issue.number}
20+
assignee={issue.assignee}
21+
commentCount={issue.comments.length}
22+
createdBy={issue.createdBy}
23+
createdDate={issue.createdDate}
24+
labels={issue.labels}
25+
status={issue.status}
26+
/>
27+
))}
28+
</ul>
29+
)}
1230
</div>
1331
);
1432
}

src/main.jsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import "./index.css";
44
import App from "./App";
55
import { BrowserRouter } from "react-router-dom";
66
import { worker } from "@uidotdev/react-query-api";
7+
import { QueryClient, QueryClientProvider } from "react-query";
8+
9+
const queryClient = new QueryClient();
710

811
new Promise((res) => setTimeout(res, 100))
912
.then(() =>
@@ -15,11 +18,13 @@ new Promise((res) => setTimeout(res, 100))
1518
.then(() => {
1619
ReactDOM.render(
1720
<React.StrictMode>
18-
<BrowserRouter>
19-
<div className="container">
20-
<App />
21-
</div>
22-
</BrowserRouter>
21+
<QueryClientProvider client={queryClient}>
22+
<BrowserRouter>
23+
<div className="container">
24+
<App />
25+
</div>
26+
</BrowserRouter>
27+
</QueryClientProvider>
2328
</React.StrictMode>,
2429
document.getElementById("root")
2530
);

0 commit comments

Comments
 (0)