From 833eb45379270cec2118b5b0e85f75dcc8c6a498 Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Sun, 8 Dec 2013 22:31:49 -0500 Subject: [PATCH] VRFS-878 use IP address to default location for band setup --- web/app/assets/javascripts/band_setup.js | 75 +++++++++++-------- web/app/assets/javascripts/jam_rest.js | 7 ++ .../api_maxmind_requests_controller.rb | 6 ++ web/config/routes.rb | 1 + 4 files changed, 59 insertions(+), 30 deletions(-) diff --git a/web/app/assets/javascripts/band_setup.js b/web/app/assets/javascripts/band_setup.js index ae27f3e27..3d51a93db 100644 --- a/web/app/assets/javascripts/band_setup.js +++ b/web/app/assets/javascripts/band_setup.js @@ -246,7 +246,17 @@ } else { loadGenres(); - loadCountries(); + + rest.getResolvedLocation() + .done(function(location) { + loadCountries(location.country, function() { + loadRegions(location.region, function() { + loadCities(location.city); + }); + }); + }); + + $("#band-setup-title").html("set up band"); $("#btn-band-setup-save").html("CREATE BAND"); } @@ -261,7 +271,7 @@ loadGenres(band.genres); loadCountries(band.country, function() { - loadRegions(band.state, function () { + loadRegions(band.region, function() { loadCities(band.city); }); }); @@ -357,24 +367,26 @@ nilOption.text(nilOptionText); $region.append(nilOption); - rest.getRegions({'country': selectedCountry}).done(function(response) { - $.each(response["regions"], function(index, region) { - if(!region) return; - var option = $(''); - option.text(region); - option.attr("value", region); + if (selectedCountry) { + rest.getRegions({'country': selectedCountry}).done(function(response) { + $.each(response["regions"], function(index, region) { + if(!region) return; + var option = $(''); + option.text(region); + option.attr("value", region); - if (initialRegion === region) { - option.attr("selected", "selected"); + if (initialRegion === region) { + option.attr("selected", "selected"); + } + + $region.append(option); + }); + + if (onRegionsLoaded) { + onRegionsLoaded(); } - - $region.append(option); }); - - if (onRegionsLoaded) { - onRegionsLoaded(); - } - }); + } } function loadCities(initialCity) { @@ -387,20 +399,22 @@ nilOption.text(nilOptionText); $city.append(nilOption); - rest.getCities({'country': selectedCountry, 'region': selectedRegion}) .done(function(response) { - $.each(response["cities"], function(index, city) { - if(!city) return; - var option = $(''); - option.text(city); - option.attr("value", city); + if (selectedCountry && selectedRegion) { + rest.getCities({'country': selectedCountry, 'region': selectedRegion}) .done(function(response) { + $.each(response["cities"], function(index, city) { + if(!city) return; + var option = $(''); + option.text(city); + option.attr("value", city); - if (initialCity === city) { - option.attr("selected", "selected"); - } + if (initialCity === city) { + option.attr("selected", "selected"); + } - $city.append(option); + $city.append(option); + }); }); - }); + } } function friendSelectorCallback(newSelections) { @@ -467,13 +481,14 @@ $('#band-country').on('change', function(evt) { evt.stopPropagation(); - loadRegions(''); + loadRegions(); + loadCities(); return false; }); $('#band-region').on('change', function(evt) { evt.stopPropagation(); - loadCities(''); + loadCities(); return false; }); diff --git a/web/app/assets/javascripts/jam_rest.js b/web/app/assets/javascripts/jam_rest.js index e2c44c86c..c6cc63d71 100644 --- a/web/app/assets/javascripts/jam_rest.js +++ b/web/app/assets/javascripts/jam_rest.js @@ -184,6 +184,12 @@ }); } + function getResolvedLocation() { + return $.ajax('/api/resolved_location', { + dataType: 'json' + }); + } + function getInstruments(options) { return $.ajax('/api/instruments', { data : { }, @@ -466,6 +472,7 @@ this.getRegions = getRegions; this.getCountries = getCountries; this.getIsps = getIsps; + this.getResolvedLocation = getResolvedLocation; this.getInstruments = getInstruments; this.getGenres = getGenres; this.updateAvatar = updateAvatar; diff --git a/web/app/controllers/api_maxmind_requests_controller.rb b/web/app/controllers/api_maxmind_requests_controller.rb index 5173c6754..98c0cf8b3 100644 --- a/web/app/controllers/api_maxmind_requests_controller.rb +++ b/web/app/controllers/api_maxmind_requests_controller.rb @@ -34,4 +34,10 @@ class ApiMaxmindRequestsController < ApiController end end + # returns location hash (country, region, state) based on requesting IP + def resolved_location + location = MaxMindManager.lookup(request.remote_ip) + render :json => { :country => location[:country], :region => location[:state], :city => location[:city] }, :status => 200 + end + end \ No newline at end of file diff --git a/web/config/routes.rb b/web/config/routes.rb index 34d492721..6e7974b7f 100644 --- a/web/config/routes.rb +++ b/web/config/routes.rb @@ -259,6 +259,7 @@ SampleApp::Application.routes.draw do match '/regions' => 'api_maxmind_requests#regions', :via => :get match '/cities' => 'api_maxmind_requests#cities', :via => :get match '/isps' => 'api_maxmind_requests#isps', :via => :get + match '/resolved_location' => 'api_maxmind_requests#resolved_location', :via => :get # Recordings