1
1
import { makeStyles } from "@material-ui/core" ;
2
- import React from "react" ;
2
+ import React , { useEffect , useState } from "react" ;
3
3
import Grid from "@material-ui/core/Grid" ;
4
4
import LearnItem from "./LearnItem" ;
5
5
6
- const videos = [
7
- {
8
- videoId : "EGmR676jMt8" ,
9
- title : "Community Call #19 (2021-03-24)" ,
10
- } ,
11
- {
12
- videoId : "oB3BeDNtN7g" ,
13
- title : "Community Call #18 (2021-03-17)" ,
14
- } ,
15
- {
16
- videoId : "zpwiZlbRRbs" ,
17
- title : "Community Call #16 (2021-02-24)" ,
18
- } ,
19
- {
20
- videoId : "A9bQPYvWb1o" ,
21
- title : "Community Call #14 (2021-02-10)" ,
22
- } ,
23
- {
24
- videoId : "Xha5l6t19Nk" ,
25
- title : "Community Call #13 (2021-02-03)" ,
26
- } ,
27
- {
28
- videoId : "tL5FRf-QvH8" ,
29
- title : "Community Call #12 (2021-01-27)" ,
30
- } ,
31
- {
32
- videoId : "CpeFQqFKksg" ,
33
- title : "Community Call #11 (2021-01-13)" ,
34
- } ,
35
- {
36
- videoId : "CFwnbgoMMBM" ,
37
- title : "Community Call #10 (2020-12-23)" ,
38
- } ,
39
- {
40
- videoId : "QTx7U6fPe_k" ,
41
- title : "Community Call #9 (2020-12-16)" ,
42
- } ,
43
- {
44
- videoId : "oBoDNGI8f3w" ,
45
- title : "Community Call #8 (2020-12-09)" ,
46
- } ,
47
- {
48
- videoId : "_KbbTmMA8WM" ,
49
- title : "Community Call #7 (2020-12-02)" ,
50
- } ,
51
- {
52
- videoId : "xi0sXZgG9NE" ,
53
- title : "Community Call #6 (2020-11-25)" ,
54
- } ,
55
- {
56
- videoId : "tt_TYVft4dQ" ,
57
- title : "Community Call #5 (2020-11-18)" ,
58
- } ,
59
- {
60
- videoId : "iNw5d1rZUqY" ,
61
- title : "Community Call #4 (2020-11-11)" ,
62
- } ,
63
- {
64
- videoId : "IBrVkzyCwb4" ,
65
- title : "Community Call #3 (2020-11-04)" ,
66
- } ,
67
- {
68
- videoId : "rC7zlCSuVEc" ,
69
- title : "Community Call #2 (2020-10-28)" ,
70
- } ,
71
- {
72
- videoId : "mGumdYAjDkY" ,
73
- title : "Community Call #1 (2020-10-21)" ,
74
- } ,
75
- ] ;
6
+ const fetchVideos = async ( ) => {
7
+ const response = await fetch (
8
+ "https://youtube.googleapis.com/youtube/v3/search?channelId=UCemVkpcBJvbzciHp_5Ly4dw&key=AIzaSyAitbrvF7nJkOlarAMAcco7zwgN-msm0Nc&part=snippet,id&order=date&maxResults=6"
9
+ ) ;
10
+ if ( response . status === 200 ) {
11
+ const responseJSON = await response . json ( ) ;
12
+ return responseJSON ;
13
+ }
14
+ } ;
76
15
77
16
const useStyles = makeStyles ( ( ) => ( {
78
17
heading : {
@@ -81,17 +20,57 @@ const useStyles = makeStyles(() => ({
81
20
} ,
82
21
} ) ) ;
83
22
23
+ interface Video {
24
+ id : {
25
+ videoId : string ;
26
+ } ;
27
+ snippet : {
28
+ title : string ;
29
+ } ;
30
+ }
31
+
32
+ interface Videos {
33
+ videoId : string ;
34
+ title : string ;
35
+ }
36
+
84
37
const Learn : React . FunctionComponent = ( ) => {
85
38
const classes = useStyles ( ) ;
39
+ const [ videos , setVideos ] = useState < Videos [ ] > ( [ ] ) ;
40
+ const [ error , setError ] = useState ( "" ) ;
41
+
42
+ const handleFetchVideos = async ( ) => {
43
+ try {
44
+ const response = await fetchVideos ( ) ;
45
+ response . items . forEach ( ( video : Video ) => {
46
+ setVideos ( ( prevState ) => {
47
+ return [
48
+ ...prevState ,
49
+ { videoId : video . id . videoId , title : video . snippet . title } ,
50
+ ] ;
51
+ } ) ;
52
+ } ) ;
53
+ } catch ( e ) {
54
+ setError ( "Unable to fetch videos." ) ;
55
+ }
56
+ } ;
57
+
58
+ useEffect ( ( ) => {
59
+ handleFetchVideos ( ) ;
60
+ } , [ ] ) ;
86
61
87
62
return (
88
63
< div >
89
64
< h1 className = { classes . heading } > Learn</ h1 >
90
- < Grid container spacing = { 2 } >
91
- { videos . map ( ( video ) => {
92
- return < LearnItem { ...video } key = { video . videoId } /> ;
93
- } ) }
94
- </ Grid >
65
+ { error ? (
66
+ < p > { error } </ p >
67
+ ) : (
68
+ < Grid container spacing = { 2 } >
69
+ { videos . map ( ( video ) => {
70
+ return < LearnItem { ...video } key = { video . videoId } /> ;
71
+ } ) }
72
+ </ Grid >
73
+ ) }
95
74
</ div >
96
75
) ;
97
76
} ;
0 commit comments