summaryrefslogtreecommitdiff
path: root/frontend/src/components/Sidebar.tsx
blob: 8bd795b93f68faa0971b4604a675057d0cc84df3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { useState, type Dispatch, type SetStateAction } from "react";
import API_URL from "../config";
import "../styles/button.css";
import "../styles/sidebar.css";
import type { Robot } from "../types/robot";
import AddRobotForm from "./AddRobotForm";
import RobotList from "./RobotList";
import SimulationActions from "./SimulationActions";

type Props = {
    errorMessage: string;
    robots: Robot[];
    token: string | null;
    setErrorMessage: Dispatch<SetStateAction<string>>;
};

function Sidebar({ errorMessage, robots, token, setErrorMessage }: Props) {
    const [isAddingRobot, setIsAddingRobot] = useState(false);

    function handleAddClick() {
        setIsAddingRobot(true);
        setErrorMessage("");
    }

    return (
        <div className="sidebar">
            <div className="sidebar-robots-header">
                <h2>Your Robots</h2>
                <button
                    className="btn btn-add-robot"
                    onClick={handleAddClick}
                    disabled={isAddingRobot}
                >
                    + Add
                </button>
            </div>

           {isAddingRobot && (
                <AddRobotForm
                    apiUrl={API_URL}
                    errorMessage={errorMessage}
                    token={token}
                    setErrorMessage={setErrorMessage}
                    setIsAddingRobot={setIsAddingRobot}
                />
            )}

            {errorMessage && (
                <div className="error-message">{errorMessage}</div>
            )}

            <SimulationActions
                apiUrl={API_URL}
                token={token}
                setErrorMessage={setErrorMessage}
            />

            <RobotList robots={robots} />
        </div>
    );
}

export default Sidebar;