Can remove disks files from UI
This commit is contained in:
		@@ -1,18 +1,23 @@
 | 
			
		||||
import { mdiHarddisk } from "@mdi/js";
 | 
			
		||||
import Icon from "@mdi/react";
 | 
			
		||||
import CheckCircleIcon from "@mui/icons-material/CheckCircle";
 | 
			
		||||
import DeleteIcon from "@mui/icons-material/Delete";
 | 
			
		||||
import {
 | 
			
		||||
  Avatar,
 | 
			
		||||
  Button,
 | 
			
		||||
  IconButton,
 | 
			
		||||
  ListItem,
 | 
			
		||||
  ListItemAvatar,
 | 
			
		||||
  ListItemText,
 | 
			
		||||
  Paper,
 | 
			
		||||
  Tooltip,
 | 
			
		||||
} from "@mui/material";
 | 
			
		||||
import { VMDisk, VMInfo } from "../../api/VMApi";
 | 
			
		||||
import { filesize } from "filesize";
 | 
			
		||||
import Icon from "@mdi/react";
 | 
			
		||||
import { mdiHarddisk } from "@mdi/js";
 | 
			
		||||
import { TextInput } from "./TextInput";
 | 
			
		||||
import { ServerApi } from "../../api/ServerApi";
 | 
			
		||||
import { VMDisk, VMInfo } from "../../api/VMApi";
 | 
			
		||||
import { useConfirm } from "../../hooks/providers/ConfirmDialogProvider";
 | 
			
		||||
import { SelectInput } from "./SelectInput";
 | 
			
		||||
import { TextInput } from "./TextInput";
 | 
			
		||||
 | 
			
		||||
