Skip to content

Commit

Permalink
Add confirmation when erasing all
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-drozd-it committed Jul 10, 2024
1 parent 8a371d4 commit 4f3c8c8
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 11 deletions.
16 changes: 5 additions & 11 deletions src/components/drawing/DrawingBoard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import AutoFixNormalIcon from '@mui/icons-material/AutoFixNormal';
import AbcIcon from '@mui/icons-material/Abc';
import UndoIcon from '@mui/icons-material/Undo';
import RedoIcon from '@mui/icons-material/Redo';
import DeleteIcon from '@mui/icons-material/Delete';
import CircleIcon from '@mui/icons-material/Circle';
import ErasingAllConfirmationButton from './ErasingAllConfirmationButton';

const DrawingBoard: React.FC = () => {
const canvasRef = useRef<HTMLCanvasElement>(null);
const [ canvas, setCanvas ] = useState<fabric.Canvas | null>(null);
const [ mode, setMode ] = useState<fabric.PencilBrush | null>(null); // eslint-disable-line
const [mode, setMode] = useState<fabric.PencilBrush | null>(null); // eslint-disable-line
const historyRedo: fabric.Object[] = [];

const paletteColors = [ 'black', 'gray', 'green', 'yellow', 'orange', 'red', 'blue', 'purple' ];
Expand Down Expand Up @@ -129,7 +129,7 @@ const DrawingBoard: React.FC = () => {
canvas.backgroundColor = backgroundColor;
}
};

return (
<Grid
container
Expand Down Expand Up @@ -176,7 +176,7 @@ const DrawingBoard: React.FC = () => {
<Grid
container
alignItems='center'
justifyContent='space-around'
// justifyContent='space-around'
>
{/* Tools: Palette */}
<Grid item>
Expand Down Expand Up @@ -234,13 +234,7 @@ const DrawingBoard: React.FC = () => {
>
<RedoIcon/>
</IconButton>
<IconButton
aria-label="Erase All"
onClick={handleEraseAll}
title="Erase All"
>
<DeleteIcon />
</IconButton>
<ErasingAllConfirmationButton handleEraseAll={handleEraseAll} />
</Grid>
</Grid>
</Grid>
Expand Down
72 changes: 72 additions & 0 deletions src/components/drawing/ErasingAllConfirmationButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import React, { useEffect, useState } from 'react';
import { Button, IconButton, Popover, Typography } from '@mui/material';
import DeleteIcon from '@mui/icons-material/Delete';

type ErasingAllConfirmationButtonProps = {
handleEraseAll: () => void;
};

const ErasingAllConfirmationButton: React.FC<ErasingAllConfirmationButtonProps> = (props) => {

const { handleEraseAll } = props;

const [ anchorEl, setAnchorEl ] = useState<HTMLButtonElement | null>(null);
const [ confirmed, setConfirmed ] = useState<boolean>(false);

const handleConfirm = (event: React.MouseEvent<HTMLButtonElement>) => {
setAnchorEl(event.currentTarget);
};

const handleConfirmed = () => {
setConfirmed(true);
setAnchorEl(null);
};

useEffect(() => {
if (confirmed) {
handleEraseAll();
setConfirmed(false);
}
}, [ confirmed ]);

const handleClose = () => {
setAnchorEl(null);
};

const open = Boolean(anchorEl);
const id = open ? 'simple-popover' : undefined;

return (
<div style={{ display: 'inline-block' }}>
<IconButton onClick={handleConfirm} aria-describedby={id}>
<DeleteIcon />
</IconButton>
<Popover
id={id}
open={open}
anchorEl={anchorEl}
onClose={handleClose}
anchorOrigin={{
vertical: 'top',
horizontal: 'center',
}}
transformOrigin={{
vertical: 'bottom',
horizontal: 'right',
}}
>
<Typography sx={{ p: 1 }}>
<Button
variant="contained"
color="warning"
onClick={handleConfirmed}
>
Confirm
</Button>
</Typography>
</Popover>
</div>
);
};

export default ErasingAllConfirmationButton;

0 comments on commit 4f3c8c8

Please sign in to comment.