Skip to content

Commit df5d45b

Browse files
authored
Merge pull request #35 from DIGOARTHUR/33-feat-api-implement-react-query
feat(API): implement react query
2 parents 6efe0f4 + 86df408 commit df5d45b

File tree

12 files changed

+120
-6728
lines changed

12 files changed

+120
-6728
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
2-
dist
2+
dist
3+
.DS_Store

.husky/commit-msg

Lines changed: 0 additions & 4 deletions
This file was deleted.

.husky/pre-commit

Lines changed: 0 additions & 5 deletions
This file was deleted.

.husky/prepare-commit-msg

Lines changed: 0 additions & 20 deletions
This file was deleted.

package-lock.json

Lines changed: 0 additions & 6615 deletions
This file was deleted.

package.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
"yalc:update": "npx yalc push --scripts --update --replace",
1111
"lint": "eslint .",
1212
"lint:fix": "eslint --fix .",
13-
"prepare": "husky install",
1413
"lint-staged": "lint-staged"
1514
},
1615
"lint-staged": {
@@ -31,7 +30,7 @@
3130
"devDependencies": {
3231
"@commitlint/cli": "^17.4.2",
3332
"@commitlint/config-conventional": "^17.4.2",
34-
"@types/react": "^18.0.26",
33+
"@types/react": "^18.3.12",
3534
"@typescript-eslint/eslint-plugin": "^5.50.0",
3635
"@typescript-eslint/parser": "^5.50.0",
3736
"commitizen": "^4.3.0",
@@ -49,17 +48,19 @@
4948
"eslint-plugin-react": "^7.28.0",
5049
"eslint-plugin-react-hooks": "^4.3.0",
5150
"eslint-plugin-typescript": "^0.14.0",
52-
"husky": "^8.0.3",
5351
"lint-staged": "^13.1.0",
5452
"prettier": "^2.8.3",
55-
"react": "^18.2.0",
56-
"react-dom": "^18.2.0",
57-
"typescript": "^4.9.4"
53+
"react": "^18.3.1",
54+
"react-dom": "^18.3.1",
55+
"typescript": "^5.7.2"
5856
},
5957
"config": {
6058
"commitizen": {
6159
"path": "./node_modules/cz-conventional-changelog"
6260
}
6361
},
64-
"dependencies": {}
62+
"dependencies": {
63+
"@tanstack/react-query": "^5.61.5",
64+
"@types/react-dom": "^18.3.1"
65+
}
6566
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React from 'react';
2+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';;
3+
4+
const queryClient = new QueryClient({
5+
defaultOptions: {
6+
queries: {
7+
staleTime: 1000 * 60 * 5, // Cache válido por 5 minutos
8+
refetchOnWindowFocus: false, // Não refetch quando o foco retorna
9+
10+
},
11+
},
12+
});
13+
14+
interface Props {
15+
children: React.ReactNode;
16+
}
17+
18+
export const ReactQueryProvider: React.FC<Props> = ({ children }) => (
19+
<QueryClientProvider client={queryClient}>
20+
{children}
21+
</QueryClientProvider>
22+
);

src/hook/useGithubAutomatedRepos.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { useEffect, useState } from 'react';
22

3-
import { _handleRepository } from './utils/_handleRepository';
3+
import { _handleBanner } from './utils/_handleBanner';
44

5+
import { useQuery } from '@tanstack/react-query';
6+
import { fetchRepositories } from './utils/_handleRepository';
57
export interface IGithubRepos {
68
name: string;
79
topics: string[];
@@ -23,14 +25,10 @@ interface IGitHubAutomatedRepos {
2325
* @param {string} keyWordDeploy - Insert a keyword chosen by you. - This key is responsible for managing your projects on GitHub in topics field Ex.: https://github.com/DIGOARTHUR/github-automated-repos#--about-library-.
2426
* @returns {(IGitHubAutomatedRepos)} - repositoriesData[] with the properties: name, topics[], html_url, description, id, homepage, banner[]. Loading to await for data. Error for a bad request.
2527
*/
26-
export function useGitHubAutomatedRepos(usernameGitHub: string, keyWordDeploy: string): IGitHubAutomatedRepos {
27-
const [repositoriesData, setRepositoriesData] = useState<IGithubRepos[]>([]);
28-
const [loading, setLoading] = useState<boolean>(true);
29-
const [error, setError] = useState<string>('');
30-
31-
useEffect(() => {
32-
_handleRepository(usernameGitHub, keyWordDeploy, setRepositoriesData, setLoading, setError);
33-
}, []);
34-
35-
return { repositoriesData, loading, error };
28+
export function useGitHubAutomatedRepos(usernameGitHub: string, keyWordDeploy: string) {
29+
return useQuery<IGithubRepos[]>({
30+
queryKey: ["githubRepos", usernameGitHub, keyWordDeploy],
31+
queryFn: () => fetchRepositories(usernameGitHub, keyWordDeploy),
32+
33+
});
3634
}
Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { fetchGitHubAPI } from '../api/fetchGitHubAPI';
22
import { _handleBanner } from './_handleBanner';
3+
import { useQuery } from '@tanstack/react-query';
4+
5+
36

47
export interface IGithubRepos {
58
name: string;
@@ -11,6 +14,8 @@ export interface IGithubRepos {
1114
banner: string[];
1215
}
1316

17+
18+
1419
/**
1520
* @param {string} usernameGitHub - Insert your username GitHub Ex.: https://github.com/USERNAME
1621
* @param {string} keyWordDeploy - Insert a keyword chosen by you. - This key is responsible for managing your projects on GitHub in topics field Ex.: https://github.com/DIGOARTHUR/github-automated-repos#--about-library-.
@@ -19,38 +24,31 @@ export interface IGithubRepos {
1924
* @param {React.Dispatch<React.SetStateAction<string>} setError - Insert <useState> function to receive request error message
2025
*/
2126

22-
export async function _handleRepository(
23-
usernameGitHub: string,
24-
keyWordDeploy: string,
25-
setRepositoriesData: React.Dispatch<React.SetStateAction<IGithubRepos[]>>,
26-
setLoading: React.Dispatch<React.SetStateAction<boolean>>,
27-
setError: React.Dispatch<React.SetStateAction<string>>
28-
) {
29-
setLoading(true);
30-
try {
31-
const jsonData = await fetchGitHubAPI(usernameGitHub);
32-
33-
const datafilter = jsonData.filter((item: IGithubRepos) => item.topics.includes(keyWordDeploy));
34-
35-
const repositories = await Promise.all(
36-
datafilter.map(async (item: IGithubRepos) => {
37-
const banner = await _handleBanner(usernameGitHub, item.name);
38-
return {
39-
id: item.id,
40-
name: item.name,
41-
html_url: item.html_url,
42-
description: item.description,
43-
topics: item.topics,
44-
homepage: item.homepage,
45-
banner,
46-
};
47-
})
48-
);
49-
50-
setRepositoriesData(repositories);
51-
} catch (err) {
52-
setError((err as Error).message);
53-
} finally {
54-
setLoading(false);
55-
}
56-
}
27+
// Função de busca
28+
export const fetchRepositories = async (usernameGitHub: string, keyWordDeploy: string): Promise<IGithubRepos[]> => {
29+
const jsonData = await fetchGitHubAPI(usernameGitHub);
30+
31+
const datafilter = jsonData.filter((item: IGithubRepos) => item.topics.includes(keyWordDeploy));
32+
33+
const repositories = await Promise.all(
34+
datafilter.map(async (item: IGithubRepos) => {
35+
const banner = await _handleBanner(usernameGitHub, item.name);
36+
return {
37+
id: item.id,
38+
name: item.name,
39+
html_url: item.html_url,
40+
description: item.description,
41+
topics: item.topics,
42+
homepage: item.homepage,
43+
banner,
44+
};
45+
})
46+
);
47+
48+
return repositories;
49+
};
50+
51+
// Hook personalizado
52+
export function _handleRepository(usernameGitHub: string, keyWordDeploy: string) {
53+
54+
}

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ export { ProjectIcons } from './components/ProjectIcons';
22
export { StackIcons } from './components/StackIcons';
33
export { StackLabels } from './components/StackLabels/StackLabels';
44
export { IGithubRepos, useGitHubAutomatedRepos } from './hook/useGithubAutomatedRepos';
5+
export { ReactQueryProvider } from './hook/api/QueryClientProvider';
6+
export {_handleRepository } from "./hook/utils/_handleRepository"

0 commit comments

Comments
 (0)