show iss position in kotlin/js client

This commit is contained in:
John O'Reilly 2021-02-05 18:03:17 +00:00
parent ccf54e9c11
commit f41de5487d
2 changed files with 15 additions and 2 deletions

View file

@ -1,21 +1,28 @@
import com.surrus.common.remote.Assignment
import com.surrus.common.remote.IssPosition
import react.*
import react.dom.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.collect
val App = functionalComponent<RProps> { _ ->
@InternalCoroutinesApi
val App = functionalComponent<RProps> {
val appDependencies = useContext(AppDependenciesContext)
val repository = appDependencies.repository
val (people, setPeople) = useState(emptyList<Assignment>())
val (issPosition, setIssPosition) = useState(IssPosition(0.0,0.0))
useEffectWithCleanup(dependencies = listOf()) {
val mainScope = MainScope()
mainScope.launch {
setPeople(repository.fetchPeople())
repository.pollISSPosition().collect {
setIssPosition(it)
}
}
return@useEffectWithCleanup { mainScope.cancel() }
}
@ -23,6 +30,9 @@ val App = functionalComponent<RProps> { _ ->
h1 {
+"People In Space"
}
h2 {
+"$issPosition"
}
ul {
people.forEach { item ->
li {
@ -30,4 +40,5 @@ val App = functionalComponent<RProps> { _ ->
}
}
}
}

View file

@ -1,6 +1,7 @@
import co.touchlab.kermit.Kermit
import com.surrus.common.di.initKoin
import com.surrus.common.repository.PeopleInSpaceRepository
import kotlinx.coroutines.InternalCoroutinesApi
import org.koin.core.KoinComponent
import org.koin.core.get
import react.child
@ -21,6 +22,7 @@ object AppDependencies : KoinComponent {
val AppDependenciesContext = createContext<AppDependencies>()
@InternalCoroutinesApi
fun main() {
render(kotlinx.browser.document.getElementById("root")) {
AppDependenciesContext.Provider(AppDependencies) {