Sha256: d811bf5713fb27231549f3dd4b4a11899d6ba88ee063afde18b9083a5f782107

Contents?: true

Size: 1.72 KB

Versions: 7

Compression:

Stored size: 1.72 KB

Contents

import _ from 'lodash';
import * as PropTypes from 'prop-types';
import React from 'react';
import ReactRouterPropTypes from 'react-router-prop-types';
import { History } from '@shopify/app-bridge/actions';
import { Page } from '@shopify/polaris';

class EmbeddedPage extends React.Component {
  static contextTypes = {
    polaris: PropTypes.object
  };

  static propTypes = {
    children: PropTypes.node.isRequired,
    history: ReactRouterPropTypes.history.isRequired,
    location: ReactRouterPropTypes.location.isRequired,
    title: PropTypes.string.isRequired
  };

  componentDidMount() {
    window.addEventListener('message', this.handleMessage);

    this.pushHistory();
  }

  componentDidUpdate(prevProps) {
    if (prevProps.location.pathname !== this.props.location.pathname) {
      this.pushHistory();
    }
  }

  componentWillUnmount() {
    window.removeEventListener('message', this.handleMessage);
  }

  pushHistory = () => {
    const history = History.create(this.context.polaris.appBridge);

    history.dispatch(History.Action.PUSH, this.props.history.location.pathname);
  };

  handleMessage = e => {
    if (e.isTrusted) {
      if (_.isString(e.data)) {
        const json = JSON.parse(e.data);

        if (json.message === 'Shopify.API.setWindowLocation') {
          const url = new URL(json.data);
          this.props.history.push(url.pathname);
        }
      }
    }
  };

  pushHistory = () => {
    const history = History.create(this.context.polaris.appBridge);

    history.dispatch(History.Action.PUSH, this.props.history.location.pathname);
  };

  render() {
    return (
      <Page title={this.props.title} {...this.props}>
        {this.props.children}
      </Page>
    );
  }
}

export default EmbeddedPage;

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
disco_app-0.17.0 lib/generators/disco_app/react/templates/app/webpack/javascripts/embedded/components/Shared/EmbeddedPage.jsx
disco_app-0.18.0 lib/generators/disco_app/react/templates/app/webpack/javascripts/embedded/components/Shared/EmbeddedPage.jsx
disco_app-0.18.2 lib/generators/disco_app/react/templates/app/webpack/javascripts/embedded/components/Shared/EmbeddedPage.jsx
disco_app-0.18.3 lib/generators/disco_app/react/templates/app/webpack/javascripts/embedded/components/Shared/EmbeddedPage.jsx
disco_app-0.18.6 lib/generators/disco_app/react/templates/app/webpack/javascripts/embedded/components/Shared/EmbeddedPage.jsx
disco_app-0.18.4 lib/generators/disco_app/react/templates/app/webpack/javascripts/embedded/components/Shared/EmbeddedPage.jsx
disco_app-0.18.1 lib/generators/disco_app/react/templates/app/webpack/javascripts/embedded/components/Shared/EmbeddedPage.jsx