Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/chore/ddw-602-improve-pin-entry-…
…component-ux' into chore/ddw-602-improve-pin-entry-component-ux
- Loading branch information
Showing
9 changed files
with
182 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// @flow | ||
import Action from './lib/Action'; | ||
|
||
// ======= CURRENCY ACTIONS ======= | ||
|
||
export default class CurrencyActions { | ||
setCurrencySelected: Action<{ code: string }> = new Action(); | ||
toggleCurrencyIsActive: Action<any> = new Action(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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); | ||
}; | ||
} |
Oops, something went wrong.