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

[DownloadButton] Simplify with switch statements. #886

Merged
merged 4 commits into from
Dec 22, 2020
Merged
Changes from all 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
71 changes: 39 additions & 32 deletions src/components/ProjectExport/DownloadButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ export default function DownloadButton() {
(state: StoreState) => state.exportProjectState
);
const dispatch = useDispatch();
const [fileName, setFileName] = useState<null | string>(null);
const [fileUrl, setFileUrl] = useState<null | string>(null);
const [fileName, setFileName] = useState<string | undefined>();
const [fileUrl, setFileUrl] = useState<string | undefined>();
let downloadLink = createRef<HTMLAnchorElement>();

useEffect(() => {
if (downloadLink.current && fileUrl !== null) {
if (downloadLink.current && fileUrl) {
downloadLink.current.click();
URL.revokeObjectURL(fileUrl);
setFileUrl(null);
setFileUrl(undefined);
}
}, [downloadLink, fileUrl]);

Expand All @@ -48,36 +48,43 @@ export default function DownloadButton() {
resetExport(exportState.projectId)(dispatch);
}

function textId() {
switch (exportState.status) {
case ExportStatus.InProgress:
return "projectExport.exportInProgress";
case ExportStatus.Success:
return "projectExport.downloadInProgress";
case ExportStatus.Failure:
return "projectExport.exportFailed";
}
}

function icon() {
switch (exportState.status) {
case ExportStatus.InProgress:
return <Cached />;
case ExportStatus.Success:
return <GetApp />;
case ExportStatus.Failure:
return <Error />;
}
}

function iconFunction() {
switch (exportState.status) {
case ExportStatus.Success:
return download;
case ExportStatus.Failure:
return reset;
}
}

return (
<React.Fragment>
{/* Nothing shows if exportState.status === ExportStatus.Default. */}
{exportState.status === ExportStatus.Success && (
<Tooltip
title={<Translate id="projectExport.downloadReady" />}
placement="bottom"
>
<IconButton tabIndex={-1} onClick={download}>
<GetApp />
</IconButton>
</Tooltip>
)}
{exportState.status === ExportStatus.InProgress && (
<Tooltip
title={<Translate id="projectExport.exportInProgress" />}
placement="bottom"
>
<IconButton tabIndex={-1}>
<Cached />
</IconButton>
</Tooltip>
)}
{exportState.status === ExportStatus.Failure && (
<Tooltip
title={<Translate id="projectExport.exportFailed" />}
placement="bottom"
>
<IconButton tabIndex={-1} onClick={reset}>
<Error />
{exportState.status !== ExportStatus.Default && (
<Tooltip title={<Translate id={textId()} />} placement="bottom">
<IconButton tabIndex={-1} onClick={iconFunction()}>
{icon()}
</IconButton>
</Tooltip>
)}
Expand Down