Skip to content

Commit

Permalink
Merge pull request #2919 from dlabrecq/3499-user-id
Browse files Browse the repository at this point in the history
Fetch user ID for Unleash
  • Loading branch information
dlabrecq committed Feb 14, 2023
2 parents 333f0ab + e5ab187 commit 390d79f
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions src/components/featureFlags/featureFlags.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<FeatureFlagsProps> = ({ 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;
Expand All @@ -47,16 +48,18 @@ const FeatureFlags: React.FC<FeatureFlagsProps> = ({ 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({
Expand All @@ -74,9 +77,11 @@ const FeatureFlags: React.FC<FeatureFlagsProps> = ({ children = null }) => {
);
});
};
if (userId && isMounted.current) {
fetchFlags();
}
fetchUser(userId => {
if (isMounted.current) {
fetchFlags(userId);
}
});
});

return <>{children}</>;
Expand Down

0 comments on commit 390d79f

Please sign in to comment.