Skip to content

Commit

Permalink
Disable Lift export when Frontier is empty (#3440)
Browse files Browse the repository at this point in the history
  • Loading branch information
andracc authored Nov 18, 2024
1 parent 9238e55 commit cb9dcd1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
43 changes: 23 additions & 20 deletions src/components/ProjectExport/ExportButton.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Tooltip } from "@mui/material";
import { ButtonProps } from "@mui/material/Button";
import { enqueueSnackbar } from "notistack";
import { ReactElement } from "react";
import { ReactElement, useEffect, useState } from "react";
import { useTranslation } from "react-i18next";

import { isFrontierNonempty } from "backend";
Expand All @@ -18,16 +18,11 @@ interface ExportButtonProps {
/** A button for exporting project to Lift file */
export default function ExportButton(props: ExportButtonProps): ReactElement {
const dispatch = useAppDispatch();
const [exports, setExports] = useState(false);
const { t } = useTranslation();

async function exportProj(): Promise<void> {
await isFrontierNonempty(props.projectId).then(async (isNonempty) => {
if (isNonempty) {
await dispatch(asyncExportProject(props.projectId));
} else {
enqueueSnackbar(t("projectExport.cannotExportEmpty"));
}
});
await dispatch(asyncExportProject(props.projectId));
}

const exportResult = useAppSelector(
Expand All @@ -38,17 +33,25 @@ export default function ExportButton(props: ExportButtonProps): ReactElement {
exportResult.status === ExportStatus.Success ||
exportResult.status === ExportStatus.Downloading;

useEffect(() => {
isFrontierNonempty(props.projectId).then(setExports);
}, [props.projectId]);

return (
<LoadingButton
loading={loading}
disabled={loading}
buttonProps={{
...props.buttonProps,
onClick: exportProj,
id: `project-${props.projectId}-export`,
}}
>
{t("buttons.export")}
</LoadingButton>
<Tooltip title={!exports ? t("projectExport.cannotExportEmpty") : ""}>
<span>
<LoadingButton
loading={loading}
disabled={loading || !exports}
buttonProps={{
...props.buttonProps,
onClick: exportProj,
id: `project-${props.projectId}-export`,
}}
>
{t("buttons.export")}
</LoadingButton>
</span>
</Tooltip>
);
}
1 change: 1 addition & 0 deletions src/components/ProjectSettings/tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jest.mock("backend", () => ({
getAllUsers: () => Promise.resolve([]),
getCurrentPermissions: () => mockGetCurrentPermissions(),
getUserRoles: () => Promise.resolve([]),
isFrontierNonempty: () => Promise.resolve(false),
}));
jest.mock("components/Project/ProjectActions");
// Mock "i18n", else `thrown: "Error: Error: connect ECONNREFUSED ::1:80 [...]`
Expand Down

0 comments on commit cb9dcd1

Please sign in to comment.