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"] static values = { initialBody: String } connect() { this.initialBody = this.bodyTarget.value.trim(); 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; if (this.initialBody != this.bodyTarget.value) { document.getElementById("save-search").classList.remove('hidden'); } else { document.getElementById("save-search").classList.add('hidden'); } }) } catch (error) { document.getElementById("results").innerHTML = '