Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle leading/trailing spaces in search input #248

Merged
merged 5 commits into from
Dec 26, 2024

Conversation

maxidragon
Copy link
Member

It's very annoying when you paste a competition name that accidentally has a space before the actual name, causing the search to fail. The same issue occurs if there are, for example, two spaces between words.

After these changes, it's no longer possible to start a search with a space or type multiple consecutive spaces. I couldn't find a better solution to this, if anyone has suggestions feel free to comment on this PR.

@jonatanklosko
Copy link
Member

We could only change the value send in the request, but I would actually move this all the way up and make the server decide. It would be here:

defp filter_by_text(query, filter) do
from c in query, where: ilike(c.name, ^"%#{filter}%")
end

You can add

filter = filter |> String.trim() |> String.replace(~r/\s+/, " ")

and then we can expand this test:

test "list_competitions/1 given :filter returns competitions with matching name" do
worlds = insert(:competition, name: "World Championship 2020")
open = insert(:competition, name: "WHAT Open 2020")
euro = insert(:competition, name: "Euro Championship 2020")
list = Competitions.list_competitions(%{filter: "champion"})
assert worlds.id in ids(list)
assert open.id not in ids(list)
assert euro.id in ids(list)
end

@maxidragon
Copy link
Member Author

That would be nice, but I've tried trimming the name on the backend (and changing the value only for the request), and it won't actually work because the Autocomplete component does its own filtering, regardless of what is in the array.

@jonatanklosko
Copy link
Member

We can skip the default Autocomplete filtering, by adding this:

filterOptions={(options, state) => options}

@maxidragon
Copy link
Member Author

We can skip the default Autocomplete filtering, by adding this:

filterOptions={(options, state) => options}

This is exactly what I was looking for but couldn’t find, thanks!

@jonatanklosko jonatanklosko merged commit 6b7cd5f into thewca:main Dec 26, 2024
2 checks passed
@jonatanklosko
Copy link
Member

Thanks!

@maxidragon maxidragon deleted the trim-search branch December 26, 2024 15:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants