diff options
Diffstat (limited to 'sw-ui-kt/src/main')
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()) +} |