@@ -14,16 +14,36 @@ import react.dom.html.ReactHTML.div
1414import react.dom.html.ReactHTML.p
1515import react.dom.html.ReactHTML.img
1616
17+ suspend fun fetchVideo (id : Int ): Video {
18+ val response = window
19+ .fetch(" https://my-json-server.typicode.com/kotlin-hands-on/kotlinconf-json/videos/$id " )
20+ .await()
21+ .text()
22+ .await()
23+ return Json .decodeFromString(response)
24+ }
25+
26+ suspend fun fetchVideos (): List <Video > = coroutineScope {
27+ (1 .. 25 ).map { id ->
28+ async {
29+ fetchVideo(id)
30+ }
31+ }.awaitAll()
32+ }
33+
34+ val mainScope = MainScope ()
35+
1736val App = FC <Props > {
1837 var currentVideo: Video ? by useState(null )
19- var unwatchedVideos: List <Video > by useState(listOf (
20- Video (1 , " Opening Keynote" , " Andrey Breslav" , " https://youtu.be/PsaFVLr8t4E" ),
21- Video (2 , " Dissecting the stdlib" , " Huyen Tue Dao" , " https://youtu.be/Fzt_9I733Yg" ),
22- Video (3 , " Kotlin and Spring Boot" , " Nicolas Frankel" , " https://youtu.be/pSiZVAeReeg" )
23- ))
24- var watchedVideos: List <Video > by useState(listOf (
25- Video (4 , " Creating Internal DSLs in Kotlin" , " Venkat Subramaniam" , " https://youtu.be/JzTeAM8N1-o" )
26- ))
38+ var unwatchedVideos: List <Video > by useState(emptyList())
39+ var watchedVideos: List <Video > by useState(emptyList())
40+
41+ useEffectOnce {
42+ mainScope.launch {
43+ unwatchedVideos = fetchVideos()
44+ }
45+ }
46+
2747 h1 {
2848 + " Hello, React+Kotlin/JS!"
2949 }
0 commit comments