UNPKG

@shopify/app-bridge-host

Version:

App Bridge Host contains components and middleware to be consumed by the app's host, as well as the host itself. The middleware and `Frame` component are responsible for facilitating communication between the client and host, and used to act on actions se

71 lines (65 loc) 3.39 kB
'use strict'; var tslib = require('tslib'); var hoistStatics = require('hoist-non-react-statics'); var React = require('react'); var Error = require('@shopify/app-bridge-core/actions/Error'); var PropTypes = require('./PropTypes.js'); function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; } var hoistStatics__default = /*#__PURE__*/_interopDefault(hoistStatics); var React__default = /*#__PURE__*/_interopDefault(React); function getComponentName(component) { return (component && (component.displayName || component.name)) || 'Component'; } function withApp(storeToProps, dispatchToProps) { return function (WrappedComponent) { var AppBridgeConnector = /** @class */ (function (_super) { tslib.__extends(AppBridgeConnector, _super); function AppBridgeConnector(props, context) { var _this = _super.call(this, props, context) || this; _this.state = { mounted: false, }; return _this; } AppBridgeConnector.prototype.augmentProps = function () { var _a = this, props = _a.props, app = _a.app, mappedProps = _a.mappedProps; var appState = app.getState(); var store = storeToProps ? storeToProps(appState) : appState; var augmented = tslib.__assign(tslib.__assign({}, props), { app: app, store: store }); if (mappedProps) { return tslib.__assign(tslib.__assign({}, mappedProps), augmented); } return augmented; }; AppBridgeConnector.prototype.componentDidMount = function () { var config = this.props.config; var appBridgeMiddleware = this.context.appBridgeMiddleware; if (!config) { Error.throwError(Error.AppActionType.MISSING_CONFIG, 'Missing required prop `config`'); } if (!appBridgeMiddleware || typeof appBridgeMiddleware.load !== 'function') { Error.throwError(Error.AppActionType.MISSING_APP_BRIDGE_MIDDLEWARE, 'Missing required context `appBridgeMiddleware`. Maybe you forgot the App Bridge `<Provider>` component?'); } this.app = this.context.appBridgeMiddleware.load({ config: config, type: 'application', }); if (dispatchToProps) { this.mappedProps = dispatchToProps(this.app); } this.setState({ mounted: true }); }; AppBridgeConnector.prototype.render = function () { return this.state.mounted ? React__default.default.createElement(WrappedComponent, tslib.__assign({}, this.augmentProps())) : null; }; AppBridgeConnector.contextTypes = { appBridgeMiddleware: PropTypes.appBridgeMiddlewareProptype, }; AppBridgeConnector.displayName = "AppBridge(".concat(getComponentName(WrappedComponent), ")"); AppBridgeConnector.WrappedComponent = WrappedComponent; return AppBridgeConnector; }(React__default.default.Component)); return hoistStatics__default.default(AppBridgeConnector, WrappedComponent); }; } exports.withApp = withApp;