mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-16 07:57:33 +08:00
code refactor
This commit is contained in:
@@ -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
@@ -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
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user