Skip to content

Commit 319c887

Browse files
authored
Merge pull request #34 from DIGOARTHUR/31-feat-add-more-quantity-of-banner-image
feat(banner):add the possibility of more banner images
2 parents 03a9e7f + 0ca697c commit 319c887

File tree

13 files changed

+230
-206
lines changed

13 files changed

+230
-206
lines changed

src/components/ProjectIcons.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,9 @@ type Props = {
1414
*/
1515

1616
export function ProjectIcons({ itemTopics, className }: Props) {
17-
1817
return (
1918
<div>
20-
{projectIconsURL[itemTopics] ? (
21-
<img className={className} alt={projectIconsURL[itemTopics]} src={projectIconsURL[itemTopics]} />
22-
) : (
23-
<> </>
24-
)}
19+
{projectIconsURL[itemTopics] ? <img className={className} alt={projectIconsURL[itemTopics]} src={projectIconsURL[itemTopics]} /> : <> </>}
2520
</div>
2621
);
2722
}

src/components/StackIcons.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import React from 'react';
22

3-
import { stackIconsURL } from '../icons/stackIconsURL';
43
import { projectIconsURL } from '../icons/projectIconsURL';
4+
import { stackIconsURL } from '../icons/stackIconsURL';
5+
56
type Props = {
67
itemTopics: string;
78
className?: string;
@@ -13,7 +14,6 @@ type Props = {
1314
* @returns {ReactNode} - Return tag img, stack svg icon.
1415
*/
1516
export function StackIcons({ itemTopics, className }: Props) {
16-
1717
return itemTopics === 'deploy' || projectIconsURL[itemTopics] ? (
1818
<> </>
1919
) : (
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { ReactNode, ReactPortal } from 'react';
1+
import React, { ReactNode } from 'react';
22

33
import { projectIconsURL } from '../../icons/projectIconsURL';
44
import { stackIconsURL } from '../../icons/stackIconsURL';
@@ -14,15 +14,13 @@ type Props = {
1414
* @param {string} className - Optional: style className.
1515
* @returns {ReactNode} - Return tag p, stack text.
1616
*/
17-
export function StackLabels({ itemTopics, className = 'styleStackLabels' }: Props){
18-
19-
20-
return itemTopics === 'deploy' || projectIconsURL[itemTopics] || stackIconsURL[itemTopics] ==undefined ? (
17+
export function StackLabels({ itemTopics, className = 'styleStackLabels' }: Props): ReactNode {
18+
return itemTopics === 'deploy' || projectIconsURL[itemTopics] || stackIconsURL[itemTopics] === undefined ? (
2119
<> </>
2220
) : (
2321
<>
24-
<style>{css}</style>
25-
<p className={className}>{itemTopics}</p>
26-
</>
22+
<style>{css}</style>
23+
<p className={className}>{itemTopics}</p>
24+
</>
2725
);
2826
}

src/hook/api/fetchGitHubAPI.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export async function fetchGitHubAPI(usernameGitHub: string) {
2+
const response = await fetch(`https://api.github.com/users/${usernameGitHub}/repos?sort=created&per_page=999`);
3+
if (!response.ok) {
4+
throw new Error(`Unsuccessful request: ${response.statusText}`);
5+
}
6+
const jsonData = await response.json();
7+
return jsonData;
8+
}

src/hook/api/fetchGitHubBanner.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export async function fetchGitHubBanner(usernameGitHub: string, repositoryName: string) {
2+
try {
3+
const response = await fetch(`https://api.github.com/repos/${usernameGitHub}/${repositoryName}/contents/src/assets/imgs`);
4+
if (!response.ok) {
5+
throw new Error(`Unsuccessful request: ${response.statusText}`);
6+
}
7+
const jsonData = await response.json();
8+
9+
return jsonData;
10+
} catch (err) {
11+
return err as Error;
12+
}
13+
}
Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
2-
31
import { useEffect, useState } from 'react';
4-
import { checkImage } from './utils/checkImage';
5-
import { fetchData } from './utils/fetchGitHubAPI';
2+
3+
import { _handleRepository } from './utils/_handleRepository';
64

75
export interface IGithubRepos {
86
name: string;
@@ -11,38 +9,28 @@ export interface IGithubRepos {
119
description: string;
1210
id: number;
1311
homepage: string;
14-
banner: () => string;
12+
banner: string[];
13+
}
14+
15+
interface IGitHubAutomatedRepos {
16+
repositoriesData: IGithubRepos[];
17+
loading: boolean;
18+
error: string;
1519
}
1620

1721
/**
1822
* @param {string} usernameGitHub - Insert your username GitHub Ex.: https://github.com/USERNAME
1923
* @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-.
20-
* @returns {(IGithubRepos[])} - Returns an array with the properties: name, topics, html_url, description, id, homepage.
24+
* @returns {(IGitHubAutomatedRepos)} - repositoriesData[] with the properties: name, topics[], html_url, description, id, homepage, banner[]. Loading to await for data. Error for a bad request.
2125
*/
22-
export function useGitHubAutomatedRepos(usernameGitHub: string, keyWordDeploy: string) {
23-
24-
const [data, setData] = useState<IGithubRepos[]>([]);
26+
export function useGitHubAutomatedRepos(usernameGitHub: string, keyWordDeploy: string): IGitHubAutomatedRepos {
27+
const [repositoriesData, setRepositoriesData] = useState<IGithubRepos[]>([]);
2528
const [loading, setLoading] = useState<boolean>(true);
2629
const [error, setError] = useState<string>('');
2730

2831
useEffect(() => {
29-
fetchData(usernameGitHub,keyWordDeploy,setData,setLoading,setError);
30-
}, [usernameGitHub, keyWordDeploy]);
31-
32-
const repository = data.map((item: IGithubRepos) => ({
33-
id: item.id,
34-
name: item.name,
35-
html_url: item.html_url,
36-
description: item.description,
37-
topics: item.topics,
38-
homepage: item.homepage,
39-
banner: checkImage(usernameGitHub, item.name),
40-
}));
41-
return { repository, loading, error };
32+
_handleRepository(usernameGitHub, keyWordDeploy, setRepositoriesData, setLoading, setError);
33+
}, []);
4234

35+
return { repositoriesData, loading, error };
4336
}
44-
45-
46-
47-
48-

src/hook/utils/_handleBanner.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { fetchGitHubBanner } from '../api/fetchGitHubBanner';
2+
3+
export interface IBanner {
4+
download_url: string;
5+
}
6+
7+
/**
8+
* @param {string} usernameGitHub - Insert your username GitHub Ex.: https://github.com/USERNAME
9+
* @param {string} repositoryName - Insert the repository name
10+
* @returns {(Promise<string[])} - repositoriesData[] with the properties: name, topics[], html_url, description, id, homepage, banner[]. Loading to await for data. Error for a bad request.
11+
*/
12+
13+
export async function _handleBanner(usernameGitHub: string, repositoryName: string): Promise<string[]> {
14+
const banners_url: string[] = [];
15+
16+
await fetchGitHubBanner(usernameGitHub, repositoryName).then((repositorie_Banners: IBanner[]) => {
17+
repositorie_Banners.map((item: IBanner) => {
18+
if (item.download_url.includes('banner')) {
19+
banners_url.push(item.download_url);
20+
}
21+
});
22+
});
23+
24+
return banners_url;
25+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { fetchGitHubAPI } from '../api/fetchGitHubAPI';
2+
import { _handleBanner } from './_handleBanner';
3+
4+
export interface IGithubRepos {
5+
name: string;
6+
topics: string[];
7+
html_url: string;
8+
description: string;
9+
id: number;
10+
homepage: string;
11+
banner: string[];
12+
}
13+
14+
/**
15+
* @param {string} usernameGitHub - Insert your username GitHub Ex.: https://github.com/USERNAME
16+
* @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-.
17+
* @param {React.Dispatch<React.SetStateAction<IGithubRepos[]>} setRepositoriesData - Insert <useState> function to receive data from the GitHub API linked to the keyword. !! Don't forget to enter keywords in GitHub projects. !!
18+
* @param {React.Dispatch<React.SetStateAction<boolean>} setLoading - Insert <useState> function to receive loading states.
19+
* @param {React.Dispatch<React.SetStateAction<string>} setError - Insert <useState> function to receive request error message
20+
*/
21+
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+
}

src/hook/utils/checkImage.ts

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

src/hook/utils/fetchGitHubAPI.ts

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

0 commit comments

Comments
 (0)