Skip to content

Commit

Permalink
feat(ui-debug): allow to delete file
Browse files Browse the repository at this point in the history
  • Loading branch information
skamril committed Sep 20, 2024
1 parent 15aca5c commit bf4ad31
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 15 deletions.
113 changes: 99 additions & 14 deletions webapp/src/components/App/Singlestudy/explore/Debug/Data/Folder.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
import {
Divider,
IconButton,
List,
ListItem,
ListItemButton,
ListItemIcon,
ListItemText,
ListSubheader,
Menu,
MenuItem,
} from "@mui/material";
import FolderIcon from "@mui/icons-material/Folder";
import MoreVertIcon from "@mui/icons-material/MoreVert";
import DeleteIcon from "@mui/icons-material/Delete";
import {
getFileIcon,
getFileType,
type TreeFolder,
type DataCompProps,
isFolder,
} from "../utils";
import { Fragment } from "react";
import { Fragment, useState } from "react";
import EmptyView from "../../../../../common/page/SimpleContent";
import { useTranslation } from "react-i18next";
import { Filename, Menubar } from "./styles";
import UploadFileButton from "../../../../../common/buttons/UploadFileButton";
import ConfirmationDialog from "../../../../../common/dialogs/ConfirmationDialog";
import useConfirm from "../../../../../../hooks/useConfirm";
import { deleteFile } from "../../../../../../services/api/studies/raw";
import useEnqueueErrorSnackbar from "../../../../../../hooks/useEnqueueErrorSnackbar";
import { toError } from "../../../../../../utils/fnUtils";

function Folder(props: DataCompProps) {
const {
Expand All @@ -35,6 +44,13 @@ function Folder(props: DataCompProps) {

const { t } = useTranslation();
const replaceFile = useConfirm();
const removeFile = useConfirm();
const [menuData, setMenuData] = useState<null | {
anchorEl: HTMLElement;
filePath: string;
}>(null);
const enqueueErrorSnackbar = useEnqueueErrorSnackbar();

const treeFolder = treeData as TreeFolder;
const list = Object.entries(treeFolder);

Expand All @@ -53,6 +69,27 @@ function Folder(props: DataCompProps) {
}
};

const handleMenuClose = () => {
setMenuData(null);
};

const handleDeleteClick = () => {
handleMenuClose();

removeFile.showConfirm().then((confirm) => {
const filePath = menuData?.filePath;
if (confirm && filePath) {
deleteFile({ studyId, path: filePath })
.then((res) => {
reloadTreeData();
})
.catch((err) => {
enqueueErrorSnackbar("Delete failed", toError(err));
});
}
});
};

////////////////////////////////////////////////////////////////
// JSX
////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -94,21 +131,45 @@ function Folder(props: DataCompProps) {

return (
<Fragment key={filename}>
<ListItemButton
onClick={() =>
setSelectedFile({
fileType,
filename,
filePath: `${filePath}/${filename}`,
treeData: data,
})
<ListItem
secondaryAction={
<IconButton
edge="end"
size="small"
onClick={(event) => {
setMenuData({
anchorEl: event.currentTarget,
filePath: `${filePath}/${filename}`,
});
}}
>
<MoreVertIcon />
</IconButton>
}
disablePadding
>
<ListItemIcon>
<Icon />
</ListItemIcon>
<ListItemText primary={filename} />
</ListItemButton>
<ListItemButton
onClick={() =>
setSelectedFile({
fileType,
filename,
filePath: `${filePath}/${filename}`,
treeData: data,
})
}
>
<ListItemIcon>
<Icon />
</ListItemIcon>
<ListItemText
title={filename}
primary={filename}
primaryTypographyProps={{
sx: { overflow: "hidden", textOverflow: "ellipsis" },
}}
/>
</ListItemButton>
</ListItem>
{!isLast && <Divider variant="fullWidth" />}
</Fragment>
);
Expand All @@ -117,6 +178,18 @@ function Folder(props: DataCompProps) {
<EmptyView title={t("study.debug.folder.empty")} icon={FolderIcon} />
)}
</List>
{/* Items menu */}
<Menu
anchorEl={menuData?.anchorEl}
open={!!menuData}
onClose={handleMenuClose}
>
<MenuItem onClick={handleDeleteClick}>
<DeleteIcon sx={{ mr: 1 }} fontSize="small" />
Delete
</MenuItem>
</Menu>
{/* Confim file replacement */}
<ConfirmationDialog
title={t("study.debug.folder.upload.replaceFileConfirm.title")}
confirmButtonText={t("global.replace")}
Expand All @@ -128,6 +201,18 @@ function Folder(props: DataCompProps) {
>
{t("study.debug.folder.upload.replaceFileConfirm.message")}
</ConfirmationDialog>
{/* Confim file deletion */}
<ConfirmationDialog
titleIcon={DeleteIcon}
confirmButtonText={t("global.delete")}
cancelButtonText={t("global.cancel")}
maxWidth="xs"
open={removeFile.isPending}
onConfirm={removeFile.yes}
onCancel={removeFile.no}
>
Delete the file?
</ConfirmationDialog>
</>
);
}
Expand Down
15 changes: 14 additions & 1 deletion webapp/src/services/api/studies/raw/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import client from "../../client";
import type { DownloadMatrixParams, ImportFileParams } from "./types";
import type {
DeleteFileParams,
DownloadMatrixParams,
ImportFileParams,
} from "./types";

export async function downloadMatrix(params: DownloadMatrixParams) {
const { studyId, ...queryParams } = params;
const url = `v1/studies/${studyId}/raw/download`;

const res = await client.get<Blob>(url, {
params: queryParams,
responseType: "blob",
Expand All @@ -16,6 +21,7 @@ export async function importFile(params: ImportFileParams) {
const { studyId, file, onUploadProgress, ...queryParams } = params;
const url = `v1/studies/${studyId}/raw`;
const body = { file };

await client.putForm<void>(url, body, {
params: {
...queryParams,
Expand All @@ -24,3 +30,10 @@ export async function importFile(params: ImportFileParams) {
onUploadProgress,
});
}

export async function deleteFile(params: DeleteFileParams) {
const { studyId, path } = params;
const url = `v1/studies/${studyId}/raw`;

await client.delete<void>(url, { params: { path } });
}
5 changes: 5 additions & 0 deletions webapp/src/services/api/studies/raw/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ export interface ImportFileParams {
createMissing?: boolean;
onUploadProgress?: AxiosRequestConfig["onUploadProgress"];
}

export interface DeleteFileParams {
studyId: StudyMetadata["id"];
path: string;
}

0 comments on commit bf4ad31

Please sign in to comment.