stackpress
Version:
Incept is a content management framework.
32 lines (31 loc) • 894 B
JavaScript
import React from 'react';
import { useContext } from 'react';
import { useNotify } from '../notify/hooks';
import ModalContext from './ModalContext';
import Confirm from './ModalConfirm';
export function useModal() {
const { className, title, body, open } = useContext(ModalContext);
return { className, title, body, open };
}
export function useConfirm(config) {
const { label, message, action } = config;
const { open, title, body } = useModal();
const { notify } = useNotify();
const confirmed = () => action().then(() => {
open(false);
}).catch(e => {
open(false);
notify('error', e.message);
});
const confirm = () => {
title(label());
body(React.createElement(Confirm, {
open,
message: message(),
confirmed
}));
open(true);
};
return { confirm };
}
;