Skip to content

Commit

Permalink
Make each step have a unique url. This allows us to refresh the page …
Browse files Browse the repository at this point in the history
…on any step and not have to go back to the beginning.
  • Loading branch information
wrandall22 committed Jun 26, 2024
1 parent 2a7f542 commit a03bf48
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 27 deletions.
52 changes: 48 additions & 4 deletions pages/accountLists/[accountListId]/tools/csv.page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Head from 'next/head';
import React, { useState } from 'react';
import { useRouter } from 'next/router';
import React, { useEffect, useState } from 'react';

Check warning on line 3 in pages/accountLists/[accountListId]/tools/csv.page.tsx

View check run for this annotation

Codecov / codecov/patch

pages/accountLists/[accountListId]/tools/csv.page.tsx#L1-L3

Added lines #L1 - L3 were not covered by tests
import { Box, Divider, Grid, Typography } from '@mui/material';
import { styled } from '@mui/material/styles';
import { useTranslation } from 'react-i18next';
Expand Down Expand Up @@ -60,7 +61,34 @@ const CsvHome: React.FC = () => {
const { t } = useTranslation();
const accountListId = useAccountListId();
const { classes } = useStyles();
const [currentTab, setCurrentTab] = useState(CsvImportViewStepEnum.Upload);
const { query, replace, pathname, isReady } = useRouter();
const urlTab = query?.tab as string;
const urlCsvFileId = query?.id as string;
const [currentTab, setCurrentTab] = useState(

Check warning on line 67 in pages/accountLists/[accountListId]/tools/csv.page.tsx

View check run for this annotation

Codecov / codecov/patch

pages/accountLists/[accountListId]/tools/csv.page.tsx#L59-L67

Added lines #L59 - L67 were not covered by tests
urlTab || CsvImportViewStepEnum.Upload,
);
const defaultPageHeader = t('Tools - Import - CSV');
const [pageTitle, setPageTitle] = useState(defaultPageHeader);

Check warning on line 71 in pages/accountLists/[accountListId]/tools/csv.page.tsx

View check run for this annotation

Codecov / codecov/patch

pages/accountLists/[accountListId]/tools/csv.page.tsx#L70-L71

Added lines #L70 - L71 were not covered by tests
const [csvFileId, setCsvFileId] = useState(urlCsvFileId ?? '');

useEffect(() => {

Check warning on line 74 in pages/accountLists/[accountListId]/tools/csv.page.tsx

View check run for this annotation

Codecov / codecov/patch

pages/accountLists/[accountListId]/tools/csv.page.tsx#L74

Added line #L74 was not covered by tests
if (!isReady) {
return;

Check warning on line 76 in pages/accountLists/[accountListId]/tools/csv.page.tsx

View check run for this annotation

Codecov / codecov/patch

pages/accountLists/[accountListId]/tools/csv.page.tsx#L76

Added line #L76 was not covered by tests
}

replace({

Check warning on line 79 in pages/accountLists/[accountListId]/tools/csv.page.tsx

View check run for this annotation

Codecov / codecov/patch

pages/accountLists/[accountListId]/tools/csv.page.tsx#L79

Added line #L79 was not covered by tests
pathname,
query: {
accountListId,
tab: currentTab,
...(csvFileId ? { id: csvFileId } : undefined),
},
});
}, [currentTab, csvFileId, isReady]);

useEffect(() => {
setPageTitle(getPageTitle());

Check warning on line 90 in pages/accountLists/[accountListId]/tools/csv.page.tsx

View check run for this annotation

Codecov / codecov/patch

pages/accountLists/[accountListId]/tools/csv.page.tsx#L89-L90

Added lines #L89 - L90 were not covered by tests
}, [currentTab, pageTitle]);

const renderTab = (accountListId: string) => {

Check warning on line 93 in pages/accountLists/[accountListId]/tools/csv.page.tsx

View check run for this annotation

Codecov / codecov/patch

pages/accountLists/[accountListId]/tools/csv.page.tsx#L93

Added line #L93 was not covered by tests
switch (currentTab) {
Expand All @@ -69,6 +97,7 @@ const CsvHome: React.FC = () => {
<CsvUpload
accountListId={accountListId}
setCurrentTab={setCurrentTab}
setCsvFileId={setCsvFileId}
/>
);
case CsvImportViewStepEnum.Headers:
Expand Down Expand Up @@ -97,11 +126,26 @@ const CsvHome: React.FC = () => {
}
};

const getPageTitle = (): string => {

Check warning on line 129 in pages/accountLists/[accountListId]/tools/csv.page.tsx

View check run for this annotation

Codecov / codecov/patch

pages/accountLists/[accountListId]/tools/csv.page.tsx#L129

Added line #L129 was not covered by tests
switch (currentTab) {
case CsvImportViewStepEnum.Upload:
return 'Tools - Import - CSV - Upload';
case CsvImportViewStepEnum.Headers:
return 'Tools - Import - CSV - Headers';
case CsvImportViewStepEnum.Values:
return 'Tools - Import - CSV - Values';
case CsvImportViewStepEnum.Preview:
return 'Tools - Import - CSV - Preview';
default:
return 'Tools - Import - CSV';

Check warning on line 140 in pages/accountLists/[accountListId]/tools/csv.page.tsx

View check run for this annotation

Codecov / codecov/patch

pages/accountLists/[accountListId]/tools/csv.page.tsx#L131-L140

Added lines #L131 - L140 were not covered by tests
}
};

return (

Check warning on line 144 in pages/accountLists/[accountListId]/tools/csv.page.tsx

View check run for this annotation

Codecov / codecov/patch

pages/accountLists/[accountListId]/tools/csv.page.tsx#L144

Added line #L144 was not covered by tests
<CsvImportProvider>
<CsvImportProvider csvFileId={csvFileId}>
<Head>
<title>
{appName} | {t('Tools - Import - CSV - Upload')}
{appName} | {t(pageTitle)}
</title>
</Head>
{accountListId ? (
Expand Down
9 changes: 8 additions & 1 deletion src/components/Tool/Import/Csv/CsvHeaders.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ const CsvHeadersMockComponent: React.FC<CsvHeadersProps> = ({
<TestWrapper>
<ThemeProvider theme={theme}>
<CsvImportContext.Provider
value={{ uploadData, setUploadData, initialData, setInitialData }}
value={{
uploadData,
setUploadData,
initialData,
setInitialData,
csvFileId: 'csvFileId',
}}
>
<CsvHeaders
accountListId={accountListId}
Expand All @@ -59,6 +65,7 @@ beforeEach(() => {
describe('CsvHeaders', () => {
beforeEach(() => {
uploadData = {
id: 'csvFileId',
fileHeadersMappings: {},
} as CsvImportType;
initialData = {} as CsvImportType;
Expand Down
17 changes: 11 additions & 6 deletions src/components/Tool/Import/Csv/CsvHeaders.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
TableRow,
Typography,
} from '@mui/material';
import { cloneDeep } from 'lodash/fp';
import { useTranslation } from 'react-i18next';
import { useApiConstants } from 'src/components/Constants/UseApiConstants';
import useGetAppSettings from 'src/hooks/useGetAppSettings';
Expand Down Expand Up @@ -68,9 +69,8 @@ const CsvHeaders: React.FC<CsvHeadersProps> = ({
accountListId,
setCurrentTab,
}) => {
const { uploadData, setUploadData, initialData, setInitialData } = useContext(
CsvImportContext,
) as CsvImportValue;
const { uploadData, setUploadData, initialData, setInitialData, csvFileId } =
useContext(CsvImportContext) as CsvImportValue;

const importHeaders = uploadData?.fileHeaders ?? {};
const supportedHeaders = useSupportedHeaders();
Expand Down Expand Up @@ -100,7 +100,7 @@ const CsvHeaders: React.FC<CsvHeadersProps> = ({
};

useEffect(() => {
if (uploadData) {
if (uploadData?.id) {
if (!initialData?.id) {
get(accountListId, uploadData.id, initialData).then((data) => {
setInitialData(data);
Expand Down Expand Up @@ -144,8 +144,13 @@ const CsvHeaders: React.FC<CsvHeadersProps> = ({
setUnmappedHeadersChecked(true);
updateHeaders(uploadData);
}
} else if (csvFileId) {
get(accountListId, csvFileId, initialData).then((data) => {
setInitialData(data);
setUploadData(cloneDeep(data));
});
}
}, [uploadData]);
}, [uploadData, csvFileId]);

const handleUpdateHeaders = (event, importHeader) => {
fileHeadersMappings[importHeader] = event.target.value;
Expand Down Expand Up @@ -186,7 +191,7 @@ const CsvHeaders: React.FC<CsvHeadersProps> = ({
});
};

if (!accountListId || !uploadData || !unmappedHeadersChecked) {
if (!accountListId || !uploadData?.id || !unmappedHeadersChecked) {
return null;
}

Expand Down
11 changes: 10 additions & 1 deletion src/components/Tool/Import/Csv/CsvImportContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export interface CsvImportValue {
setUploadData: React.Dispatch<React.SetStateAction<CsvImportType | null>>;
initialData: CsvImportType | null;
setInitialData: React.Dispatch<React.SetStateAction<CsvImportType | null>>;
csvFileId: string | null;
}

export const CsvImportContext = React.createContext<CsvImportValue | null>(
Expand All @@ -58,10 +59,12 @@ export const CsvImportContext = React.createContext<CsvImportValue | null>(

interface CsvImportProviderProps {
children: React.ReactNode;
csvFileId: string;
}

export const CsvImportProvider: React.FC<CsvImportProviderProps> = ({
children,
csvFileId,
}) => {
const [uploadData, setUploadData] = useState<CsvImportType | null>(

Check warning on line 69 in src/components/Tool/Import/Csv/CsvImportContext.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/Tool/Import/Csv/CsvImportContext.tsx#L68-L69

Added lines #L68 - L69 were not covered by tests
{} as CsvImportType,
Expand All @@ -73,7 +76,13 @@ export const CsvImportProvider: React.FC<CsvImportProviderProps> = ({

return (

Check warning on line 77 in src/components/Tool/Import/Csv/CsvImportContext.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/Tool/Import/Csv/CsvImportContext.tsx#L77

Added line #L77 was not covered by tests
<CsvImportContext.Provider
value={{ uploadData, setUploadData, initialData, setInitialData }}
value={{
uploadData,
setUploadData,
initialData,
setInitialData,
csvFileId,
}}
>
{children}
</CsvImportContext.Provider>
Expand Down
9 changes: 8 additions & 1 deletion src/components/Tool/Import/Csv/CsvPreview.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const constants = {

const initializeData = () => {
uploadData = {
id: 'csvFileId',
fileConstants: {
weird: 'Odd Value',
foo: 'bar',
Expand Down Expand Up @@ -117,7 +118,13 @@ const CsvPreviewMockComponent: React.FC<CsvPreviewProps> = ({
<TestWrapper>
<ThemeProvider theme={theme}>
<CsvImportContext.Provider
value={{ uploadData, setUploadData, initialData, setInitialData }}
value={{
uploadData,
setUploadData,
initialData,
setInitialData,
csvFileId: uploadData?.csvFileId,
}}
>
<CsvPreview
accountListId={accountListId}
Expand Down
21 changes: 15 additions & 6 deletions src/components/Tool/Import/Csv/CsvPreview.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { ReactElement, useContext, useState } from 'react';
import React, { ReactElement, useContext, useEffect, useState } from 'react';
import {
Autocomplete,
Box,
Expand All @@ -11,6 +11,7 @@ import {
Typography,
} from '@mui/material';
import { styled } from '@mui/material/styles';
import { cloneDeep } from 'lodash/fp';
import { useTranslation } from 'react-i18next';
import { useApiConstants } from 'src/components/Constants/UseApiConstants';
import { useGetContactTagListQuery } from 'src/components/Contacts/ContactDetails/ContactDetailsTab/Tags/ContactTags.generated';
Expand All @@ -24,7 +25,7 @@ import {
} from './CsvImportContext';
import { CsvImportSuccessModal } from './CsvImportSuccessModal';
import { HeaderBox } from './HeaderBox';
import { save } from './csvImportService';
import { get, save } from './csvImportService';
import { useSupportedHeaders } from './uploadCsvFile';

const StripedTableBody = styled(TableBody)(() => ({
Expand All @@ -51,9 +52,8 @@ const CsvPreview: React.FC<CsvPreviewProps> = ({
accountListId,
setCurrentTab,
}) => {
const { uploadData, setUploadData, initialData, setInitialData } = useContext(
CsvImportContext,
) as CsvImportValue;
const { uploadData, setUploadData, initialData, setInitialData, csvFileId } =
useContext(CsvImportContext) as CsvImportValue;

const { t } = useTranslation();
const { appName } = useGetAppSettings();
Expand All @@ -69,6 +69,15 @@ const CsvPreview: React.FC<CsvPreviewProps> = ({
const [accept, setAccept] = useState(false);
const [showSuccessModal, setShowSuccessModal] = useState(false);

useEffect(() => {
if (!uploadData?.id && csvFileId) {
get(accountListId, csvFileId, initialData).then((data) => {
setInitialData(data);
setUploadData(cloneDeep(data));

Check warning on line 76 in src/components/Tool/Import/Csv/CsvPreview.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/Tool/Import/Csv/CsvPreview.tsx#L74-L76

Added lines #L74 - L76 were not covered by tests
});
}
}, [csvFileId, uploadData]);

const handleBack = () => {
setCurrentTab(
Object.keys(uploadData?.valuesToConstantsMappings ?? {}).length === 0
Expand Down Expand Up @@ -97,7 +106,7 @@ const CsvPreview: React.FC<CsvPreviewProps> = ({
});
};

if (!uploadData) {
if (!uploadData?.id) {
return null;
}

Expand Down
12 changes: 11 additions & 1 deletion src/components/Tool/Import/Csv/CsvUpload.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ beforeEach(() => {

describe('CsvUpload', () => {
const setCurrentTab = jest.fn();
const setCsvFileId = jest.fn();

it('should show the max file size', () => {
const { getByTestId } = render(
<TestWrapper>
<CsvUpload
accountListId="wee"
setCurrentTab={setCurrentTab}
setCsvFileId={setCsvFileId}
></CsvUpload>
</TestWrapper>,
);
Expand All @@ -71,6 +73,7 @@ describe('CsvUpload', () => {
<CsvUpload
accountListId="wee"
setCurrentTab={setCurrentTab}
setCsvFileId={setCsvFileId}
></CsvUpload>
</TestWrapper>,
);
Expand Down Expand Up @@ -111,11 +114,18 @@ describe('CsvUpload', () => {
const { getByTestId } = render(
<TestWrapper>
<CsvImportContext.Provider
value={{ uploadData, setUploadData, initialData, setInitialData }}
value={{
uploadData,
setUploadData,
initialData,
setInitialData,
csvFileId: '',
}}
>
<CsvUpload
accountListId="wee"
setCurrentTab={setCurrentTab}
setCsvFileId={setCsvFileId}
></CsvUpload>
</CsvImportContext.Provider>
</TestWrapper>,
Expand Down
3 changes: 3 additions & 0 deletions src/components/Tool/Import/Csv/CsvUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import { getMaxFileSize, uploadFile } from './uploadCsvFile';
interface CsvUploadProps {
accountListId: string;
setCurrentTab: (currentTab: CsvImportViewStepEnum) => void;
setCsvFileId: (csvFileId: string) => void;
}

const CsvUpload: React.FC<CsvUploadProps> = ({
accountListId,
setCurrentTab,
setCsvFileId,
}) => {
const { t } = useTranslation();
const fileRef = useRef<HTMLInputElement>(null);
Expand Down Expand Up @@ -62,6 +64,7 @@ const CsvUpload: React.FC<CsvUploadProps> = ({
if (setUploadData) {
setUploadData(transformedData);
}
setCsvFileId(transformedData.id);
}
});
setCurrentTab(CsvImportViewStepEnum.Headers);
Expand Down
9 changes: 8 additions & 1 deletion src/components/Tool/Import/Csv/CsvValues.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const constants = {

const initializeData = () => {
uploadData = {
id: 'csvFileId',
fileConstants: {
weird: 'Odd Value',
foo: 'bar',
Expand Down Expand Up @@ -111,7 +112,13 @@ const CsvValuesMockComponent: React.FC<CsvValuesProps> = ({
<TestWrapper>
<ThemeProvider theme={theme}>
<CsvImportContext.Provider
value={{ uploadData, setUploadData, initialData, setInitialData }}
value={{
uploadData,
setUploadData,
initialData,
setInitialData,
csvFileId: uploadData?.csvFileId,
}}
>
<CsvValues
accountListId={accountListId}
Expand Down
Loading

0 comments on commit a03bf48

Please sign in to comment.