Introduce categories into navbar tags/search dropdown
https://github.com/publiclab/plots2/blob/77188bace30ac41174dfbdb2f4754f8da39303d2/app/controllers/tag_controller.rb#L398-L405
So when typing water this endpoint returns:
[
"water",
"water-samples",
"waterkeepers",
"water-sampling",
"wateristic",
"wateristics",
"water-monitoring-network",
"water-monitoring",
"water-monitor",
"water-sensing"
]
My guess from this code: https://github.com/bassjobsen/Bootstrap-3-Typeahead/blob/fc2d5dce4a5406d9b924224e395d5647918707c7/bootstrap3-typeahead.js#L335-L352
...is that if an item has an attribute "category" then they will be sorted by that category.
But above they are just strings. We should switch to the JSON type shown here: https://github.com/bassjobsen/Bootstrap-3-Typeahead#using-json-objects-instead-of-simple-strings
So we will need to modify def suggested above - and will have to be careful that doesn't break anything else (some other systems may depend on it returning a string -- https://github.com/publiclab/plots2/search?q=suggested shows a few!)
So as not to break anything else, maybe we should use a params flag like params[:json] and the request should be like: https://publiclab.org/tag/suggested/water?json=true?
Actually i believe that this line shows that it doesn't sort the results at all -- it only shows a category header if the previous item is not of the same category type as the current one:
https://github.com/bassjobsen/Bootstrap-3-Typeahead/blob/fc2d5dce4a5406d9b924224e395d5647918707c7/bootstrap3-typeahead.js#L339
That may mean that we can introduce the "search" option from the Ruby side, whether or not we use categories -- JavaScript really isn't re-ordering the returned results!
So the steps would be:
- change the
suggestedmethod to look forparams[:json]and return json if it's the case (probably by creating a Ruby hash (associative array) and then returning it with something like:
render json: items
- modify the Ruby method to append a category to each item
- add a new item to the "beginning" of the hash, with a category like "search"]
- modify the logic here to run a search instead of look up a tag for the newly added item, by changing line 56 to look for "category" of "search" as well: https://github.com/publiclab/plots2/blob/ec2b994dafb4e8e2b89082a34be1177edce994ef/app/assets/javascripts/restfulTypeahead.js#L55-L73
I may have missed some steps but that sounds like a good start! Does it make sense?
@KarishmaVanwari let me know what you think about this!
Hi @jywarren! Apologies for delayed response. I have been reading this and went through the details as mentioned. It's been difficult for me to figure out this complete task, since it sounds a little complicated (it seems so, I'm not sure though). I guess I'll need time to figure this out. Also can we discuss this over the meet this Tuesday? I need some input to understand this better. Thanks!
Yes we can def. discuss Tuesday! It is a bit complex. I would break it into parts:
- get the JSON format response working first, test it manually
- get the navbar autocomplete code to request the JSON format by adding the
?json=trueflag - get the navbar autocomplete code to read JSON format by setting that using https://github.com/bassjobsen/Bootstrap-3-Typeahead#using-json-objects-instead-of-simple-strings
We could make a PR here and merge it if you want since it's all working OK at this point!
- now try introducing a new item into the list by adding it in Ruby in the
def suggestedmethod. It may take some trial and error to correctly add it. Monitor the results by manually fetching them in the JS console from any PublicLab.org page using$.post('https://publiclab.org/tag/suggested/water').done(function(r){console.log(r)}) - Once you figure it out, you should be able to try searching in your local dev environment by just typing into the navbar typeahead field!
- Now, we adjust what we want it to say and where it should link to
Now we could do another PR and merge
- Now we can try adding the "category" value, so that the JSON ends up looking something like the below:
[
{id: "typeahead-search", name: "Search for 'water'", category: "search"},
{id: "typeahead-water-quality", name: "water-quality", category: "tags"},
{id: "typeahead-water-pollution", name: "water-pollution", category: "tags"}
]
This will probably mean editing the outgoing Ruby Hash, doing something like this: https://stackoverflow.com/a/16573500
OK, this is a lot... let me know if it makes any sense, but the big message is, do it in small pieces and ask for clarifications along the way! You don't have to understand the whole thing at once, and you're learning a lot of moving pieces at the same time, so it's OK to solve one piece at a time. 🎉
Good day my name is jesutobi, i am an outreachy applicant, i would like to contribute on this issue
Hey, can I work on this issue?