Skip to content

Commit

Permalink
Asap 358 b (#4207)
Browse files Browse the repository at this point in the history
* make analytics accessible to staff only

* linting

* linting
  • Loading branch information
lctrt authored Mar 18, 2024
1 parent 1009b46 commit 4671c29
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 17 deletions.
5 changes: 3 additions & 2 deletions apps/crn-frontend/src/AuthenticatedApp.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isEnabled } from '@asap-hub/flags';
import { SkeletonHeaderFrame as Frame } from '@asap-hub/frontend-utils';
import { Layout, Loading, NotFoundPage } from '@asap-hub/react-components';
import { useAuth0CRN, useCurrentUserCRN } from '@asap-hub/react-context';
Expand Down Expand Up @@ -76,6 +75,7 @@ const AuthenticatedApp: FC<Record<string, never>> = () => {

const user = useCurrentUserCRN();
const tabRoute = useCurrentUserProfileTabRoute();
const canViewAnalytics = user?.role === 'Staff';
if (!user || !recoilAuth0) {
return <Loading />;
}
Expand All @@ -86,6 +86,7 @@ const AuthenticatedApp: FC<Record<string, never>> = () => {
<Layout
userOnboarded={user.onboarded}
onboardable={onboardable}
canViewAnalytics={canViewAnalytics}
onboardModalHref={
tabRoute ? tabRoute({}).editOnboarded({}).$ : undefined
}
Expand Down Expand Up @@ -141,7 +142,7 @@ const AuthenticatedApp: FC<Record<string, never>> = () => {
<About />
</Frame>
</Route>
{isEnabled('ANALYTICS') && (
{canViewAnalytics && (
<Route path={analytics.template}>
<Frame title="Analytics">
<Analytics />
Expand Down
2 changes: 0 additions & 2 deletions packages/flags/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
export type Flag =
| 'PERSISTENT_EXAMPLE'
| 'VERSION_RESEARCH_OUTPUT'
| 'ANALYTICS'
| 'DISPLAY_EVENTS';

export type Flags = Partial<Record<Flag, boolean | undefined>>;
let overrides: Flags = {
// flags already live in prod:
// can also be used to manually disable a flag in development:
ANALYTICS: false,
DISPLAY_EVENTS: false,
};

Expand Down
9 changes: 6 additions & 3 deletions packages/react-components/src/organisms/MainNavigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
events,
analytics,
} from '@asap-hub/routing';
import { isEnabled } from '@asap-hub/flags';

import {
perRem,
Expand Down Expand Up @@ -46,9 +45,13 @@ const listStyles = css({

export interface MainNavigationProps {
readonly userOnboarded: boolean;
readonly canViewAnalytics?: boolean;
}

const MainNavigation: React.FC<MainNavigationProps> = ({ userOnboarded }) => (
const MainNavigation: React.FC<MainNavigationProps> = ({
userOnboarded,
canViewAnalytics = false,
}) => (
<nav>
<ul css={listStyles}>
<li>
Expand Down Expand Up @@ -105,7 +108,7 @@ const MainNavigation: React.FC<MainNavigationProps> = ({ userOnboarded }) => (
About ASAP
</NavigationLink>
</li>
{isEnabled('ANALYTICS') && (
{canViewAnalytics && (
<li>
<NavigationLink
href={analytics({}).$}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,10 @@ import { StaticRouter } from 'react-router-dom';
import { render } from '@testing-library/react';
import { network } from '@asap-hub/routing';
import { findParentWithStyle } from '@asap-hub/dom-test-utils';
import { renderHook } from '@testing-library/react-hooks';
import { useFlags } from '@asap-hub/react-context';

import MainNavigation from '../MainNavigation';

it('renders the navigation items', () => {
const {
result: { current },
} = renderHook(useFlags);

current.enable('ANALYTICS');

const { getAllByRole } = render(<MainNavigation userOnboarded={true} />);

expect(
Expand All @@ -28,10 +20,16 @@ it('renders the navigation items', () => {
expect.stringMatching(/news/i),
expect.stringMatching(/guides/i),
expect.stringMatching(/about/i),
expect.stringMatching(/analytics/i),
]);
});

it('renders the analytics menu item when allowed', () => {
const { getByTitle } = render(
<MainNavigation userOnboarded={true} canViewAnalytics={true} />,
);
expect(getByTitle(/analytics/i)).toBeInTheDocument();
});

describe('a navigation item', () => {
it('is highlighted when it links to the current page', () => {
const { getByTitle, rerender } = render(
Expand Down
6 changes: 5 additions & 1 deletion packages/react-components/src/templates/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ const Layout: FC<LayoutProps> = ({
children,
onboardable,
onboardModalHref,
canViewAnalytics,
...userNavProps
}) => {
const [menuShown, setMenuShown] = useState(false);
Expand Down Expand Up @@ -278,7 +279,10 @@ const Layout: FC<LayoutProps> = ({
<div css={[menuStyles, menuShown && menuMenuShownStyles]}>
<div css={[mainMenuStyles]}>
<Suspense fallback={<LoadingMenu />}>
<MainNavigation userOnboarded={userNavProps.userOnboarded} />
<MainNavigation
userOnboarded={userNavProps.userOnboarded}
canViewAnalytics={canViewAnalytics}
/>
</Suspense>
</div>
<div css={[userMenuStyles, menuShown && userMenuShownStyles]}>
Expand Down

0 comments on commit 4671c29

Please sign in to comment.