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: {