Skip to content

Commit cab1d31

Browse files
authored
Add abort button (#26)
* add abort button
1 parent 057a282 commit cab1d31

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

src/components/AbortButton.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Button } from "@mui/material";
2+
3+
import { setWorkerState, type WorkerRequest } from "../utils/api";
4+
5+
const AbortButton = () => {
6+
return (
7+
<Button
8+
variant="contained"
9+
color="error"
10+
sx={{ width: "150px" }}
11+
onClick={async () => {
12+
const workerRequest: WorkerRequest = {
13+
new_state: "ABORTING",
14+
defer: false,
15+
reason: "UI Intervention",
16+
};
17+
await setWorkerState(workerRequest);
18+
}}
19+
>
20+
Abort
21+
</Button>
22+
);
23+
};
24+
25+
export default AbortButton;

src/components/spectroscopy/SpectroscopyForm.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useInstrumentSession } from "../../context/instrumentSession/useInstrumentSession";
22
import RunPlanButton from "../RunPlanButton";
3+
import AbortButton from "../AbortButton";
34
import { useState } from "react";
45
import { NumberInput } from "../NumberInput";
56
import { Box } from "@mui/material";
@@ -97,6 +98,9 @@ export function SpectroscopyForm() {
9798
instrumentSession={instrumentSession}
9899
/>
99100
</Box>
101+
<Box sx={{ mt: 4 }} display={"flex"} justifyContent={"center"}>
102+
<AbortButton />
103+
</Box>
100104
</Box>
101105
);
102106
}

src/mocks/handlers.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ export const handlers = [
4141
});
4242
}),
4343

44+
http.put("/api/worker/state", () => {
45+
return HttpResponse.json("IDLE");
46+
}),
47+
4448
http.get("/api/data/map", ({ request }) => {
4549
const url = new URL(request.url);
4650
const filepath = url.searchParams.get("filepath");

src/utils/api.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,35 @@ export async function startTask(task_id: string): Promise<TaskResponse> {
7777

7878
return await response.json();
7979
}
80+
81+
export interface WorkerRequest {
82+
new_state: string;
83+
defer: boolean;
84+
reason: string;
85+
}
86+
87+
export interface WorkerResponse {
88+
worker_state: string;
89+
}
90+
91+
export async function setWorkerState(
92+
worker_request: WorkerRequest,
93+
): Promise<WorkerResponse> {
94+
const url = "/api/worker/state";
95+
96+
const headers = new Headers();
97+
headers.append("Content-Type", "application/json");
98+
headers.append("X-Requested-By", "XMLHttpRequest");
99+
100+
const response = await fetch(url, {
101+
method: "PUT",
102+
headers: headers,
103+
body: JSON.stringify({
104+
new_state: worker_request.new_state,
105+
defer: worker_request.defer,
106+
reason: worker_request.reason,
107+
}),
108+
});
109+
110+
return await response.json();
111+
}

0 commit comments

Comments
 (0)