Skip to content

Instantly share code, notes, and snippets.

@semoal
Created August 21, 2019 08:13
Show Gist options
  • Save semoal/9ff2ac000040062d71ee6add04141dc1 to your computer and use it in GitHub Desktop.
Save semoal/9ff2ac000040062d71ee6add04141dc1 to your computer and use it in GitHub Desktop.
Zoid HOC
import React from 'react';
import Head from 'next/head';
const withZoid = (Page, wantedWidget) => {
return class extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
zoidProps: null
}
}
componentDidMount() {
if (window.xprops) {
this.setState({ zoidProps: window.xprops });
window.xprops.onProps(props => this.setState({ zoidProps: props }));
}
}
static async getInitialProps(ctx) {
if(Page.getInitialProps) return Page.getInitialProps(ctx);
return {};
}
render() {
const { zoidProps } = this.state;
return (
<>
<Head>
<script src="/static/zoid.min.js"></script>
<script src={`/static/widgets/${wantedWidget}-widget.js`}></script>
</Head>
<Page zoidProps={zoidProps} {...this.props} />
</>
)
}
}
}
export default withZoid;
@semoal
Copy link
Author

semoal commented Aug 21, 2019

Common widgets:
/static/widgets/contracts-widget.js

zoid.create({
  tag: 'contracts-widget',
  url: 'http://localhost:2000/contracts'
});

@semoal
Copy link
Author

semoal commented Aug 21, 2019

Parent widget:

import * as zoid from 'zoid/dist/zoid.frameworks';

const BASE_URL = 'http://localhost:2000';

const ContractsWidget = zoid.create({
  tag: 'contracts-widget',
  attributes: {
    iframe: {
      scrolling: 'yes'
    }
  },
  url: () => `${BASE_URL}/contracts`,
  props: {
    configuration: {
      type: 'object',
      required: false
    }
  },
});
export default ContractsWidget;

@semoal
Copy link
Author

semoal commented Aug 21, 2019

Parent page app:

import React from 'react';
import ReactDOM from 'react-dom';

import { ContractsWidget } from './Widgets';

const ContractsPreview = ContractsWidget.driver('react',{
  React,
  ReactDOM
});

function App() {
  const [value, setValue] = React.useState('hola');

  return (
    <>
    <input type="text" onChange={(e) => setValue(e.target.value)} value={value} />
    <ContractsPreview configuration={{ metadata: { items: ['hola', 'hola2']}}} />
    </>
  );
}

export default App;

@semoal
Copy link
Author

semoal commented Aug 21, 2019

Common widgets:
/static/widgets/contracts-widget.js

zoid.create({
  tag: 'contracts-widget',
  url: 'http://localhost:2000/contracts'
});

Example of /pages/contracts/index.js (NEXTJS):

import React from 'react';
import withZoid from '../../components/withZoid';

class ContractsPage extends React.Component {
  render() {
    return (
      <>
        <h1>Showing from iframe from contracts-page: </h1> 
        <div style={{ color: "yellow", background: "red" }}>{JSON.stringify(this.props.zoidProps)}</div>
      </>
    );
  }
}

export default withZoid(ContractsPage, 'contracts');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment