From 5de447c6cf0cdf15a13a3f40adb534e7ec7c9a13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20=C4=86wirko?= Date: Sat, 4 Mar 2023 13:26:03 +0100 Subject: [PATCH] fix passing custom configuration --- CHANGELOG.md | 3 +++ package.json | 2 +- .../common-helpers/useConfigurationSync.tsx | 14 -------------- ...{useSyncNetwork.tsx => useNetworkSync.tsx} | 8 ++++---- src/index.tsx | 2 +- src/store/config.ts | 19 +++++++++++++++++++ 6 files changed, 28 insertions(+), 20 deletions(-) delete mode 100644 src/hooks/common-helpers/useConfigurationSync.tsx rename src/hooks/{useSyncNetwork.tsx => useNetworkSync.tsx} (91%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 88fd350..9455f61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +### [0.0.8](https://github.com/useElven/core/releases/tag/v0.0.8) (2023-03-04) +- bugfix for not passing the configuration setup in `useNetworkSync` + ### [0.0.7](https://github.com/useElven/core/releases/tag/v0.0.7) (2023-03-01) - export `TransactionArgs` type - dependencies update diff --git a/package.json b/package.json index 079cec9..944e8ec 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@useelven/core", - "version": "0.0.7", + "version": "0.0.8", "description": "Core hooks for MultiversX React DApps", "license": "MIT", "author": "Julian Ćwirko ", diff --git a/src/hooks/common-helpers/useConfigurationSync.tsx b/src/hooks/common-helpers/useConfigurationSync.tsx deleted file mode 100644 index d18376e..0000000 --- a/src/hooks/common-helpers/useConfigurationSync.tsx +++ /dev/null @@ -1,14 +0,0 @@ -/* eslint-disable react-hooks/exhaustive-deps */ -import { useEffect } from 'react'; -import { useConfig } from '../useConfig'; -import { NetworkType } from '../../types/network'; - -export const useConfigurationSync = (config?: NetworkType) => { - let configStateSnap = useConfig(); - - useEffect(() => { - if (config) { - configStateSnap = { ...configStateSnap, ...config }; - } - }, []); -}; diff --git a/src/hooks/useSyncNetwork.tsx b/src/hooks/useNetworkSync.tsx similarity index 91% rename from src/hooks/useSyncNetwork.tsx rename to src/hooks/useNetworkSync.tsx index d231cae..a7888f9 100644 --- a/src/hooks/useSyncNetwork.tsx +++ b/src/hooks/useNetworkSync.tsx @@ -6,17 +6,17 @@ import { useLocalStorageSync } from './common-helpers/useLocalStorageSync'; import { useApiProviderSync } from './common-helpers/useApiProviderSync'; import { useDappProvidersSync } from './common-helpers/useDappProvidersSync'; import { useAccountNetworkSync } from './common-helpers/useAccountNetworkSync'; -import { useConfigurationSync } from './common-helpers/useConfigurationSync'; +import { initConfigState } from '../store/config'; export const useNetworkSync = (config?: NetworkType) => { + // Sync main configuration + initConfigState(config); + const [accountDone, setAccountDone] = useState(false); const [loginInfoDone, setLoginInfoDone] = useState(false); const apiNetworkProviderRef = useRef(); - // Sync main configuration - useConfigurationSync(config); - // Sync data from local storage useLocalStorageSync(setAccountDone, setLoginInfoDone); diff --git a/src/index.tsx b/src/index.tsx index 68d0020..9cf97eb 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,5 +1,5 @@ // Hooks -export * from './hooks/useSyncNetwork'; +export * from './hooks/useNetworkSync'; export * from './hooks/useTransaction'; export * from './hooks/useLogout'; export * from './hooks/useLogin'; diff --git a/src/store/config.ts b/src/store/config.ts index 4feab29..9181576 100644 --- a/src/store/config.ts +++ b/src/store/config.ts @@ -15,6 +15,25 @@ export const setConfigState = ( configState[key] = value; }; +// When there is a custom configuration replace keys in defaults +// When there is only a change in the chainType replace the rest with defaults but by the chainType +export const initConfigState = (config: NetworkType | undefined) => { + const customConfig = { ...config }; + + Object.keys(initialState).forEach((key) => { + if (customConfig[key]) { + setConfigState(key, customConfig[key] as string | string[]); + } else if (customConfig.chainType) { + setConfigState( + key, + networkConfig[customConfig.chainType][key] as string | string[] + ); + } else { + setConfigState(key, initialState[key] as string | string[]); + } + }); +}; + export const clearConfigState = () => { const resetObj = cloneDeep(initialState); Object.keys(resetObj).forEach((key) => {