-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared.js
133 lines (114 loc) · 4.51 KB
/
shared.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import React, { Component } from 'react';
import { Meteor } from 'meteor/meteor';
import { ExchangeRates } from './collections';
import { Tracker } from 'meteor/tracker';
import currencies from './currency-data';
const countries = require('./country-data.json');
const dependency = new Tracker.Dependency()
let localCountry = getDefaultCountry();
function observeCurrency(){
dependency.depend();
Meteor.subscribe('currency-caribou.exchangerates');
}
Meteor.startup(()=>{
Meteor.subscribe('currency-caribou.exchangerates');
ExchangeRates.find({}, { sort: { timestamp: -1 } }).observe({
added: ()=>{ dependency.changed(); },
});
});
function alpha2ToCountry(alpha2){
return countries.find((c) => { return c.alpha2 === alpha2 });
}
function codeToCurrency(code){
return currencies.find((c) => { return c.code === code });
}
function alpha2ToCurrency(alpha2) {
const country = alpha2ToCountry(alpha2)
if(country) return codeToCurrency(country.currencies[0]);
return null;
}
function currencyToCountry(code) {
if(typeof code === 'object') code = code.code;
return countries.find((c) => { return c.currencies[0] === code });
}
function getDefaultCountry() { return alpha2ToCountry(Meteor.settings.public.currencyCaribou.defaultCountry); }
function getLocalCurrency(){ return alpha2ToCurrency(localCountry.alpha2); }
function getDefaultCurrency() { return alpha2ToCurrency(Meteor.settings.public.currencyCaribou.defaultCountry); }
if (Meteor.isClient) {
// send a request for the geoip data and save it when it arrives
$.getJSON('https://freegeoip.net/json/')
.done((data) => {
if(!data.country_code){
console.log('Error geolocating IP address, no country code: ', error);
return;
}
const newLocalCountry = alpha2ToCountry(data.country_code);
if(newLocalCountry) localCountry = newLocalCountry;
dependency.changed();
})
.fail((jqxhr, textStatus, error) => {
console.log('Error geolocating IP address: ', error);
});
}
function getLatestExchangeRates(){
return ExchangeRates.findOne({}, { sort: { timestamp: -1 } });
}
function convertAmount(amount, toCurrency, fromCurrency) {
const latestRates = getLatestExchangeRates();
let rate = 1;
if (latestRates) {
rate = latestRates.rates[toCurrency.code]/latestRates.rates[fromCurrency.code];
}
const convertedAmount = amount * rate;
return convertedAmount;
}
class CurrencyAmount extends Component {
render(){
const toCurrency = codeToCurrency(this.props.toCurrencyCode);
const fromCurrency = codeToCurrency(this.props.fromCurrencyCode);
const amount = Math.abs(convertAmount(this.props.amount, toCurrency, fromCurrency));
const negative = this.props.amount < 0;
const country = currencyToCountry(toCurrency);
const integerAmount = Math.floor(amount);
const fractionAmount = amount.toFixed(toCurrency.decimals).substr(-toCurrency.decimals);
const className = 'currency-caribou amount' +
(this.props.raisedFraction ? ' raised-fraction' : '') +
(negative ? ' negative' : '');
return (
<span className={className}>
{this.props.symbol && !this.props.currencyOnly && negative ? <span className="minus">-</span> : null}
{this.props.symbol && !this.props.currencyOnly ? <span className="symbol">{toCurrency.symbol}</span> : null}
{!this.props.currencyOnly ? <span className="integer">{integerAmount}</span> : null}
{toCurrency.decimals && !this.props.excludeFraction && !this.props.currencyOnly ? (
<span className="fraction-portion">
<span className="point">.</span>
<span className="fraction">{fractionAmount}</span>
</span>
) : null}
{this.props.flag ? <i className={country.alpha2.toLowerCase() + ' flag'} /> : null }
{this.props.code ? <span className="code">{toCurrency.code}</span> : null }
</span>
)
}
}
CurrencyAmount.propTypes = {
raisedFraction: React.PropTypes.bool,
excludeFraction: React.PropTypes.bool,
flag: React.PropTypes.bool,
currencyOnly: React.PropTypes.bool,
code: React.PropTypes.bool,
symbol: React.PropTypes.bool,
amount: React.PropTypes.number,
toCurrencyCode: React.PropTypes.string,
fromCurrencyCode: React.PropTypes.string,
}
CurrencyAmount.defaultProps = {
excludeFraction: false,
symbol: true,
flag: false,
code: false,
raisedFraction: false,
toCurrencyCode: getLocalCurrency().code,
fromCurrencyCode: getDefaultCurrency().code,
}
export { CurrencyAmount, convertAmount, observeCurrency, localCountry, getLocalCurrency };