Skip to content

Commit 147fd6a

Browse files
committed
started 'Upload' feature
1 parent e320402 commit 147fd6a

File tree

13 files changed

+122
-102
lines changed

13 files changed

+122
-102
lines changed

package-lock.json

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

package.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
"react-collapsible": "^2.10.0",
2424
"react-dom": "^18.2.0",
2525
"react-draggable": "^4.4.5",
26-
"react-numeric-input": "^2.2.3",
2726
"react-router-dom": "^6.4.3",
2827
"react-scripts": "5.0.1",
2928
"react-xarrows": "^2.0.2",
@@ -56,8 +55,5 @@
5655
"last 1 firefox version",
5756
"last 1 safari version"
5857
]
59-
},
60-
"devDependencies": {
61-
"@types/react-numeric-input": "^2.2.4"
6258
}
6359
}

src/components/Editor.tsx

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,16 @@ import TransitionTable from "../features/TransitionTable";
1313
import { promptNewStateName } from "../utils/PromptNewStateName";
1414
import { PossibleTransitionValues } from "../consts/PossibleTransitionValues";
1515
import { StateNameMaxLength } from "../consts/StateNameMaxLength";
16-
import { PlaygroundSize } from "./interfaces/playgroundSize";
16+
import { PlaygroundSize } from "./types/PlaygroundSize";
1717
import { StyledTransitionLabel } from "../features/components/playground/StyledTransitionLabel";
1818
import { MaxNumberOfStates } from "../consts/MaxNumberOfStates";
19+
import { AutomataData } from "./types/AutomataData";
1920

20-
export const DataContext = createContext<{
21-
rows: RowModel[];
22-
columns: GridColumns;
23-
states: DraggableStateModel[];
24-
transitions: TransitionModel[];
25-
}>(
26-
{} as {
27-
rows: RowModel[];
28-
columns: GridColumns;
29-
states: DraggableStateModel[];
30-
transitions: TransitionModel[];
31-
}
32-
);
21+
export const DataContext = createContext<AutomataData>({} as AutomataData);
3322

