Skip to content

Commit 4cf9bab

Browse files
committed
First version of MCC, needs debugging
1 parent 07d652b commit 4cf9bab

File tree

5 files changed

+90
-51
lines changed

5 files changed

+90
-51
lines changed

gs/frontend/mcc/package-lock.json

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gs/frontend/mcc/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
},
1212
"dependencies": {
1313
"@tailwindcss/vite": "^4.1.13",
14+
"@tanstack/react-query": "^5.90.2",
1415
"bootstrap": "^5.3.8",
1516
"react": "^19.1.1",
1617
"react-bootstrap": "^2.10.10",

gs/frontend/mcc/src/aro-requests/aro-requests.tsx

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import Table from "react-bootstrap/Table";
2-
import { useEffect, useState } from "react";
2+
import { useQuery } from '@tanstack/react-query';
33

44
type AROItemProps = {
55
id: number;
66
latitude: number;
77
longitude: number;
88
status: string;
99
};
10+
1011
function AROItem({ id, latitude, longitude, status }: AROItemProps) {
1112
return (
1213
<>
@@ -21,17 +22,14 @@ function AROItem({ id, latitude, longitude, status }: AROItemProps) {
2122
}
2223

2324
function ARORequests() {
24-
const [loading, setLoading] = useState(true);
25-
const [aroRequests, setARORequests] = useState<AROItemProps[]>([]);
26-
27-
useEffect(() => {
28-
fetch("http://localhost:5000/aro-request")
29-
.then((response) => response.json())
30-
.then((data) => {
31-
setARORequests(data);
32-
setLoading(false);
33-
});
34-
}, []);
25+
const { data, isLoading} = useQuery<AROItemProps[]>({
26+
queryKey: ["aro-requests"],
27+
queryFn: async () => {
28+
const response = await fetch("http://localhost:5000/aro-request");
29+
if(!response.ok) throw new Error("Network response was not ok");
30+
return response.json();
31+
},
32+
});
3533

3634
return (
3735
<div className="arorequests layout">
@@ -45,14 +43,14 @@ function ARORequests() {
4543
</tr>
4644
</thead>
4745
<tbody>
48-
{loading
46+
{isLoading || !data
4947
? (
5048
<tr>
5149
<td colSpan={4}>Loading...</td>
5250
</tr>
5351
)
54-
: (
55-
aroRequests.map((item) => {
52+
:(
53+
data.map((item) => {
5654
return (
5755
<AROItem
5856
key={item.id}

gs/frontend/mcc/src/main.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ import React from "react";
33
import ReactDOM from "react-dom/client";
44
import App from "./App.tsx";
55
import "./index.css";
6+
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
7+
8+
const queryClient = new QueryClient();
69

710
ReactDOM.createRoot(document.getElementById("root")!).render(
811
<React.StrictMode>
12+
<QueryClientProvider client={queryClient}>
913
<App />
14+
</QueryClientProvider>
1015
</React.StrictMode>
1116
);

gs/frontend/mcc/src/mission-commands/mission-commands.tsx

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,55 @@
11
import InputGroup from "react-bootstrap/InputGroup";
22
import Form from "react-bootstrap/Form";
33
import Table from "react-bootstrap/Table";
4-
import { useEffect, useRef, useState } from "react";
4+
import { useRef, useState } from "react";
5+
import { useQuery, useMutation } from '@tanstack/react-query';
56

67
const MISSON_COMMAND_PREFIX = "MCC_";
78

89
function MissionCommands() {
9-
const [commands, setCommands] = useState("");
10-
const [commandResponse, setCommandResponse] = useState("");
1110
const inputRef = useRef(null);
1211

13-
useEffect(() => {
14-
try {
15-
const command = localStorage.getItem(MISSON_COMMAND_PREFIX + "command");
16-
const response = localStorage.getItem(MISSON_COMMAND_PREFIX + "response");
17-
if (!command) return;
18-
if (!response) return;
19-
setCommands(command);
20-
setCommandResponse(response);
21-
} catch (error) {
22-
console.error(error);
23-
}
24-
}, []);
12+
const { data: savedData } = useQuery({
13+
queryKey: ["missionCommand"],
14+
queryFn: () => {
15+
const command = localStorage.getItem(MISSON_COMMAND_PREFIX + "command") || "";
16+
const response = localStorage.getItem(MISSON_COMMAND_PREFIX + "response") || "";
17+
return { command, response };
18+
},
19+
initialData: {
20+
command: localStorage.getItem(MISSON_COMMAND_PREFIX + "command") || "",
21+
response: localStorage.getItem(MISSON_COMMAND_PREFIX + "response") || "",
22+
},
23+
});
24+
25+
const [commands, setCommands] = useState(savedData.command);
2526

2627
// const handleChange = (event) => {
2728
// setCommands(event.target.value);
2829
// localStorage.setItem(MISSON_COMMAND_PREFIX + "command", event.target.value);
2930
// }
30-
31-
const handleCommand = () => {
32-
try {
33-
const requestOptions = {
31+
const mutation = useMutation({
32+
mutationFn: async (command: string) => {
33+
const response = await fetch("http://localhost:5000/mission-control", {
3434
method: "POST",
3535
headers: { "Content-Type": "application/json" },
36-
body: JSON.stringify({ command: commands }),
37-
};
38-
fetch("http://localhost:5000/mission-control", requestOptions)
39-
.then((response) => response.json())
40-
.then((data) => {
41-
setCommandResponse(data.response);
42-
localStorage.setItem(
43-
MISSON_COMMAND_PREFIX + "response",
44-
data.response,
45-
);
46-
});
47-
} catch (error) {
48-
console.error(error);
49-
}
36+
body: JSON.stringify({ command }),
37+
});
38+
if (!response.ok) {
39+
throw new Error(`Server error: ${response.status}`);
40+
}
41+
return response.json();
42+
},
43+
onSuccess: (data) => {
44+
localStorage.setItem(
45+
MISSON_COMMAND_PREFIX + "response",
46+
data.response
47+
);
48+
},
49+
});
50+
51+
const handleCommand = () => {
52+
mutation.mutate(commands);
5053
};
5154

5255
const clear = () => {
@@ -56,8 +59,7 @@ function MissionCommands() {
5659
console.error(error);
5760
}
5861
setCommands("");
59-
setCommandResponse("");
60-
// inputRef.current.value = ''
62+
mutation.reset();
6163
};
6264

6365
return (
@@ -80,7 +82,13 @@ function MissionCommands() {
8082
</tr>
8183
</thead>
8284
<tbody>
83-
<tr>{commandResponse && <td>{commandResponse}</td>}</tr>
85+
<tr>
86+
<td>
87+
{mutation.status === "pending" && "Loading..."}
88+
{mutation.status === "error" && `Error: ${(mutation.error as Error).message}`}
89+
{mutation.status === "success" && mutation.data.response}
90+
</td>
91+
</tr>
8492
</tbody>
8593
</Table>
8694
<button onClick={clear}>Clear</button>

0 commit comments

Comments
 (0)