code refactor

This commit is contained in:
MaysWind
2021-01-05 22:55:34 +08:00
parent fbfba764d4
commit abae1c85e7
16 changed files with 172 additions and 122 deletions
-54
View File
@@ -1,54 +0,0 @@
import utils from './utils.js';
const exchangeRatesLocalStorageKey = 'lab_app_exchange_rates';
function getExchangeRates() {
const storageData = localStorage.getItem(exchangeRatesLocalStorageKey) || '{}';
return JSON.parse(storageData);
}
function setExchangeRates(value) {
const storageData = JSON.stringify(value);
localStorage.setItem(exchangeRatesLocalStorageKey, storageData);
}
function clearExchangeRates() {
localStorage.removeItem(exchangeRatesLocalStorageKey);
}
function getExchangeRate(fromCurrency, toCurrency) {
const exchangeRates = getExchangeRates().exchangeRates;
const exchangeRateMap = {};
for (let i = 0; i < exchangeRates.length; i++) {
const exchangeRate = exchangeRates[i];
exchangeRateMap[exchangeRate.currency] = exchangeRate;
}
const fromCurrencyExchangeRate = exchangeRateMap[fromCurrency];
const toCurrencyExchangeRate = exchangeRateMap[toCurrency];
if (!fromCurrencyExchangeRate || !toCurrencyExchangeRate) {
return null;
}
return parseFloat(toCurrencyExchangeRate.rate) / parseFloat(fromCurrencyExchangeRate.rate);
}
function getOtherCurrencyAmount(amount, fromCurrency, toCurrency) {
const exchangeRate = getExchangeRate(fromCurrency, toCurrency);
if (!utils.isNumber(exchangeRate)) {
return null;
}
return amount * exchangeRate;
}
export default {
getExchangeRates,
setExchangeRates,
clearExchangeRates,
getExchangeRate,
getOtherCurrencyAmount,
};
+3 -21
View File
@@ -1,7 +1,6 @@
import axios from 'axios';
import moment from 'moment';
import userState from "./userstate.js";
import exchangeRates from "./exchangeRates.js";
const baseUrlPath = '/api';
@@ -330,26 +329,9 @@ export default {
id
});
},
getLatestExchangeRates: () => {
return axios.get('v1/exchange_rates/latest.json');
},
autoRefreshLatestExchangeRates: () => {
const currentExchangeRateData = exchangeRates.getExchangeRates();
if (currentExchangeRateData && currentExchangeRateData.date === moment().format('YYYY-MM-DD')) {
return;
}
getLatestExchangeRates: ({ ignoreError }) => {
return axios.get('v1/exchange_rates/latest.json', {
ignoreError: true
}).then(response => {
const data = response.data;
if (data && data.success && data.result && data.result.exchangeRates) {
exchangeRates.setExchangeRates(data.result);
}
return data.result;
ignoreError: !!ignoreError
});
},
};