Skip to content

Commit b6f6496

Browse files
committed
feat: add confirmation dialog
1 parent 4bd1617 commit b6f6496

File tree

4 files changed

+73
-8
lines changed

4 files changed

+73
-8
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import {
2+
Button,
3+
Dialog,
4+
DialogActions,
5+
DialogContent,
6+
DialogContentText,
7+
DialogTitle,
8+
makeStyles
9+
} from "@material-ui/core";
10+
import { redStyle } from "../utils/styles";
11+
12+
interface Props {
13+
open: boolean;
14+
onCancel: () => void;
15+
onConfirm: () => void;
16+
text: string;
17+
}
18+
19+
const useStyles = makeStyles(theme => ({
20+
red: redStyle(theme)
21+
}));
22+
23+
function ConfirmationDialog(props: Props) {
24+
const classes = useStyles();
25+
26+
return (
27+
<Dialog
28+
open={props.open}
29+
onClose={props.onCancel}
30+
>
31+
<DialogTitle>Confirm</DialogTitle>
32+
<DialogContent>
33+
<DialogContentText>{props.text}</DialogContentText>
34+
</DialogContent>
35+
<DialogActions>
36+
<Button onClick={props.onCancel}>
37+
Cancel
38+
</Button>
39+
<Button className={classes.red} onClick={props.onConfirm}>
40+
Yes
41+
</Button>
42+
</DialogActions>
43+
</Dialog>
44+
);
45+
}
46+
47+
export default ConfirmationDialog;

src/components/Layout.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import {
2121
import SettingsDialog from "./SettingsDialog";
2222
import { blue } from '@material-ui/core/colors';
2323

24-
type Props = {
25-
children?: any
24+
interface Props {
25+
children?: any;
2626
};
2727

2828
const useStyles = makeStyles(theme => ({

src/components/SettingsDialog.tsx

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ import {
2828
Export as ExportIcon,
2929
Delete as DeleteIcon
3030
} from "mdi-material-ui";
31+
import ConfirmationDialog from "./ConfirmationDialog";
3132

3233
interface Props {
33-
open: boolean,
34-
onClose: () => void
34+
open: boolean;
35+
onClose: () => void;
3536
}
3637

3738
const useStyles = makeStyles(theme => ({
@@ -72,6 +73,8 @@ function SettingsDialog(props: Props) {
7273
const [server, setServer] = useState(settings.server ?? "");
7374
const [password, setPassword] = useState("");
7475
const [oldToken, setOldToken] = useState(settings.token ?? "");
76+
const [confirmationText, setConfirmationText] = useState("");
77+
const [confirmationDialog, setConfirmationDialog] = useState(false);
7578

7679
const reset = () => {
7780
setMaxPriorities(rootState.settings.maxPriorities.toString());
@@ -114,14 +117,22 @@ function SettingsDialog(props: Props) {
114117
], { type: "text/plain;charset=utf-8" });
115118
FileSaver.saveAs(blob, "task.json");
116119
};
117-
120+
118121
const clearData = () => {
119122
dispatch(rootActions.setTaskJson(initTaskJson()));
120123
dispatch(rootActions.addNotification({
121124
severity: "success",
122125
text: "Successfully cleared data"
123-
}))
126+
}));
127+
};
128+
const handleClearCancel = () => {
129+
setConfirmationDialog(false);
124130
};
131+
const handleClearData = () => {
132+
setConfirmationText("Warning: This will clear all the data in the local storage. Are you sure to clear?");
133+
setConfirmationDialog(true);
134+
};
135+
125136
const login = () => {
126137
dispatch(asyncActions.login({
127138
server,
@@ -152,6 +163,13 @@ function SettingsDialog(props: Props) {
152163
>
153164
<DialogTitle className={classes.title}>Settings</DialogTitle>
154165
<DialogContent>
166+
<ConfirmationDialog
167+
open={confirmationDialog}
168+
text={confirmationText}
169+
onCancel={handleClearCancel}
170+
onConfirm={clearData}
171+
/>
172+
155173
<List>
156174
<div className={classes.divider}>
157175
<Typography color="textSecondary" className={classes.subtitle}>
@@ -226,7 +244,7 @@ function SettingsDialog(props: Props) {
226244
</IconButton>
227245
</label>
228246
<IconButton
229-
onClick={clearData}
247+
onClick={handleClearData}
230248
className={`${classes.dataButton} ${classes.red}`}
231249
title="Clear"
232250
>

src/utils/styles.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const redBgStyle = (theme: Theme): CSSProperties => ({
1010
}
1111
});
1212

13-
export const redStyle = (theme: Theme): CSSProperties => ({
13+
export const redStyle = (_theme: Theme): CSSProperties => ({
1414
color: red[500],
1515
"&:hover": {
1616
backgroundColor: fade(red[500], 0.05)

0 commit comments

Comments
 (0)