Skip to content

Refactor/state management #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jun 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,15 @@
"lodash": "^4.17.4",
"markdown-it": "^8.3.1",
"prop-types": "^15.5.8",
"qs": "^6.4.0",
"react": "^15.5.4",
"react-addons-create-fragment": "^15.5.4",
"react-document-title": "^2.0.2",
"react-dom": "^15.5.4",
"react-json-view": "^1.8.2",
"react-redux": "^5.0.3",
"react-router": "3.0.0",
"react-router-redux": "^4.0.8",
"react-json-view": "^1.8.4",
"react-router": "^4.1.1",
"react-router-dom": "^4.1.1",
"react-scrollable-anchor": "^0.4.2",
"redux": "^3.6.0",
"redux-thunk": "^2.2.0",
"superagent": "^3.5.2"
},
"devDependencies": {
Expand All @@ -61,11 +59,11 @@
"babel-preset-stage-1": "^6.22.0",
"balloon-css": "^0.4.0",
"css-loader": "^0.28.4",
"eslint": "^3.19.0",
"eslint": "^4.0.0",
"eslint-config-standard": "^10.2.1",
"eslint-config-standard-react": "^5.0.0",
"eslint-plugin-import": "^2.3.0",
"eslint-plugin-node": "^4.2.2",
"eslint-plugin-node": "^5.0.0",
"eslint-plugin-promise": "^3.5.0",
"eslint-plugin-react": "^7.0.1",
"eslint-plugin-standard": "^3.0.1",
Expand Down
3 changes: 2 additions & 1 deletion src/components/Example/Example.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ export default class Example extends Component {
displayObjectSize={false}
collapsed={this.state.collapseAll}
enableClipboard={false}
/>}
/>
}
</div>
)
}
Expand Down
59 changes: 59 additions & 0 deletions src/containers/Base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import React, { Component } from 'react'
import { configureAnchors } from 'react-scrollable-anchor'
import DocumentTitle from 'react-document-title'
import PropTypes from 'prop-types'
import { parse as parseQuery } from 'qs'

import Page from '../components/Page/Page'
import { getDefinition, parseDefinition } from '../lib/definitions'
import '../general.scss'

export default class Base extends Component {
state = {
parserType: 'open-api-v3',
definitionUrl: null,
definition: null,
parsedDefinition: null
}

setDefinition = async ({ definitionUrl, parserType = this.state.parserType }) => {
const definition = await getDefinition(definitionUrl)
const parsedDefinition = await parseDefinition(definition, parserType)

this.setState({ definitionUrl, definition, parsedDefinition, parserType })
}

async componentDidMount () {
const { location } = this.props
const { parserType } = this.state

const definitionUrl = parseQuery(location.search.split('?')[1]).url

await this.setDefinition({ definitionUrl, parserType })

configureAnchors({ offset: -10, scrollDuration: 100 })
}

render () {
const { parsedDefinition: definition, definitionUrl } = this.state
const { location } = this.props

// TODO: add input to add a url
return (
<DocumentTitle title={definition ? definition.title : 'Open API v3 renderer'}>
<div className='main'>
{!definition && "Welcome to Temando's new Open API Renderer. Watch this space!"}
{definition && <Page definition={definition} location={location} specUrl={definitionUrl} />}
</div>
</DocumentTitle>
)
}
}

Base.contextTypes = {
router: PropTypes.object
}

Base.propTypes = {
location: PropTypes.object
}
58 changes: 0 additions & 58 deletions src/handlers/BaseHandler.js

This file was deleted.

19 changes: 3 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
import React from 'react'
import ReactDOM from 'react-dom'
import { Provider } from 'react-redux'
import { Router, browserHistory } from 'react-router'
import { syncHistoryWithStore } from 'react-router-redux'
import 'babel-polyfill'
import { BrowserRouter as Router } from 'react-router-dom'

import routes from './routes'
import configureStore from './store/configure'

// const isBrowser = typeof navigator !== 'undefined' && navigator.indexOf('Node.js') === -1;

const store = configureStore()
const history = syncHistoryWithStore(browserHistory, store)
import { routes } from './routes'

export default class App extends React.Component {
render () {
return (
<Provider store={store}>
<Router history={history}>{routes}</Router>
</Provider>
)
return <Router>{routes}</Router>
}
}

Expand Down
33 changes: 33 additions & 0 deletions src/lib/definitions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import request from 'superagent'
import yaml from 'js-yaml'
import getParserFunction from '../parser/parserFactory'

export async function getDefinition (url) {
if (!url) { throw new Error('Missing url') }

const response = await request
.get(url)
.timeout({
response: 5000,
deadline: 60000
})

let definition = response.body

// TODO move this to another place
if (url.endsWith('.yaml') || url.endsWith('.yml')) {
definition = yaml.safeLoad(response.text)
} else if (url.endsWith('json')) {
definition = JSON.parse(response.text)
}

return definition
}

export async function parseDefinition (definition, parserType) {
const parser = getParserFunction(parserType)

const parsedDefinition = await parser(definition)

return parsedDefinition
}
10 changes: 4 additions & 6 deletions src/routes.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import React from 'react'
import { Route } from 'react-router'
import BaseHandler from './handlers/BaseHandler'
import { Route } from 'react-router-dom'
import Base from './containers/Base'

const routes = (
<Route path='/' component={BaseHandler} />
export const routes = (
<Route exact path='/' component={Base} />
)

export default routes
27 changes: 0 additions & 27 deletions src/store/configure.js

This file was deleted.

73 changes: 0 additions & 73 deletions src/store/definition/actions.js

This file was deleted.

6 changes: 0 additions & 6 deletions src/store/definition/constants.js

This file was deleted.

25 changes: 0 additions & 25 deletions src/store/definition/reducer.js

This file was deleted.

3 changes: 2 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const extractSass = new ExtractTextPlugin({
module.exports = {
context: `${__dirname}/src`,
entry: {
bundle: ['core-js/es7', './index.js'],
// TODO: remove babel polyfill, use transforms
bundle: ['babel-polyfill', 'core-js/es7', './index.js'],

// Everything in the `dependencies` should be considered a `vendor` library
vendor: [
Expand Down
Loading