migrate exchange rates store to composition API and typescript

This commit is contained in:
MaysWind
2025-01-05 23:15:45 +08:00
parent 5b241d2547
commit ad1eec7d47
28 changed files with 182 additions and 163 deletions
+1 -1
View File
@@ -24,7 +24,7 @@ import { useRootStore } from '@/stores/index.js';
import { useSettingsStore } from '@/stores/setting.ts';
import { useUserStore } from '@/stores/user.ts';
import { useTokensStore } from '@/stores/token.ts';
import { useExchangeRatesStore } from '@/stores/exchangeRates.js';
import { useExchangeRatesStore } from '@/stores/exchangeRates.ts';
import { APPLICATION_LOGO_PATH } from '@/consts/asset.ts';
import { ThemeType } from '@/core/theme.ts';
+1 -1
View File
@@ -13,7 +13,7 @@ import { useRootStore } from '@/stores/index.js';
import { useSettingsStore } from '@/stores/setting.ts';
import { useUserStore } from '@/stores/user.ts';
import { useTokensStore } from '@/stores/token.ts';
import { useExchangeRatesStore } from '@/stores/exchangeRates.js';
import { useExchangeRatesStore } from '@/stores/exchangeRates.ts';
import { APPLICATION_LOGO_PATH } from '@/consts/asset.ts';
import { ThemeType } from '@/core/theme.ts';
+2 -2
View File
@@ -276,7 +276,7 @@ export function getAdaptiveDisplayAmountRate(amount1: number, amount2: number, f
}
}
export function getExchangedAmount(amount: number, fromRate: string, toRate: string): number | null {
export function getExchangedAmountByRate(amount: number, fromRate: string, toRate: string): number | null {
const exchangeRate = parseFloat(toRate) / parseFloat(fromRate);
if (!isNumber(exchangeRate)) {
@@ -295,5 +295,5 @@ export function getConvertedAmount(baseAmount: number | '', fromExchangeRate: {
return 0;
}
return getExchangedAmount(baseAmount as number, fromExchangeRate.rate, toExchangeRate.rate);
return getExchangedAmountByRate(baseAmount as number, fromExchangeRate.rate, toExchangeRate.rate);
}
+1 -1
View File
@@ -1,7 +1,7 @@
import { defineStore } from 'pinia';
import { useUserStore } from './user.ts';
import { useExchangeRatesStore } from './exchangeRates.js';
import { useExchangeRatesStore } from './exchangeRates.ts';
import { AccountType, AccountCategory } from '@/core/account.ts';
import { PARENT_ACCOUNT_CURRENCY_PLACEHOLDER } from '@/consts/currency.ts';
-136
View File
@@ -1,136 +0,0 @@
import { defineStore } from 'pinia';
import services from '@/lib/services.ts';
import logger from '@/lib/logger.ts';
import { isEquals } from '@/lib/common.ts';
import { getCurrentUnixTime, formatUnixTime } from '@/lib/datetime.ts';
import { getExchangedAmount } from '@/lib/numeral.ts';
const exchangeRatesLocalStorageKey = 'ebk_app_exchange_rates';
function getExchangeRatesFromLocalStorage() {
const storageData = localStorage.getItem(exchangeRatesLocalStorageKey) || '{}';
return JSON.parse(storageData);
}
function setExchangeRatesToLocalStorage(value) {
const storageData = JSON.stringify(value);
localStorage.setItem(exchangeRatesLocalStorageKey, storageData);
}
function clearExchangeRatesFromLocalStorage() {
localStorage.removeItem(exchangeRatesLocalStorageKey);
}
export const useExchangeRatesStore = defineStore('exchangeRates', {
state: () => ({
latestExchangeRates: getExchangeRatesFromLocalStorage()
}),
getters: {
exchangeRatesLastUpdateTime(state) {
const exchangeRates = state.latestExchangeRates || {};
return exchangeRates && exchangeRates.data ? exchangeRates.data.updateTime : null;
},
latestExchangeRateMap(state) {
const exchangeRateMap = {};
if (!state.latestExchangeRates || !state.latestExchangeRates.data || !state.latestExchangeRates.data.exchangeRates) {
return exchangeRateMap;
}
for (let i = 0; i < state.latestExchangeRates.data.exchangeRates.length; i++) {
const exchangeRate = state.latestExchangeRates.data.exchangeRates[i];
exchangeRateMap[exchangeRate.currency] = exchangeRate;
}
return exchangeRateMap;
}
},
actions: {
resetLatestExchangeRates() {
this.latestExchangeRates = {};
clearExchangeRatesFromLocalStorage();
},
getLatestExchangeRates({ silent, force }) {
const self = this;
const currentExchangeRateData = self.latestExchangeRates;
const now = getCurrentUnixTime();
if (!force) {
if (currentExchangeRateData && currentExchangeRateData.time && currentExchangeRateData.data &&
formatUnixTime(currentExchangeRateData.data.updateTime, 'YYYY-MM-DD') === formatUnixTime(now, 'YYYY-MM-DD')) {
return Promise.resolve(currentExchangeRateData.data);
}
if (currentExchangeRateData && currentExchangeRateData.time && currentExchangeRateData.data &&
formatUnixTime(currentExchangeRateData.time, 'YYYY-MM-DD HH') === formatUnixTime(now, 'YYYY-MM-DD HH')) {
return Promise.resolve(currentExchangeRateData.data);
}
}
return new Promise((resolve, reject) => {
services.getLatestExchangeRates({
ignoreError: silent
}).then(response => {
const data = response.data;
if (!data || !data.success || !data.result) {
reject({ message: 'Unable to retrieve exchange rates data' });
return;
}
const currentData = getExchangeRatesFromLocalStorage();
if (force && currentData && currentData.data && isEquals(currentData.data, data.result)) {
reject({ message: 'Exchange rates data is up to date' });
return;
}
this.latestExchangeRates = {
time: now,
data: data.result
};
setExchangeRatesToLocalStorage(this.latestExchangeRates);
resolve(data.result);
}).catch(error => {
logger.error('failed to retrieve latest exchange rates data', error);
if (error && error.processed) {
reject(error);
} else if (error.response && error.response.data && error.response.data.errorMessage) {
reject({ error: error.response.data });
} else {
reject({ message: 'Unable to retrieve exchange rates data' });
}
});
});
},
getExchangedAmount(amount, fromCurrency, toCurrency) {
if (amount === 0) {
return 0;
}
if (!this.latestExchangeRates || !this.latestExchangeRates.data || !this.latestExchangeRates.data.exchangeRates) {
return null;
}
const exchangeRates = this.latestExchangeRates.data.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 getExchangedAmount(amount, fromCurrencyExchangeRate.rate, toCurrencyExchangeRate.rate);
}
}
});
+155
View File
@@ -0,0 +1,155 @@
import { type Ref, ref, computed } from 'vue';
import { defineStore } from 'pinia';
import type {LatestExchangeRate, LatestExchangeRateResponse} from '@/models/exchange_rate.ts';
import { isEquals } from '@/lib/common.ts';
import { getCurrentUnixTime, formatUnixTime } from '@/lib/datetime.ts';
import { getExchangedAmountByRate } from '@/lib/numeral.ts';
import logger from '@/lib/logger.ts';
import services from '@/lib/services.ts';
const exchangeRatesLocalStorageKey = 'ebk_app_exchange_rates';
interface LatestExchangeRates {
time?: number;
data?: LatestExchangeRateResponse;
}
function getExchangeRatesFromLocalStorage(): LatestExchangeRates {
const storageData = localStorage.getItem(exchangeRatesLocalStorageKey) || '{}';
return JSON.parse(storageData) as LatestExchangeRates;
}
function setExchangeRatesToLocalStorage(value: LatestExchangeRates): void {
const storageData = JSON.stringify(value);
localStorage.setItem(exchangeRatesLocalStorageKey, storageData);
}
function clearExchangeRatesFromLocalStorage(): void {
localStorage.removeItem(exchangeRatesLocalStorageKey);
}
export const useExchangeRatesStore = defineStore('exchangeRates', () => {
const latestExchangeRates: Ref<LatestExchangeRates> = ref(getExchangeRatesFromLocalStorage());
const exchangeRatesLastUpdateTime = computed<number | null>(() => {
const exchangeRates = latestExchangeRates.value || {};
return exchangeRates && exchangeRates.data ? exchangeRates.data.updateTime : null;
});
const latestExchangeRateMap = computed<Record<string, LatestExchangeRate>>(() => {
const exchangeRateMap: Record<string, LatestExchangeRate> = {};
if (!latestExchangeRates.value || !latestExchangeRates.value.data || !latestExchangeRates.value.data.exchangeRates) {
return exchangeRateMap;
}
for (let i = 0; i < latestExchangeRates.value.data.exchangeRates.length; i++) {
const exchangeRate = latestExchangeRates.value.data.exchangeRates[i];
exchangeRateMap[exchangeRate.currency] = exchangeRate;
}
return exchangeRateMap;
});
function resetLatestExchangeRates(): void {
latestExchangeRates.value = {};
clearExchangeRatesFromLocalStorage();
}
function getLatestExchangeRates(req: { silent: boolean, force: boolean }): Promise<LatestExchangeRateResponse> {
const currentExchangeRateData = latestExchangeRates.value;
const now = getCurrentUnixTime();
if (!req.force) {
if (currentExchangeRateData && currentExchangeRateData.time && currentExchangeRateData.data &&
formatUnixTime(currentExchangeRateData.data.updateTime, 'YYYY-MM-DD') === formatUnixTime(now, 'YYYY-MM-DD')) {
return Promise.resolve(currentExchangeRateData.data);
}
if (currentExchangeRateData && currentExchangeRateData.time && currentExchangeRateData.data &&
formatUnixTime(currentExchangeRateData.time, 'YYYY-MM-DD HH') === formatUnixTime(now, 'YYYY-MM-DD HH')) {
return Promise.resolve(currentExchangeRateData.data);
}
}
return new Promise((resolve, reject) => {
services.getLatestExchangeRates({
ignoreError: req.silent
}).then(response => {
const data = response.data;
if (!data || !data.success || !data.result) {
reject({ message: 'Unable to retrieve exchange rates data' });
return;
}
const currentData = getExchangeRatesFromLocalStorage();
if (req.force && currentData && currentData.data && isEquals(currentData.data, data.result)) {
reject({ message: 'Exchange rates data is up to date' });
return;
}
latestExchangeRates.value = {
time: now,
data: data.result
};
setExchangeRatesToLocalStorage(latestExchangeRates.value);
resolve(data.result);
}).catch(error => {
logger.error('failed to retrieve latest exchange rates data', error);
if (error && error.processed) {
reject(error);
} else if (error.response && error.response.data && error.response.data.errorMessage) {
reject({ error: error.response.data });
} else {
reject({ message: 'Unable to retrieve exchange rates data' });
}
});
});
}
function getExchangedAmount(amount: number, fromCurrency: string, toCurrency: string): number | null {
if (amount === 0) {
return 0;
}
if (!latestExchangeRates.value || !latestExchangeRates.value.data || !latestExchangeRates.value.data.exchangeRates) {
return null;
}
const exchangeRates = latestExchangeRates.value.data.exchangeRates;
const exchangeRateMap: Record<string, LatestExchangeRate> = {};
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 getExchangedAmountByRate(amount, fromCurrencyExchangeRate.rate, toCurrencyExchangeRate.rate);
}
return {
// state
latestExchangeRates,
// computed state
exchangeRatesLastUpdateTime,
latestExchangeRateMap,
// functions
resetLatestExchangeRates,
getLatestExchangeRates,
getExchangedAmount
};
});
+1 -1
View File
@@ -9,7 +9,7 @@ import { useTransactionTemplatesStore } from './transactionTemplate.js';
import { useTransactionsStore } from './transaction.js';
import { useOverviewStore } from './overview.js';
import { useStatisticsStore } from './statistics.js';
import { useExchangeRatesStore } from './exchangeRates.js';
import { useExchangeRatesStore } from './exchangeRates.ts';
import {
hasUserAppLockState,
+1 -1
View File
@@ -2,7 +2,7 @@ import { defineStore } from 'pinia';
import { useSettingsStore } from '@/stores/setting.ts';
import { useUserStore } from './user.ts';
import { useExchangeRatesStore } from './exchangeRates.js';
import { useExchangeRatesStore } from './exchangeRates.ts';
import { isNumber, isEquals } from '@/lib/common.ts';
import {
+1 -1
View File
@@ -4,7 +4,7 @@ import { useSettingsStore } from './setting.ts';
import { useUserStore } from './user.ts';
import { useAccountsStore } from './account.js';
import { useTransactionCategoriesStore } from './transactionCategory.js';
import { useExchangeRatesStore } from './exchangeRates.js';
import { useExchangeRatesStore } from './exchangeRates.ts';
import { DateRangeScene, DateRange } from '@/core/datetime';
import { CategoryType } from '@/core/category.ts';
+1 -1
View File
@@ -6,7 +6,7 @@ import { useAccountsStore } from './account.js';
import { useTransactionCategoriesStore } from './transactionCategory.js';
import { useOverviewStore } from './overview.js';
import { useStatisticsStore } from './statistics.js';
import { useExchangeRatesStore } from './exchangeRates.js';
import { useExchangeRatesStore } from './exchangeRates.ts';
import { DateRange } from '@/core/datetime.ts';
import { CategoryType } from '@/core/category.ts';
+1 -1
View File
@@ -122,7 +122,7 @@
<script>
import { mapStores } from 'pinia';
import { useUserStore } from '@/stores/user.ts';
import { useExchangeRatesStore } from '@/stores/exchangeRates.js';
import { useExchangeRatesStore } from '@/stores/exchangeRates.ts';
import { getMapProvider } from '@/lib/server_settings.ts';
import { getMapWebsite } from '@/lib/map/index.ts';
+1 -1
View File
@@ -139,7 +139,7 @@ import { useDisplay } from 'vuetify';
import { mapStores } from 'pinia';
import { useSettingsStore } from '@/stores/setting.ts';
import { useUserStore } from '@/stores/user.ts';
import { useExchangeRatesStore } from '@/stores/exchangeRates.js';
import { useExchangeRatesStore } from '@/stores/exchangeRates.ts';
import logger from '@/lib/logger.ts';
import { getConvertedAmount } from '@/lib/numeral.ts';
+1 -1
View File
@@ -173,7 +173,7 @@ import { useTheme } from 'vuetify';
import { mapStores } from 'pinia';
import { useRootStore } from '@/stores/index.js';
import { useSettingsStore } from '@/stores/setting.ts';
import { useExchangeRatesStore } from '@/stores/exchangeRates.js';
import { useExchangeRatesStore } from '@/stores/exchangeRates.ts';
import { APPLICATION_LOGO_PATH } from '@/consts/asset.ts';
import { KnownErrorCode } from '@/consts/api.ts';
+1 -1
View File
@@ -242,7 +242,7 @@ import { useRootStore } from '@/stores/index.js';
import { useSettingsStore } from '@/stores/setting.ts';
import { useUserStore } from '@/stores/user.ts';
import { useTransactionCategoriesStore } from '@/stores/transactionCategory.js';
import { useExchangeRatesStore } from '@/stores/exchangeRates.js';
import { useExchangeRatesStore } from '@/stores/exchangeRates.ts';
import { APPLICATION_LOGO_PATH } from '@/consts/asset.ts';
import { CategoryType } from '@/core/category.ts';
+1 -1
View File
@@ -115,7 +115,7 @@ import { useSettingsStore } from '@/stores/setting.ts';
import { useUserStore } from '@/stores/user.ts';
import { useTokensStore } from '@/stores/token.ts';
import { useTransactionsStore } from '@/stores/transaction.js';
import { useExchangeRatesStore } from '@/stores/exchangeRates.js';
import { useExchangeRatesStore } from '@/stores/exchangeRates.ts';
import { APPLICATION_LOGO_PATH } from '@/consts/asset.ts';
import { ThemeType } from '@/core/theme.ts';
+1 -1
View File
@@ -263,7 +263,7 @@ import { mapStores } from 'pinia';
import { useSettingsStore } from '@/stores/setting.ts';
import { useUserStore } from '@/stores/user.ts';
import { useAccountsStore } from '@/stores/account.js';
import { useExchangeRatesStore } from '@/stores/exchangeRates.js';
import { useExchangeRatesStore } from '@/stores/exchangeRates.ts';
import { AccountType, AccountCategory } from '@/core/account.ts';
import { isObject } from '@/lib/common.ts';
@@ -212,7 +212,7 @@ import { useUserStore } from '@/stores/user.ts';
import { useTransactionsStore } from '@/stores/transaction.js';
import { useOverviewStore } from '@/stores/overview.js';
import { useStatisticsStore } from '@/stores/statistics.js';
import { useExchangeRatesStore } from '@/stores/exchangeRates.js';
import { useExchangeRatesStore } from '@/stores/exchangeRates.ts';
import { ThemeType } from '@/core/theme.ts';
import { getSystemTheme } from '@/lib/ui/common.ts';
@@ -177,7 +177,7 @@ import { useUserStore } from '@/stores/user.ts';
import { useAccountsStore } from '@/stores/account.js';
import { useTransactionCategoriesStore } from '@/stores/transactionCategory.js';
import { useTransactionTagsStore } from '@/stores/transactionTag.js';
import { useExchangeRatesStore } from '@/stores/exchangeRates.js';
import { useExchangeRatesStore } from '@/stores/exchangeRates.ts';
import { CategoryType } from '@/core/category.ts';
import {
@@ -398,7 +398,7 @@ import { useTransactionCategoriesStore } from '@/stores/transactionCategory.js';
import { useTransactionTagsStore } from '@/stores/transactionTag.js';
import { useTransactionsStore } from '@/stores/transaction.js';
import { useTransactionTemplatesStore } from '@/stores/transactionTemplate.js';
import { useExchangeRatesStore } from '@/stores/exchangeRates.js';
import { useExchangeRatesStore } from '@/stores/exchangeRates.ts';
import { CategoryType } from '@/core/category.ts';
import { TransactionType, TransactionEditScopeType } from '@/core/transaction.ts';
@@ -604,7 +604,7 @@ import { useTransactionTagsStore } from '@/stores/transactionTag.js';
import { useTransactionsStore } from '@/stores/transaction.js';
import { useOverviewStore } from '@/stores/overview.js';
import { useStatisticsStore } from '@/stores/statistics.js';
import { useExchangeRatesStore } from '@/stores/exchangeRates.js';
import { useExchangeRatesStore } from '@/stores/exchangeRates.ts';
import { CategoryType } from '@/core/category.ts';
import { TransactionType } from '@/core/transaction.ts';
+1 -1
View File
@@ -60,7 +60,7 @@
<script>
import { mapStores } from 'pinia';
import { useUserStore } from '@/stores/user.ts';
import { useExchangeRatesStore } from '@/stores/exchangeRates.js';
import { useExchangeRatesStore } from '@/stores/exchangeRates.ts';
import { getMapProvider } from '@/lib/server_settings.ts';
import { getMapWebsite } from '@/lib/map/index.ts';
+1 -1
View File
@@ -94,7 +94,7 @@
import { mapStores } from 'pinia';
import { useSettingsStore } from '@/stores/setting.ts';
import { useUserStore } from '@/stores/user.ts';
import { useExchangeRatesStore } from '@/stores/exchangeRates.js';
import { useExchangeRatesStore } from '@/stores/exchangeRates.ts';
import { TRANSACTION_MIN_AMOUNT, TRANSACTION_MAX_AMOUNT } from '@/consts/transaction.ts';
import { getConvertedAmount } from '@/lib/numeral.ts';
+1 -1
View File
@@ -185,7 +185,7 @@
import { mapStores } from 'pinia';
import { useRootStore } from '@/stores/index.js';
import { useSettingsStore } from '@/stores/setting.ts';
import { useExchangeRatesStore } from '@/stores/exchangeRates.js';
import { useExchangeRatesStore } from '@/stores/exchangeRates.ts';
import { APPLICATION_LOGO_PATH } from '@/consts/asset.ts';
import { KnownErrorCode } from '@/consts/api.ts';
+1 -1
View File
@@ -78,7 +78,7 @@ import { useUserStore } from '@/stores/user.ts';
import { useTransactionsStore } from '@/stores/transaction.js';
import { useOverviewStore } from '@/stores/overview.js';
import { useStatisticsStore } from '@/stores/statistics.js';
import { useExchangeRatesStore } from '@/stores/exchangeRates.js';
import { useExchangeRatesStore } from '@/stores/exchangeRates.ts';
import { getDesktopVersionPath } from '@/lib/version.ts';
import { isUserScheduledTransactionEnabled } from '@/lib/server_settings.ts';
+1 -1
View File
@@ -180,7 +180,7 @@ import { useRootStore } from '@/stores/index.js';
import { useSettingsStore } from '@/stores/setting.ts';
import { useUserStore } from '@/stores/user.ts';
import { useTransactionCategoriesStore } from '@/stores/transactionCategory.js';
import { useExchangeRatesStore } from '@/stores/exchangeRates.js';
import { useExchangeRatesStore } from '@/stores/exchangeRates.ts';
import { CategoryType } from '@/core/category.ts';
import { getNameByKeyValue, categorizedArrayToPlainArray } from '@/lib/common.ts';
+1 -1
View File
@@ -71,7 +71,7 @@ import { useSettingsStore } from '@/stores/setting.ts';
import { useUserStore } from '@/stores/user.ts';
import { useTokensStore } from '@/stores/token.ts';
import { useTransactionsStore } from '@/stores/transaction.js';
import { useExchangeRatesStore } from '@/stores/exchangeRates.js';
import { useExchangeRatesStore } from '@/stores/exchangeRates.ts';
import { APPLICATION_LOGO_PATH } from '@/consts/asset.ts';
import logger from '@/lib/logger.ts';
+1 -1
View File
@@ -178,7 +178,7 @@ import { mapStores } from 'pinia';
import { useSettingsStore } from '@/stores/setting.ts';
import { useUserStore } from '@/stores/user.ts';
import { useAccountsStore } from '@/stores/account.js';
import { useExchangeRatesStore } from '@/stores/exchangeRates.js';
import { useExchangeRatesStore } from '@/stores/exchangeRates.ts';
import { AccountType, AccountCategory } from '@/core/account.ts';
import { onSwipeoutDeleted } from '@/lib/ui/mobile.js';
+1 -1
View File
@@ -437,7 +437,7 @@ import { useTransactionCategoriesStore } from '@/stores/transactionCategory.js';
import { useTransactionTagsStore } from '@/stores/transactionTag.js';
import { useTransactionsStore } from '@/stores/transaction.js';
import { useTransactionTemplatesStore } from '@/stores/transactionTemplate.js';
import { useExchangeRatesStore } from '@/stores/exchangeRates.js';
import { useExchangeRatesStore } from '@/stores/exchangeRates.ts';
import { CategoryType } from '@/core/category.ts';
import { TransactionType, TransactionEditScopeType } from '@/core/transaction.ts';