From 4cb4a945e83229f3807c9775c871a75c4be37be0 Mon Sep 17 00:00:00 2001 From: Thisyahlen Date: Tue, 23 Apr 2024 14:50:54 +0800 Subject: [PATCH 1/2] chore: fix isAuthorized flag --- src/context/auth-context.tsx | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/context/auth-context.tsx b/src/context/auth-context.tsx index e2ce7b1..78c9e4b 100644 --- a/src/context/auth-context.tsx +++ b/src/context/auth-context.tsx @@ -25,6 +25,12 @@ export const AuthDataProvider = ({ children }: AuthDataProviderProps) => { const { data, mutate, isSuccess, isPending } = useAuthorize(); + const tradingAccounts: Record = JSON.parse(Cookies.get('tradingAccounts') ?? '{}'); + const isAuthorized = useMemo( + () => !!activeLoginid || !!Object.keys(tradingAccounts).length, + [activeLoginid, tradingAccounts] + ); + const authorizeAccount = useCallback((token?: string) => { if (token) mutate({ authorize: token }); }, []); @@ -86,18 +92,18 @@ export const AuthDataProvider = ({ children }: AuthDataProviderProps) => { const logout = useCallback(() => { Cookies.remove('authToken'); - Cookies.remove('accountsList'); + Cookies.remove('tradingAccounts'); setActiveLoginid(''); }, []); const value = useMemo( () => ({ activeLoginid, - isAuthorized: !!activeLoginid && isSuccess, isAuthorizing: isPending, switchAccount, appendAccountCookie, logout, + isAuthorized, }), [activeLoginid, isSuccess] ); From 05af867f31fe9ba1fdc442341d889a714b909604 Mon Sep 17 00:00:00 2001 From: Thisyahlen Date: Tue, 23 Apr 2024 15:23:15 +0800 Subject: [PATCH 2/2] chore: remove multiple variables and rename --- src/context/auth-context.tsx | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/context/auth-context.tsx b/src/context/auth-context.tsx index 78c9e4b..04dce7b 100644 --- a/src/context/auth-context.tsx +++ b/src/context/auth-context.tsx @@ -25,10 +25,11 @@ export const AuthDataProvider = ({ children }: AuthDataProviderProps) => { const { data, mutate, isSuccess, isPending } = useAuthorize(); - const tradingAccounts: Record = JSON.parse(Cookies.get('tradingAccounts') ?? '{}'); + const accountsList: Record = JSON.parse(Cookies.get('accountsList') ?? '{}'); + const isAuthorized = useMemo( - () => !!activeLoginid || !!Object.keys(tradingAccounts).length, - [activeLoginid, tradingAccounts] + () => !!activeLoginid || !!Object.keys(accountsList).length, + [activeLoginid, accountsList] ); const authorizeAccount = useCallback((token?: string) => { @@ -37,9 +38,7 @@ export const AuthDataProvider = ({ children }: AuthDataProviderProps) => { const switchAccount = useCallback( (loginId: string) => { - const tradingAccounts: Record = JSON.parse(Cookies.get('tradingAccounts') ?? '{}'); - - const token = tradingAccounts[loginId]; + const token = accountsList[loginId]; if (!token) return; authorizeAccount(token); @@ -61,13 +60,13 @@ export const AuthDataProvider = ({ children }: AuthDataProviderProps) => { if (!defaultActiveAccount) return; setActiveLoginid(loginInfo[0].loginid); - const tradingAccounts: Record = {}; + const accountsList: Record = {}; loginInfo.forEach(account => { - tradingAccounts[account.loginid] = account.token; + accountsList[account.loginid] = account.token; }); - Cookies.set('tradingAccounts', JSON.stringify(tradingAccounts)); + Cookies.set('accountsList', JSON.stringify(accountsList)); URLUtils.filterSearchParams(paramsToDelete); authorizeAccount(loginInfo[0].token); @@ -81,18 +80,16 @@ export const AuthDataProvider = ({ children }: AuthDataProviderProps) => { }, []); const appendAccountCookie = useCallback((loginid: string, token: string) => { - const tradingAccounts: Record = JSON.parse(Cookies.get('tradingAccounts') ?? '{}'); - - tradingAccounts[loginid] = token; + accountsList[loginid] = token; - Cookies.set('tradingAccounts', JSON.stringify(tradingAccounts)); + Cookies.set('accountsList', JSON.stringify(accountsList)); switchAccount(loginid); }, []); const logout = useCallback(() => { Cookies.remove('authToken'); - Cookies.remove('tradingAccounts'); + Cookies.remove('accountsList'); setActiveLoginid(''); }, []);