Better API handling

This commit is contained in:
Elnu 2023-07-21 17:13:07 -07:00
parent 61c1ce5502
commit f558d7f0c1
4 changed files with 62 additions and 52 deletions

View file

@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jidict</title>
<title>{{ with .Query }}{{ . }} search - {{ end }}jidict</title>
<link rel="stylesheet" href="https://unpkg.com/missing.css@1.0.9/dist/missing.min.css">
<style>
li {
@ -18,14 +18,13 @@
<img src="https://jichan.org/logo.svg" style="height: 4em; display: block; margin: 1em auto 1em auto">
</a>
<form
hx-get="/api/search"
hx-on::before-request="this.setAttribute('hx-replace-url', `/search/${this.querySelector('input').value}`)"
hx-target="#results"
hx-swap="innerHTML">
hx-get="/search"
hx-replace-url="true"
hx-target="#results">
<input type="text" name="q"{{ with .Query }} value="{{ . }}"{{ end }} placeholder="辞書をサーチする" class="width:100%" autocomplete="false">
</form>
<div id="results">
{{ with .Results }}{{ template "search" . }}{{ end }}
{{ if .Count }}{{ template "search" . }}{{ end }}
</div>
<br>
</main>

View file

@ -1,9 +1,9 @@
{{- define "search" -}}
<p><i>{{ if .Truncated }}Truncated results, showing first {{ .Count }}{{ else }}{{ if eq .Count 0 }}No results{{ else }}{{ .Count }} result{{ if ne .Count 1}}s{{ end }}{{ end }}{{ end }}.</i></p>
{{- range .ExactResults -}}
{{ range .ExactResults -}}
{{- template "word" . -}}
{{- end }}
<hr>
{{ if and (ne (len .ExactResults) 0) (ne (len .OtherResults) 0) }}<hr>{{ end }}
{{ range .OtherResults -}}
{{ template "word" . }}
{{- end -}}