import React from 'react'; import Modal, { ModalProps } from '@/components/elements/Modal'; import ModalContext from '@/context/ModalContext'; export interface AsModalProps { visible: boolean; onModalDismissed?: () => void; } type ExtendedModalProps = Omit; interface State { render: boolean; visible: boolean; showSpinnerOverlay?: boolean; } type ExtendedComponentType = (C: React.ComponentType) => React.ComponentType; // eslint-disable-next-line @typescript-eslint/ban-types function asModal

(modalProps?: ExtendedModalProps | ((props: P) => ExtendedModalProps)): ExtendedComponentType

{ return function (Component) { return class extends React.PureComponent

{ static displayName = `asModal(${Component.displayName})`; constructor (props: P & AsModalProps) { super(props); this.state = { render: props.visible, visible: props.visible, showSpinnerOverlay: undefined, }; } get modalProps () { return { ...(typeof modalProps === 'function' ? modalProps(this.props) : modalProps), showSpinnerOverlay: this.state.showSpinnerOverlay, }; } componentDidUpdate (prevProps: Readonly

) { if (prevProps.visible && !this.props.visible) { // noinspection JSPotentiallyInvalidUsageOfThis this.setState({ visible: false }); } else if (!prevProps.visible && this.props.visible) { // noinspection JSPotentiallyInvalidUsageOfThis this.setState({ render: true, visible: true }); } } dismiss = () => this.setState({ visible: false }); toggleSpinner = (value?: boolean) => this.setState({ showSpinnerOverlay: value }); render () { return ( this.state.render ? this.setState({ render: false }, () => { if (typeof this.props.onModalDismissed === 'function') { this.props.onModalDismissed(); } })} {...this.modalProps} > : null ); } }; }; } export default asModal;