Skip to content

Commit

Permalink
Fritz2 Rework
Browse files Browse the repository at this point in the history
Fixed filtered pagination and extended count badge to reflect searches
  • Loading branch information
mpetuska committed Mar 8, 2021
1 parent 0f04eb7 commit 71f0d3d
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 15 deletions.
2 changes: 1 addition & 1 deletion app/src/commonMain/kotlin/app/service/LibraryService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import kamp.domain.*

expect class LibraryService {
suspend fun getAll(page: Int, size: Int = 20, search: String? = null, targets: Set<String>? = null): PagedResponse<KotlinMPPLibrary>
suspend fun getCount(): LibraryCount
suspend fun getCount(search: String? = null, targets: Set<String>? = null): LibraryCount

companion object
}
Expand Down
18 changes: 13 additions & 5 deletions app/src/jsMain/kotlin/app/service/LibraryService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@ import kamp.domain.*
actual class LibraryService(private val client: HttpClient) {
actual suspend fun getAll(page: Int, size: Int, search: String?, targets: Set<String>?): PagedResponse<KotlinMPPLibrary> {
val pagination = "page=$page&size=$size"
val searchQuery = search?.let { "&search=$it" } ?: ""
val targetsQuery = targets?.joinToString(prefix = "&target=", separator = "&target=") ?: ""
val searchQuery = search?.let { "search=$it" } ?: ""
val targetsQuery = targets?.joinToString(prefix = "target=", separator = "&target=") ?: ""

return client.get("${path}?$pagination$searchQuery$targetsQuery".toApi())
return client.get("${path}${buildQuery(pagination, searchQuery, targetsQuery)}".toApi())
}

actual suspend fun getCount(): LibraryCount =
client.get("${path}/count".toApi())
actual suspend fun getCount(search: String?, targets: Set<String>?): LibraryCount {
val searchQuery = search?.let { "search=$it" }
val targetsQuery = targets?.joinToString(prefix = "target=", separator = "&target=")

return client.get("${path}/count${buildQuery(searchQuery, targetsQuery)}".toApi())
}

private fun buildQuery(vararg query: String?): String {
return query.toSet().filterNotNull().takeIf(List<String>::isNotEmpty)?.joinToString("&", prefix = "?") ?: ""
}

actual companion object
}
15 changes: 11 additions & 4 deletions app/src/jsMain/kotlin/app/store/thunk/appThunk.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fun fetchLibraryPage(page: Int, size: Int = 12, search: String? = null, targets:
val theTargets = targets ?: state.targets

val service by di.instance<LibraryService>()
val libraries = service.getAll(page, size, theSearch, targets)
val libraries = service.getAll(page, size, theSearch, theTargets)
window.scrollTo(0.0, 0.0)
state.copy(
libraries = libraries,
Expand All @@ -21,8 +21,15 @@ fun fetchLibraryPage(page: Int, size: Int = 12, search: String? = null, targets:
)
}

fun fetchLibraryCount() = LibraryStore.handle { state ->
fun fetchLibraryCount(search: String? = null, targets: Set<String>? = null) = LibraryStore.handle { state ->
val theSearch = search ?: state.search
val theTargets = targets ?: state.targets

val service by di.instance<LibraryService>()
val count = service.getCount().count
state.copy(count = count)
val count = service.getCount(theSearch, theTargets).count
state.copy(
count = count,
search = theSearch,
targets = theTargets,
)
}
8 changes: 8 additions & 0 deletions app/src/jsMain/kotlin/app/view/Header.kt
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ private fun RenderContext.SearchModal() = modal({
search = searchStore.current.takeIf(String::isNotEmpty),
targets = targetsStore.current.takeIf(Set<String>::isNotEmpty)
)()
fetchLibraryCount(
search = searchStore.current.takeIf(String::isNotEmpty),
targets = targetsStore.current.takeIf(Set<String>::isNotEmpty)
)()
} handledBy close
}
}
Expand Down Expand Up @@ -260,6 +264,7 @@ fun RenderContext.Header() {
verticalAlign { sub }
fontSize(sm = { large }, md = { larger })
fontWeight { lighter }
display(sm = { none }, md = { flex })
}) { +"KAMP" }
}
LibraryStore.data.map { it.count }.render { count ->
Expand Down Expand Up @@ -291,6 +296,9 @@ fun RenderContext.Header() {
}
lineUp({
alignItems { center }
margins {
left { tiny }
}
}) {
spacing { tiny }
items {
Expand Down
1 change: 1 addition & 0 deletions app/src/jsMain/kotlin/app/view/component/LibraryCard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ private fun RenderContext.CardBody(library: KotlinMPPLibrary, selectedVersion: R
paddings { horizontal { tiny } }
overflow { auto }
width { "100%" }
minWidth { "4rem" }
}) {
flexBox({
css("direction: ltr")
Expand Down
2 changes: 1 addition & 1 deletion app/src/jvmMain/kotlin/app/config/routing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private fun Routing.libraries() = route(LibraryService.path) {
}
get("/count") {
val service by inject<LibraryService>()
call.respond(service.getCount())
call.respond(service.getCount(call.request.search, call.request.targets))
}

authenticate {
Expand Down
12 changes: 8 additions & 4 deletions app/src/jvmMain/kotlin/app/service/LibraryService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ actual class LibraryService(
private val call: ApplicationCall,
private val collection: CoroutineCollection<KotlinMPPLibrary>,
) {
actual suspend fun getAll(page: Int, size: Int, search: String?, targets: Set<String>?): PagedResponse<KotlinMPPLibrary> {
private fun buildQuery(search: String?, targets: Set<String>?): String {
val searchQuery = search?.let {
"""
{
Expand Down Expand Up @@ -43,7 +43,11 @@ actual class LibraryService(
}
""".trimIndent()
} ?: "{}"
val data = collection.find(query).sort(ascending(KotlinMPPLibrary::name)).skip(size * (page - 1)).limit(size).toList()
return query
}

actual suspend fun getAll(page: Int, size: Int, search: String?, targets: Set<String>?): PagedResponse<KotlinMPPLibrary> {
val data = collection.find(buildQuery(search, targets)).sort(ascending(KotlinMPPLibrary::name)).skip(size * (page - 1)).limit(size).toList()
return PagedResponse(
data = data,
page = page,
Expand All @@ -52,8 +56,8 @@ actual class LibraryService(
)
}

actual suspend fun getCount(): LibraryCount {
return LibraryCount(collection.countDocuments())
actual suspend fun getCount(search: String?, targets: Set<String>?): LibraryCount {
return LibraryCount(collection.countDocuments(buildQuery(search, targets)))
}

suspend fun create(library: KotlinMPPLibrary) {
Expand Down

0 comments on commit 71f0d3d

Please sign in to comment.