From 853b01e2cadff12668f6671623972a566940ab4d Mon Sep 17 00:00:00 2001 From: MaysWind Date: Sun, 2 Apr 2023 18:06:36 +0800 Subject: [PATCH] show message when force update exchange rates data and the data is up to date --- src/lib/utils.js | 52 ++++++++++++++++++++++++++++++++++++++ src/locales/en.js | 1 + src/locales/zh_Hans.js | 1 + src/store/exchangeRates.js | 7 +++++ 4 files changed, 61 insertions(+) diff --git a/src/lib/utils.js b/src/lib/utils.js index 45cc1052..da5e7714 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -36,6 +36,57 @@ function isBoolean(val) { return typeof(val) === 'boolean'; } +function isEquals(obj1, obj2) { + if (obj1 === obj2) { + return true; + } + + if (isArray(obj1) && isArray(obj2)) { + if (obj1.length !== obj2.length) { + return false; + } + + for (let i = 0; i < obj1.length; i++) { + if (!isEquals(obj1[i], obj2[i])) { + return false; + } + } + + return true; + } else if (isObject(obj1) && isObject(obj2)) { + const keys1 = Object.keys(obj1); + const keys2 = Object.keys(obj2); + + if (keys1.length !== keys2.length) { + return false; + } + + const keyExistsMap2 = {}; + + for (let i = 0; i < keys2.length; i++) { + const key = keys2[i]; + + keyExistsMap2[key] = true; + } + + for (let i = 0; i < keys1.length; i++) { + const key = keys1[i]; + + if (!keyExistsMap2[key]) { + return false; + } + + if (!isEquals(obj1[key], obj2[key])) { + return false; + } + } + + return true; + } else { + return obj1 === obj2; + } +} + function getUtcOffsetMinutesByUtcOffset(utcOffset) { if (!utcOffset) { return 0; @@ -677,6 +728,7 @@ export default { isString, isNumber, isBoolean, + isEquals, getUtcOffsetMinutesByUtcOffset, getUtcOffsetByUtcOffsetMinutes, getTimezoneOffset, diff --git a/src/locales/en.js b/src/locales/en.js index 3031f6c6..6d8934e3 100644 --- a/src/locales/en.js +++ b/src/locales/en.js @@ -1001,6 +1001,7 @@ export default { 'No exchange rates data': 'No exchange rates data', 'There is no exchange rates data for your default currency': 'There is no exchange rates data for your default currency', 'Exchange rates data has been updated': 'Exchange rates data has been updated', + 'Exchange rates data is up to date': 'Exchange rates data is up to date', 'Unable to get exchange rates data': 'Unable to get exchange rates data', 'About': 'About', 'Build Time': 'Build Time', diff --git a/src/locales/zh_Hans.js b/src/locales/zh_Hans.js index e5d7fa51..a471bcc6 100644 --- a/src/locales/zh_Hans.js +++ b/src/locales/zh_Hans.js @@ -1001,6 +1001,7 @@ export default { 'No exchange rates data': '没有汇率数据', 'There is no exchange rates data for your default currency': '没有您默认货币的汇率数据', 'Exchange rates data has been updated': '汇率数据已更新', + 'Exchange rates data is up to date': '汇率数据已是最新', 'Unable to get exchange rates data': '无法获取汇率数据', 'About': '关于', 'Build Time': '编译时间', diff --git a/src/store/exchangeRates.js b/src/store/exchangeRates.js index 4d06332c..b538c758 100644 --- a/src/store/exchangeRates.js +++ b/src/store/exchangeRates.js @@ -35,6 +35,13 @@ export function getLatestExchangeRates(context, { silent, force }) { return; } + const currentData = getExchangeRatesFromLocalStorage(); + + if (currentData && currentData.data && utils.isEquals(currentData.data, data.result)) { + reject({ message: 'Exchange rates data is up to date' }); + return; + } + context.commit(STORE_LATEST_EXCHANGE_RATES, { time: now, data: data.result