Skip to content

Commit

Permalink
Add keywords search filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Jezorko committed Jan 4, 2022
1 parent b9bedbd commit 033a3e7
Show file tree
Hide file tree
Showing 9 changed files with 513 additions and 16 deletions.
462 changes: 462 additions & 0 deletions src/commonMain/kotlin/jezorko/github/gfngameslist/games/Keywords.kt

Large diffs are not rendered by default.

Expand Up @@ -21,6 +21,7 @@ data class Messages(
val publisher: String,
val genreLabel: String,
val storeLabel: String,
val keywordsLabel: String,
val searchPlaceholder: String,
val status: String,
val specificStatus: GameStatusMessages,
Expand Down
8 changes: 5 additions & 3 deletions src/jsMain/kotlin/api/ApiClient.kt
@@ -1,8 +1,8 @@
package api

import jezorko.github.gfngameslist.games.GameGenre
import jezorko.github.gfngameslist.games.GetGamesResponse
import jezorko.github.gfngameslist.games.GameStore
import jezorko.github.gfngameslist.games.GetGamesResponse
import jezorko.github.gfngameslist.localization.Messages
import jezorko.github.gfngameslist.shared.serialize
import jezorko.github.gfngameslist.versions.VersionInfo
Expand Down Expand Up @@ -39,13 +39,15 @@ object ApiClient {
page: Int,
textSearch: String?,
stores: List<GameStore>?,
genres: List<GameGenre>?
genres: List<GameGenre>?,
keywords: List<String>?
): Promise<GetGamesResponse> {
val textSearchParam = if (textSearch != null) "&query=${encodeURIComponent(textSearch)}" else ""
val storeParam = if (stores != null) "&store=${encodeURIComponent(stores.serialize())}" else ""
val genreParam = if (genres != null) "&genre=${encodeURIComponent(genres.serialize())}" else ""
val keywordsParam = if (keywords != null) "&keywords=${encodeURIComponent(keywords.joinToString(","))}" else ""
return window.fetch(
"/api/games?limit=$limit&page=$page$textSearchParam$storeParam$genreParam",
"/api/games?limit=$limit&page=$page$textSearchParam$storeParam$genreParam$keywordsParam",
object : RequestInit {
override var method: String? = "GET"
}
Expand Down
18 changes: 13 additions & 5 deletions src/jsMain/kotlin/components/MainPage.kt
Expand Up @@ -2,10 +2,7 @@ package components

import api.ApiClient
import getMainContainer
import jezorko.github.gfngameslist.games.GameGenre
import jezorko.github.gfngameslist.games.GameStore
import jezorko.github.gfngameslist.games.GetGamesResponse
import jezorko.github.gfngameslist.games.validStores
import jezorko.github.gfngameslist.games.*
import jezorko.github.gfngameslist.localization.Messages
import jezorko.github.gfngameslist.localization.get
import kotlinx.browser.window
Expand Down Expand Up @@ -39,6 +36,7 @@ external interface MainPageState : State {
var textSearch: String?
var genresSearch: List<String>?
var storeSearch: List<String>?
var keywordsSearch: List<String>?
var getGamesResponse: GetGamesResponse?
}

Expand Down Expand Up @@ -112,6 +110,15 @@ class MainPage(props: Props) : RComponent<Props, MainPageState>(props) {
messages = state.messages
}
}
child(MultiSelect::class) {
attrs {
id = "keywords-search"
name = state.messages[Messages::keywordsLabel]
options = keywords.map(::Option)
onSelection = updateState(MainPageState::keywordsSearch)
messages = state.messages
}
}
state.getGamesResponse?.games?.let { games ->
child(GameDataTable::class) {
attrs {
Expand Down Expand Up @@ -197,7 +204,8 @@ class MainPage(props: Props) : RComponent<Props, MainPageState>(props) {
page,
state.textSearch,
state.storeSearch?.map(GameStore::valueOf),
state.genresSearch?.map(GameGenre::valueOf)
state.genresSearch?.map(GameGenre::valueOf),
state.keywordsSearch
).flatThen { response -> setState { loadingMoreGames = false }.then { response } }
}

Expand Down
2 changes: 1 addition & 1 deletion src/jsMain/kotlin/components/MultiSelect.kt
Expand Up @@ -21,7 +21,7 @@ import styled.css
import styled.styledButton
import styled.styledDiv

data class Option(val name: String, val value: String)
data class Option(val name: String, val value: String = name)

external interface MultiSelectProps : Props {
var id: String?
Expand Down
Expand Up @@ -12,8 +12,18 @@ fun Application.gamesRoutes() = routing {
val searchQuery = call.request.queryParameters["query"]
val storesFilter = GameStore::class.deserializeSet(call.request.queryParameters["store"])
val genresFilter = GameGenre::class.deserializeSet(call.request.queryParameters["genre"])
val keywordsFilter = call.request.queryParameters["keywords"]?.split(",")?.toSet()

call.respondJson(provider = { GamesService.getGames(limit, page, searchQuery, storesFilter, genresFilter) })
call.respondJson(provider = {
GamesService.getGames(
limit,
page,
searchQuery,
storesFilter,
genresFilter,
keywordsFilter
)
})
GamesService.updateIfNeeded()
}
}
Expand Up @@ -22,19 +22,31 @@ internal object GamesService {
page: Int,
searchQuery: String?,
storesFilter: Set<GameStore>,
genresFilter: Set<GameGenre>
genresFilter: Set<GameGenre>,
keywordsFilter: Set<String>?
) =
GetGamesResponse(
supportedGamesCount = localGamesCache.get().distinctBy(Game::title).count(),
games = localGamesCache.get()
.asSequence()
.filter { game -> storesFilter.isEmpty() || game.stores.containsAll(storesFilter) }
.filter { game -> genresFilter.isEmpty() || game.genres.containsAll(genresFilter) }
.filter { game -> searchQuery?.uppercase()?.let {
listOf(game.title, game.publisher, game.keywords)
.map(String::uppercase)
.any { textFragment->textFragment.contains(it) }
} ?: true }
.filter { game ->
keywordsFilter?.map(String::uppercase)?.all { keyword ->
game.keywords
.replace('_', ' ')
.replace('-', ' ')
.uppercase()
.contains(keyword)
} ?: true
}
.filter { game ->
searchQuery?.uppercase()?.let {
listOf(game.title, game.publisher, game.keywords)
.map(String::uppercase)
.any { textFragment -> textFragment.contains(it) }
} ?: true
}
.sortedBy(Game::title)
.drop(page * limit)
.take(limit)
Expand Down
1 change: 1 addition & 0 deletions src/jvmMain/resources/localization/messages_en_US.json
Expand Up @@ -6,6 +6,7 @@
"publisher": "publisher",
"genreLabel": "genres",
"storeLabel": "stores",
"keywordsLabel": "keywords",
"searchPlaceholder": "search",
"status": "status",
"specificStatus": {
Expand Down
1 change: 1 addition & 0 deletions src/jvmMain/resources/localization/messages_pl_PL.json
Expand Up @@ -6,6 +6,7 @@
"publisher": "wydawca",
"genreLabel": "kategorie",
"storeLabel": "sklepy",
"keywordsLabel": "słowa kluczowe",
"searchPlaceholder": "wyszukaj",
"status": "status",
"specificStatus": {
Expand Down

0 comments on commit 033a3e7

Please sign in to comment.