|
1 | | -import { useState } from "react"; |
2 | | -import { MuiFileInput } from "mui-file-input"; |
| 1 | +import { useContext } from "react"; |
3 | 2 | import { Button, MenuItem } from "@mui/material"; |
4 | 3 | import FileUploadRoundedIcon from "@mui/icons-material/FileUploadRounded"; |
5 | | -import { RowModel, DraggableStateModel, TransitionModel } from "../models"; |
6 | 4 | import { AutomataData } from "../components/types/AutomataData"; |
| 5 | +import { DataContext } from "../components/Editor"; |
| 6 | +import { TransitionModel } from "../models"; |
| 7 | +import { StyledTransitionLabel } from "./components/playground/StyledTransitionLabel"; |
7 | 8 |
|
8 | 9 | export const Upload = ({ |
9 | 10 | handleCloseToolsMenu, |
10 | 11 | }: { |
11 | 12 | handleCloseToolsMenu: () => void; |
12 | 13 | }) => { |
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 | + }; |
14 | 26 |
|
15 | 27 | const handleFileChange = (event: any) => { |
16 | 28 | 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]; |
24 | 31 |
|
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 | + } |
33 | 39 |
|
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); |
36 | 42 |
|
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 | + } |
42 | 48 |
|
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 | + } |
49 | 55 |
|
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 | + } |
53 | 66 | handleCloseToolsMenu(); |
54 | 67 | }; |
55 | 68 |
|
56 | | - // const handleChange = (newFile: File) => { |
57 | | - // console.log("newFile", newFile); |
58 | | - // setFile(newFile); |
59 | | - // }; |
60 | | - |
61 | 69 | return ( |
62 | | - <MenuItem |
63 | | - // onClick={() => { |
64 | | - // // handleCloseToolsMenu(); |
65 | | - // }} |
66 | | - > |
67 | | - {/* <MuiFileInput value={file} onChange={handleChange} /> |
68 | | - Upload */} |
| 70 | + <MenuItem> |
69 | 71 | <Button |
70 | 72 | variant="text" |
71 | 73 | component="label" |
|
0 commit comments