Skip to content

Commit 95bbee1

Browse files
committed
completed 'Download' and 'Upload' feature
1 parent 147fd6a commit 95bbee1

File tree

11 files changed

+90
-95
lines changed

11 files changed

+90
-95
lines changed

src/components/Editor.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,9 +549,14 @@ export const Editor = () => {
549549
return (
550550
<DataContext.Provider
551551
value={{
552+
rowId,
553+
setRowId,
552554
rows,
555+
setRows,
553556
states,
557+
setStates,
554558
transitions,
559+
setTransitions,
555560
}}
556561
>
557562
<>

src/components/Navbar.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as React from "react";
21
import AppBar from "@mui/material/AppBar";
32
import Box from "@mui/material/Box";
43
import Toolbar from "@mui/material/Toolbar";
@@ -7,23 +6,20 @@ import Typography from "@mui/material/Typography";
76
import Menu from "@mui/material/Menu";
87
import MenuIcon from "@mui/icons-material/Menu";
98
import Container from "@mui/material/Container";
10-
import MoreIcon from "@mui/icons-material/MoreVert";
119
import Button from "@mui/material/Button";
12-
import Tooltip from "@mui/material/Tooltip";
1310
import MenuItem from "@mui/material/MenuItem";
1411
import AdbIcon from "@mui/icons-material/Adb";
1512
import { Pages } from "../enums/Pages";
1613
import { Link as RouterLink } from "react-router-dom";
1714
import Link from "@mui/material/Link";
18-
import { useContext } from "react";
19-
import { DataContext } from "./Editor";
15+
import { useState } from "react";
2016

2117
const pages = Object.values(Pages);
2218

2319
export function NavBar() {
2420
console.log("re rendering Navbar");
2521

26-
const [anchorElNav, setAnchorElNav] = React.useState<null | HTMLElement>(
22+
const [anchorElNav, setAnchorElNav] = useState<null | HTMLElement>(
2723
null
2824
);
2925

@@ -35,8 +31,6 @@ export function NavBar() {
3531
setAnchorElNav(null);
3632
};
3733

38-
const data = useContext(DataContext);
39-
4034
return (
4135
<AppBar position="static">
4236
<Container maxWidth="xl">
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { RowModel, DraggableStateModel, TransitionModel } from "../../models";
22

33
export type AutomataData = {
4+
rowId: number;
5+
setRowId: React.Dispatch<React.SetStateAction<number>>;
46
rows: RowModel[];
7+
setRows: React.Dispatch<React.SetStateAction<RowModel[]>>;
58
states: DraggableStateModel[];
9+
setStates: React.Dispatch<React.SetStateAction<DraggableStateModel[]>>;
610
transitions: TransitionModel[];
11+
setTransitions: React.Dispatch<React.SetStateAction<TransitionModel[]>>;
712
};

src/features/Download.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,22 @@ export const Download = ({
88
}: {
99
handleCloseToolsMenu: () => void;
1010
}) => {
11-
const data = useContext(DataContext);
11+
const dataContext = useContext(DataContext);
1212

1313
return (
1414
<MenuItem
1515
onClick={() => {
1616
handleCloseToolsMenu();
17-
if (!data) {
17+
if (!dataContext) {
1818
console.log("no data.");
1919
return;
2020
}
21-
if (data?.states?.length === 0) {
21+
if (dataContext?.states?.length === 0) {
2222
alert("no data to download.");
2323
return;
2424
}
2525
const element = document.createElement("a");
26-
const file = new Blob([JSON.stringify(data)], {
26+
const file = new Blob([JSON.stringify(dataContext)], {
2727
type: "application/json",
2828
});
2929
element.href = URL.createObjectURL(file);

src/features/Playground.tsx

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ import State from "./components/playground/State";
88
import Xarrow from "./components/playground/Xarrow";
99
import useElementSize from "./hooks/useElementSize";
1010
import { useEffect } from "react";
11+
import { XarrowProps } from "./components/playground/props/XarrowProps";
1112

1213
const Playground = (props: PlaygroundProps) => {
1314
console.log("re rendering Playground: props", props);
1415

1516
const [boxRef, { width, height }] = useElementSize();
17+
const { setSize } = props;
18+
1619
useEffect(() => {
1720
console.log("useEffect of playground due to width & height", width, height);
18-
props.setSize({ width, height });
19-
}, [width, height]);
21+
setSize({ width, height });
22+
}, [width, height, setSize]);
2023

2124
const topBarprops: TopBarProps = {
2225
states: props.states,
@@ -47,6 +50,11 @@ const Playground = (props: PlaygroundProps) => {
4750
setRows: props.setRows,
4851
};
4952

53+
const xarrowProps: XarrowProps = {
54+
selected: props.selected,
55+
setSelected: props.setSelected,
56+
};
57+
5058
return (
5159
<div>
5260
<Xwrapper>
@@ -59,10 +67,7 @@ const Playground = (props: PlaygroundProps) => {
5967
<div className="toolboxTitle">Drag & drop me!</div>
6068
<hr />
6169
<div className="toolboxContainer">
62-
<div
63-
className="state"
64-
draggable
65-
>
70+
<div className="state" draggable>
6671
state
6772
</div>
6873
</div>
@@ -89,10 +94,9 @@ const Playground = (props: PlaygroundProps) => {
8994
{/* xarrow connections*/}
9095
{props.transitions.map((transition, i) => (
9196
<Xarrow
97+
{...xarrowProps}
9298
key={transition.props.start + "-" + transition.props.end + i}
9399
transition={transition}
94-
selected={props.selected}
95-
setSelected={props.setSelected}
96100
/>
97101
))}
98102
</div>

src/features/TransitionTable.tsx

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
1-
import {
2-
Box,
3-
Button,
4-
Grid,
5-
IconButton,
6-
Menu,
7-
MenuItem,
8-
Tooltip,
9-
} from "@mui/material";
1+
import { Box, Button, Grid, IconButton, Menu, Tooltip } from "@mui/material";
102
import { DataGrid } from "@mui/x-data-grid";
113
import { darken, lighten } from "@mui/material/styles";
124
import { TransitionTableProps } from "./props/TransitionTableProps";
135
import { RowModel } from "../models";
14-
import { useEffect, useState } from "react";
6+
import { useState } from "react";
157
import { MaxNumberOfStates } from "../consts/MaxNumberOfStates";
168
import MoreIcon from "@mui/icons-material/MoreVert";
179
import { Download } from "./Download";
@@ -33,13 +25,6 @@ const TransitionTable = (props: TransitionTableProps) => {
3325
setAnchorElUser(null);
3426
};
3527

36-
useEffect(() => {
37-
console.log(
38-
"useEffect of transition table due to props.rows: props",
39-
props
40-
);
41-
}, [props.rows]);
42-
4328
return (
4429
<>
4530
<Grid container alignItems={"center"}>

src/features/Upload.tsx

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,73 @@
1-
import { useState } from "react";
2-
import { MuiFileInput } from "mui-file-input";
1+
import { useContext } from "react";
32
import { Button, MenuItem } from "@mui/material";
43
import FileUploadRoundedIcon from "@mui/icons-material/FileUploadRounded";
5-
import { RowModel, DraggableStateModel, TransitionModel } from "../models";
64
import { AutomataData } from "../components/types/AutomataData";
5+
import { DataContext } from "../components/Editor";
6+
import { TransitionModel } from "../models";
7+
import { StyledTransitionLabel } from "./components/playground/StyledTransitionLabel";
78

89
export const Upload = ({
910
handleCloseToolsMenu,
1011
}: {
1112
handleCloseToolsMenu: () => void;
1213
}) => {
13-
const [data, setData] = useState<AutomataData>({} as AutomataData);
14+
const dataContext = useContext(DataContext);
15+
16+
const handleSetTransitions = (transitions: TransitionModel[]) => {
17+
transitions.forEach((transition) => {
18+
transition.props.labels =
19+
transition.props.value === "" ? (
20+
""
21+
) : (
22+
<StyledTransitionLabel label={transition.props.value} />
23+
);
24+
});
25+
};
1426

1527
const handleFileChange = (event: any) => {
1628
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-
}
29+
if (event?.target?.files?.length > 0) {
30+
const newFile: File = event?.target?.files[0];
2431

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-
}
32+
const reader = new FileReader();
33+
reader.onload = (e) => {
34+
// if no result, return
35+
if (!e?.target?.result) {
36+
alert("Failed reading data.");
37+
return;
38+
}
3339

34-
const rawData = JSON.parse(e.target?.result as string);
35-
console.log("raw data", rawData);
40+
const rawData = JSON.parse(e.target?.result as string);
41+
console.log("raw data", rawData);
3642

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-
}
43+
// data is corrupted if there are more or less arrays
44+
if (Object.keys(rawData)?.length !== 4) {
45+
alert("Data is corrupted.");
46+
return;
47+
}
4248

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+
const data = rawData as AutomataData;
50+
if (!data?.rows || data?.rows?.length <= 0) {
51+
// if no rows found in data, return
52+
alert("No data found");
53+
return;
54+
}
4955

50-
console.log("Successfuly uploaded data.");
51-
setData(data);
52-
};
56+
dataContext?.setRowId(data.rowId);
57+
dataContext?.setRows(data.rows);
58+
dataContext?.setStates(data.states);
59+
handleSetTransitions(data.transitions);
60+
dataContext?.setTransitions(data.transitions);
61+
console.log("Successfuly uploaded data.");
62+
};
63+
reader.readAsText(newFile);
64+
event.target.value = "";
65+
}
5366
handleCloseToolsMenu();
5467
};
5568

56-
// const handleChange = (newFile: File) => {
57-
// console.log("newFile", newFile);
58-
// setFile(newFile);
59-
// };
60-
6169
return (
62-
<MenuItem
63-
// onClick={() => {
64-
// // handleCloseToolsMenu();
65-
// }}
66-
>
67-
{/* <MuiFileInput value={file} onChange={handleChange} />
68-
Upload */}
70+
<MenuItem>
6971
<Button
7072
variant="text"
7173
component="label"

src/features/components/playground/State.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import React, { useEffect, useState } from "react";
21
import "./css/Box.css";
32
import Draggable from "react-draggable";
43
import { useXarrow } from "react-xarrows";
@@ -106,7 +105,7 @@ export const State = (props: any) => {
106105
}
107106

108107
return (
109-
<React.Fragment>
108+
<>
110109
<Draggable
111110
onStart={props.position !== "static" ? () => {} : undefined}
112111
bounds="parent"
@@ -127,7 +126,7 @@ export const State = (props: any) => {
127126
{props.state.name ? props.state.name : props.state.id}
128127
</div>
129128
</Draggable>
130-
</React.Fragment>
129+
</>
131130
);
132131
};
133132

src/features/components/playground/TopBar.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import "./css/TopBar.css";
22
import { TopBarProps } from "./props/TopBarProps";
3-
import {
4-
DraggableStateModel,
5-
RowModel,
6-
TransitionModel,
7-
} from "../../../models";
3+
import { RowModel } from "../../../models";
84
import { SelectedElementTypeId } from "../../props/SelectedElementType";
95
import { promptNewStateName } from "../../../utils/PromptNewStateName";
106
import { promptNewTransitionValue } from "../../../utils/PromptNewTransitionValue";

src/features/components/playground/Xarrow.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import React, { useState } from "react";
1+
import { useState } from "react";
22
import Xarrow from "react-xarrows";
3-
import { TransitionModel } from "../../../models";
43

5-
//{props: {transition, setSelected, selected}}
64
export default ({ setSelected, selected, transition: { props } }: any) => {
75
console.log("re rendering Xarrow: props", props);
86
const [state, setState] = useState({ color: "coral" });

0 commit comments

Comments
 (0)