Skip to content

Commit

Permalink
fix: improvements to tags suggestions, from contributors feedback (#1…
Browse files Browse the repository at this point in the history
…0147)

fix: improvements to tags suggestions
  • Loading branch information
stephanegigandet committed Apr 19, 2024
1 parent 6ae219c commit 8780632
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
5 changes: 3 additions & 2 deletions html/js/product-multilingual.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,13 +535,14 @@ function initializeTagifyInputs() {
forEach((input) => initializeTagifyInput(input));
}

const maximumRecentEntriesPerTag = 3;
const maximumRecentEntriesPerTag = 10;

function initializeTagifyInput(el) {
const input = new Tagify(el, {
autocomplete: true,
whitelist: get_recents(el.id) || [],
dropdown: {
highlightFirst: false,
enabled: 0,
maxItems: 100
}
Expand Down Expand Up @@ -579,7 +580,7 @@ function initializeTagifyInput(el) {
then((RES) => RES.json()).
then(function (json) {
const lc = (/^\w\w:/).exec(value);
let whitelist = Object.values(json.matched_synonyms);
let whitelist = json.suggestions;
if (lc) {
whitelist = whitelist.map(function (e) {
return {"value": lc + e, "searchBy": e};
Expand Down
12 changes: 10 additions & 2 deletions lib/ProductOpener/APITaxonomySuggestions.pm
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ use ProductOpener::Tags qw/%taxonomy_fields/;
use ProductOpener::Lang qw/:all/;
use ProductOpener::TaxonomySuggestions qw/get_taxonomy_suggestions_with_synonyms/;
use ProductOpener::API qw/add_error/;
use Tie::IxHash;

use Encode;

Expand Down Expand Up @@ -130,10 +131,17 @@ sub taxonomy_suggestions_api ($request_ref) {
$log->debug("taxonomy_suggestions_api", @suggestions) if $log->is_debug();
$response_ref->{suggestions} = [map {$_->{tag}} @suggestions];
if ($options_ref->{get_synonyms}) {
$response_ref->{matched_synonyms} = {};
# We need a tie hash so that the keys are ordered by insertion order when returned as JSON
my %matched_synonyms;
tie(%matched_synonyms, 'Tie::IxHash');
foreach (@suggestions) {
$response_ref->{matched_synonyms}->{$_->{tag}} = ucfirst($_->{matched_synonym});
$matched_synonyms{$_->{tag}} = ucfirst($_->{matched_synonym});
}
$response_ref->{matched_synonyms} = \%matched_synonyms;
# Note: this does not seem to work with JSON::PP, even though the "canonical" option
# should preserve the order of the keys of tied hashes.
# As JSON hashes are unordered, we will use the "suggestions" array on the client side to get the right order.
# It would have been nice to order the matched synonyms anyway, but it is not a huge issue.
}
}

Expand Down
1 change: 0 additions & 1 deletion lib/ProductOpener/Data.pm
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ use experimental 'smartmatch';
use ProductOpener::Config qw/:all/;

use MongoDB;
use Tie::IxHash;
use JSON::PP;
use CGI ':cgi-lib';
use Log::Any qw($log);
Expand Down

0 comments on commit 8780632

Please sign in to comment.