diff options
| author | Miquel Sabaté Solà <msabate@suse.com> | 2024-03-13 18:03:43 +0100 |
|---|---|---|
| committer | Miquel Sabaté Solà <msabate@suse.com> | 2024-03-13 18:03:43 +0100 |
| commit | a7791635aa98f012d35f97c3c5288494bbb28e38 (patch) | |
| tree | bad987e6b4d10ad2f7cd02e40658d464907595ee /app/javascript/controllers/search_controller.js | |
| download | operum-a7791635aa98f012d35f97c3c5288494bbb28e38.tar.gz operum-a7791635aa98f012d35f97c3c5288494bbb28e38.zip | |
Initial commit
Signed-off-by: Miquel Sabaté Solà <msabate@suse.com>
Diffstat (limited to 'app/javascript/controllers/search_controller.js')
| -rw-r--r-- | app/javascript/controllers/search_controller.js | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/app/javascript/controllers/search_controller.js b/app/javascript/controllers/search_controller.js new file mode 100644 index 0000000..b35593a --- /dev/null +++ b/app/javascript/controllers/search_controller.js @@ -0,0 +1,36 @@ +import { Controller } from "@hotwired/stimulus" + +// Controller that dynamically fetches search results from the body as +// introduced on an input. Then it fills the `results` elements with them. +export default class extends Controller { + static targets = ["body"] + + connect() { + document.getElementById("search_body").focus(); + } + + load(event) { + event.preventDefault(); + + try { + fetch("/searches/search?" + new URLSearchParams({body: this.bodyTarget.value})) + .then(response => response.text()) + .then(html => { + document.getElementById("results").innerHTML = html; + document.getElementById("save-search").classList.toggle("hidden"); + }) + } catch (error) { + document.getElementById("results").innerHTML = '<div class="notice">Something went wrong!</div>' + } + } + + save(event) { + event.preventDefault(); + + document.getElementById("search-form-submit").classList.toggle("hidden"); + document.getElementById("save-search").classList.toggle("hidden"); + document.getElementById("search-form-name").classList.toggle("hidden"); + document.getElementById("search-form-shared").classList.toggle("hidden"); + document.getElementById("search_name").focus(); + } +} |
