From 4434287c330ea3e9aa5b0b6739f12a528d34ddc2 Mon Sep 17 00:00:00 2001 From: jejolare Date: Sun, 23 Apr 2023 05:12:59 +0400 Subject: [PATCH] add fetchBackground method --- src/app/App.js | 57 +++++++++++++++++------------------------- src/app/utils/utils.js | 13 ++++++++++ src/content/content.js | 2 +- 3 files changed, 37 insertions(+), 35 deletions(-) create mode 100644 src/app/utils/utils.js diff --git a/src/app/App.js b/src/app/App.js index 0d9c73c..c43dd3d 100644 --- a/src/app/App.js +++ b/src/app/App.js @@ -6,6 +6,7 @@ import Header from "./components/Header/Header"; import TokensTabs from "./components/TokensTabs/TokensTabs"; import Loader from "./components/UI/Loader/Loader"; import Wallet from "./components/Wallet/Wallet"; +import { fetchBackground } from "./utils/utils"; import { updateWalletConnected, updateWalletData, @@ -22,44 +23,32 @@ function App() { useEffect(() => { const checkConnection = async () => { // eslint-disable-next-line no-undef - if (chrome.runtime.sendMessage) { - // eslint-disable-next-line no-undef - chrome.runtime.sendMessage( - { message: "GET_WALLET_BALANCE" }, - (response) => { - if (response.data) { - updateWalletConnected(dispatch, true); - } else { - updateWalletConnected(dispatch, false); - } - } - ); - } + if (!chrome?.runtime?.sendMessage) return; + + const balanceData = await fetchBackground({ method: 'GET_WALLET_BALANCE' }); + updateWalletConnected(dispatch, !!balanceData.data); }; const getWalletData = async () => { // eslint-disable-next-line no-undef - if (chrome.runtime.sendMessage) { - // eslint-disable-next-line no-undef - chrome.runtime.sendMessage( - { message: "GET_WALLET_DATA" }, - (response) => { - if (response.data) { - const { address, alias, balance, transactions, assets } = - response.data; - updateWalletData(dispatch, { - address, - alias, - balance, - assets, - transactions, - }); - console.log("wallet data updated"); - updateLoading(dispatch, false); - } - } - ); - } + if (!chrome?.runtime?.sendMessage) return; + const walletData = await fetchBackground({ method: 'GET_WALLET_DATA' }); + + if (!walletData.data) return; + + const { address, alias, balance, transactions, assets } = walletData.data; + + updateWalletData(dispatch, { + address, + alias, + balance, + assets, + transactions, + }); + + console.log("wallet data updated"); + updateLoading(dispatch, false); + }; const intervalId = setInterval(async () => { diff --git a/src/app/utils/utils.js b/src/app/utils/utils.js new file mode 100644 index 0000000..99c5425 --- /dev/null +++ b/src/app/utils/utils.js @@ -0,0 +1,13 @@ +export async function fetchBackground(data) { + return new Promise((resolve, reject) => { + try { + // eslint-disable-next-line no-undef + chrome.runtime.sendMessage(data, function (response) { + resolve(response); + }); + } catch (error) { + console.error(`Error while fetching data (${data.method}):`, error); + reject(error); + } + }); +}; \ No newline at end of file diff --git a/src/content/content.js b/src/content/content.js index 5faf39e..2d52fc1 100644 --- a/src/content/content.js +++ b/src/content/content.js @@ -5,7 +5,7 @@ async function fetchData(data) { resolve(response); }); } catch (error) { - console.error(`Error while fetching data (${method}):`, error); + console.error(`Error while fetching data (${data.method}):`, error); reject(error); } });