export function VMDisksList(p: {
 | 
			
		||||
  vm: VMInfo;
 | 
			
		||||
@@ -39,6 +44,10 @@ export function VMDisksList(p: {
 | 
			
		||||
          editable={p.editable}
 | 
			
		||||
          disk={d}
 | 
			
		||||
          onChange={p.onChange}
 | 
			
		||||
          removeFromList={() => {
 | 
			
		||||
            p.vm.disks.splice(num, 1);
 | 
			
		||||
            p.onChange?.();
 | 
			
		||||
          }}
 | 
			
		||||
        />
 | 
			
		||||
      ))}
 | 
			
		||||
 | 
			
		||||
@@ -51,22 +60,74 @@ function DiskInfo(p: {
 | 
			
		||||
  editable: boolean;
 | 
			
		||||
  disk: VMDisk;
 | 
			
		||||
  onChange?: () => void;
 | 
			
		||||
  removeFromList: () => void;
 | 
			
		||||
}): React.ReactElement {
 | 
			
		||||
  const confirm = useConfirm();
 | 
			
		||||
  const deleteDisk = async () => {
 | 
			
		||||
    if (p.disk.deleteType) {
 | 
			
		||||
      p.disk.deleteType = undefined;
 | 
			
		||||
      p.onChange?.();
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const keepFile = await confirm(
 | 
			
		||||
      `You asked to delete the disk ${p.disk.name}. Do you want to keep the block file or not ? `,
 | 
			
		||||
      "Delete disk",
 | 
			
		||||
      "Keep the file",
 | 
			
		||||
      "Delete the file"
 | 
			
		||||
    );
 | 
			
		||||
 | 
			
		||||
    if (!(await confirm("Do you really want to delete this disk?"))) return;
 | 
			
		||||
 | 
			
		||||
    p.disk.deleteType = keepFile ? "keepfile" : "deletefile";
 | 
			
		||||
    p.onChange?.();
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  if (!p.editable || !p.disk.new)
 | 
			
		||||
    return (
 | 
			
		||||
      <ListItem>
 | 
			
		||||
      <ListItem
 | 
			
		||||
        secondaryAction={
 | 
			
		||||
          p.editable && (
 | 
			
		||||
            <IconButton
 | 
			
		||||
              edge="end"
 | 
			
		||||
              aria-label="delete disk"
 | 
			
		||||
              onClick={deleteDisk}
 | 
			
		||||
            >
 | 
			
		||||
              {p.disk.deleteType ? (
 | 
			
		||||
                <Tooltip title="Cancel disk removal">
 | 
			
		||||
                  <CheckCircleIcon />
 | 
			
		||||
                </Tooltip>
 | 
			
		||||
              ) : (
 | 
			
		||||
                <Tooltip title="Remove disk">
 | 
			
		||||
                  <DeleteIcon />
 | 
			
		||||
                </Tooltip>
 | 
			
		||||
              )}
 | 
			
		||||
            </IconButton>
 | 
			
		||||
          )
 | 
			
		||||
        }
 | 
			
		||||
      >
 | 
			
		||||
        <ListItemAvatar>
 | 
			
		||||
          <Avatar>
 | 
			
		||||
            <Icon path={mdiHarddisk} />
 | 
			
		||||
          </Avatar>
 | 
			
		||||
        </ListItemAvatar>
 | 
			
		||||
        <ListItemText
 | 
			
		||||
          primary={p.disk.name}
 | 
			
		||||
          primary={
 | 
			
		||||
            <>
 | 
			
		||||
              {p.disk.name}{" "}
 | 
			
		||||
              {p.disk.deleteType && (
 | 
			
		||||
                <span style={{ color: "red" }}>
 | 
			
		||||
                  {p.disk.deleteType === "deletefile"
 | 
			
		||||
                    ? "Remove, DELETING block file"
 | 
			
		||||
                    : "Remove, keeping block file"}
 | 
			
		||||
                </span>
 | 
			
		||||
              )}
 | 
			
		||||
            </>
 | 
			
		||||
          }
 | 
			
		||||
          secondary={`${filesize(p.disk.size * 1000 * 1000)} - ${
 | 
			
		||||
            p.disk.alloc_type
 | 
			
		||||
          }`}
 | 
			
		||||
        />
 | 
			
		||||
        {/* TODO delete disk if editable */}
 | 
			
		||||
      </ListItem>
 | 
			
		||||
    );
 | 
			
		||||
 | 
			
		||||
@@ -96,19 +157,25 @@ function DiskInfo(p: {
 | 
			
		||||
        type="number"
 | 
			
		||||
      />
 | 
			
		||||
 | 
			
		||||
      <SelectInput
 | 
			
		||||
        editable={true}
 | 
			
		||||
        label="File allocation type"
 | 
			
		||||
        options={[
 | 
			
		||||
          { label: "Sparse allocation", value: "Sparse" },
 | 
			
		||||
          { label: "Fixed allocation", value: "Fixed" },
 | 
			
		||||
        ]}
 | 
			
		||||
        value={p.disk.alloc_type}
 | 
			
		||||
        onValueChange={(v) => {
 | 
			
		||||
          p.disk.alloc_type = v as any;
 | 
			
		||||
          p.onChange?.();
 | 
			
		||||
        }}
 | 
			
		||||
      />
 | 
			
		||||
      <div style={{ display: "flex", justifyContent: "space-between" }}>
 | 
			
		||||
        <SelectInput
 | 
			
		||||
          editable={true}
 | 
			
		||||
          label="File allocation type"
 | 
			
		||||
          options={[
 | 
			
		||||
            { label: "Sparse allocation", value: "Sparse" },
 | 
			
		||||
            { label: "Fixed allocation", value: "Fixed" },
 | 
			
		||||
          ]}
 | 
			
		||||
          value={p.disk.alloc_type}
 | 
			
		||||
          onValueChange={(v) => {
 | 
			
		||||
            p.disk.alloc_type = v as any;
 | 
			
		||||
            p.onChange?.();
 | 
			
		||||
          }}
 | 
			
		||||
        />
 | 
			
		||||
 | 
			
		||||
        <IconButton onClick={p.removeFromList}>
 | 
			
		||||
          <DeleteIcon />
 | 
			
		||||
        </IconButton>
 | 
			
		||||
      </div>
 | 
			
		||||
    </Paper>
 | 
			
		||||
  );
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user