stackpress
Version:
Incept is a content management framework.
32 lines (31 loc) • 903 B
JavaScript
import React from 'react';
import { useContext } from 'react';
import { useNotify } from '../notify/hooks.js';
import ModalContext from './ModalContext.js';
import Confirm from './ModalConfirm.js';
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 };
}
;