11import "./App.css" ;
2- import { useState } from "react" ;
2+ import { useState , useEffect } from "react" ;
33import Header from "./components/header" ;
44import SearchBar from "./components/searchBar" ;
55import Card from "./components/card" ;
66
7- const cards = [
8- {
9- title : "facebook/react" ,
10- description : "placeholder description" ,
11- stars : 500 ,
12- forks : 100 ,
13- } ,
14- {
15- title : "vuejs/vue" ,
16- description : "vue vuejs" ,
17- stars : 500 ,
18- forks : 100 ,
19- } ,
20- {
21- title : "sveltejs/svelte" ,
22- description : "placeholder description" ,
23- stars : 500 ,
24- forks : 100 ,
25- } ,
26- ] ;
27-
287function App ( ) {
29- const [ filterCards , setFilterCards ] = useState ( cards ) ;
8+ const [ filterCards , setFilterCards ] = useState ( [ ] ) ;
309
3110 const cardsJSX = filterCards . map ( ( card ) => (
3211 < Card
33- title = { card . title }
12+ title = { card . name }
3413 description = { card . description }
35- stars = { card . stars }
14+ stars = { card . score }
3615 forks = { card . forks }
3716 />
3817 ) ) ;
39- console . log ( "render" , filterCards ) ;
18+
19+ useEffect ( ( ) => {
20+ fetch ( "https://api.github.com/search/repositories?q=stars:>10000" , {
21+ headers : {
22+ Authorization : "Bearer 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" ,
23+ } ,
24+ } )
25+ . then ( ( response ) => response . json ( ) )
26+ . then ( ( data ) => {
27+ console . log ( data ) ;
28+ setFilterCards ( data . items ) ;
29+ } ) ;
30+ } , [ ] ) ;
4031 return (
4132 < >
4233 < Header />
@@ -45,14 +36,12 @@ function App() {
4536 onSearch = { ( value ) => {
4637 console . log ( value ) ;
4738 const items = [ ] ;
48- for ( let i = 0 ; i < cards . length ; i ++ ) {
49- if (
50- cards [ i ] . title . includes ( value ) ||
51- cards [ i ] . description . includes ( value )
52- ) {
53- items . push ( cards [ i ] ) ;
54- }
55- }
39+ fetch ( `https://api.github.com/search/repositories?q=${ value } ` )
40+ . then ( ( response ) => response . json ( ) )
41+ . then ( ( data ) => {
42+ console . log ( data ) ;
43+ setFilterCards ( data . items ) ;
44+ } ) ;
5645 console . log ( items ) ;
5746 setFilterCards ( items ) ;
5847 } }
0 commit comments