UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

123 lines 3.25 kB
import React from 'react'; import PropTypes from 'prop-types'; import GlobalStatusProvider from "./GlobalStatusProvider.js"; import { isTrue } from "../../shared/component-helper.js"; export class GlobalStatusInterceptor { constructor(props) { let GSP = null; try { GSP = GlobalStatusProvider; } catch (e) {} if (!GSP && typeof window !== 'undefined') { GSP = window.GlobalStatusProvider; } this.provider = GSP.init(props.id || 'main', provider => { const { status_id } = provider.add(props); this.status_id = status_id; }); return this; } add(props) { return this.provider.add({ status_id: this.status_id, ...props }); } update(props) { return this.provider.update(this.status_id, props); } remove() { return this.provider.remove(this.status_id); } } class GlobalStatusController extends React.PureComponent { static defaultProps = { id: 'main', status_id: null, remove_on_unmount: false }; static getDerivedStateFromProps(props, state) { if (state._props !== props) { state.provider.update(state.status_id, props); state._props = props; } return state; } state = {}; constructor(props) { super(props); let GSP = null; try { GSP = GlobalStatusProvider; } catch (e) {} if (!GSP && typeof window !== 'undefined') { GSP = window.GlobalStatusProvider; } this.state.provider = GSP.init(props.id); this.state._props = props; return this; } componentDidMount() { const { status_id } = this.state.provider.add(this.props); this.setState({ status_id }); } componentWillUnmount() { if (this.state.provider && isTrue(this.props.remove_on_unmount)) { this.state.provider.remove(this.state.status_id); } } render() { return null; } } process.env.NODE_ENV !== "production" ? GlobalStatusController.propTypes = { id: PropTypes.string, status_id: PropTypes.string, remove_on_unmount: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]) } : void 0; class GlobalStatusRemove extends React.PureComponent { static defaultProps = { id: 'main' }; static getDerivedStateFromProps(props, state) { if (state._props !== props) { state.provider.update(props.status_id, props); } return state; } state = {}; constructor(props) { super(props); let GSP = null; try { GSP = GlobalStatusProvider; } catch (e) {} if (!GSP && typeof window !== 'undefined') { GSP = window.GlobalStatusProvider; } this.state.provider = GSP.init(props.id); this.state._props = props; } componentDidMount() { this.state.provider.remove(this.props.status_id, this.props); } render() { return null; } } process.env.NODE_ENV !== "production" ? GlobalStatusRemove.propTypes = { id: PropTypes.string, status_id: PropTypes.string.isRequired } : void 0; GlobalStatusController.Remove = GlobalStatusRemove; GlobalStatusController.Update = GlobalStatusController; GlobalStatusController._supportsSpacingProps = true; export default GlobalStatusController; export { GlobalStatusRemove }; //# sourceMappingURL=GlobalStatusController.js.map