Skip to content

Commit fbdfb68

Browse files
committed
added suitable errors for all tools
1 parent 7f7790a commit fbdfb68

File tree

3 files changed

+81
-37
lines changed

3 files changed

+81
-37
lines changed

src/common/ErrorSnackbar.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Snackbar, Alert, AlertTitle } from "@mui/material";
2+
import { ErrorSnackbarProps } from "./props/ErrorSnackbarProps";
3+
4+
export const ErrorSnackbar = (props: ErrorSnackbarProps) => {
5+
return (
6+
<Snackbar
7+
open={true}
8+
autoHideDuration={6000}
9+
onClose={props.handleErrorSnackbarClose}
10+
anchorOrigin={{
11+
vertical: "top",
12+
horizontal: "center",
13+
}}
14+
>
15+
<Alert severity="error">
16+
<AlertTitle>{props?.titleMessage}</AlertTitle>
17+
{props?.bodyMessage}
18+
</Alert>
19+
</Snackbar>
20+
);
21+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export type ErrorSnackbarProps = {
2+
handleErrorSnackbarClose: () => void;
3+
titleMessage?: string;
4+
bodyMessage?: string;
5+
};

src/components/Editor.tsx

Lines changed: 55 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import {
4545
StateMaxSize,
4646
StateMinSize,
4747
} from "../consts/StateSizes";
48+
import { ErrorSnackbar } from "../common/ErrorSnackbar";
4849

4950
export const DataContext = createContext<AutomataData>({} as AutomataData);
5051

@@ -613,6 +614,10 @@ export const Editor = () => {
613614
}
614615
};
615616

617+
const handleErrorSnackbarClose = () => {
618+
setToolSelected(null);
619+
};
620+
616621
const transitionTableProps: TransitionTableProps = {
617622
rows,
618623
columns,
@@ -767,46 +772,59 @@ export const Editor = () => {
767772
</Box>
768773

769774
{toolSelected && toolSelected === AvailableTools.IS_DFA && (
770-
<Snackbar
771-
open={true}
772-
autoHideDuration={6000}
773-
onClose={() => setToolSelected(null)}
774-
anchorOrigin={{
775-
vertical: "top",
776-
horizontal: "center",
777-
}}
778-
>
779-
<Alert severity={IsDFA(rows)?.[0] ? "success" : "error"}>
780-
<AlertTitle>{AvailableTools.IS_DFA}</AlertTitle>
781-
{IsDFA(rows)?.[1]}
782-
</Alert>
783-
</Snackbar>
775+
<ErrorSnackbar
776+
handleErrorSnackbarClose={handleErrorSnackbarClose}
777+
titleMessage={AvailableTools.IS_DFA}
778+
bodyMessage={IsDFA(rows)?.[1] as string}
779+
/>
784780
)}
781+
785782
{toolSelected && toolSelected === AvailableTools.IS_NFA && (
786-
<Snackbar
787-
open={true}
788-
autoHideDuration={6000}
789-
onClose={() => setToolSelected(null)}
790-
anchorOrigin={{
791-
vertical: "top",
792-
horizontal: "center",
793-
}}
794-
>
795-
<Alert severity={IsNFA(rows)?.[0] ? "success" : "error"}>
796-
<AlertTitle>{AvailableTools.IS_NFA}</AlertTitle>
797-
{IsNFA(rows)?.[1]}
798-
</Alert>
799-
</Snackbar>
800-
)}
801-
{toolSelected && toolSelected === AvailableTools.NFA_TO_DFA && (
802-
<NfaToDfa />
803-
)}
804-
{toolSelected && toolSelected === AvailableTools.MINIMIZE_DFA && (
805-
<MinimizeDfa />
806-
)}
807-
{toolSelected && toolSelected === AvailableTools.TEST_A_STRING && (
808-
<TestAString {...testAStringProps} />
783+
<ErrorSnackbar
784+
handleErrorSnackbarClose={handleErrorSnackbarClose}
785+
titleMessage={AvailableTools.IS_NFA}
786+
bodyMessage={IsNFA(rows)?.[1] as string}
787+
/>
809788
)}
789+
790+
{toolSelected &&
791+
toolSelected === AvailableTools.NFA_TO_DFA &&
792+
(IsNFA(rows)?.[0] ? (
793+
<NfaToDfa />
794+
) : IsDFA(rows)?.[0] ? (
795+
<ErrorSnackbar
796+
handleErrorSnackbarClose={handleErrorSnackbarClose}
797+
titleMessage="The Automaton is already DFA."
798+
/>
799+
) : (
800+
<ErrorSnackbar
801+
handleErrorSnackbarClose={handleErrorSnackbarClose}
802+
titleMessage="Please create a valid Automaton for conversion."
803+
/>
804+
))}
805+
806+
{toolSelected &&
807+
toolSelected === AvailableTools.MINIMIZE_DFA &&
808+
(IsDFA(rows)?.[0] ? (
809+
<MinimizeDfa />
810+
) : (
811+
<ErrorSnackbar
812+
handleErrorSnackbarClose={handleErrorSnackbarClose}
813+
titleMessage="The Automaton is not DFA."
814+
/>
815+
))}
816+
817+
{toolSelected &&
818+
toolSelected === AvailableTools.TEST_A_STRING &&
819+
(IsNFA(rows)?.[0] || IsDFA(rows)?.[0] ? (
820+
<TestAString {...testAStringProps} />
821+
) : (
822+
<ErrorSnackbar
823+
handleErrorSnackbarClose={handleErrorSnackbarClose}
824+
titleMessage="Please create a valid Automaton for testing."
825+
/>
826+
))}
827+
810828
{toolSelected &&
811829
toolSelected === AvailableTools.HIGHLIGHT_NULL_TRANSITIONS &&
812830
handleHighlightNullTransitions()}

0 commit comments

Comments
 (0)