mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Take the first name match from the post-filter results to avoid matching off a result the user can't see
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
Darkswarm.controller "EnterprisesCtrl", ($scope, $rootScope, Enterprises, Search, $document, HashNavigation, FilterSelectorsService, EnterpriseModal, enterpriseMatchesNameQueryFilter, distanceWithinKmFilter) ->
|
||||
Darkswarm.controller "EnterprisesCtrl", ($scope, $rootScope, $timeout, Enterprises, Search, $document, HashNavigation, FilterSelectorsService, EnterpriseModal, enterpriseMatchesNameQueryFilter, distanceWithinKmFilter) ->
|
||||
$scope.Enterprises = Enterprises
|
||||
$scope.totalActive = FilterSelectorsService.totalActive
|
||||
$scope.clearAll = FilterSelectorsService.clearAll
|
||||
@@ -13,11 +13,15 @@ Darkswarm.controller "EnterprisesCtrl", ($scope, $rootScope, Enterprises, Search
|
||||
|
||||
|
||||
$scope.$watch "query", (query)->
|
||||
Enterprises.evaluateQuery query
|
||||
Enterprises.flagMatching query
|
||||
Search.search query
|
||||
$rootScope.$broadcast 'enterprisesChanged'
|
||||
$scope.distanceMatchesShown = false
|
||||
|
||||
$timeout ->
|
||||
Enterprises.calculateDistance query, $scope.nameMatchesFiltered[0]
|
||||
$rootScope.$broadcast 'enterprisesChanged'
|
||||
|
||||
|
||||
$rootScope.$on "enterprisesChanged", ->
|
||||
$scope.filterEnterprises()
|
||||
|
||||
@@ -28,10 +28,6 @@ Darkswarm.factory 'Enterprises', (enterprises, CurrentHub, Taxons, Dereferencer,
|
||||
Dereferencer.dereference enterprise.taxons, Taxons.taxons_by_id
|
||||
Dereferencer.dereference enterprise.supplied_taxons, Taxons.taxons_by_id
|
||||
|
||||
evaluateQuery: (query) ->
|
||||
@flagMatching query
|
||||
@calculateDistance query
|
||||
|
||||
flagMatching: (query) ->
|
||||
for enterprise in @enterprises
|
||||
enterprise.matches_name_query = if query? && query.length > 0
|
||||
@@ -39,15 +35,12 @@ Darkswarm.factory 'Enterprises', (enterprises, CurrentHub, Taxons, Dereferencer,
|
||||
else
|
||||
false
|
||||
|
||||
firstMatching: ->
|
||||
(enterprise for enterprise in @enterprises when enterprise.matches_name_query)[0]
|
||||
|
||||
calculateDistance: (query) ->
|
||||
calculateDistance: (query, firstMatching) ->
|
||||
if query?.length > 0
|
||||
if @firstMatching()?
|
||||
@setDistanceFrom @firstMatching()
|
||||
if firstMatching?
|
||||
@setDistanceFrom firstMatching
|
||||
else
|
||||
@calculateDistanceGeo(query)
|
||||
@calculateDistanceGeo query
|
||||
else
|
||||
@resetDistance()
|
||||
|
||||
|
||||
@@ -10,17 +10,17 @@
|
||||
|
||||
.row
|
||||
.small-12.columns
|
||||
.name-matches{"ng-show" => "nameMatches.length > 0"}
|
||||
.name-matches{"ng-show" => "nameMatchesFiltered.length > 0"}
|
||||
%h2 Did you mean?
|
||||
= render "home/hubs_table", enterprises: "nameMatches"
|
||||
|
||||
.distance-matches{"ng-if" => "nameMatches.length == 0 || distanceMatchesShown"}
|
||||
%h2{"ng-show" => "nameMatches.length > 0 || query.length > 0"}
|
||||
.distance-matches{"ng-if" => "nameMatchesFiltered.length == 0 || distanceMatchesShown"}
|
||||
%h2{"ng-show" => "nameMatchesFiltered.length > 0 || query.length > 0"}
|
||||
Closest to
|
||||
%span{"ng-show" => "nameMatches.length > 0"} {{ nameMatches[0].name }}...
|
||||
%span{"ng-hide" => "nameMatches.length > 0"} {{ query }}...
|
||||
%span{"ng-show" => "nameMatchesFiltered.length > 0"} {{ nameMatchesFiltered[0].name }}...
|
||||
%span{"ng-hide" => "nameMatchesFiltered.length > 0"} {{ query }}...
|
||||
|
||||
= render "home/hubs_table", enterprises: "distanceMatches"
|
||||
|
||||
.show-distance-matches{"ng-show" => "nameMatches.length > 0 && !distanceMatchesShown"}
|
||||
%a{href: "", "ng-click" => "showDistanceMatches()"} Show me shops near {{ nameMatches[0].name }}
|
||||
.show-distance-matches{"ng-show" => "nameMatchesFiltered.length > 0 && !distanceMatchesShown"}
|
||||
%a{href: "", "ng-click" => "showDistanceMatches()"} Show me shops near {{ nameMatchesFiltered[0].name }}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
.active_table
|
||||
%hub.active_table_node.row{"ng-repeat" => "hub in filteredEnterprises = (#{enterprises} | visible | taxons:activeTaxons | shipping:shippingTypes | showHubProfiles:show_profiles | orderBy:['-active', '+distance', '+orders_close_at'])",
|
||||
%hub.active_table_node.row{"ng-repeat" => "hub in #{enterprises}Filtered = (#{enterprises} | visible | taxons:activeTaxons | shipping:shippingTypes | showHubProfiles:show_profiles | orderBy:['-active', '+distance', '+orders_close_at'])",
|
||||
"ng-class" => "{'is_profile' : hub.category == 'hub_profile', 'closed' : !open(), 'open' : open(), 'inactive' : !hub.active, 'current' : current()}",
|
||||
"ng-controller" => "HubNodeCtrl",
|
||||
id: "{{hub.hash}}"}
|
||||
@@ -7,4 +7,4 @@
|
||||
= render 'home/skinny'
|
||||
= render 'home/fat'
|
||||
|
||||
= render 'shared/components/enterprise_no_results', enterprises: enterprises
|
||||
= render 'shared/components/enterprise_no_results', enterprises: "#{enterprises}Filtered"
|
||||
|
||||
@@ -101,27 +101,16 @@ describe "Enterprises service", ->
|
||||
Enterprises.flagMatching ''
|
||||
expect(e.matches_name_query).toBe false for e in enterprises
|
||||
|
||||
describe "finding the first enterprise matched by a query", ->
|
||||
it "returns the enterprise when one exists", ->
|
||||
enterprises[0].matches_name_query = false
|
||||
enterprises[1].matches_name_query = true
|
||||
expect(Enterprises.firstMatching()).toEqual enterprises[1]
|
||||
|
||||
it "returns undefined otherwise", ->
|
||||
e.matches_name_query = false for e in enterprises
|
||||
expect(Enterprises.firstMatching()).toBeUndefined()
|
||||
|
||||
describe "calculating the distance of enterprises from a location", ->
|
||||
describe "when a query is provided", ->
|
||||
it "sets the distance from the enterprise when a name match is available", ->
|
||||
spyOn(Enterprises, "firstMatching").andReturn('match')
|
||||
spyOn(Enterprises, "setDistanceFrom")
|
||||
Enterprises.calculateDistance "asdf"
|
||||
Enterprises.calculateDistance "asdf", 'match'
|
||||
expect(Enterprises.setDistanceFrom).toHaveBeenCalledWith('match')
|
||||
|
||||
it "calculates the distance from the geocoded query otherwise", ->
|
||||
spyOn(Enterprises, "calculateDistanceGeo")
|
||||
Enterprises.calculateDistance "asdf"
|
||||
Enterprises.calculateDistance "asdf", undefined
|
||||
expect(Enterprises.calculateDistanceGeo).toHaveBeenCalledWith("asdf")
|
||||
|
||||
it "resets the distance when query is null", ->
|
||||
|
||||
Reference in New Issue
Block a user