refactor changeExportDirectory

This commit is contained in:
Abhinav 2023-03-22 16:38:03 +05:30
parent 50b059eba3
commit f48df568e2

View file

@ -25,7 +25,6 @@ import DialogTitleWithCloseButton from './DialogBox/TitleWithCloseButton';
import MoreHoriz from '@mui/icons-material/MoreHoriz'; import MoreHoriz from '@mui/icons-material/MoreHoriz';
import OverflowMenu from './OverflowMenu/menu'; import OverflowMenu from './OverflowMenu/menu';
import { OverflowMenuOption } from './OverflowMenu/option'; import { OverflowMenuOption } from './OverflowMenu/option';
import { CustomError } from 'utils/error';
import { AppContext } from 'pages/_app'; import { AppContext } from 'pages/_app';
import { getExportDirectoryDoesNotExistMessage } from 'utils/ui'; import { getExportDirectoryDoesNotExistMessage } from 'utils/ui';
import { t } from 'i18next'; import { t } from 'i18next';
@ -142,9 +141,6 @@ export default function ExportModal(props: Props) {
const preExportRun = async () => { const preExportRun = async () => {
const exportFolder = getData(LS_KEYS.EXPORT)?.folder; const exportFolder = getData(LS_KEYS.EXPORT)?.folder;
if (!exportFolder) {
await selectExportDirectory();
}
const exportFolderExists = exportService.exists(exportFolder); const exportFolderExists = exportService.exists(exportFolder);
if (!exportFolderExists) { if (!exportFolderExists) {
appContext.setDialogMessage( appContext.setDialogMessage(
@ -161,15 +157,6 @@ export default function ExportModal(props: Props) {
await syncExportStatsWithRecord(); await syncExportStatsWithRecord();
}; };
const selectExportDirectory = async () => {
const newFolder = await exportService.selectExportDirectory();
if (newFolder) {
updateExportFolder(newFolder);
} else {
throw Error(CustomError.REQUEST_CANCELLED);
}
};
const syncExportStatsWithRecord = async () => { const syncExportStatsWithRecord = async () => {
const exportRecord = await exportService.getExportRecord(); const exportRecord = await exportService.getExportRecord();
const failed = exportRecord?.failedFiles?.length ?? 0; const failed = exportRecord?.failedFiles?.length ?? 0;
@ -181,6 +168,17 @@ export default function ExportModal(props: Props) {
// UI functions // UI functions
// ============= // =============
const changeExportDirectory = async () => {
try {
const newFolder = await exportService.selectExportDirectory();
if (newFolder) {
updateExportFolder(newFolder);
}
} catch (e) {
logError(e, 'selectExportDirectory failed');
}
};
const startExport = async () => { const startExport = async () => {
try { try {
await preExportRun(); await preExportRun();
@ -201,10 +199,8 @@ export default function ExportModal(props: Props) {
await postExportRun(); await postExportRun();
} catch (e) { } catch (e) {
if (e.message !== CustomError.REQUEST_CANCELLED) {
logError(e, 'resumeExport failed'); logError(e, 'resumeExport failed');
} }
}
}; };
const stopExport = async () => { const stopExport = async () => {
@ -212,10 +208,8 @@ export default function ExportModal(props: Props) {
exportService.stopRunningExport(); exportService.stopRunningExport();
await postExportRun(); await postExportRun();
} catch (e) { } catch (e) {
if (e.message !== CustomError.REQUEST_CANCELLED) {
logError(e, 'stopExport failed'); logError(e, 'stopExport failed');
} }
}
}; };
const startExportHandler = () => { const startExportHandler = () => {
@ -262,7 +256,7 @@ export default function ExportModal(props: Props) {
<DialogContent> <DialogContent>
<ExportDirectory <ExportDirectory
exportFolder={exportFolder} exportFolder={exportFolder}
selectExportDirectory={selectExportDirectory} changeExportDirectory={changeExportDirectory}
exportStage={exportStage} exportStage={exportStage}
/> />
<TotalFileCount totalFileCount={totalFileCount} /> <TotalFileCount totalFileCount={totalFileCount} />
@ -273,13 +267,13 @@ export default function ExportModal(props: Props) {
); );
} }
function ExportDirectory({ exportFolder, selectExportDirectory, exportStage }) { function ExportDirectory({ exportFolder, changeExportDirectory, exportStage }) {
return ( return (
<SpaceBetweenFlex minHeight={'40px'}> <SpaceBetweenFlex minHeight={'40px'}>
<Typography color="text.secondary">{t('DESTINATION')}</Typography> <Typography color="text.secondary">{t('DESTINATION')}</Typography>
<> <>
{!exportFolder ? ( {!exportFolder ? (
<Button color={'accent'} onClick={selectExportDirectory}> <Button color={'accent'} onClick={changeExportDirectory}>
{t('SELECT_FOLDER')} {t('SELECT_FOLDER')}
</Button> </Button>
) : ( ) : (
@ -292,7 +286,7 @@ function ExportDirectory({ exportFolder, selectExportDirectory, exportStage }) {
{exportStage === ExportStage.FINISHED || {exportStage === ExportStage.FINISHED ||
exportStage === ExportStage.INIT ? ( exportStage === ExportStage.INIT ? (
<ExportDirectoryOption <ExportDirectoryOption
selectExportDirectory={selectExportDirectory} changeExportDirectory={changeExportDirectory}
/> />
) : ( ) : (
<Box sx={{ width: '48px' }} /> <Box sx={{ width: '48px' }} />
@ -315,16 +309,7 @@ function TotalFileCount({ totalFileCount }) {
); );
} }
function ExportDirectoryOption({ selectExportDirectory }) { function ExportDirectoryOption({ changeExportDirectory }) {
const handleClick = () => {
try {
selectExportDirectory();
} catch (e) {
if (e.message !== CustomError.REQUEST_CANCELLED) {
logError(e, 'startExport failed');
}
}
};
return ( return (
<OverflowMenu <OverflowMenu
triggerButtonProps={{ triggerButtonProps={{
@ -335,7 +320,7 @@ function ExportDirectoryOption({ selectExportDirectory }) {
ariaControls={'export-option'} ariaControls={'export-option'}
triggerButtonIcon={<MoreHoriz />}> triggerButtonIcon={<MoreHoriz />}>
<OverflowMenuOption <OverflowMenuOption
onClick={handleClick} onClick={changeExportDirectory}
startIcon={<FolderIcon />}> startIcon={<FolderIcon />}>
{t('CHANGE_FOLDER')} {t('CHANGE_FOLDER')}
</OverflowMenuOption> </OverflowMenuOption>