summaryrefslogtreecommitdiff
path: root/sw-ui-kt/src/main/kotlin
diff options
context:
space:
mode:
authorJoffrey BION <joffrey.bion@gmail.com>2019-07-11 22:20:55 +0200
committerJoffrey Bion <joffrey.bion@gmail.com>2019-08-03 17:25:07 +0200
commit6205110094e2bf6c76011624164b87c5c0b069b5 (patch)
tree0e2ae00c257e21f1cd193c60379ac9d43b68f788 /sw-ui-kt/src/main/kotlin
parentAttempt at Kotlin/JS EAP build 1.3.50-eap-54 (diff)
downloadseven-wonders-6205110094e2bf6c76011624164b87c5c0b069b5.tar.gz
seven-wonders-6205110094e2bf6c76011624164b87c5c0b069b5.tar.bz2
seven-wonders-6205110094e2bf6c76011624164b87c5c0b069b5.zip
Implement basic redux
Diffstat (limited to 'sw-ui-kt/src/main/kotlin')
-rw-r--r--sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/SevenWondersUi.kt17
-rw-r--r--sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Actions.kt5
-rw-r--r--sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Reducers.kt8
-rw-r--r--sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Store.kt20
4 files changed, 49 insertions, 1 deletions
diff --git a/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/SevenWondersUi.kt b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/SevenWondersUi.kt
index 5d9d9be6..8ad20702 100644
--- a/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/SevenWondersUi.kt
+++ b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/SevenWondersUi.kt
@@ -1,13 +1,28 @@
package org.luxons.sevenwonders.ui
+import org.luxons.sevenwonders.ui.redux.configureStore
+import org.w3c.dom.Element
import react.RBuilder
import react.dom.*
+import react.redux.provider
import kotlin.browser.document
import kotlin.browser.window
fun main() {
window.onload = {
- render(document.getElementById("root")!!) {
+ val rootElement = document.getElementById("root")
+ if (rootElement != null) {
+ initializeAndRender(rootElement)
+ } else {
+ console.error("Element with ID 'root' was not found, cannot bootstrap react app")
+ }
+ }
+}
+
+private fun initializeAndRender(rootElement: Element) {
+ val store = configureStore()
+ render(rootElement) {
+ provider(store) {
app()
}
}
diff --git a/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Actions.kt b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Actions.kt
new file mode 100644
index 00000000..3fc51d6e
--- /dev/null
+++ b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Actions.kt
@@ -0,0 +1,5 @@
+package org.luxons.sevenwonders.ui.redux
+
+import redux.RAction
+
+class ChooseUserName(val newUsername: String): RAction
diff --git a/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Reducers.kt b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Reducers.kt
new file mode 100644
index 00000000..d811d32e
--- /dev/null
+++ b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Reducers.kt
@@ -0,0 +1,8 @@
+package org.luxons.sevenwonders.ui.redux
+
+import redux.RAction
+
+fun rootReducer(state: SwState, action: RAction) = when (action) {
+ is ChooseUserName -> state.copy(playerName = action.newUsername)
+ else -> state
+}
diff --git a/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Store.kt b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Store.kt
new file mode 100644
index 00000000..75f7e8a8
--- /dev/null
+++ b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Store.kt
@@ -0,0 +1,20 @@
+package org.luxons.sevenwonders.ui.redux
+
+import redux.RAction
+import redux.Store
+import redux.WrapperAction
+import redux.createStore
+import redux.rEnhancer
+
+data class SwState(
+ val playerName: String
+)
+
+val INITIAL_STATE = SwState("Bob")
+
+fun configureStore(initialState: SwState = INITIAL_STATE): Store<SwState, RAction, WrapperAction> {
+
+ // TODO configure middlewares
+
+ return createStore(::rootReducer, initialState, rEnhancer())
+}
bgstack15