@@ -34,19 +34,33 @@ export interface IGithubRepos {
3434 * @returns {(IGithubRepos[]) } - Returns an array with the properties: name, topics, html_url, description, id, homepage.
3535 */
3636export function useGitHubAutomatedRepos ( usernameGitHub : string , keyWordDeploy : string ) {
37- const [ repository , setRepository ] = useState < IGithubRepos [ ] > ( [ ] ) ;
3837
39- useEffect ( ( ) => {
40- fetch ( `https://api.github.com/users/${ usernameGitHub } /repos?sort=created&per_page=999` )
41- . then ( ( response ) => response . json ( ) )
42- . then ( ( data ) => setRepository ( data ) ) ;
43- } , [ ] ) ;
44-
45- let dataFilter = [ ] ;
38+ const [ data , setData ] = useState < IGithubRepos [ ] > ( [ ] ) ;
39+ const [ loading , setLoading ] = useState < boolean > ( true ) ;
40+ const [ error , setError ] = useState < string > ( '' ) ;
4641
47- dataFilter = repository . filter ( ( item : IGithubRepos ) => item . topics . includes ( keyWordDeploy as never ) ) ;
42+ useEffect ( ( ) => {
43+ const fetchData = async ( ) => {
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 ) ) ) ;
52+ } catch ( err ) {
53+ setError ( ( err as Error ) . message ) ;
54+ } finally {
55+ setLoading ( false ) ;
56+ }
57+ } ;
4858
49- const typeImg = [ 'svg' , 'png' ] ;
59+ fetchData ( ) ;
60+ } , [ usernameGitHub , keyWordDeploy ] ) ;
61+
62+
63+ const typeImg = [ 'svg' , 'png' ] ;
5064 function checkImage ( usernameGitHub : string , repositoryName : string ) : string {
5165 let checkURL = '' ;
5266 typeImg . map ( ( type ) => {
@@ -61,8 +75,11 @@ export function useGitHubAutomatedRepos(usernameGitHub: string, keyWordDeploy: s
6175 } ) ;
6276 return checkURL ;
6377 }
78+
79+
80+
81+ const repository = data . map ( ( item : IGithubRepos ) => ( {
6482
65- return dataFilter . map ( ( item : IGithubRepos ) => ( {
6683 id : item . id ,
6784 name : item . name ,
6885 html_url : item . html_url ,
@@ -71,6 +88,8 @@ export function useGitHubAutomatedRepos(usernameGitHub: string, keyWordDeploy: s
7188 homepage : item . homepage ,
7289 banner : checkImage ( usernameGitHub , item . name ) ,
7390 } ) ) ;
91+ return { repository, loading, error } ;
92+
7493}
7594
7695export function IconsData ( ) {
0 commit comments