Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix intl errors in browser console #595

Merged
merged 7 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion audit-ci.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"low": true,
"skip-dev": true,
"allowlist": [
"GHSA-xvch-5gv4-984h", // Not used at runtime
"GHSA-93q8-gq69-wqmw", // Not used at runtime
Expand Down Expand Up @@ -31,6 +32,7 @@
"GHSA-c2qf-rxjj-qqgw", // Not used at runtime
"GHSA-pxg6-pf52-xh8x|@lhci/cli>*", // Used by lighthouse CI, not at runtime
"GHSA-pxg6-pf52-xh8x|@lhci/utils>*", // Used by lighthouse CI, not at runtime
"GHSA-pxg6-pf52-xh8x|express>cookie" // Used by lighthouse CI, not at runtime
"GHSA-pxg6-pf52-xh8x|express>cookie", // Used by lighthouse CI, not at runtime
"GHSA-3xgq-45jj-v275" // cross-spawn is used by @next/eslint-plugin-next, audit-ci, cross-env, eslint, and jest. It is not used at runtime
]
}
25 changes: 0 additions & 25 deletions src/components/HOCs/withAuthGuard.tsx

This file was deleted.

18 changes: 0 additions & 18 deletions src/components/HOCs/withFailStates.spec.tsx

This file was deleted.

44 changes: 0 additions & 44 deletions src/components/HOCs/withFailStates.tsx

This file was deleted.

36 changes: 0 additions & 36 deletions src/components/HOCs/withIntl.tsx

This file was deleted.

4 changes: 2 additions & 2 deletions src/components/organisms/cardSlider/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ describe('Slider', () => {
},
});

expect(screen.getByText('1')).toBeInTheDocument();
expect(screen.getByText('2')).toBeInTheDocument();
expect(await screen.findByText('1')).toBeInTheDocument();
expect(await screen.findByText('2')).toBeInTheDocument();
});

it('displays one item per page on mobile', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,27 @@ import Cookies from 'js-cookie';
import { __loadRouter } from 'next/router';
import React, { ReactElement } from 'react';

import withAuthGuard from '~components/HOCs/withAuthGuard';
import { RegisterSocialDocument } from '~containers/account/__generated__/register';
import { fetchApi } from '~lib/api/fetchApi';
import { GetIsAuthenticatedDocument } from '~lib/hooks/__generated__/useIsAuthenticated';
import renderWithProviders from '~lib/test/renderWithProviders';
import AndAuthGuard from '~src/components/templates/andAuthGuard';

function render() {
const Comp = withAuthGuard(() => <>hello world</>);
const Comp = () => <>hello world</>;
return (async function (
ui: ReactElement,
renderOptions?: RenderOptions,
): Promise<RenderResult & { queryClient: QueryClient }> {
return renderWithProviders(ui, renderOptions);
})(<Comp />);
})(
<AndAuthGuard>
<Comp />
</AndAuthGuard>,
);
}

