Search by Title

So far we've explored how we can filter results from the Surreal Estate API with the query query parameter, and sort them with the sort query parameter. Now we need to use the query query parameter to search. We can do so like so:
Previously we have filtered like so:
When we pass an object with a $regex key, we say instead "match the given value (in this case <search_query_here>) against part of the field's value. So if we did:
Then we'd get back all properties with the word bungalow in the title. Note that this isn't very advanced search functionality, but will suffice for not overcomplicating this track too much. Large organisations will more likely use something like Elastic Search to handle their search operations.
What you need to do is to create a search form that - when submitted - programatically (without the <Link /> component) changes the URL to include your search query. A spanner in the works is that you will still need to be able to filter search results by city, so you will need to find a way of combining query objects inside your buildQueryString method so that the following is possible:
To complete this challenge, you will need to:
- Create a search form inside your
<SideBar />component which should consist of a text<input>and a submit<button>. The text<input>should be tied to a field in your state (queryorsearchwould be good field names) - refer back to the AddProperty step if you are unsure about this. On form submission, ahandleSearch()function should be called on your component. - Inside the
handleSearch()function you should call yourbuildQueryString()function, passing in'query'as your first argument and{ title: { $regex: <search query here> } }as your second argument. Note that<search query here>will be replaced with the value of your text<input>, which should come from the state that you set earlier. Assign the result of invokingbuildQueryString()to a variable. - Use the
useNavigate()hook fromreact-router-domto programatically change the URL to your new query string returned from thebuildQueryString()function. - Give this a test run in your browser to check everything is working A-OK. You should notice that it isn't currently possible to search and filter by city.
- Modify your
buildQueryString()function so it supports more than onequerykey. Hint: you can achieve this by spreading one level deeper.