3423
export const Editor = () => {
3524
console.log("re rendering Editor");
3625

37-
// const [tempState, setTempState] = useState(23);
3826
const [rowId, setRowId] = useState(0);
3927
const [rows, setRows] = useState<RowModel[]>([]);
4028
const columns: GridColumns = [
@@ -125,13 +113,12 @@ export const Editor = () => {
125113
size.width,
126114
size.height
127115
);
128-
let newstate = {
129-
id: row.state,
130-
x: Math.floor(Math.random() * size.width),
131-
y: Math.floor(Math.random() * size.height),
132-
shape: "state",
133-
};
134-
setStates((prev) => [...prev, newstate]);
116+
const newState = new DraggableStateModel(
117+
row.state,
118+
Math.floor(Math.random() * size.width),
119+
Math.floor(Math.random() * size.height)
120+
);
121+
setStates((prev) => [...prev, newState]);
135122
};
136123

137124
const handleDeleteRow = (row: RowModel) => {
@@ -514,18 +501,12 @@ export const Editor = () => {
514501
let { x, y } = e.target.getBoundingClientRect();
515502
const stateName = promptNewStateName(states, `q${rowId}`);
516503
if (stateName) {
517-
let newState = new DraggableStateModel(
504+
const newState = new DraggableStateModel(
518505
stateName,
519506
e.clientX - x,
520507
e.clientY - y
521508
);
522-
let newstate = {
523-
id: stateName,
524-
x: e.clientX - x,
525-
y: e.clientY - y,
526-
shape: "state",
527-
};
528-
setStates([...states, newstate]);
509+
setStates([...states, newState]);
529510
}
530511
console.log("states", states);
531512

@@ -569,7 +550,6 @@ export const Editor = () => {
569550
<DataContext.Provider
570551
value={{
571552
rows,
572-
columns,
573553
states,
574554
transitions,
575555
}}

src/components/Navbar.tsx

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,15 @@ export function NavBar() {
2626
const [anchorElNav, setAnchorElNav] = React.useState<null | HTMLElement>(
2727
null
2828
);
29-
const [anchorElUser, setAnchorElUser] = React.useState<null | HTMLElement>(
30-
null
31-
);
3229

3330
const handleOpenNavMenu = (event: React.MouseEvent<HTMLElement>) => {
3431
setAnchorElNav(event.currentTarget);
3532
};
3633

37-
const handleOpenUserMenu = (event: React.MouseEvent<HTMLElement>) => {
38-
setAnchorElUser(event.currentTarget);
39-
};
40-
4134
const handleCloseNavMenu = () => {
4235
setAnchorElNav(null);
4336
};
4437

45-
const handleCloseUserMenu = () => {
46-
setAnchorElUser(null);
47-
};
48-
4938
const data = useContext(DataContext);
5039

5140
return (
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { RowModel, DraggableStateModel, TransitionModel } from "../../models";
2+
3+
export type AutomataData = {
4+
rows: RowModel[];
5+
states: DraggableStateModel[];
6+
transitions: TransitionModel[];
7+
};

src/features/Download.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
import { Button, Tooltip, Menu, MenuItem } from "@mui/material";
1+
import { Button, MenuItem } from "@mui/material";
22
import { useContext } from "react";
33
import { DataContext } from "../components/Editor";
4+
import FileDownloadRoundedIcon from "@mui/icons-material/FileDownloadRounded";
45

56
export const Download = ({
6-
handleCloseUserMenu,
7+
handleCloseToolsMenu,
78
}: {
8-
handleCloseUserMenu: () => void;
9+
handleCloseToolsMenu: () => void;
910
}) => {
1011
const data = useContext(DataContext);
1112

1213
return (
1314
<MenuItem
1415
onClick={() => {
15-
handleCloseUserMenu();
16+
handleCloseToolsMenu();
1617
if (!data) {
1718
console.log("no data.");
1819
return;
@@ -31,7 +32,13 @@ export const Download = ({
3132
element.click();
3233
}}
3334
>
34-
Download
35+
<Button
36+
variant="text"
37+
component="label"
38+
startIcon={<FileDownloadRoundedIcon />}
39+
>
40+
Download
41+
</Button>
3542
</MenuItem>
3643
);
3744
};

src/features/Playground.tsx

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,12 @@ const Playground = (props: PlaygroundProps) => {
5959
<div className="toolboxTitle">Drag & drop me!</div>
6060
<hr />
6161
<div className="toolboxContainer">
62-
{/* {shapes.map((shapeName) => ( */}
6362
<div
6463
className="state"
65-
// onDragStart={(e) => {
66-
// console.log("drag start");
67-
// e.dataTransfer.setData("shape", "state");
68-
// }}
6964
draggable
7065
>
7166
state
72-
{/* <div style={{ textAlign: "center" }}> state</div>
73-
<img src={shapeName2Icon[shapeName]} alt="SwitchIcon" className={"switchIcon"} /> */}
7467
</div>
75-
{/* ))} */}
7668
</div>
7769
</div>
7870
<div

src/features/TransitionTable.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { useEffect, useState } from "react";
1515
import { MaxNumberOfStates } from "../consts/MaxNumberOfStates";
1616
import MoreIcon from "@mui/icons-material/MoreVert";
1717
import { Download } from "./Download";
18+
import { Upload } from "./Upload";
1819

1920
const getBackgroundColor = (color: string, mode: string) =>
2021
mode === "dark" ? darken(color, 0) : lighten(color, 0);
@@ -28,7 +29,7 @@ const TransitionTable = (props: TransitionTableProps) => {
2829
setAnchorElUser(event.currentTarget);
2930
};
3031

31-
const handleCloseUserMenu = () => {
32+
const handleCloseToolsMenu = () => {
3233
setAnchorElUser(null);
3334
};
3435

@@ -90,10 +91,10 @@ const TransitionTable = (props: TransitionTableProps) => {
9091
horizontal: "left",
9192
}}
9293
open={Boolean(anchorElUser)}
93-
onClose={handleCloseUserMenu}
94+
onClose={handleCloseToolsMenu}
9495
>
95-
<Download handleCloseUserMenu={handleCloseUserMenu} />
96-
<MenuItem onClick={handleCloseUserMenu}>Upload</MenuItem>
96+
<Download handleCloseToolsMenu={handleCloseToolsMenu} />
97+
<Upload handleCloseToolsMenu={handleCloseToolsMenu} />
9798
</Menu>
9899
</Box>
99100
</Grid>

src/features/Upload.tsx

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { useState } from "react";
2+
import { MuiFileInput } from "mui-file-input";
3+
import { Button, MenuItem } from "@mui/material";
4+
import FileUploadRoundedIcon from "@mui/icons-material/FileUploadRounded";
5+
import { RowModel, DraggableStateModel, TransitionModel } from "../models";
6+
import { AutomataData } from "../components/types/AutomataData";
7+
8+
export const Upload = ({
9+
handleCloseToolsMenu,
10+
}: {
11+
handleCloseToolsMenu: () => void;
12+
}) => {
13+
const [data, setData] = useState<AutomataData>({} as AutomataData);
14+
15+
const handleFileChange = (event: any) => {
16+
console.log("handleFileChange");
17+
const newFile: File = event?.target?.files[0];
18+
19+
// if any error occurs, return
20+
if (!newFile || newFile?.size <= 0) {
21+
alert("Upload failed.");
22+
return;
23+
}
24+
25+
const reader = new FileReader();
26+
reader.readAsText(newFile);
27+
reader.onload = (e) => {
28+
// if no result, return
29+
if (!e?.target?.result) {
30+
alert("Failed reading data.");
31+
return;
32+
}
33+
34+
const rawData = JSON.parse(e.target?.result as string);
35+
console.log("raw data", rawData);
36+
37+
// data is corrupted if there are more or less arrays
38+
if (Object.keys(rawData)?.length !== 3) {
39+
alert("Data is corrupted.");
40+
return;
41+
}
42+
43+
const data = rawData as AutomataData;
44+
if (!data.rows || data?.rows?.length <= 0) {
45+
// if no rows found in data, return
46+
alert("No data found");
47+
return;
48+
}
49+
50+
console.log("Successfuly uploaded data.");
51+
setData(data);
52+
};
53+
handleCloseToolsMenu();
54+
};
55+
56+
// const handleChange = (newFile: File) => {
57+
// console.log("newFile", newFile);
58+
// setFile(newFile);
59+
// };
60+
61+
return (
62+
<MenuItem
63+
// onClick={() => {
64+
// // handleCloseToolsMenu();
65+
// }}
66+
>
67+
{/* <MuiFileInput value={file} onChange={handleChange} />
68+
Upload */}
69+
<Button
70+
variant="text"
71+
component="label"
72+
startIcon={<FileUploadRoundedIcon />}
73+
>
74+
Upload
75+
<input
76+
hidden
77+
accept="application/json"
78+
type="file"
79+
onChange={handleFileChange}
80+
/>
81+
</Button>
82+
</MenuItem>
83+
);
84+
};

0 commit comments

Comments
 (0)