-
Notifications
You must be signed in to change notification settings - Fork 181
Expand file tree
/
Copy pathCardSet.js
More file actions
151 lines (138 loc) · 4.86 KB
/
CardSet.js
File metadata and controls
151 lines (138 loc) · 4.86 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import React, { useState, useEffect, useContext } from "react";
import SingleCard from "./SingleCard";
import axios from "axios";
import { isEmpty } from "lodash";
// import { dotenv } from "dotenv";
// dotenv.config();
//Context
import { ThemeContext } from "../Context/themeContext";
import Loading from "./Loading/index";
const CardSet = ({
language,
inputSearch,
pageNumber,
reducedState,
sortByStars,
sortByForks,
setMaxPageNumber,
setHidePagination,
}) => {
const [repositores, setRepositories] = useState([]);
const [isLoading, setIsLoading] = useState(false);
const [wasRejected, setWasRejected] = useState(false);
const { theme } = useContext(ThemeContext);
const [forksQuery, setForksQuery] = useState("");
const [starsQuery, setStarsQuery] = useState("");
const [CardSetLength, setCardSetLength] = useState(0);
useEffect(() => {
setCardSetLength(document.getElementById("header").getBoundingClientRect().height + "px");
}, []);
let url = `https://api.github.com/search/repositories?q=good-first-issues:>0+language:${language}${
!isEmpty(inputSearch) ? `:${inputSearch}+in%3Atitle` : ""
}&page=${pageNumber}&per_page=10`;
let urlSuffix = "";
useEffect(() => {
const onAppliedFilters = () => {
if (reducedState.minForks !== "" && reducedState.maxForks === "") {
setForksQuery(`forks:>=${reducedState.minForks}`);
} else if (reducedState.maxForks !== "" && reducedState.minForks === "") {
setForksQuery(`forks:<=${reducedState.maxForks}`);
} else if (reducedState.maxForks !== "" && reducedState.minForks !== "") {
setForksQuery(`forks:${reducedState.minForks}..${reducedState.maxForks}`);
} else {
setForksQuery("");
}
if (forksQuery !== "") {
const urlSplits = url.split("?q=");
url = urlSplits[0] + "?q=" + forksQuery + "+" + urlSplits[1];
}
if (reducedState.minStars !== "" && reducedState.maxStars === "") {
setStarsQuery(`stars:>=${reducedState.minForks}`);
} else if (reducedState.maxStars !== "" && reducedState.minStars === "") {
setStarsQuery(`stars:<=${reducedState.maxStars}`);
} else if (reducedState.maxStars !== "" && reducedState.minStars !== "") {
setStarsQuery(`stars:${reducedState.minStars}..${reducedState.maxStars}`);
} else {
setStarsQuery("");
}
if (starsQuery !== "") {
const urlSplits = url.split("?q=");
url = urlSplits[0] + "?q=" + starsQuery + "+" + urlSplits[1];
}
};
onAppliedFilters();
}, [language, inputSearch, pageNumber, reducedState, sortByForks, sortByStars, forksQuery, starsQuery]);
if (sortByStars === "desc") urlSuffix = "&sort=stars&order=desc";
else if (sortByStars === "asc") urlSuffix = "&sort=stars&order=asc";
else if (sortByForks === "desc") urlSuffix = "&sort=forks&order=desc";
else if (sortByForks === "asc") urlSuffix = "&sort=forks&order=asc";
useEffect(() => {
// console.log("stars", sortByStars, "forks", sortByForks);
url += urlSuffix;
console.log(url);
setIsLoading(true);
// GET request using axios inside useEffect React hook
axios
.get(url)
.then(
async (response) => {
// console.log(response.data.items);
let maxPageNumber = Math.floor(response.data.total_count / 10);
setMaxPageNumber(maxPageNumber);
setRepositories(response.data.items);
setWasRejected(false);
setIsLoading(false);
response.data.items.length ? setHidePagination(false) : setHidePagination(true);
},
(rejection) => {
if (rejection.response.status === 403) {
setWasRejected(true);
setHidePagination(true);
}
// console.log(rejection.response.data)
}
)
.catch((errors) => {
setIsLoading(false);
//catch all (show some message)
//console.log(errors)
});
// empty dependency array means this effect will only run once (like componentDidMount in classes)
}, [
language,
inputSearch,
pageNumber,
reducedState,
sortByForks,
sortByStars,
url,
urlSuffix,
setMaxPageNumber,
setHidePagination,
]);
return (
<div
className="flex justify-center items-center"
style={{
backgroundColor: theme.bg,
color: theme.color,
minHeight: `calc(100vh - ${CardSetLength})`,
}}
>
{isLoading ? (
<Loading></Loading>
) : (
<div className={` grid grid-cols-1 lg:grid-cols-3 `}>
{isEmpty(repositores) ? (
<div>
<h5>No issues to be shown at the moment, please try again later.</h5>
</div>
) : (
!isEmpty(repositores) && repositores.map((repo) => <SingleCard key={repo.id} repo={repo} />)
)}
</div>
)}
</div>
);
};
export default CardSet;