Skip to content

Commit 7f7790a

Browse files
committed
changed js prompt to Material Dialog when dropping state in Editor
1 parent c716600 commit 7f7790a

File tree

1 file changed

+83
-14
lines changed

1 file changed

+83
-14
lines changed

src/components/Editor.tsx

Lines changed: 83 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@ import {
44
AlertTitle,
55
Box,
66
Button,
7+
Dialog,
8+
DialogActions,
9+
DialogContent,
10+
DialogContentText,
11+
DialogTitle,
712
Grid,
813
Slider,
914
Snackbar,
15+
TextField,
1016
} from "@mui/material";
1117
import { GridColumns, GridActionsCellItem } from "@mui/x-data-grid";
1218
import DeleteIcon from "@mui/icons-material/Delete";
@@ -18,7 +24,6 @@ import { PlaygroundProps } from "../features/props/PlaygroundProps";
1824
import { SelectedElementType } from "../features/props/SelectedElementType";
1925
import { TransitionTableProps } from "../features/props/TransitionTableProps";
2026
import TransitionTable from "../features/TransitionTable";
21-
import { promptNewStateName } from "../utils/PromptNewStateName";
2227
import { PossibleTransitionValues } from "../consts/PossibleTransitionValues";
2328
import { StateNameMaxLength } from "../consts/StateNameMaxLength";
2429
import { PlaygroundSize } from "./types/PlaygroundSize";
@@ -144,6 +149,14 @@ export const Editor = () => {
144149

145150
const [alertMessage, setAlertMessage] = useState("");
146151

152+
const [isStateNameDialogOpen, setIsStateNameDialogOpen] = useState(false);
153+
const [stateNameDialogValue, setStateNameDialogValue] = useState("");
154+
const [stateNameDialogError, setStateNameDialogError] = useState("");
155+
const [{ stateX, stateY }, setStateDropPosition] = useState({
156+
stateX: 0,
157+
stateY: 0,
158+
});
159+
147160
const handleAddRow = (row: RowModel) => {
148161
if (states.length >= MaxNumberOfStates) {
149162
setAlertMessage(`Maximum ${MaxNumberOfStates} states allowed.`);
@@ -520,19 +533,9 @@ export const Editor = () => {
520533
stateY = e.clientY - rect.y;
521534
}
522535

523-
const stateName = promptNewStateName(states, `q${rowId}`);
524-
525-
if (stateName) {
526-
const newState = new DraggableStateModel(stateName, stateX, stateY);
527-
setStates((prev) => [...prev, newState]);
528-
}
529-
530-
setRows((prev) => [
531-
...prev,
532-
new RowModel(rowId, stateName, "", "", "", false, false),
533-
]);
534-
535-
setRowId((prev) => prev + 1);
536+
setStateNameDialogValue(`q${rowId}`);
537+
setIsStateNameDialogOpen(true);
538+
setStateDropPosition({ stateX, stateY });
536539
};
537540

538541
const handleHighlightNullTransitions = () => {
@@ -571,6 +574,45 @@ export const Editor = () => {
571574
);
572575
};
573576

577+
const handleStateNameDialogValue = (e: any) => {
578+
if (!stateNameDialogValue)
579+
setStateNameDialogError("Name cannot be empty, choose another one: ");
580+
else if (
581+
stateNameDialogValue &&
582+
[...states].map((s) => s.id).includes(stateNameDialogValue)
583+
)
584+
setStateNameDialogError("Name already taken, choose another one: ");
585+
else if (stateNameDialogValue.length > StateNameMaxLength)
586+
setStateNameDialogError(
587+
`State name cannot be more than ${StateNameMaxLength} characters.`
588+
);
589+
else if (PossibleTransitionValues.includes(stateNameDialogValue))
590+
setStateNameDialogError(
591+
`State name cannot be one of the following: ${PossibleTransitionValues.join(
592+
", "
593+
)}`
594+
);
595+
else {
596+
setStateNameDialogError("");
597+
setIsStateNameDialogOpen(false);
598+
setStateNameDialogValue(stateNameDialogValue);
599+
600+
const newState = new DraggableStateModel(
601+
stateNameDialogValue,
602+
stateX,
603+
stateY
604+
);
605+
setStates((prev) => [...prev, newState]);
606+
607+
setRows((prev) => [
608+
...prev,
609+
new RowModel(rowId, stateNameDialogValue, "", "", "", false, false),
610+
]);
611+
612+
setRowId((prev) => prev + 1);
613+
}
614+
};
615+
574616
const transitionTableProps: TransitionTableProps = {
575617
rows,
576618
columns,
@@ -619,6 +661,33 @@ export const Editor = () => {
619661
}}
620662
>
621663
<>
664+
<Dialog
665+
open={isStateNameDialogOpen}
666+
onClose={() => setIsStateNameDialogOpen(false)}
667+
>
668+
<DialogTitle>Enter New State Name</DialogTitle>
669+
<DialogContent>
670+
<DialogContentText>{stateNameDialogError}</DialogContentText>
671+
<TextField
672+
autoFocus
673+
margin="dense"
674+
id="testString"
675+
label="State Name"
676+
type="text"
677+
fullWidth
678+
variant="standard"
679+
value={stateNameDialogValue}
680+
onChange={(e) => setStateNameDialogValue(e.target.value)}
681+
/>
682+
</DialogContent>
683+
<DialogActions>
684+
<Button onClick={() => setIsStateNameDialogOpen(false)}>
685+
Cancel
686+
</Button>
687+
<Button onClick={handleStateNameDialogValue}>Ok</Button>
688+
</DialogActions>
689+
</Dialog>
690+
622691
{alertMessage !== "" && (
623692
<Snackbar
624693
open={true}

0 commit comments

Comments
 (0)