add class for js repo of states and start adding of posts list mvvm
This commit is contained in:
client/src/jsMain/kotlin/dev/inmo/postssystem/client
services/posts/client/src
commonMain
kotlin
dev
inmo
postssystem
services
posts
client
jsMain
kotlin
dev
inmo
postssystem
services
posts
client
@ -1,19 +1,16 @@
|
||||
package dev.inmo.postssystem.client
|
||||
|
||||
import dev.inmo.postssystem.client.fsm.ui.*
|
||||
import dev.inmo.postssystem.features.auth.common.AuthTokenInfo
|
||||
import dev.inmo.micro_utils.coroutines.ContextSafelyExceptionHandler
|
||||
import dev.inmo.micro_utils.fsm.common.CheckableHandlerHolder
|
||||
import dev.inmo.micro_utils.fsm.common.StatesMachine
|
||||
import dev.inmo.micro_utils.repos.mappers.withMapper
|
||||
import dev.inmo.micro_utils.serialization.typed_serializer.TypedSerializer
|
||||
import dev.inmo.postssystem.features.auth.client.settings.AuthSettings
|
||||
import dev.inmo.postssystem.features.auth.client.ui.*
|
||||
import dev.inmo.postssystem.features.common.common.baseKoin
|
||||
import dev.inmo.postssystem.features.common.common.getAllDistinct
|
||||
import dev.inmo.postssystem.features.common.common.ui.fsm.UIFSMHandler
|
||||
import dev.inmo.postssystem.features.common.common.ui.fsm.UIFSMStateSerializer
|
||||
import dev.inmo.postssystem.services.posts.client.ui.create.*
|
||||
import dev.inmo.postssystem.services.posts.client.ui.posts_list.PostsListUIFSMState
|
||||
import dev.inmo.postssystem.services.posts.client.ui.posts_list.PostsListUIState
|
||||
import kotlinx.browser.*
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.serialization.builtins.serializer
|
||||
@ -21,11 +18,8 @@ import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.serializer
|
||||
import org.koin.core.Koin
|
||||
import org.koin.core.context.loadKoinModules
|
||||
import org.koin.core.parameter.ParametersHolder
|
||||
import org.koin.core.qualifier.Qualifier
|
||||
import org.koin.dsl.module
|
||||
import org.w3c.dom.HTMLElement
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
val defaultTypedSerializer = TypedSerializer<Any>(
|
||||
"AuthTokenInfo" to AuthTokenInfo.serializer(),
|
||||
@ -81,7 +75,7 @@ fun baseKoin(): Koin {
|
||||
)
|
||||
},
|
||||
{
|
||||
JSUIFSMStatesRepo(window.history)
|
||||
JSUIFSMStatesRepo(window.history, AuthUIFSMState(PostsListUIFSMState()), getAllDistinct())
|
||||
}
|
||||
) {
|
||||
val scope = first
|
||||
|
@ -3,18 +3,18 @@ package dev.inmo.postssystem.client
|
||||
import dev.inmo.micro_utils.fsm.common.managers.DefaultStatesManagerRepo
|
||||
import dev.inmo.postssystem.features.common.common.ui.fsm.UIFSMState
|
||||
import kotlinx.browser.window
|
||||
import kotlinx.serialization.StringFormat
|
||||
import org.w3c.dom.*
|
||||
import org.w3c.dom.url.URL
|
||||
|
||||
private fun History.refreshHistory(
|
||||
states: Iterable<UIFSMState>,
|
||||
fillers: List<UIFSMStateSearchParamsHandler>
|
||||
) {
|
||||
val currentUrl = window.location.pathname
|
||||
val params = states.mapNotNull<UIFSMState, Pair<String, String>> {
|
||||
when (it) {
|
||||
is AuthUIFSMState -> null
|
||||
is CreatePostUIFSMState -> null
|
||||
val currentParams = (URL(window.location.href)).searchParams
|
||||
val params = states.flatMap<UIFSMState, Pair<String, String>> { state ->
|
||||
fillers.flatMap {
|
||||
it.takeParams(state, currentParams)
|
||||
}
|
||||
}
|
||||
pushState(
|
||||
@ -24,9 +24,11 @@ private fun History.refreshHistory(
|
||||
)
|
||||
}
|
||||
|
||||
private fun takeStates(initialState: UIFSMState): List<UIFSMState> {
|
||||
private fun takeStates(initialState: UIFSMState, fillers: List<UIFSMStateSearchParamsHandler>): List<UIFSMState> {
|
||||
val params = (URL(window.location.href)).searchParams
|
||||
val additionalStates = listOfNotNull<UIFSMState>()
|
||||
val additionalStates = fillers.mapNotNull {
|
||||
it.takeState(params)
|
||||
}
|
||||
|
||||
return additionalStates + listOfNotNull(
|
||||
if (additionalStates.isEmpty()) {
|
||||
@ -41,12 +43,13 @@ private fun takeStates(initialState: UIFSMState): List<UIFSMState> {
|
||||
|
||||
class JSUIFSMStatesRepo(
|
||||
private val history: History,
|
||||
private val initialState: UIFSMState = DefaultAuthUIFSMState
|
||||
private val initialState: UIFSMState,
|
||||
private val fillers: List<UIFSMStateSearchParamsHandler>
|
||||
) : DefaultStatesManagerRepo<UIFSMState> {
|
||||
private val statesMap = mutableMapOf<String, UIFSMState>()
|
||||
|
||||
init {
|
||||
val states = takeStates(initialState)
|
||||
val states = takeStates(initialState, fillers)
|
||||
states.forEach {
|
||||
statesMap[it.context] = it
|
||||
}
|
||||
@ -68,12 +71,12 @@ class JSUIFSMStatesRepo(
|
||||
|
||||
override suspend fun removeState(state: UIFSMState) {
|
||||
statesMap.remove((state.context as? String) ?: return)
|
||||
history.refreshHistory(statesMap.values)
|
||||
history.refreshHistory(statesMap.values, fillers)
|
||||
}
|
||||
|
||||
override suspend fun set(state: UIFSMState) {
|
||||
console.log(state)
|
||||
statesMap[state.context] = state
|
||||
history.refreshHistory(statesMap.values)
|
||||
history.refreshHistory(statesMap.values, fillers)
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,9 @@
|
||||
package dev.inmo.postssystem.client
|
||||
|
||||
import dev.inmo.postssystem.features.common.common.ui.fsm.UIFSMState
|
||||
import org.w3c.dom.url.URLSearchParams
|
||||
|
||||
interface UIFSMStateSearchParamsHandler {
|
||||
fun takeParams(state: UIFSMState, currentParams: URLSearchParams): List<Pair<String, String>>
|
||||
fun takeState(params: URLSearchParams): UIFSMState?
|
||||
}
|
Reference in New Issue
Block a user