1 | "use strict";
|
2 | var __extends = (this && this.__extends) || (function () {
|
3 | var extendStatics = function (d, b) {
|
4 | extendStatics = Object.setPrototypeOf ||
|
5 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
6 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
7 | return extendStatics(d, b);
|
8 | };
|
9 | return function (d, b) {
|
10 | extendStatics(d, b);
|
11 | function __() { this.constructor = d; }
|
12 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
13 | };
|
14 | })();
|
15 | var __assign = (this && this.__assign) || function () {
|
16 | __assign = Object.assign || function(t) {
|
17 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
18 | s = arguments[i];
|
19 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
20 | t[p] = s[p];
|
21 | }
|
22 | return t;
|
23 | };
|
24 | return __assign.apply(this, arguments);
|
25 | };
|
26 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
27 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
28 | };
|
29 | Object.defineProperty(exports, "__esModule", { value: true });
|
30 | exports.withApp = void 0;
|
31 | var hoist_non_react_statics_1 = __importDefault(require("hoist-non-react-statics"));
|
32 | var react_1 = __importDefault(require("react"));
|
33 | var Error_1 = require("@shopify/app-bridge/actions/Error");
|
34 | var PropTypes_1 = require("./PropTypes");
|
35 | function getComponentName(component) {
|
36 | return (component && (component.displayName || component.name)) || 'Component';
|
37 | }
|
38 | function withApp(storeToProps, dispatchToProps) {
|
39 | return function (WrappedComponent) {
|
40 | var AppBridgeConnector = (function (_super) {
|
41 | __extends(AppBridgeConnector, _super);
|
42 | function AppBridgeConnector(props, context) {
|
43 | var _this = _super.call(this, props, context) || this;
|
44 | _this.state = {
|
45 | mounted: false,
|
46 | };
|
47 | return _this;
|
48 | }
|
49 | AppBridgeConnector.prototype.augmentProps = function () {
|
50 | var _a = this, props = _a.props, app = _a.app, mappedProps = _a.mappedProps;
|
51 | var appState = app.getState();
|
52 | var store = storeToProps ? storeToProps(appState) : appState;
|
53 | var augmented = __assign(__assign({}, props), { app: app, store: store });
|
54 | if (mappedProps) {
|
55 | return __assign(__assign({}, mappedProps), augmented);
|
56 | }
|
57 | return augmented;
|
58 | };
|
59 | AppBridgeConnector.prototype.componentDidMount = function () {
|
60 | var config = this.props.config;
|
61 | var appBridgeMiddleware = this.context.appBridgeMiddleware;
|
62 | if (!config) {
|
63 | Error_1.throwError(Error_1.AppActionType.MISSING_CONFIG, 'Missing required prop `config`');
|
64 | }
|
65 | if (!appBridgeMiddleware || typeof appBridgeMiddleware.load !== 'function') {
|
66 | Error_1.throwError(Error_1.AppActionType.MISSING_APP_BRIDGE_MIDDLEWARE, 'Missing required context `appBridgeMiddleware`. Maybe you forgot the App Bridge `<Provider>` component?');
|
67 | }
|
68 | this.app = this.context.appBridgeMiddleware.load({
|
69 | config: config,
|
70 | type: 'application',
|
71 | });
|
72 | if (dispatchToProps) {
|
73 | this.mappedProps = dispatchToProps(this.app);
|
74 | }
|
75 | this.setState({ mounted: true });
|
76 | };
|
77 | AppBridgeConnector.prototype.render = function () {
|
78 | return this.state.mounted ? react_1.default.createElement(WrappedComponent, __assign({}, this.augmentProps())) : null;
|
79 | };
|
80 | AppBridgeConnector.contextTypes = {
|
81 | appBridgeMiddleware: PropTypes_1.appBridgeMiddlewareProptype,
|
82 | };
|
83 | AppBridgeConnector.displayName = "AppBridge(" + getComponentName(WrappedComponent) + ")";
|
84 | AppBridgeConnector.WrappedComponent = WrappedComponent;
|
85 | return AppBridgeConnector;
|
86 | }(react_1.default.Component));
|
87 | return hoist_non_react_statics_1.default(AppBridgeConnector, WrappedComponent);
|
88 | };
|
89 | }
|
90 | exports.withApp = withApp;
|