Replies: 4 comments 1 reply
-
Just some alternatives from the top of my head:
I don't know, I'm just thinking out loud. You probably have much better ideas :-) |
Beta Was this translation helpful? Give feedback.
-
Also apologies if this should be a bug/feature request, or some other discussion category - I wasn't sure. |
Beta Was this translation helpful? Give feedback.
-
Here's how I've done it now without any changes to autocomplete.js; function insertError(query, err) {
return [
{
key1: query,
error: err
}
]
}
// data.key: ["key1"],
//data.src:
let data
await fetch("/", params)
.then(res => {
if (!res.ok || (res.status >= 400 && res.status < 600)) {
throw new Error(
"Custom error message."
)
}
return res.json()
})
.then(json => (data = json))
.catch(error => (data = error))
if (
data instanceof Error || data.foo.bar.validation.fails
) {
return insertError(
query.replace(/\s/g, ""),
data instanceof Error
? data
: new Error(
"Returned JSON fails validation"
)
)
}
return data.foo.bar
// ...
resultItem: {
content: (data, element) => {
if (data.value.error instanceof Error) {
element.innerHTML = data.value.error.message
element.classList.add("error")
} else {
element.innerHTML = `<span>${data.value.key1}</span>`
}
}
}
resultsList: {
container: element => {
if (
element.children.length === 1 &&
element.children[0].classList.contains("error")
) {
let errorMessage = element.children[0].innerHTML
element.innerHTML = ""
const result = document.createElement("p")
result.setAttribute("class", "autoComplete_error")
result.setAttribute("tabindex", "-1")
result.innerHTML = `<span class="iconify-inline" data-icon="fa-solid:exclamation-triangle"></span>${errorMessage}.`
result.addEventListener("click", ev => {
ev.stopPropagation()
})
element.prepend(result)
}
}
} Obviously this is heavily edited from the actual code, to translate it to english and strip it down to the relevant parts. It works but I'm hoping someone can come up with a more elegant solution :-) |
Beta Was this translation helpful? Give feedback.
-
Hello @folknor, I'll think of different approaches for handling this use case and I'll share them with you. Have a nice day! :) |
Beta Was this translation helpful? Give feedback.
-
Hi,
I'm wondering how would you let the user know that there was an error in an async
data.src
request?Currently I catch those errors and show them in a separate part of the UI, but what I'd like is to show them "inline" in the resultsList like
Of course I can fake the look and such of my custom error UI to look like the box there, but I was hoping you had some ideas or thoughts.
My first thought was just to return a single custom result from
data.src
like thisI've not expanded this into working code yet. I'm just brainstorming.
Any thoughts?
Thank you!
Beta Was this translation helpful? Give feedback.
All reactions