Skip to content

Commit

Permalink
Fix export/download
Browse files Browse the repository at this point in the history
  • Loading branch information
imnasnainaec committed Oct 27, 2023
1 parent 08cc060 commit da7899b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 51 deletions.
33 changes: 14 additions & 19 deletions src/components/ProjectExport/DownloadButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,8 @@ interface DownloadButtonProps {
export default function DownloadButton(
props: DownloadButtonProps
): ReactElement {
const projectId = useAppSelector((state: StoreState) => {
console.info(state.exportProjectState);
return state.exportProjectState.projectId;
});
const status = useAppSelector(
(state: StoreState) => state.exportProjectState.status
const exportState = useAppSelector(
(state: StoreState) => state.exportProjectState
);
const dispatch = useAppDispatch();
const [fileName, setFileName] = useState<string | undefined>();
Expand All @@ -64,26 +60,25 @@ export default function DownloadButton(

useEffect(() => {
if (fileName) {
dispatch(asyncDownloadExport(projectId)).then((url) => {
dispatch(asyncDownloadExport(exportState.projectId)).then((url) => {
if (url) {
setFileUrl(url);
reset();
}
});
}
}, [dispatch, fileName, projectId, reset]);
}, [dispatch, exportState.projectId, fileName, reset, setFileUrl]);

useEffect(() => {
console.info("exportState updated");
if (status === ExportStatus.Success) {
getProjectName(projectId).then((projectName) => {
if (exportState.status === ExportStatus.Success) {
getProjectName(exportState.projectId).then((projectName) => {
setFileName(makeExportName(projectName));
});
}
}, [projectId, status]);
}, [exportState, setFileName]);

function textId(): string {
switch (status) {
switch (exportState.status) {
case ExportStatus.Exporting:
return "projectExport.exportInProgress";
case ExportStatus.Success:
Expand All @@ -97,7 +92,7 @@ export default function DownloadButton(
}

function icon(): ReactElement {
switch (status) {
switch (exportState.status) {
case ExportStatus.Exporting:
case ExportStatus.Downloading:
case ExportStatus.Success:
Expand All @@ -110,15 +105,15 @@ export default function DownloadButton(
}

function iconColor(): `#${string}` {
return status === ExportStatus.Failure
return exportState.status === ExportStatus.Failure
? themeColors.error
: props.colorSecondary
? themeColors.secondary
: themeColors.primary;
}

function iconFunction(): () => void {
switch (status) {
switch (exportState.status) {
case ExportStatus.Failure:
return reset;
default:
Expand All @@ -127,8 +122,8 @@ export default function DownloadButton(
}

return (
<>
{status !== ExportStatus.Default && (
<Fragment>
{exportState.status !== ExportStatus.Default && (
<Tooltip title={t(textId())} placement="bottom">
<IconButton
tabIndex={-1}
Expand All @@ -150,6 +145,6 @@ export default function DownloadButton(
(This link should not be visible)
</a>
)}
</>
</Fragment>
);
}
5 changes: 0 additions & 5 deletions src/components/ProjectExport/Redux/ExportProjectActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,14 @@ export function success(projectId: string): PayloadAction {

export function asyncExportProject(projectId: string) {
return async (dispatch: StoreStateDispatch) => {
console.info("dispatching export");
dispatch(exporting(projectId));
console.info("exporting");
await exportLift(projectId).catch(() => dispatch(failure(projectId)));
console.info("exported");
};
}

export function asyncDownloadExport(projectId: string) {
return async (dispatch: StoreStateDispatch) => {
console.info("dispatching download");
dispatch(downloading(projectId));
console.info("downloading");
return await downloadLift(projectId).catch(() => {
dispatch(failure(projectId));
});
Expand Down
37 changes: 10 additions & 27 deletions src/components/ProjectExport/Redux/ExportProjectReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,24 @@ const exportProjectSlice = createSlice({
/* eslint-disable @typescript-eslint/no-unused-vars */
reducers: {
downloadingAction: (state, action) => {
console.info("updating state to downloading");
console.info(action);
state = {
...defaultState,
projectId: action.payload,
status: ExportStatus.Downloading,
};
console.info(state);
state.projectId = action.payload;
state.status = ExportStatus.Downloading;
},
exportingAction: (state, action) => {
console.info("updating state to exporting");
console.info(action);
state = {
...defaultState,
projectId: action.payload,
status: ExportStatus.Exporting,
};
console.info(state);
state.projectId = action.payload;
state.status = ExportStatus.Exporting;
},
failureAction: (state, action) => {
state = {
...defaultState,
projectId: action.payload,
status: ExportStatus.Failure,
};
state.projectId = action.payload;
state.status = ExportStatus.Failure;
},
resetAction: (state) => {
state = { ...defaultState, status: ExportStatus.Default };
state.projectId = "";
state.status = ExportStatus.Default;
},
successAction: (state, action) => {
state = {
...defaultState,
projectId: action.payload,
status: ExportStatus.Success,
};
state.projectId = action.payload;
state.status = ExportStatus.Success;
},
},
/* eslint-enable @typescript-eslint/no-unused-vars */
Expand Down

0 comments on commit da7899b

Please sign in to comment.