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

MPDX-8380 Data Export Redirect #1136

Merged
merged 3 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -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 { 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';
Expand All @@ -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,
Expand All @@ -48,13 +50,15 @@ interface MocksProvidersProps {
canUserExportData: boolean;
singleOrg?: boolean;
setup?: string;
router?: Partial<NextRouter> | undefined;
}

const MocksProviders: React.FC<MocksProvidersProps> = ({
children,
canUserExportData,
singleOrg,
setup,
router = defaultRouter,
}) => (
<ThemeProvider theme={theme}>
<TestRouter router={router}>
Expand Down Expand Up @@ -213,6 +217,35 @@ describe('Preferences page', () => {
expect(queryByText('Primary Organization')).not.toBeInTheDocument(),
);
});
describe('Export Redirect', () => {
it('redirects when exportId is provided in the URL', () => {
const router = {
isReady: true,
query: {
exportId: 'export_id',
accountListId,
},
};

const {} = render(
<MocksProviders
canUserExportData={false}
singleOrg={true}
router={router}
>
<Preferences />
</MocksProviders>,
);

expect(window.location.replace).toHaveBeenCalledWith(
`${
process.env.REST_API_URL
}/account_lists/${accountListId}/exports/${encodeURIComponent(
'export_id',
)}.xml?access_token=${session.user.apiToken}`,
);
});
});

describe('Setup Tour', () => {
it('should not show setup banner and accordions should not be disabled', async () => {
Expand Down
22 changes: 22 additions & 0 deletions pages/accountLists/[accountListId]/settings/preferences.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
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';

Expand All @@ -46,6 +47,7 @@
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);
Expand All @@ -57,6 +59,26 @@

const [updateUserOptions] = useUpdateUserOptionsMutation();

useEffect(() => {
const redirectToDownloadExportedData = (exportDataExportId: string) => {
if (!exportDataExportId) {
caleballdrin marked this conversation as resolved.
Show resolved Hide resolved
return;

Check warning on line 65 in pages/accountLists/[accountListId]/settings/preferences.page.tsx

View check run for this annotation

Codecov / codecov/patch

pages/accountLists/[accountListId]/settings/preferences.page.tsx#L65

Added line #L65 was not covered by tests
}

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: {
Expand Down
Loading