Skip to content

Commit

Permalink
fix(Modal): don't allow user to dismiss modal when waiting for async …
Browse files Browse the repository at this point in the history
…request
  • Loading branch information
ramfox committed Jul 26, 2019
1 parent 2c5e247 commit 6f56293
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions app/components/modals/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,24 +101,21 @@ export interface ModalProps {
*/
const Modal: React.FunctionComponent<ModalProps> = ({ title, dismissable, onDismissed, id, type, onSubmit, className, disabled, loading, children }) => {
const [modalElement, setModalElement] = React.useState<HTMLElement | null>(null)
const [isDismissable, setIsDismissable] = React.useState(!!dismissable)

React.useEffect(() => {
if (modalElement) {
(modalElement as any).showModal()
}
}, [modalElement]) // eslint-disable-line

const isDismissable = () => {
return dismissable === undefined || dismissable
}

const onDialogCancel = (e: Event) => {
e.preventDefault()
onDismiss()
}

const onModalClick = (e: React.MouseEvent<HTMLElement>) => {
if (isDismissable() === false) {
if (isDismissable === false) {
return
}

Expand Down Expand Up @@ -178,7 +175,7 @@ const Modal: React.FunctionComponent<ModalProps> = ({ title, dismissable, onDism
}

const onDismiss = () => {
if (isDismissable()) {
if (isDismissable) {
if (onDismissed) {
onDismissed()
}
Expand All @@ -189,7 +186,11 @@ const Modal: React.FunctionComponent<ModalProps> = ({ title, dismissable, onDism
e.preventDefault()

if (onSubmit) {
onSubmit()
setIsDismissable(false)
new Promise((resolve) => {
onSubmit()
resolve()
}).then(() => setIsDismissable(!!dismissable))
} else {
onDismiss()
}
Expand All @@ -203,7 +204,7 @@ const Modal: React.FunctionComponent<ModalProps> = ({ title, dismissable, onDism
return (
<ModalHeader
title={title}
dismissable={isDismissable()}
dismissable={isDismissable}
onDismissed={onDismiss}
loading={loading}
/>
Expand Down

0 comments on commit 6f56293

Please sign in to comment.