UNPKG

@botonic/react

Version:

Build Chatbots using React

69 lines 2.7 kB
import { isCarousel } from '../message-utils'; import { strToBool } from '../util/objects'; import { deepMapWithIndex } from '../util/react'; import { Button } from './button'; export class ButtonsDisabler { static constructBrowserProps(props) { const disabledProps = {}; if (props.autodisable !== undefined) disabledProps.autodisable = strToBool(props.autodisable); if (props.disabledstyle !== undefined) disabledProps.disabledstyle = props.disabledstyle; return disabledProps; } static constructNodeProps(props) { const disabledProps = {}; if (props.autodisable !== undefined) disabledProps.autodisable = String(props.autodisable); if (props.disabledstyle !== undefined) disabledProps.disabledstyle = JSON.stringify(props.disabledstyle); return disabledProps; } static withDisabledProps(props) { return { disabled: props.disabled, autodisable: props.autodisable, disabledstyle: props.disabledstyle, }; } static updateChildrenButtons(children, additionalProps) { return deepMapWithIndex(children, (node) => { if (node.type === Button) return this.updateButtons(node, additionalProps); return node; }); } static updateButtons(node, additionalProps) { if (additionalProps) { additionalProps = { parentId: additionalProps.parentId, disabled: node.props.disabled === true ? node.props.disabled : additionalProps.disabled, setDisabled: additionalProps.setDisabled, }; } return Object.assign(Object.assign({}, node), { props: Object.assign(Object.assign({}, node.props), additionalProps) }); } static getUpdatedMessage(messageToUpdate) { const updateMsgButton = (button) => { return Object.assign(Object.assign({}, button), { disabled: true, }); }; if (isCarousel(messageToUpdate) && messageToUpdate.data && messageToUpdate.data.elements) { messageToUpdate.data.elements = messageToUpdate.data.elements.map(e => (Object.assign(Object.assign({}, e), { buttons: e.buttons.map(updateMsgButton), }))); return messageToUpdate; } else { return Object.assign(Object.assign({}, messageToUpdate), { buttons: messageToUpdate.buttons.map(updateMsgButton), }); } } } //# sourceMappingURL=buttons-disabler.js.map