diff --git a/client/src/jsMain/kotlin/dev/inmo/postssystem/client/JSDI.kt b/client/src/jsMain/kotlin/dev/inmo/postssystem/client/JSDI.kt index c814d93f..53d1cb18 100644 --- a/client/src/jsMain/kotlin/dev/inmo/postssystem/client/JSDI.kt +++ b/client/src/jsMain/kotlin/dev/inmo/postssystem/client/JSDI.kt @@ -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( "AuthTokenInfo" to AuthTokenInfo.serializer(), @@ -81,7 +75,7 @@ fun baseKoin(): Koin { ) }, { - JSUIFSMStatesRepo(window.history) + JSUIFSMStatesRepo(window.history, AuthUIFSMState(PostsListUIFSMState()), getAllDistinct()) } ) { val scope = first diff --git a/client/src/jsMain/kotlin/dev/inmo/postssystem/client/JSUIFSMStatesRepo.kt b/client/src/jsMain/kotlin/dev/inmo/postssystem/client/JSUIFSMStatesRepo.kt index e0d289f2..3ba0a44c 100644 --- a/client/src/jsMain/kotlin/dev/inmo/postssystem/client/JSUIFSMStatesRepo.kt +++ b/client/src/jsMain/kotlin/dev/inmo/postssystem/client/JSUIFSMStatesRepo.kt @@ -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, + fillers: List ) { val currentUrl = window.location.pathname - val params = states.mapNotNull> { - when (it) { - is AuthUIFSMState -> null - is CreatePostUIFSMState -> null + val currentParams = (URL(window.location.href)).searchParams + val params = states.flatMap> { state -> + fillers.flatMap { + it.takeParams(state, currentParams) } } pushState( @@ -24,9 +24,11 @@ private fun History.refreshHistory( ) } -private fun takeStates(initialState: UIFSMState): List { +private fun takeStates(initialState: UIFSMState, fillers: List): List { val params = (URL(window.location.href)).searchParams - val additionalStates = listOfNotNull() + val additionalStates = fillers.mapNotNull { + it.takeState(params) + } return additionalStates + listOfNotNull( if (additionalStates.isEmpty()) { @@ -41,12 +43,13 @@ private fun takeStates(initialState: UIFSMState): List { class JSUIFSMStatesRepo( private val history: History, - private val initialState: UIFSMState = DefaultAuthUIFSMState + private val initialState: UIFSMState, + private val fillers: List ) : DefaultStatesManagerRepo { private val statesMap = mutableMapOf() 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) } } diff --git a/client/src/jsMain/kotlin/dev/inmo/postssystem/client/UIFSMStateSearchParamsHandler.kt b/client/src/jsMain/kotlin/dev/inmo/postssystem/client/UIFSMStateSearchParamsHandler.kt new file mode 100644 index 00000000..c0ca9547 --- /dev/null +++ b/client/src/jsMain/kotlin/dev/inmo/postssystem/client/UIFSMStateSearchParamsHandler.kt @@ -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> + fun takeState(params: URLSearchParams): UIFSMState? +} diff --git a/services/posts/client/src/commonMain/kotlin/dev/inmo/postssystem/services/posts/client/ModuleLoader.kt b/services/posts/client/src/commonMain/kotlin/dev/inmo/postssystem/services/posts/client/ModuleLoader.kt index 3e589582..b24d8222 100644 --- a/services/posts/client/src/commonMain/kotlin/dev/inmo/postssystem/services/posts/client/ModuleLoader.kt +++ b/services/posts/client/src/commonMain/kotlin/dev/inmo/postssystem/services/posts/client/ModuleLoader.kt @@ -2,11 +2,10 @@ package dev.inmo.postssystem.services.posts.client import dev.inmo.postssystem.features.auth.client.AuthorizedModuleLoader import dev.inmo.postssystem.features.auth.client.AuthorizedQualifiers -import dev.inmo.postssystem.features.common.common.DefaultQualifiers -import dev.inmo.postssystem.features.common.common.getAllDistinct +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.common.* -import org.koin.core.qualifier.Qualifier import org.koin.dsl.binds val loader = AuthorizedModuleLoader { @@ -15,6 +14,9 @@ val loader = AuthorizedModuleLoader { WritePostsService::class ) + UIFSMStateSerializer.include("posts_create", PostsCreateUIFSMState.serializer()) + UIFSMStateSerializer.include("posts_list", PostsListUIFSMState.serializer()) + factory { DefaultPostCreateUIModel(get(), get()) } factory { PostCreateUIViewModel(get()) } } diff --git a/services/posts/client/src/commonMain/kotlin/dev/inmo/postssystem/services/posts/client/ui/create/PostsCreateUIFSMState.kt b/services/posts/client/src/commonMain/kotlin/dev/inmo/postssystem/services/posts/client/ui/create/PostsCreateUIFSMState.kt new file mode 100644 index 00000000..6414fa6b --- /dev/null +++ b/services/posts/client/src/commonMain/kotlin/dev/inmo/postssystem/services/posts/client/ui/create/PostsCreateUIFSMState.kt @@ -0,0 +1,10 @@ +package dev.inmo.postssystem.services.posts.client.ui.create + +import dev.inmo.postssystem.features.common.common.ui.fsm.UIFSMState +import kotlinx.serialization.Serializable + +@Serializable +data class PostsCreateUIFSMState( + override val from: UIFSMState? = null, + override val context: String = "main" +) : UIFSMState diff --git a/services/posts/client/src/commonMain/kotlin/dev/inmo/postssystem/services/posts/client/ui/posts_list/PostsListUIFSMState.kt b/services/posts/client/src/commonMain/kotlin/dev/inmo/postssystem/services/posts/client/ui/posts_list/PostsListUIFSMState.kt new file mode 100644 index 00000000..664a8216 --- /dev/null +++ b/services/posts/client/src/commonMain/kotlin/dev/inmo/postssystem/services/posts/client/ui/posts_list/PostsListUIFSMState.kt @@ -0,0 +1,10 @@ +package dev.inmo.postssystem.services.posts.client.ui.posts_list + +import dev.inmo.postssystem.features.common.common.ui.fsm.UIFSMState +import kotlinx.serialization.Serializable + +@Serializable +data class PostsListUIFSMState( + override val from: UIFSMState? = null, + override val context: String = "main" +) : UIFSMState diff --git a/services/posts/client/src/jsMain/kotlin/dev/inmo/postssystem/services/posts/client/PostCreateView.kt b/services/posts/client/src/jsMain/kotlin/dev/inmo/postssystem/services/posts/client/PostCreateView.kt index 7dc65f26..c2c8c484 100644 --- a/services/posts/client/src/jsMain/kotlin/dev/inmo/postssystem/services/posts/client/PostCreateView.kt +++ b/services/posts/client/src/jsMain/kotlin/dev/inmo/postssystem/services/posts/client/PostCreateView.kt @@ -13,10 +13,10 @@ import dev.inmo.postssystem.features.common.common.ui.JSView import dev.inmo.postssystem.features.common.common.ui.fsm.* import dev.inmo.postssystem.features.content.client.ContentClientProvider import dev.inmo.postssystem.features.content.common.Content +import dev.inmo.postssystem.services.posts.client.ui.create.PostsCreateUIFSMState import dev.inmo.postssystem.services.posts.client.ui.create.PostCreateUIViewModel import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.CoroutineScope -import kotlinx.serialization.Serializable import org.jetbrains.compose.web.dom.Div import org.jetbrains.compose.web.dom.Text import org.w3c.dom.HTMLElement @@ -31,21 +31,15 @@ val jsLoader = DefaultModuleLoader { } } -@Serializable -data class CreatePostUIFSMState( - override val from: UIFSMState? = null, - override val context: String = "main" -) : UIFSMState - class PostCreateView( private val createPostCreateUIModel: PostCreateUIViewModel, private val contentClientProviders: List, private val uiScope: CoroutineScope, defaultExceptionsHandlers: Iterable -) : JSView(defaultExceptionsHandlers) { +) : JSView(defaultExceptionsHandlers) { override suspend fun StatesMachine.safeHandleState( htmlElement: HTMLElement, - state: CreatePostUIFSMState + state: PostsCreateUIFSMState ): UIFSMState? { val result = CompletableDeferred()