From 2487843f092cd7031256a0eaff4480fe84a0801d Mon Sep 17 00:00:00 2001 From: Caleb Alldrin Date: Mon, 14 Oct 2024 16:38:28 -0700 Subject: [PATCH 1/3] Add redirect --- .../settings/preferences.page.test.tsx | 41 ++++++++++++++++++- .../settings/preferences.page.tsx | 20 +++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/pages/accountLists/[accountListId]/settings/preferences.page.test.tsx b/pages/accountLists/[accountListId]/settings/preferences.page.test.tsx index cc888ef1b..4e9aae650 100644 --- a/pages/accountLists/[accountListId]/settings/preferences.page.test.tsx +++ b/pages/accountLists/[accountListId]/settings/preferences.page.test.tsx @@ -1,7 +1,9 @@ +import { NextRouter } from 'next/router'; import React from 'react'; import { ThemeProvider } from '@mui/material/styles'; import { render, waitFor } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; +import { getSession } from 'next-auth/react'; import TestRouter from '__tests__/util/TestRouter'; import { GqlMockedProvider } from '__tests__/util/graphqlMocking'; import { GetUserOptionsQuery } from 'src/components/Contacts/ContactFlow/GetUserOptions.generated'; @@ -25,7 +27,7 @@ const mockEnqueue = jest.fn(); const mutationSpy = jest.fn(); const push = jest.fn(); -const router = { +const defaultRouter = { query: { accountListId }, pathname: '/accountLists/[accountListId]/settings/preferences', isReady: true, @@ -48,6 +50,7 @@ interface MocksProvidersProps { canUserExportData: boolean; singleOrg?: boolean; setup?: string; + router?: Partial | undefined; } const MocksProviders: React.FC = ({ @@ -55,6 +58,7 @@ const MocksProviders: React.FC = ({ canUserExportData, singleOrg, setup, + router = defaultRouter, }) => ( @@ -213,6 +217,41 @@ describe('Preferences page', () => { expect(queryByText('Primary Organization')).not.toBeInTheDocument(), ); }); + describe('Export Redirect', () => { + const apiToken = 'apiToken'; + beforeEach(() => { + (getSession as jest.Mock).mockResolvedValue({ + apiToken, + }); + }); + it('redirects when exportId is provided in the URL', () => { + const router = { + isReady: true, + query: { + exportId: 'export_id', + accountListId, + }, + }; + + const {} = render( + + + , + ); + + expect(window.location.replace).toHaveBeenCalledWith( + `${ + process.env.REST_API_URL + }/account_lists/${accountListId}/exports/${encodeURIComponent( + 'export_id', + )}.xml?access_token=${apiToken}`, + ); + }); + }); describe('Setup Tour', () => { it('should not show setup banner and accordions should not be disabled', async () => { diff --git a/pages/accountLists/[accountListId]/settings/preferences.page.tsx b/pages/accountLists/[accountListId]/settings/preferences.page.tsx index 555430342..cd34bda17 100644 --- a/pages/accountLists/[accountListId]/settings/preferences.page.tsx +++ b/pages/accountLists/[accountListId]/settings/preferences.page.tsx @@ -32,6 +32,7 @@ import { AccordionGroup } from 'src/components/Shared/Forms/Accordions/Accordion import { StickyBox } from 'src/components/Shared/Header/styledComponents'; import { useAccountListId } from 'src/hooks/useAccountListId'; import { useGetTimezones } from 'src/hooks/useGetTimezones'; +import { useRequiredSession } from 'src/hooks/useRequiredSession'; import { getCountries } from 'src/lib/data/countries'; import { SettingsWrapper } from './Wrapper'; @@ -46,6 +47,7 @@ const Preferences: React.FC = () => { const { push, query } = useRouter(); const { enqueueSnackbar } = useSnackbar(); const { onSetupTour } = useSetupContext(); + const session = useRequiredSession(); const setupAccordions = ['locale', 'monthly goal', 'home country']; const [setup, setSetup] = useState(0); @@ -57,6 +59,24 @@ const Preferences: React.FC = () => { const [updateUserOptions] = useUpdateUserOptionsMutation(); + useEffect(() => { + const redirectToDownloadExportedData = (exportDataExportId: string) => { + if (!exportDataExportId) {return;} + + const url = `${ + process.env.REST_API_URL + }/account_lists/${accountListId}/exports/${encodeURIComponent( + exportDataExportId, + )}.xml?access_token=${session.apiToken}`; + + window.location.replace(url); + }; + + if (query.exportId && typeof query.exportId === 'string') { + redirectToDownloadExportedData(query.exportId); + } + }, [query.exportId, accountListId]); + const { data: personalPreferencesData, loading: personalPreferencesLoading } = useGetPersonalPreferencesQuery({ variables: { From 2ba0fe56acbc3ecc07d992dedda16d9a09d10dcc Mon Sep 17 00:00:00 2001 From: Caleb Alldrin Date: Tue, 15 Oct 2024 08:29:26 -0700 Subject: [PATCH 2/3] Remove mocking session --- .../[accountListId]/settings/preferences.page.test.tsx | 10 ++-------- .../[accountListId]/settings/preferences.page.tsx | 4 +++- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/pages/accountLists/[accountListId]/settings/preferences.page.test.tsx b/pages/accountLists/[accountListId]/settings/preferences.page.test.tsx index 4e9aae650..a772d3e84 100644 --- a/pages/accountLists/[accountListId]/settings/preferences.page.test.tsx +++ b/pages/accountLists/[accountListId]/settings/preferences.page.test.tsx @@ -3,7 +3,7 @@ import React from 'react'; import { ThemeProvider } from '@mui/material/styles'; import { render, waitFor } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; -import { getSession } from 'next-auth/react'; +import { session } from '__tests__/fixtures/session'; import TestRouter from '__tests__/util/TestRouter'; import { GqlMockedProvider } from '__tests__/util/graphqlMocking'; import { GetUserOptionsQuery } from 'src/components/Contacts/ContactFlow/GetUserOptions.generated'; @@ -218,12 +218,6 @@ describe('Preferences page', () => { ); }); describe('Export Redirect', () => { - const apiToken = 'apiToken'; - beforeEach(() => { - (getSession as jest.Mock).mockResolvedValue({ - apiToken, - }); - }); it('redirects when exportId is provided in the URL', () => { const router = { isReady: true, @@ -248,7 +242,7 @@ describe('Preferences page', () => { process.env.REST_API_URL }/account_lists/${accountListId}/exports/${encodeURIComponent( 'export_id', - )}.xml?access_token=${apiToken}`, + )}.xml?access_token=${session.user.apiToken}`, ); }); }); diff --git a/pages/accountLists/[accountListId]/settings/preferences.page.tsx b/pages/accountLists/[accountListId]/settings/preferences.page.tsx index cd34bda17..0a32d8ee4 100644 --- a/pages/accountLists/[accountListId]/settings/preferences.page.tsx +++ b/pages/accountLists/[accountListId]/settings/preferences.page.tsx @@ -61,7 +61,9 @@ const Preferences: React.FC = () => { useEffect(() => { const redirectToDownloadExportedData = (exportDataExportId: string) => { - if (!exportDataExportId) {return;} + if (!exportDataExportId) { + return; + } const url = `${ process.env.REST_API_URL From 69b6e27ba8441008f99bcc013e5f0836a06f8b9e Mon Sep 17 00:00:00 2001 From: Caleb Alldrin Date: Tue, 15 Oct 2024 08:54:45 -0700 Subject: [PATCH 3/3] removed unneeded check --- .../[accountListId]/settings/preferences.page.tsx | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pages/accountLists/[accountListId]/settings/preferences.page.tsx b/pages/accountLists/[accountListId]/settings/preferences.page.tsx index 0a32d8ee4..4e9a6c663 100644 --- a/pages/accountLists/[accountListId]/settings/preferences.page.tsx +++ b/pages/accountLists/[accountListId]/settings/preferences.page.tsx @@ -61,10 +61,6 @@ const Preferences: React.FC = () => { useEffect(() => { const redirectToDownloadExportedData = (exportDataExportId: string) => { - if (!exportDataExportId) { - return; - } - const url = `${ process.env.REST_API_URL }/account_lists/${accountListId}/exports/${encodeURIComponent(