describe('withAuthGuard', () => {
describe('andAuthGuard', () => {
beforeEach(() => __loadRouter({ query: {} }));
it('displays login if no email', async () => {
when(fetchApi)
Expand Down
25 changes: 25 additions & 0 deletions src/components/templates/andAuthGuard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from 'react';

import Login from '~components/molecules/login';
import { getCurrentRequest } from '~lib/api/storeRequest';
import { getSessionToken } from '~lib/cookies';
import useIsAuthenticated from '~lib/hooks/useIsAuthenticated';

function AndAuthGuard({
children,
LoggedOutComponent = Login,
}: {
children: React.ReactNode;
LoggedOutComponent?: React.ComponentType;
}) {
const sessionToken = getSessionToken(getCurrentRequest());
const { isUserLoggedIn, isFetching } = useIsAuthenticated();

return (sessionToken && isFetching) || isUserLoggedIn ? (
children
) : (
<LoggedOutComponent />
);
}

export default AndAuthGuard;
20 changes: 20 additions & 0 deletions src/components/templates/andFailStates.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { render } from '@testing-library/react';
import { useRouter } from 'next/router';
import React from 'react';

import AndFailStates from '~src/components/templates/andFailStates';

jest.mock('next/router');

describe('AndFailStates HOC', () => {
it('supports no should404 prop', async () => {
jest.mocked(useRouter).mockReturnValue({ isFallback: false } as any);

render(
<AndFailStates
Component={() => <p>hello world</p>}
componentProps={{}}
/>,
);
});
});
45 changes: 45 additions & 0 deletions src/components/templates/andFailStates.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { useRouter } from 'next/router';
import React from 'react';

import LoadingCards from '~components/molecules/loadingCards';
import NotFoundBase from '~components/organisms/notFound';
import { Must } from '~src/types/types';

type WithFailStateOptions<P> = {
should404?: (props: P) => boolean;
isLoading?: (props: P) => boolean;
Loading?: (props: P) => JSX.Element;
NotFound?: (props: P) => JSX.Element;
};

const AndFailStates = <P,>({
Component,
componentProps,
options = {},
}: {
Component: React.FunctionComponent<Must<P>>;
componentProps: P;
options?: WithFailStateOptions<P>;
}): JSX.Element => {
const {
should404: _should404 = () => false,
isLoading: _isLoading = () => false,
Loading = LoadingCards,
NotFound = NotFoundBase,
} = options;

const { isFallback = false } = useRouter() || {};
const should404 = _should404(componentProps);
const isLoading = _isLoading(componentProps);

if (!isFallback && should404) {
return <NotFound {...(componentProps as Must<P>)} />;
}

if (isFallback || isLoading) {
return <Loading {...(componentProps as Must<P>)} />;
}

return <Component {...(componentProps as Must<P>)} />;
};
export default AndFailStates;
23 changes: 23 additions & 0 deletions src/components/templates/andIntl.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from 'react';
import { IntlProvider } from 'react-intl';

import handleIntlError from '~lib/handleIntlError';
import useLanguageRoute from '~lib/useLanguageRoute';
import useIntlMessages from '~src/lib/useIntlMessages';

function AndIntl({ children }: { children: React.ReactNode }) {
const language = useLanguageRoute();
const messages = useIntlMessages(language);
return (
<IntlProvider
messages={messages}
locale={language}
defaultLocale="en"
onError={handleIntlError}
>
{children}
</IntlProvider>
);
}

export default AndIntl;
5 changes: 4 additions & 1 deletion src/components/templates/andMiniplayer.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { waitFor } from '@testing-library/dom';
import React, { useContext, useEffect, useState } from 'react';

import AndMiniplayer from '~components/templates/andMiniplayer';
Expand Down Expand Up @@ -90,7 +91,9 @@ describe('miniplayer template', () => {
},
});

expect(mockPlayer.play).toBeCalled();
await waitFor(() => {
expect(mockPlayer.play).toBeCalled();
});
});

it('loads recording', async () => {
Expand Down
11 changes: 9 additions & 2 deletions src/containers/about/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React from 'react';

import Heading1 from '~components/atoms/heading1';
import withFailStates from '~components/HOCs/withFailStates';
import ContentWidthLimiter from '~components/molecules/contentWidthLimiter';
import AboutNav from '~components/organisms/aboutNav';
import AndFailStates from '~src/components/templates/andFailStates';
import { Must } from '~src/types/types';

import { GetAboutPageDataQuery } from './__generated__';
Expand All @@ -26,4 +26,11 @@ function About({ page: { body, title, slug } }: Must<AboutProps>): JSX.Element {
);
}

export default withFailStates(About, { useShould404: ({ page }) => !page });
const WithFailStates = (props: Parameters<typeof About>[0]) => (
<AndFailStates
Component={About}
componentProps={props}
options={{ should404: ({ page }) => !page }}
/>
);
export default WithFailStates;
26 changes: 14 additions & 12 deletions src/containers/account/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@ import { FormattedMessage } from 'react-intl';
import PreferencesForm from '~components/organisms/preferencesForm';
import ProfileForm from '~components/organisms/profileForm';
import Heading1 from '~src/components/atoms/heading1';
import withAuthGuard from '~src/components/HOCs/withAuthGuard';
import AccountNav from '~src/components/organisms/accountNav';
import AndAuthGuard from '~src/components/templates/andAuthGuard';

function Account() {
const [tab, setTab] = useState<'profile' | 'preferences'>('profile');
return (
<div>
<Heading1>
<FormattedMessage
id="account__heading"
defaultMessage="Account Settings"
/>
</Heading1>
<AccountNav current={tab} onClick={(tab) => setTab(tab)} />
<AndAuthGuard>
<div>
<Heading1>
<FormattedMessage
id="account__heading"
defaultMessage="Account Settings"
/>
</Heading1>
<AccountNav current={tab} onClick={(tab) => setTab(tab)} />

{tab === 'profile' ? <ProfileForm /> : <PreferencesForm />}
</div>
{tab === 'profile' ? <ProfileForm /> : <PreferencesForm />}
</div>
</AndAuthGuard>
);
}

export default withAuthGuard(Account);
export default Account;
12 changes: 10 additions & 2 deletions src/containers/account/login.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import withAuthGuard from '~components/HOCs/withAuthGuard';
import React from 'react';

import AndAuthGuard from '~src/components/templates/andAuthGuard';

import LoginRedirect from './loginRedirect';

export default withAuthGuard(LoginRedirect);
const Login = () => (
<AndAuthGuard>
<LoginRedirect />
</AndAuthGuard>
);

export default Login;
Loading
Loading