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

Disable Lift export when Frontier is empty #3440

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
52 changes: 32 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 @@ -13,21 +13,17 @@ import { type StoreState } from "rootRedux/types";
interface ExportButtonProps {
projectId: string;
buttonProps?: ButtonProps & { "data-testid"?: string };
disabled?: boolean;
}

/** A button for exporting project to Lift file */
export default function ExportButton(props: ExportButtonProps): ReactElement {
const dispatch = useAppDispatch();
const [exports, setExports] = useState<boolean>(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 +34,33 @@ export default function ExportButton(props: ExportButtonProps): ReactElement {
exportResult.status === ExportStatus.Success ||
exportResult.status === ExportStatus.Downloading;

useEffect(() => {
const fetchNonempty = async (): Promise<void> => {
await isFrontierNonempty(props.projectId).then(async (isNonempty) => {
if (isNonempty) {
setExports(true);
}
});
};
fetchNonempty().catch(console.error);
});

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}
buttonProps={{
...props.buttonProps,
onClick: exportProj,
id: `project-${props.projectId}-export`,
disabled: !exports,
}}
>
{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
Loading