refactor: moved dialog to prompt to make dialog reusable

This commit is contained in:
JohnMark Sill 2022-01-06 13:02:51 -06:00
parent 9edf38347c
commit 0e85c70a96
3 changed files with 26 additions and 16 deletions

View File

@ -1,10 +1,8 @@
import { h, Fragment } from 'preact'; import { h, Fragment } from 'preact';
import Button from './Button';
import Heading from './Heading';
import { createPortal } from 'preact/compat'; import { createPortal } from 'preact/compat';
import { useState, useEffect } from 'preact/hooks'; import { useState, useEffect } from 'preact/hooks';
export default function Dialog({ actions = [], portalRootID = 'dialogs', title, text }) { export default function Dialog({ children, portalRootID = 'dialogs' }) {
const portalRoot = portalRootID && document.getElementById(portalRootID); const portalRoot = portalRootID && document.getElementById(portalRootID);
const [show, setShow] = useState(false); const [show, setShow] = useState(false);
@ -27,17 +25,7 @@ export default function Dialog({ actions = [], portalRootID = 'dialogs', title,
show ? 'scale-100 opacity-100' : '' show ? 'scale-100 opacity-100' : ''
}`} }`}
> >
<div className="p-4"> {children}
<Heading size="lg">{title}</Heading>
<p>{text}</p>
</div>
<div className="p-2 flex justify-start flex-row-reverse space-x-2">
{actions.map(({ color, text, onClick, ...props }, i) => (
<Button className="ml-2" color={color} key={i} onClick={onClick} type="text" {...props}>
{text}
</Button>
))}
</div>
</div> </div>
</div> </div>
</Fragment> </Fragment>

View File

@ -0,0 +1,22 @@
import { h, Fragment } from 'preact';
import Button from './Button';
import Heading from './Heading';
import Dialog from './Dialog';
export default function Prompt({ actions = [], title, text }) {
return (
<Dialog>
<div className="p-4">
<Heading size="lg">{title}</Heading>
<p>{text}</p>
</div>
<div className="p-2 flex justify-start flex-row-reverse space-x-2">
{actions.map(({ color, text, onClick, ...props }, i) => (
<Button className="ml-2" color={color} key={i} onClick={onClick} type="text" {...props}>
{text}
</Button>
))}
</div>
</Dialog>
)
}

View File

@ -9,11 +9,11 @@ import Clip from '../icons/Clip';
import Close from '../icons/Close'; import Close from '../icons/Close';
import Delete from '../icons/Delete'; import Delete from '../icons/Delete';
import Snapshot from '../icons/Snapshot'; import Snapshot from '../icons/Snapshot';
import Dialog from '../components/Dialog';
import Heading from '../components/Heading'; import Heading from '../components/Heading';
import VideoPlayer from '../components/VideoPlayer'; import VideoPlayer from '../components/VideoPlayer';
import { Table, Thead, Tbody, Th, Tr, Td } from '../components/Table'; import { Table, Thead, Tbody, Th, Tr, Td } from '../components/Table';
import { FetchStatus, useApiHost, useEvent, useDelete } from '../api'; import { FetchStatus, useApiHost, useEvent, useDelete } from '../api';
import Prompt from '../components/Prompt';
const ActionButtonGroup = ({ className, handleClickDelete, close }) => ( const ActionButtonGroup = ({ className, handleClickDelete, close }) => (
<div className={`space-y-2 space-x-2 sm:space-y-0 xs:space-x-4 ${className}`}> <div className={`space-y-2 space-x-2 sm:space-y-0 xs:space-x-4 ${className}`}>
@ -121,7 +121,7 @@ export default function Event({ eventId, close, scrollRef }) {
</div> </div>
<ActionButtonGroup handleClickDelete={handleClickDelete} close={close} className="hidden sm:block" /> <ActionButtonGroup handleClickDelete={handleClickDelete} close={close} className="hidden sm:block" />
{showDialog ? ( {showDialog ? (
<Dialog <Prompt
onDismiss={handleDismissDeleteDialog} onDismiss={handleDismissDeleteDialog}
title="Delete Event?" title="Delete Event?"
text={ text={