Keep search query on reload

This commit is contained in:
Hubert Van De Walle 2020-08-21 17:44:43 +02:00
parent 8ba89d3e05
commit c02f7c039a
3 changed files with 9 additions and 7 deletions

View File

@ -57,10 +57,11 @@ class NoteController(
}
fun search(request: Request, jwtPayload: JwtPayload): Response {
val terms = request.searchTerms()
val query = request.form("search") ?: ""
val terms = parseSearchTerms(query)
val notes = noteService.search(jwtPayload.userId, terms)
val deletedCount = noteService.countDeleted(jwtPayload.userId)
return Response(OK).html(view.search(jwtPayload, notes, deletedCount))
return Response(OK).html(view.search(jwtPayload, notes, query, deletedCount))
}
fun note(request: Request, jwtPayload: JwtPayload): Response {
@ -132,6 +133,4 @@ class NoteController(
null
}
}
private fun Request.searchTerms(): SearchTerms = parseSearchTerms(form("search") ?: "")
}

View File

@ -71,10 +71,11 @@ class NoteView(staticFileResolver: StaticFileResolver) : View(staticFileResolver
fun search(
jwtPayload: JwtPayload,
notes: List<PersistedNoteMetadata>,
query: String,
numberOfDeletedNotes: Int,
) = renderPage("Notes", jwtPayload = jwtPayload) {
div("container mx-auto p-4") {
noteListHeader(numberOfDeletedNotes)
noteListHeader(numberOfDeletedNotes, query)
noteTable(notes)
}
}

View File

@ -4,7 +4,7 @@ import kotlinx.html.*
import kotlinx.html.ButtonType.submit
import kotlinx.html.FormMethod.post
fun DIV.noteListHeader(numberOfDeletedNotes: Int) {
fun DIV.noteListHeader(numberOfDeletedNotes: Int, query: String = "") {
div("flex justify-between mb-4") {
h1("text-2xl underline") { +"Notes" }
span {
@ -23,7 +23,9 @@ fun DIV.noteListHeader(numberOfDeletedNotes: Int) {
input(
name = "search",
classes = "$colors rounded w-3/4 border appearance-none focus:outline-none text-base p-2"
)
) {
attributes["value"] = query
}
button(type = submit, classes = "btn btn-green w-1/4") {
+"search"
}