From 2e201d58fbfddaa9f624bae9c3988e515886ad42 Mon Sep 17 00:00:00 2001 From: Edmundas Ramanauskas Date: Mon, 11 Nov 2024 14:25:55 +0200 Subject: [PATCH] Fetch node state only when user is authenticated --- src/mobx/Indications.store.ts | 5 ++++- src/mobx/store.ts | 1 - src/pages/StateInitializer.tsx | 8 ++++++++ src/redux/listeners.ts | 13 ++++++++++++- 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/mobx/Indications.store.ts b/src/mobx/Indications.store.ts index 56ce91a6..174dd9e4 100644 --- a/src/mobx/Indications.store.ts +++ b/src/mobx/Indications.store.ts @@ -17,6 +17,7 @@ export class IndicationsStore { status: string = 'pending' quality: number = 0 loading: boolean = true + interval: ReturnType | undefined = undefined constructor() { makeObservable(this, { @@ -32,7 +33,9 @@ export class IndicationsStore { } setupReactions(): void { - setInterval(() => this.fetchState(), 60 * 1000) + if (!this.interval) { + this.interval = setInterval(() => this.fetchState(), 60 * 1000) + } this.fetchState() } diff --git a/src/mobx/store.ts b/src/mobx/store.ts index 4c70275d..a6ec33c0 100644 --- a/src/mobx/store.ts +++ b/src/mobx/store.ts @@ -22,7 +22,6 @@ export class MobXRootStore { constructor() { this.sessionsStore = new SessionsStore() this.indicationsStore = new IndicationsStore() - this.indicationsStore.setupReactions() this.beneficiaryStore = new BeneficiaryStore() this.clickBoardingStore = new ClickBoardingStore() this.menuStore = new MenuStore() diff --git a/src/pages/StateInitializer.tsx b/src/pages/StateInitializer.tsx index 095fc95b..aec18b97 100644 --- a/src/pages/StateInitializer.tsx +++ b/src/pages/StateInitializer.tsx @@ -14,6 +14,7 @@ import { selectors } from '../redux/selectors' import { tequila } from '../api/tequila' import ConnectToSSE from '../sse/server-sent-events' import listeners from '../redux/listeners' +import { useStores } from '../mobx/store' import { IdentityRegistrationStatusListener } from './Authenticated/Components/Listeners/IdentityRegistrationStatusListener' interface Props { @@ -21,6 +22,7 @@ interface Props { } export const StateInitializer = ({ children }: Props) => { + const { indicationsStore } = useStores() const dispatch = useAppDispatch() const actions = { updateLoadingStore: async (loading: boolean) => dispatch(updateLoadingStore(loading)), @@ -56,6 +58,12 @@ export const StateInitializer = ({ children }: Props) => { }, [loggedIn]) useEffect(() => { + listeners.registerAuthenticatedListener(({ payload }) => { + if (!payload.authenticated) { + return + } + indicationsStore.setupReactions() + }) listeners.registerMinimumRegistrationAmountListener() }, []) diff --git a/src/redux/listeners.ts b/src/redux/listeners.ts index a034e5fc..342ab7b3 100644 --- a/src/redux/listeners.ts +++ b/src/redux/listeners.ts @@ -8,7 +8,7 @@ import { myst } from '../commons/mysts' import { addListener, PayloadAction } from '@reduxjs/toolkit' import { FeesResponse } from 'mysterium-vpn-js' import { store } from './store' -import { updateFeesStore, updateMinimumRegistrationAmountWeiStore } from './app.slice' +import { updateAuthenticatedStore, updateFeesStore, updateMinimumRegistrationAmountWeiStore } from './app.slice' const FEE_SPIKE_MULTIPLIER = 1.5 @@ -26,7 +26,18 @@ const registerMinimumRegistrationAmountListener = () => }), ) +const registerAuthenticatedListener = ( + callback: (action: PayloadAction<{ authenticated: boolean }>) => Promise | void, +) => + store.dispatch( + addListener({ + actionCreator: updateAuthenticatedStore, + effect: callback, + }), + ) + const listeners = { + registerAuthenticatedListener, registerMinimumRegistrationAmountListener, }