Skip to content
This repository has been archived by the owner on Oct 8, 2019. It is now read-only.

Commit

Permalink
feat(Signup): guess currency from country code #688
Browse files Browse the repository at this point in the history
  • Loading branch information
Sjors authored Sep 29, 2016
2 parents 0e09a14 + 1866232 commit bbd0dc4
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 16 deletions.
2 changes: 1 addition & 1 deletion app/partials/signup.jade
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ form.form-horizontal(
button.button-primary(
type="submit"
data-style="expand-left"
ng-disabled="signupForm.$invalid"
ng-disabled="signupForm.$invalid || !currency_guess"
ladda-translate="CONTINUE"
ui-ladda="working")
141 changes: 127 additions & 14 deletions assets/js/controllers/signup.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ angular
.module('walletApp')
.controller('SignupCtrl', SignupCtrl);

SignupCtrl.$inject = ['$scope', '$state', '$cookies', '$filter', '$timeout', '$translate', 'Wallet', 'currency', 'languages', 'MyWallet'];
SignupCtrl.$inject = ['$scope', '$state', '$cookies', '$filter', '$timeout', '$translate', 'Wallet', 'currency', 'languages', 'MyWallet', '$http', '$rootScope'];

function SignupCtrl ($scope, $state, $cookies, $filter, $timeout, $translate, Wallet, currency, languages, MyWallet) {
function SignupCtrl ($scope, $state, $cookies, $filter, $timeout, $translate, Wallet, currency, languages, MyWallet, $http, $rootScope) {
$scope.working = false;
$scope.browser = {disabled: true};

Expand All @@ -18,21 +18,134 @@ function SignupCtrl ($scope, $state, $cookies, $filter, $timeout, $translate, Wa
language_guess = $filter('getByProperty')('code', 'en', languages);
}

var cur = 'USD';
$scope.language_guess = language_guess;

switch (language_guess.code) {
case 'zh-cn':
cur = 'CNY';
break;
case 'nl':
cur = 'EUR';
break;
default:
}
// Get country code from server:
$http.get($rootScope.rootURL + 'wallet/browser-info')
.success(data => {
let cur;
const country_code = data.country_code;
switch (country_code) { // iso-3166-2
case 'US':
case 'IO': // British Indian Ocean Territory (de-facto)
cur = 'USD';
break;
// Eurozone countries:
case 'AT':
case 'BE':
case 'CY':
case 'EE':
case 'FI':
case 'FR':
case 'DE':
case 'GR':
case 'IE':
case 'IT':
case 'LV':
case 'LT':
case 'LU':
case 'MT':
case 'NL':
case 'PT':
case 'SK':
case 'SI':
case 'ES':
// Euro de-facto currency:
case 'MC': // Monaco
case 'SM': // San Marino
case 'VA': // Vatican City
case 'AD': // Andorra
case 'PM': // Saint Pierre and Miquelon
case 'YT': // Mayotte
case 'BL': // Saint Barthélemy
case 'XK': // Kosovo
case 'ME': // Montenegro
// EUR is probably best alternative we support:
case 'NO': // Norway
case 'AL': // Albania
case 'BA': // Bosnia and Herzegovina
case 'MK': // Macedonia
case 'RS': // Serbia
case 'TR': // Turkey
case 'MD': // Moldova
case 'CZ': // Czech Republic
cur = 'EUR';
break;
case 'IS': // Iceland
cur = 'ISK';
break;
case 'HK': // Hong Kong
cur = 'HKD';
break;
case 'TW': // Taiwan
cur = 'TWD';
break;
case 'CH': // Switserland
case 'LI': // Liechtenstein
cur = 'CHF';
break;
case 'DK': // Denmark
case 'GL': // Greenland
case 'FO': // Faroe Islands
cur = 'DKK';
break;
case 'CL': // Chili
cur = 'CLP';
break;
case 'CA': // Canada
cur = 'CAD';
break;
case 'CN': // China
case 'MO': // Macau (or is HKD better?)
cur = 'CNY';
break;
case 'TH': // Thailand
cur = 'THB';
break;
case 'AU': // Australia
cur = 'AUD';
break;
case 'SG': // Singapore
cur = 'SGD';
break;
case 'KR': // South Korea
cur = 'KRW';
break;
case 'JP': // Japan
cur = 'JPY';
break;
case 'PL': // Poland
cur = 'PLN';
break;
case 'GB': // Great Britain, United Kingdom, Channel Islands
case 'IM': // Isle of Man
case 'GG': // Guernsey
case 'SS': // South Georgia and the South Sandwich Islands
case 'JE': // Jersey
cur = 'GBP';
break;
case 'SE': // Sweden
cur = 'SEK';
break;
case 'NZ': // New Zealand
cur = 'NZD';
break;
case 'BR': // Brazil
cur = 'BRL';
break;
case 'RU': // Russia
cur = 'RUB';
break;
default:
cur = 'USD'; // One day this will be BTC
}

$scope.language_guess = language_guess;
$scope.currency_guess = $filter('getByProperty')('code', cur, currency.currencies);
}).error(() => {
// Fallback to USD
$scope.currency_guess = $filter('getByProperty')('code', 'USD', currency.currencies);
});

$scope.currency_guess = $filter('getByProperty')('code', cur, currency.currencies);
$scope.fields = {
password: '',
confirmation: '',
Expand Down
8 changes: 7 additions & 1 deletion tests/controllers/signup_controller_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ describe "SignupCtrl", ->
modalInstance =
close: ->
dismiss: ->
$httpBackend = undefined

beforeEach angular.mock.module("walletApp")

beforeEach ->
angular.mock.inject ($injector, $rootScope, $controller, $compile, $templateCache) ->
Wallet = $injector.get("Wallet")

$rootScope.rootURL = "/"
$httpBackend = $injector.get("$httpBackend")
$httpBackend.expectGET("/wallet/browser-info").respond {country_code: "NL"}

Wallet.my.browserCheck = () -> true
Wallet.my.browserCheckFast = () -> true

Expand Down Expand Up @@ -161,4 +166,5 @@ describe "SignupCtrl", ->

describe "currency", ->
it "should guess the correct currency", ->
expect(scope.currency_guess.code).toBe("USD")
$httpBackend.flush()
expect(scope.currency_guess.code).toBe("EUR")

0 comments on commit bbd0dc4

Please sign in to comment.