From b1dce526a9c1c68c5d636f62f24e268f38bf4f99 Mon Sep 17 00:00:00 2001 From: Dom Harrington Date: Mon, 24 Sep 2018 10:50:28 -0700 Subject: [PATCH] Refactor similar parts of Preview/Demo into HoC --- example/preview/src/Preview.jsx | 69 +++++-------------- example/src/Demo.jsx | 113 +++++++++++--------------------- example/src/SpecFetcher.jsx | 53 +++++++++++++++ 3 files changed, 106 insertions(+), 129 deletions(-) create mode 100644 example/src/SpecFetcher.jsx diff --git a/example/preview/src/Preview.jsx b/example/preview/src/Preview.jsx index 74ff2318e..273f0b437 100644 --- a/example/preview/src/Preview.jsx +++ b/example/preview/src/Preview.jsx @@ -1,59 +1,20 @@ const React = require('react'); -const swagger2openapi = require('swagger2openapi'); const PropTypes = require('prop-types'); const extensions = require('../../../packages/oas-extensions/'); -const createDocs = require('../../../packages/api-explorer/lib/create-docs'); - const ApiExplorer = require('../../../packages/api-explorer/src'); +const withSpecFetching = require('../../src/SpecFetcher'); require('../../../packages/api-explorer/api-explorer.css'); class Preview extends React.Component { - constructor(props) { - super(props); - this.state = { status: [], oas: {}, docs: [] }; - this.fetchSwagger = this.fetchSwagger.bind(this); - this.convertSwagger = this.convertSwagger.bind(this); - this.updateStatus = this.updateStatus.bind(this); - } componentDidMount() { const searchParams = new URLSearchParams(window.location.search); - this.fetchSwagger(searchParams.get('url') || '../swagger-files/petstore.json'); - } - updateStatus(status, next) { - this.setState(prev => ({ status: [...prev.status, status] }), next); - } - fetchSwagger(url) { - this.updateStatus('Fetching swagger file', () => { - fetch(url) - .then(res => res.json()) - .then((json) => { - if (json.swagger) return this.convertSwagger(json); - - return this.dereference(json); - }); - }); - } - dereference(oas) { - this.createDocs(oas); - this.setState({ oas }); - this.updateStatus('Done!', () => { - setTimeout(() => { - this.setState({ status: [] }); - }, 1000); - }); - } - convertSwagger(swagger) { - this.updateStatus('Converting swagger file to OAS 3', () => { - swagger2openapi.convertObj(swagger, {}) - .then(({ openapi }) => this.dereference(openapi)); - }); - } - createDocs(oas) { - this.setState({ docs: createDocs(oas, 'api-setting') }); + this.props.fetchSwagger(searchParams.get('url') || '../swagger-files/petstore.json'); } render() { + const { status, docs, oas, oauth } = this.props; + return (
@@ -70,22 +31,17 @@ class Preview extends React.Component {
- { this.state.status.length ?
{this.state.status.join('\n')}
: null } + { status.length ?
{status.join('\n')}
: null } { - this.state.status.length === 0 && ( + status.length === 0 && ( ({ status: [...prev.status, status] }), next); - } - fetchSwagger(url) { - this.updateStatus('Fetching swagger file', () => { - fetch(url) - .then(res => res.json()) - .then((json) => { - if (json.swagger) return this.convertSwagger(json); - - return this.dereference(json); - }); - }); - } - dereference(oas) { - this.createDocs(oas); - this.setState({ oas }); - this.updateStatus('Done!', () => { - setTimeout(() => { - this.setState({ status: [] }); - }, 1000); - }); - } - convertSwagger(swagger) { - this.updateStatus('Converting swagger file to OAS 3', () => { - swagger2openapi.convertObj(swagger, {}) - .then(({ openapi }) => this.dereference(openapi)); - }); - } - createDocs(oas) { - this.setState({ docs: createDocs(oas, 'api-setting') }); - } - render() { - return ( -
-
- -
{this.state.status.join('\n')}
-
- { - this.state.status.length === 0 && ( - - ) - } +function Demo({ fetchSwagger, status, docs, oas, oauth }) { + return ( +
+
+ +
{status.join('\n')}
- ); - } + { + status.length === 0 && ( + + ) + } +
+ ); } Demo.propTypes = { oauth: PropTypes.bool, + oas: PropTypes.shape({}).isRequired, + docs: PropTypes.arrayOf(PropTypes.shape).isRequired, + status: PropTypes.arrayOf(PropTypes.string).isRequired, + fetchSwagger: PropTypes.func.isRequired, }; Demo.defaultProps = { oauth: false, }; -module.exports = Demo; + +module.exports = withSpecFetching(Demo); diff --git a/example/src/SpecFetcher.jsx b/example/src/SpecFetcher.jsx new file mode 100644 index 000000000..4d1c90782 --- /dev/null +++ b/example/src/SpecFetcher.jsx @@ -0,0 +1,53 @@ +const React = require('react'); +const swagger2openapi = require('swagger2openapi'); + +const createDocs = require('../../packages/api-explorer/lib/create-docs'); + +function withSpecFetching(Component) { + return class extends React.Component { + constructor(props) { + super(props); + this.state = { status: [], oas: {}, docs: [] }; + this.fetchSwagger = this.fetchSwagger.bind(this); + this.convertSwagger = this.convertSwagger.bind(this); + this.updateStatus = this.updateStatus.bind(this); + } + updateStatus(status, next) { + this.setState(prev => ({ status: [...prev.status, status] }), next); + } + fetchSwagger(url) { + this.updateStatus('Fetching swagger file', () => { + fetch(url) + .then(res => res.json()) + .then((json) => { + if (json.swagger) return this.convertSwagger(json); + + return this.dereference(json); + }); + }); + } + dereference(oas) { + this.createDocs(oas); + this.setState({ oas }); + this.updateStatus('Done!', () => { + setTimeout(() => { + this.setState({ status: [] }); + }, 1000); + }); + } + convertSwagger(swagger) { + this.updateStatus('Converting swagger file to OAS 3', () => { + swagger2openapi.convertObj(swagger, {}) + .then(({ openapi }) => this.dereference(openapi)); + }); + } + createDocs(oas) { + this.setState({ docs: createDocs(oas, 'api-setting') }); + } + render() { + return ; + } + }; +} + +module.exports = withSpecFetching;