-
Notifications
You must be signed in to change notification settings - Fork 295
/
CurrencyStore.js
123 lines (107 loc) · 3.72 KB
/
CurrencyStore.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
// @flow
import { action, observable, computed, runInAction } from 'mobx';
import Store from './lib/Store';
import {
CURRENCY_REQUEST_RATE_INTERVAL,
getLocalizedCurrency,
getLocalizedCurrenciesList,
getCurrencyFromCode,
} from '../config/currencyConfig';
import type { Currency, LocalizedCurrency } from '../types/currencyTypes.js';
export default class CurrencyStore extends Store {
@observable isFetchingList: boolean = false;
@observable isFetchingRate: boolean = false;
@observable isAvailable: boolean = false;
@observable isActive: boolean = false;
@observable list: Array<Currency> = [];
@observable selected: ?Currency = null;
@observable rate: ?number = null;
@observable lastFetched: ?Date = null;
_getCurrencyRateInterval: ?IntervalID = null;
setup() {
const { currency: currencyActions } = this.actions;
currencyActions.setCurrencySelected.listen(this._setCurrencySelected);
currencyActions.toggleCurrencyIsActive.listen(this._toggleCurrencyIsActive);
this._setupCurrency();
}
// PUBLIC
@computed get localizedCurrencyList(): Array<LocalizedCurrency> {
const { list, stores } = this;
const { currentLocale } = stores.profile;
return getLocalizedCurrenciesList(list, currentLocale);
}
@computed get localizedCurrency(): ?LocalizedCurrency {
const { selected, stores } = this;
const { currentLocale } = stores.profile;
if (!selected) return null;
return getLocalizedCurrency(selected, currentLocale);
}
@action getCurrencyList = async () => {
this.isFetchingList = true;
const list = await this.api.ada.getCurrencyList();
runInAction(() => {
this.list = list;
this.isFetchingList = false;
});
};
@action getCurrencyRate = async () => {
const { localizedCurrency } = this;
if (localizedCurrency && localizedCurrency.code) {
try {
this.isFetchingRate = true;
const rate = await this.api.ada.getCurrencyRate(localizedCurrency);
runInAction(() => {
this.isFetchingRate = false;
this.lastFetched = new Date();
if (rate) {
this.rate = rate;
this.isAvailable = true;
} else {
throw new Error('Error fetching the Currency rate');
}
});
} catch (error) {
runInAction(() => {
this.rate = null;
this.isAvailable = false;
});
clearInterval(this._getCurrencyRateInterval);
}
}
};
// PRIVATE
@action _setupCurrency = async () => {
// Check if the user has enabled currencies
// Otherwise applies the default config
const isActive = await this.api.localStorage.getCurrencyIsActive();
// Check if the user has already selected a currency
// Otherwise applies the default currency
const localCurrencyCode = await this.api.localStorage.getCurrencySelected();
const selected = getCurrencyFromCode(localCurrencyCode);
runInAction(() => {
this.isActive = isActive;
this.selected = selected;
});
clearInterval(this._getCurrencyRateInterval);
this._getCurrencyRateInterval = setInterval(
this.getCurrencyRate,
CURRENCY_REQUEST_RATE_INTERVAL
);
// Fetch the currency list and rate
this.getCurrencyList();
this.getCurrencyRate();
};
@action _setCurrencySelected = async ({ code }: { code: string }) => {
const { list } = this;
const selected = list.find((item) => item.code === code);
if (selected) {
this.selected = selected;
this.getCurrencyRate();
await this.api.localStorage.setCurrencySelected(selected.code);
}
};
@action _toggleCurrencyIsActive = () => {
this.isActive = !this.isActive;
this.api.localStorage.setCurrencyIsActive(this.isActive);
};
}