Skip to content

Commit

Permalink
components/KillButton: add KillButtonDialog component
Browse files Browse the repository at this point in the history
This commit also removes "--user" from the t-api /kill
route request.

Signed-off-by: Vallari Agrawal <val.agl002@gmail.com>
  • Loading branch information
VallariAg committed Feb 12, 2024
1 parent 493dc92 commit c7d842a
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 12 deletions.
82 changes: 73 additions & 9 deletions src/components/KillButton/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { useState } from "react";
import type { UseMutationResult } from "@tanstack/react-query";
import Button from "@mui/material/Button";
import Box from "@mui/material/Box";
import { CircularProgress } from "@mui/material";
import CircularProgress from "@mui/material/CircularProgress";
import DialogTitle from '@mui/material/DialogTitle';
import DialogContent from '@mui/material/DialogContent';
import Dialog from '@mui/material/Dialog';
import Paper from "@mui/material/Paper";
import Typography from "@mui/material/Typography";

import { KillRunPayload } from "../../lib/teuthologyAPI.d";
import { useSession } from "../../lib/teuthologyAPI";
Expand All @@ -15,37 +21,95 @@ type KillButtonProps = {
disabled?: boolean;
};

type KillButtonDialogProps = {
mutation: UseMutationResult;
payload: KillRunPayload;
open: boolean;
handleClose: () => void;
};

export default function KillButton(props: KillButtonProps) {
const [open, setOpen] = useState(false);
const mutation: UseMutationResult = props.mutation;
const sessionQuery = useSession();
const loggedUser = sessionQuery.data?.session.username;

if (loggedUser?.toLowerCase() != props.payload["--user"].toLowerCase()) {
if (loggedUser?.toLowerCase() != props.payload["--owner"].toLowerCase()) {
// logged user and owner of the job should be equal (case insensitive)
return null
}

const toggleDialog = () => {
setOpen(!open);
};


return (
<div>
<div style={{ display: "flex" }}>
<Button
variant="contained"
color="error"
size="large"
onClick={() => mutation.mutate(props.payload)}
onClick={toggleDialog}
disabled={(props.disabled || mutation.isLoading)}
>
{props.text}
</Button>
{(mutation.isLoading) ? (
<Box sx={{ p: 1 }}>
<CircularProgress size={20} color="inherit" />
</Box>
) : null}
<KillButtonDialog
mutation={mutation}
payload={props.payload}
open={open}
handleClose={toggleDialog}
/>
</div>
{ (mutation.isError) ? <Alert severity="error" message="Unable to kill run" /> : null }
{ (mutation.isSuccess) ? <Alert severity="success" message="Run killed successfully" /> : null }
{ (mutation.isSuccess) ? <Alert severity="success" message={`Run killed successfully! \n`} /> : null }
</div>
);
};

function KillButtonDialog({mutation, open, handleClose, payload}: KillButtonDialogProps) {
return (
<div>
<Dialog onClose={handleClose} open={open} scroll="paper" fullWidth={true} maxWidth="sm">
<DialogTitle>Kill confirmation</DialogTitle>
<DialogContent>
{ (mutation.data && mutation.data.data ) ?
<div>
<Typography variant="h6" display="block" gutterBottom>
{mutation.isSuccess ? "Successful!": "Failed!"}
</Typography>
<Paper>
<Typography variant="caption" display="block" gutterBottom>
{mutation.data.data.logs}
</Typography>
</Paper>
</div> :
(mutation.isLoading) ? (
<Box sx={{ p: 1 }}>
<Typography variant="subtitle1" display="block" gutterBottom>
<CircularProgress size={20} color="inherit" />
Killing run...
</Typography>
</Box>
) :
<div>
<Typography variant="overline" display="block" gutterBottom>
Are you sure you want to kill this run/job?
</Typography>
<Button
variant="contained"
color="error"
size="large"
onClick={() => mutation.mutate(payload)}
>
Yes, I'm sure
</Button>
</div>
}
</DialogContent>
</Dialog>
</div>
)
}
3 changes: 1 addition & 2 deletions src/lib/teuthologyAPI.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ export type Session = {
export type KillRunPayload = {
"--run": string,
"--owner": string,
"--machine-type": string,
"--user": string,
"--machine-type": string,
}
1 change: 0 additions & 1 deletion src/pages/Run/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export default function Run() {
"--run": data?.name || "",
"--owner": run_owner,
"--machine-type": data?.machine_type || "",
"--user": data?.user || "",
}
return (
<Root className={classes.root}>
Expand Down

0 comments on commit c7d842a

Please sign in to comment.