Last active
December 8, 2020 23:19
-
-
Save daveteu/f129d0eda7d8805481ff613bcab58796 to your computer and use it in GitHub Desktop.
usePromise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from 'react' | |
import { Grid, Dialog, DialogContent, DialogActions, Button } from '@material-ui/core' | |
const useConfirm = () => { | |
const [ open, setOpen ] = useState(false); | |
const [ resolver, setResolver ] = useState({ resolver: null }) | |
const [ label, setLabel ] = useState('') | |
const getConfirmation = async (text) => { | |
setLabel(text); | |
setOpen(true); | |
const [ promise, resolve ] = await createPromise() | |
setResolver({ resolve }) | |
return promise; | |
} | |
const onClick = async(status) => { | |
setOpen(false); | |
resolver.resolve(status) | |
} | |
const Confirmation = () => ( | |
<Dialog open={open}> | |
<DialogContent> | |
{label} | |
</DialogContent> | |
<DialogActions> | |
<Button onClick={ () => onClick(false)}> Cancel </Button> | |
<Button onClick={ () => onClick(true)}> OK </Button> | |
</DialogActions> | |
</Dialog> | |
) | |
return [ getConfirmation, Confirmation ] | |
} | |
export default useConfirm; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment