Skip to content

Instantly share code, notes, and snippets.

@daveteu
Last active December 8, 2020 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daveteu/f129d0eda7d8805481ff613bcab58796 to your computer and use it in GitHub Desktop.
Save daveteu/f129d0eda7d8805481ff613bcab58796 to your computer and use it in GitHub Desktop.
usePromise
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