From e5ab18747711e087cd0c6f39f4cf1a84d21ab872 Mon Sep 17 00:00:00 2001 From: Dan Labrecque Date: Tue, 14 Feb 2023 12:31:21 -0500 Subject: [PATCH] Fetch user ID for Unleash https://issues.redhat.com/browse/COST-3499 --- src/components/featureFlags/featureFlags.tsx | 39 +++++++++++--------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/src/components/featureFlags/featureFlags.tsx b/src/components/featureFlags/featureFlags.tsx index eab9bae82..968a91a26 100644 --- a/src/components/featureFlags/featureFlags.tsx +++ b/src/components/featureFlags/featureFlags.tsx @@ -23,20 +23,21 @@ export const enum FeatureToggle { ros = 'cost-management.ui.ros', // ROS support https://issues.redhat.com/browse/COST-3477 } -let userId; -const insights = (window as any).insights; -if (insights && insights.chrome && insights.chrome.auth && insights.chrome.auth.getUser) { - insights.chrome.auth.getUser().then(user => { - userId = user.identity.account_number; - }); -} - // The FeatureFlags component saves feature flags in store for places where Unleash hooks not available const FeatureFlags: React.FC = ({ children = null }) => { const updateContext = useUnleashContext(); const client = useUnleashClient(); const dispatch = useDispatch(); + const fetchUser = callback => { + const insights = (window as any).insights; + if (insights && insights.chrome && insights.chrome.auth && insights.chrome.auth.getUser) { + insights.chrome.auth.getUser().then(user => { + callback(user.identity.account_number); + }); + } + }; + const isMounted = useRef(false); useLayoutEffect(() => { isMounted.current = true; @@ -47,16 +48,18 @@ const FeatureFlags: React.FC = ({ children = null }) => { // Update everytime or flags may be false useLayoutEffect(() => { - if (userId && isMounted.current) { - updateContext({ - userId, - }); - } + fetchUser(userId => { + if (isMounted.current) { + updateContext({ + userId, + }); + } + }); }); useLayoutEffect(() => { // Wait for the new flags to pull in from the different context - const fetchFlags = async () => { + const fetchFlags = async userId => { await updateContext({ userId }).then(() => { dispatch( featureFlagsActions.setFeatureFlags({ @@ -74,9 +77,11 @@ const FeatureFlags: React.FC = ({ children = null }) => { ); }); }; - if (userId && isMounted.current) { - fetchFlags(); - } + fetchUser(userId => { + if (isMounted.current) { + fetchFlags(userId); + } + }); }); return <>{children};