Skip to content

Commit

Permalink
Merge pull request #58 from Elizabethhub/fix-bug/modal-edit
Browse files Browse the repository at this point in the history
Fix bug/modal edit
  • Loading branch information
Elizabethhub authored Mar 18, 2024
2 parents 5925709 + 6f37467 commit f7f2450
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 29 deletions.
22 changes: 8 additions & 14 deletions src/components/ModalDeleteWater/ModalDeleteWater.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,30 @@ import {
StyledModalDeleteForm,
StyledModalDeleteWrapper,
} from './ModalDeleteWater.styled.js';
import useClickBackdrop from '../../hooks/modalCloseBackdrop.js';
import useKeyDown from '../../hooks/modalCloseEsc.js';

const ModalDeleteWater = () => {
const isModalOpen = useSelector(modalDeleteOpen);

const dispatch = useDispatch();

const clickBackdrop = (e) => {
if (e.target === e.currentTarget) {
dispatch(changeModalClose(false));
}
};
useEffect(() => {
const handleKeyDown = (e) => {
if (e.key === 'Escape') {
dispatch(changeModalClose(false));
}
};
const clickBackdrop = useClickBackdrop();

useEffect(() => {
if (isModalOpen) {
document.body.style.overflow = 'hidden';
} else {
document.body.style.overflow = 'auto';
}

document.addEventListener('keydown', handleKeyDown);

return () => {
document.body.style.overflow = 'auto';
document.removeEventListener('keydown', handleKeyDown);
};
}, [isModalOpen]);

useKeyDown(() => {
dispatch(changeModalClose(false));
}, [dispatch, isModalOpen]);

const onSubmit = (e) => {
Expand Down
23 changes: 8 additions & 15 deletions src/components/ModalWater/ModalWater.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
StyledModalAddWrapper,
} from '../ModalAddWater/ModalAddWater.styled.js';
import ModalEditWater from '../ModalEditWater/ModalEditWater.jsx';
import useClickBackdrop from '../../hooks/modalCloseBackdrop.js';
import useKeyDown from '../../hooks/modalCloseEsc.js';

const ModalWater = () => {
const isModalOpen = useSelector(modalIsOpen);
Expand All @@ -22,30 +24,21 @@ const ModalWater = () => {

const dispatch = useDispatch();

const clickBackdrop = (e) => {
if (e.target === e.currentTarget) {
dispatch(changeModalClose(false));
}
};
useEffect(() => {
const handleKeyDown = (e) => {
if (e.key === 'Escape') {
dispatch(changeModalClose(false));
}
};
const clickBackdrop = useClickBackdrop();

useEffect(() => {
if (isModalOpen) {
document.body.style.overflow = 'hidden';
} else {
document.body.style.overflow = 'auto';
}

document.addEventListener('keydown', handleKeyDown);

return () => {
document.body.style.overflow = 'auto';
document.removeEventListener('keydown', handleKeyDown);
};
}, [isModalOpen]);

useKeyDown(() => {
dispatch(changeModalClose(false));
}, [dispatch, isModalOpen]);

return (
Expand Down
16 changes: 16 additions & 0 deletions src/hooks/modalCloseBackdrop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useDispatch } from 'react-redux';
import { changeModalClose } from '../store/water/waterSlice.js';

const useClickBackdrop = () => {
const dispatch = useDispatch();

const clickBackdrop = (e) => {
if (e.target === e.currentTarget) {
dispatch(changeModalClose(false));
}
};

return clickBackdrop;
};

export default useClickBackdrop;
23 changes: 23 additions & 0 deletions src/hooks/modalCloseEsc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useEffect } from 'react';
import { useDispatch } from 'react-redux';
import { changeModalClose } from '../store/water/waterSlice';

const useKeyDown = () => {
const dispatch = useDispatch();

useEffect(() => {
const handleKeyDown = (e) => {
if (e.key === 'Escape') {
dispatch(changeModalClose(false));
}
};

document.addEventListener('keydown', handleKeyDown);

return () => {
document.removeEventListener('keydown', handleKeyDown);
};
});
};

export default useKeyDown;

0 comments on commit f7f2450

Please sign in to comment.