confirmAlert.tsx 2.85 KB
Newer Older
Maxym Borodenko's avatar
Maxym Borodenko committed
1 2
import * as React from 'react';
import * as ReactDOM from 'react-dom';
Matija Obreza's avatar
Matija Obreza committed
3 4 5 6 7 8
import Button from '@material-ui/core/Button';
import Dialog from '@material-ui/core/Dialog';
import DialogActions from '@material-ui/core/DialogActions';
import DialogContent from '@material-ui/core/DialogContent';
import DialogContentText from '@material-ui/core/DialogContentText';
import DialogTitle from '@material-ui/core/DialogTitle';
9
import { StylesProvider } from '@material-ui/core/styles';
Matija Obreza's avatar
Matija Obreza committed
10

Maxym Borodenko's avatar
Maxym Borodenko committed
11 12

interface IConfirmProps extends React.ClassAttributes<any> {
13 14
    message?: string;
    description?: string;
Maxym Borodenko's avatar
Maxym Borodenko committed
15 16
    confirmLabel?: string;
    abortLabel?: string;
17
    confirmOnly?: boolean;
Maxym Borodenko's avatar
Maxym Borodenko committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
}

function PromiseWrapper() {
    this.with = (callback) => new Promise((resolve, reject) => {
        this.resolve = () => {
            resolve();
            callback();
        };
        this.reject = () => {
            reject();
            callback();
        };
    });
}

class Confirm extends React.Component<IConfirmProps, any> {

    public promise = new PromiseWrapper();

    private resolve = () => {
        this.promise.resolve();
    }

    private reject = () => {
        this.promise.reject();
    }

    public render() {
46
        const {message, description, confirmLabel = 'OK', abortLabel = 'Cancel', confirmOnly = false} = this.props;
Maxym Borodenko's avatar
Maxym Borodenko committed
47 48

        return (
49
          <StylesProvider injectFirst>
Maxym Borodenko's avatar
Maxym Borodenko committed
50 51
            <Dialog
                open
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
52
                onClose={ this.reject }
Maxym Borodenko's avatar
Maxym Borodenko committed
53 54 55 56
                aria-labelledby="alert-dialog-title"
                aria-describedby="alert-dialog-description"
            >
                <DialogTitle id="alert-dialog-title">{ message }</DialogTitle>
57 58 59 60 61 62 63
                { description && (
                    <DialogContent>
                        <DialogContentText id="alert-dialog-description">
                            { description }
                        </DialogContentText>
                    </DialogContent>
                ) }
Maxym Borodenko's avatar
Maxym Borodenko committed
64
                <DialogActions>
65 66 67 68 69 70
                    { ! confirmOnly && (
                        <Button onClick={ this.reject } autoFocus>
                            { abortLabel }
                        </Button>
                    ) }
                    <Button onClick={ this.resolve } color="primary" autoFocus={ confirmOnly }>
Maxym Borodenko's avatar
Maxym Borodenko committed
71 72 73 74
                      { confirmLabel }
                    </Button>
                </DialogActions>
            </Dialog>
75
          </StylesProvider>
Maxym Borodenko's avatar
Maxym Borodenko committed
76 77 78 79 80
        );
    }

}

81
export default (message: any, options: IConfirmProps = {}) => {
Maxym Borodenko's avatar
Maxym Borodenko committed
82 83 84 85 86 87 88 89 90 91 92
    const props: any = {message, ...options};

    const wrapper = document.body.appendChild(document.createElement('div'));
    const component = ReactDOM.render(React.createElement(Confirm, props), wrapper);

    const cleanup = () => {
        ReactDOM.unmountComponentAtNode(wrapper);
        setTimeout(() => wrapper.remove());
    };
    return component.promise.with(cleanup);
};