Skip to content

Commit

Permalink
refactor: Extract util
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Anton committed May 9, 2024
1 parent 6aded3a commit c51c649
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
18 changes: 3 additions & 15 deletions libs/shared-web/src/providers/LaunchDarklyProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IOrganizationEntity, IUserEntity } from '@novu/shared';
import { IOrganizationEntity } from '@novu/shared';
import { asyncWithLDProvider } from 'launchdarkly-react-client-sdk';
import { PropsWithChildren, ReactNode, useEffect, useMemo, useRef, useState } from 'react';
import { selectHasUserCompletedSignUp } from '../utils/auth-selectors';
import { LAUNCH_DARKLY_CLIENT_SIDE_ID } from '../config';
import { useFeatureFlags } from '../hooks';
import { checkShouldUseLaunchDarkly } from '../utils';
Expand Down Expand Up @@ -118,24 +119,11 @@ function checkShouldInitializeLaunchDarkly(userCtx: UserContext): { shouldWaitFo
// }

// allow LD to load when the user is created but still in onboarding
const isUserFullyRegistered = checkIsUserFullyRegistered(userCtx);
const isUserFullyRegistered = selectHasUserCompletedSignUp(userCtx);
if (!isUserFullyRegistered) {
return { shouldWaitForLd: true };
}

// if a user is fully on-boarded, but no organization has loaded, we must wait for the organization to initialize the client.
return { shouldWaitForLd: !!currentOrganization, doesNeedOrg: true };
}

/**
* Determine if a user is fully-registered; if not, they're still in onboarding.
*/
function checkIsUserFullyRegistered(userCtx: UserContext): boolean {
/*
* Determine if the user has completed registration based on if they have an associated orgId.
* Use jobTitle as a back-up
*/
const isUserFullyRegistered = !!userCtx.jwtPayload?.organizationId || !!userCtx.currentUser?.jobTitle;

return isUserFullyRegistered;
}
1 change: 1 addition & 0 deletions libs/shared-web/src/utils/auth-selectors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './selectHasUserCompletedSignUp';
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { UserContext } from '../../providers';

/**
* Determine if a user is fully-registered; if not, they're still in onboarding.
*/
export const selectHasUserCompletedSignUp = (userCtx: UserContext): boolean => {
if (!userCtx) {
return false;
}

// User has completed registration if they have an associated orgId.
return !!userCtx.jwtPayload?.organizationId;
};

0 comments on commit c51c649

Please sign in to comment.