Skip to content

Commit dda12e8

Browse files
authored
#18 [useGithubAutomatedRepos] Repeat Data - add implement data tratament (#27)
* feat: add implement data tratament with the possibility of waiting, loading while the data actually loads.
1 parent a59cb08 commit dda12e8

File tree

1 file changed

+19
-50
lines changed

1 file changed

+19
-50
lines changed

src/hooks/useGithubAutomatedRepos.ts

Lines changed: 19 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ export interface IGithubRepos {
2626
id: number;
2727
homepage: string;
2828
banner: () => string;
29-
3029
}
3130

3231
/**
@@ -36,60 +35,31 @@ export interface IGithubRepos {
3635
*/
3736
export function useGitHubAutomatedRepos(usernameGitHub: string, keyWordDeploy: string) {
3837

39-
const [repository, setRepository] = useState<IGithubRepos[]>([]);
40-
41-
useEffect(() => {
42-
fetch(`https://api.github.com/users/${usernameGitHub}/repos?sort=created&per_page=999`)
43-
.then((response) => response.json())
44-
.then((data) => setRepository(data));
45-
}, []);
46-
47-
let dataFilter = [];
48-
38+
const [data, setData] = useState<IGithubRepos[]>([]);
39+
const [loading, setLoading] = useState<boolean>(true);
40+
const [error, setError] = useState<string>('');
4941

50-
const [data,setData] = useState <IGithubRepos[]>([])
51-
const [loading, setLoading] =useState <boolean>(true)
52-
const [error, setError] = useState<string>("")
5342
useEffect(() => {
5443
const fetchData = async () => {
55-
setLoading(true)
56-
try {
57-
const response = await fetch(`https://api.github.com/users/${usernameGitHub}/repos?sort=created&per_page=999`);
58-
if (!response.ok) {
59-
throw new Error(`Unsuccessful request: ${response.statusText}`);
60-
}
61-
const jsonData = await response.json();
62-
setData(jsonData.filter((item: IGithubRepos) => item.topics.includes(keyWordDeploy as never)));
44+
setLoading(true);
45+
try {
46+
const response = await fetch(`https://api.github.com/users/${usernameGitHub}/repos?sort=created&per_page=999`);
47+
if (!response.ok) {
48+
throw new Error(`Unsuccessful request: ${response.statusText}`);
49+
}
50+
const jsonData = await response.json();
51+
setData(jsonData.filter((item: IGithubRepos) => item.topics.includes(keyWordDeploy as never)));
6352
} catch (err) {
64-
setError((err as Error).message)
65-
}finally{
66-
setLoading(false)
53+
setError((err as Error).message);
54+
} finally {
55+
setLoading(false);
6756
}
68-
};
69-
70-
fetchData();
71-
}, [usernameGitHub,keyWordDeploy]);
57+
};
7258

59+
fetchData();
60+
}, [usernameGitHub, keyWordDeploy]);
7361

74-
let repository = data.map((item: IGithubRepos) => ({
75-
76-
const typeImg = ['svg', 'png'];
77-
function checkImage(usernameGitHub: string, repositoryName: string): string {
78-
let checkURL = '';
79-
typeImg.map((type)=> {
80-
const url = `https://raw.githubusercontent.com/${usernameGitHub}/${repositoryName}/main/src/assets/imgs/banner.${type}`;
81-
const http = new XMLHttpRequest();
82-
http.open('HEAD', url, false);
83-
http.send();
84-
85-
if (http.status === 200) {
86-
checkURL = url;
87-
}
88-
});
89-
return checkURL;
90-
}
91-
92-
return dataFilter.map((item: IGithubRepos) => ({
62+
const repository = data.map((item: IGithubRepos) => ({
9363

9464
id: item.id,
9565
name: item.name,
@@ -99,8 +69,7 @@ export function useGitHubAutomatedRepos(usernameGitHub: string, keyWordDeploy: s
9969
homepage: item.homepage,
10070
banner: checkImage(usernameGitHub, item.name),
10171
}));
102-
return {repository, loading, error};
103-
72+
return { repository, loading, error };
10473

10574
}
10675

0 commit comments

Comments
 (0)