import {Op, ops} from "../../../../framework/ops.js" import {html} from "../../../../framework/component.js" import {ModalSystem} from "../types/modal-system.js" import {renderOp} from "../../../../framework/op-rendering/render-op.js" import {ConfirmActionOptions} from "../types/confirm-action-options.js" export function prepareConfirmAction( popup: ModalSystem["popup"] ) { return async({ title, message, loadingMessage, cancelButtonLabel = "no", confirmButtonLabel = "yes", actionWhenConfirmed, }: ConfirmActionOptions) => { let op: Op = ops.ready(undefined) function setOp(newOp: Op) { op = newOp modal.controls.rerender() } const modal = popup({ focusNthElement: 1, renderContent: (controls) => { const isError = ops.isError(op) return html`

${title}

${renderOp(op, () => html`
${message}
{ await ops.operation({ promise: actionWhenConfirmed(), setOp, }) controls.close()}}> ${confirmButtonLabel} ${cancelButtonLabel}
`, null, {loadingMessage} )} ${isError ? html `
ok
` : null } ` }, onBlanketClick: (controls) => { const isLoading = ops.isLoading(op) if(!isLoading) controls.close() } }) } } // function confirmAction({ // title, // message, // cancelButtonLabel, // confirmButtonLabel, // actionWhenConfirmed, // }: { // actionWhenConfirmed: () => void // message: string // title: string // confirmButtonLabel: string // cancelButtonLabel: string // }) { // }