Skip to content

Commit

Permalink
fix: translation doesnt apply on space listings
Browse files Browse the repository at this point in the history
  • Loading branch information
d1snin committed May 5, 2024
1 parent 3766cc5 commit bfe534d
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,9 @@ public class DefaultBeamClient(

public override suspend fun getResolvedTranslation(languageCode: LanguageCode): Result<Translation> =
runCatching {
httpClient.get(Paths.GET_RESOLVED_TRANSLATION) {
setLanguageCode(languageCode)
}.body()
val path = Paths.GET_RESOLVED_TRANSLATION.replaceLanguageCodePlaceholder(languageCode)

httpClient.get(path).body()
}

override suspend fun getTranslations(): Result<Translations> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import dev.d1s.beam.bundle.util.respondHtml
import dev.d1s.beam.commons.Paths
import dev.d1s.exkt.ktor.server.koin.configuration.ApplicationConfigurer
import dev.d1s.exkt.ktor.server.statuspages.HttpStatusException
import dev.d1s.exkt.ktor.server.statuspages.httpStatusException
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.config.*
Expand All @@ -47,6 +48,12 @@ object StatusPages : ApplicationConfigurer, KoinComponent {
handleExceptions()
handleStatuses()
handleNotFoundStatus()

httpStatusException { call, exception ->
val status = exception.status
val message = exception.toMessage(status)
call.respond(status, message)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class DefaultSpaceService : SpaceService, KoinComponent {
val addedSpace = handleUniqueSlugViolation {
spaceRepository.addSpace(space).getOrThrow()
}

val translatedSpace = translateOptionally(addedSpace, languageCode)
val translatedSpaceDto = spaceDtoConverter.convertToDto(translatedSpace)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class SpaceCardComponent : Component<SpaceCardComponent.Config>(::Config), KoinC
}

private suspend fun getSpace() =
config.spaceIdentifier.value?.let {
config.space.value ?: config.spaceIdentifier.value?.let {
spaceCache.getOrPut(it) {
client.getSpace(it, currentLanguageCode).getOrNull()
}
Expand Down Expand Up @@ -149,7 +149,8 @@ class SpaceCardComponent : Component<SpaceCardComponent.Config>(::Config), KoinC

class Config {

val spaceIdentifier = atomic<String?>(null)
val space = atomic<Space?>(null)
val spaceIdentifier = atomic<SpaceIdentifier?>(null)

val bare = atomic<Boolean?>(null)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ import kotlinx.atomicfu.atomic
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import org.koin.core.component.KoinComponent
import org.koin.core.component.get
import org.koin.core.component.inject
Expand All @@ -54,40 +56,26 @@ class SpaceListingComponent : Component<SpaceListingComponent.Config>(::Config),

private val beamClient by inject<BeamClient>()

private val spaces = ObservableValue(emptyList)

private val paginator by lazy {
val limit = if (currentSpace != null) PAGE_LIMIT + 1 else PAGE_LIMIT

Paginator(limit, currentPage = 0)
}

private val renderingScope = CoroutineScope(Dispatchers.Main)

private val initialized = atomic(false)
private var mainPanel: SimplePanel? = null

override fun SimplePanel.render(): Effect {
val (state, effect) = Effect.lazy()

fun render() {
renderingScope.launch {
renderingScope.launch {
renderMutex.withLock {
initialize()
renderSpaceContainer(state)
}
}

currentTranslationObservable.subscribeSkipping {
reset()
render()
}

render()

return effect
}

private suspend fun initialize() {
if (!initialized.getAndSet(true)) {
mainPanel?.dispose()
getMoreSpaces()
}
}
Expand All @@ -106,13 +94,6 @@ class SpaceListingComponent : Component<SpaceListingComponent.Config>(::Config),
}
}

private fun SimplePanel.reset() {
getChildren().forEach { it.dispose() }
spaces.value = emptyList
paginator.currentPage = 0
initialized.value = false
}

private fun SimplePanel.renderBoundContainer(
effectState: MutableLazyEffectState,
block: SimplePanel.(FetchedSpaces) -> Unit
Expand Down Expand Up @@ -146,7 +127,7 @@ class SpaceListingComponent : Component<SpaceListingComponent.Config>(::Config),
val spaceCard = get<Component<SpaceCardComponent.Config>>(Qualifier.SpaceCardComponent)

render(spaceCard) {
this.spaceIdentifier.value = space.id
this.space.value = space
this.fullHeight.value = true
}
}
Expand All @@ -170,8 +151,8 @@ class SpaceListingComponent : Component<SpaceListingComponent.Config>(::Config),
}
}

private suspend fun getSpaces(offset: Int = paginator.offset): FetchedSpaces? =
beamClient.getSpaces(paginator.limit, offset, currentLanguageCode).getOrNull()?.let { page ->
private suspend fun getSpaces(): FetchedSpaces? =
beamClient.getSpaces(paginator.limit, paginator.offset, currentLanguageCode).getOrNull()?.let { page ->
val spaces = page.elements
val filteredSpaces = spaces.filterSpaces()
val totalCount = page.count()
Expand Down Expand Up @@ -232,10 +213,23 @@ class SpaceListingComponent : Component<SpaceListingComponent.Config>(::Config),
val singleColumn = atomic(false)
}

private companion object {
companion object {

private const val PAGE_LIMIT = 4

private val emptyList: FetchedSpaces = listOf<Space>() to 0
private val spaces = ObservableValue(listOf<Space>() to 0)
private val initialized = atomic(false)

private val renderMutex = Mutex()

private val paginator by lazy {
Paginator(PAGE_LIMIT, currentPage = 0)
}

fun reset() {
spaces.value = listOf<Space>() to 0
paginator.currentPage = 0
initialized.value = false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package dev.d1s.beam.ui.util
import dev.d1s.beam.client.BeamClient
import dev.d1s.beam.commons.*
import dev.d1s.beam.ui.Qualifier
import dev.d1s.beam.ui.component.SpaceListingComponent
import dev.d1s.beam.ui.state.CurrentSpaceContentChangeObservable
import dev.d1s.beam.ui.state.Observable
import dev.d1s.beam.ui.state.SpaceContentChange
Expand Down Expand Up @@ -70,6 +71,8 @@ fun setCurrentTranslation(translation: Translation) {

actualizeCurrentTranslation()

SpaceListingComponent.reset()

(currentSpaceContentChangeObservable as? CurrentSpaceContentChangeObservable)?.let { observable ->
currentSpace?.id?.let {
observable.setCurrentSpaceContent(currentRows with currentBlocks)
Expand Down
31 changes: 14 additions & 17 deletions http/daemon.http
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ Authorization: Bearer {{token}}
Content-Type: application/json

{
"slug": "test-1",
"slug": "test-2",
"metadata": {},
"view": {
"theme": "catppuccin-mocha",
"icon": null,
"favicon": null,
"preview": null,
"title": "test",
"description": "test",
"title": "${test-1.title}",
"description": "${test-1.description}",
"remark": null
}
}
Expand All @@ -37,7 +37,7 @@ Content-Type: application/json
}

### GET Space
GET http://localhost:8573/api/spaces/917824b0-3b07-42f3-951a-dc81e30fde18?language=en
GET http://localhost:8573/api/spaces/test-1?language=en

### GET Spaces
GET http://localhost:8573/api/spaces?limit=10&offset=0
Expand Down Expand Up @@ -96,15 +96,15 @@ Content-Type: application/json
"size": "EXTRA_LARGE",
"entities": [
{
"type": "space",
"type": "text",
"parameters": {
"identifier": "root"
"value": "${test-1.title}"
}
}
],
"metadata": {
},
"spaceId": "root"
"spaceId": "test-1"
}

### GET Blocks
Expand Down Expand Up @@ -156,17 +156,17 @@ Content-Type: application/json
}

### POST Translation
POST http://localhost:8573/api/translations/en
POST http://localhost:8573/api/translations/ru
Authorization: Bearer {{token}}
Content-Type: application/json

{
"languageName": "English",
"languageName": null,
"default": false,
"translations": {
"ui.icon-alt": "Space icon",
"ui.default-title": "Beam",
"ui.default-remark": "Running [Beam][https://github.com/d1snin/beam] VERSION <version>",
"ui.default-remark": "Running [Beam][https://github.com/d1snin/beam] v<version>",
"ui.failure-card.not-found.icon-alt": "404 icon",
"ui.failure-card.not-found.message": "We ended up with nothing.",
"ui.failure-card.empty-space.icon-alt": "Empty space icon",
Expand All @@ -177,9 +177,8 @@ Content-Type: application/json
"ui.space-listing.message": "Explore other spaces:",
"ui.space-listing.fetch-more-button": "Fetch more",
"ui.block.collapsed-content-entity.button.message": "More",
"test-1.title": "en title",
"test-1.description": "en description",
"random.text": "random text emmm eeee"
"test-1.title": "ru title",
"test-1.description": "ru description"
}
}

Expand All @@ -203,7 +202,7 @@ Content-Type: application/json
"translations": {
"ui.icon-alt": "Space icon",
"ui.default-title": "Beam",
"ui.default-remark": "Running [Beam][https://github.com/d1snin/beam] VERSION <version>",
"ui.default-remark": "Running [Beam][https://github.com/d1snin/beam] v<version>",
"ui.failure-card.not-found.icon-alt": "404 icon",
"ui.failure-card.not-found.message": "We ended up with nothing.",
"ui.failure-card.empty-space.icon-alt": "Empty space icon",
Expand All @@ -213,9 +212,7 @@ Content-Type: application/json
"ui.footer.language-switcher.message": "Language...",
"ui.space-listing.message": "Explore other spaces:",
"ui.space-listing.fetch-more-button": "Fetch more",
"ui.block.collapsed-content-entity.button.message": "More",
"test-1.title": "en title",
"test-1.description": "en description"
"ui.block.collapsed-content-entity.button.message": "More"
}
}

Expand Down

0 comments on commit bfe534d

Please sign in to comment.