Skip to content

Commit 2336324

Browse files
update
1 parent 3059f58 commit 2336324

File tree

12 files changed

+2078
-271
lines changed

12 files changed

+2078
-271
lines changed

context/GlobalProvider.jsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ export const GlobalContext = createContext();
66

77
const GlobalProvider = ({ children }) => {
88
const [reposData, setReposData] = useState(null);
9+
const [loading, setLoading] = useState(true);
910

1011
useEffect(() => {
11-
// Pour éviter de multiples refetch pendant le dev (github api rate limit)
1212
if (!reposData) {
1313
fetchReposData(ORGNAME)
1414
.then((res) => {
@@ -21,6 +21,9 @@ const GlobalProvider = ({ children }) => {
2121
})
2222
.catch((error) => {
2323
console.log(error);
24+
})
25+
.finally(() => {
26+
setLoading(false);
2427
});
2528
}
2629
// eslint-disable-next-line react-hooks/exhaustive-deps
@@ -30,6 +33,8 @@ const GlobalProvider = ({ children }) => {
3033
<GlobalContext.Provider
3134
value={{
3235
reposData,
36+
loading,
37+
setLoading,
3338
setReposData,
3439
}}
3540
>

hooks/useDiscordInfo.jsx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { useState, useEffect } from "react";
2+
3+
function useDiscordInfo(guildId) {
4+
const [infos, setInfos] = useState(null);
5+
const [loading, setLoading] = useState(true);
6+
const [error, setError] = useState(null);
7+
8+
useEffect(() => {
9+
if (!guildId) {
10+
console.error("Vous devez fournir un id de guild");
11+
setError("Vous devez fournir un id de guild");
12+
setLoading(false);
13+
return;
14+
}
15+
16+
const getDiscordData = async () => {
17+
try {
18+
const response = await fetch(
19+
`https://discord.com/api/v10/guilds/${guildId}/widget.json`,
20+
);
21+
22+
if (!response.ok) {
23+
throw new Error(`Error: ${response.status} ${response.statusText}`);
24+
}
25+
const data = await response.json();
26+
setInfos(data);
27+
} catch (error) {
28+
console.error("Fetch error:", error);
29+
setError(error.message);
30+
} finally {
31+
setLoading(false);
32+
}
33+
};
34+
35+
getDiscordData();
36+
}, [guildId]);
37+
38+
return { infos, loading, error };
39+
}
40+
41+
export default useDiscordInfo;

hooks/useProjectInfo.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import { useMemo } from "react";
33
function useProjectInfo(reposData, githubRepoName) {
44
const findProjectObjectByName = (array, name) => {
55
if (!array) {
6-
console.error("Vous devez fournir un tableau");
6+
console.log("Vous devez fournir un tableau");
77
return null;
88
}
99

1010
if (!name) {
11-
console.error("Vous devez fournir un élément à rechercher");
11+
console.log("Vous devez fournir un élément à rechercher");
1212
return null;
1313
}
1414
return array.find((item) => item.name === name);

0 commit comments

Comments
 (0)