Skip to content

Commit a5e2d90

Browse files
nfourbrendo
authored andcommitted
Refactor/state management (temando#109)
Remove redux, upgrade react-router to V4. Closes temando#95.
1 parent dcd1e9e commit a5e2d90

File tree

13 files changed

+198
-506
lines changed

13 files changed

+198
-506
lines changed

package.json

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,15 @@
3737
"lodash": "^4.17.4",
3838
"markdown-it": "^8.3.1",
3939
"prop-types": "^15.5.8",
40+
"qs": "^6.4.0",
4041
"react": "^15.5.4",
4142
"react-addons-create-fragment": "^15.5.4",
4243
"react-document-title": "^2.0.2",
4344
"react-dom": "^15.5.4",
44-
"react-json-view": "^1.8.2",
45-
"react-redux": "^5.0.3",
46-
"react-router": "3.0.0",
47-
"react-router-redux": "^4.0.8",
45+
"react-json-view": "^1.8.4",
46+
"react-router": "^4.1.1",
47+
"react-router-dom": "^4.1.1",
4848
"react-scrollable-anchor": "^0.4.2",
49-
"redux": "^3.6.0",
50-
"redux-thunk": "^2.2.0",
5149
"superagent": "^3.5.2"
5250
},
5351
"devDependencies": {
@@ -61,11 +59,11 @@
6159
"babel-preset-stage-1": "^6.22.0",
6260
"balloon-css": "^0.4.0",
6361
"css-loader": "^0.28.4",
64-
"eslint": "^3.19.0",
62+
"eslint": "^4.0.0",
6563
"eslint-config-standard": "^10.2.1",
6664
"eslint-config-standard-react": "^5.0.0",
6765
"eslint-plugin-import": "^2.3.0",
68-
"eslint-plugin-node": "^4.2.2",
66+
"eslint-plugin-node": "^5.0.0",
6967
"eslint-plugin-promise": "^3.5.0",
7068
"eslint-plugin-react": "^7.0.1",
7169
"eslint-plugin-standard": "^3.0.1",

src/components/Example/Example.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ export default class Example extends Component {
5858
displayObjectSize={false}
5959
collapsed={this.state.collapseAll}
6060
enableClipboard={false}
61-
/>}
61+
/>
62+
}
6263
</div>
6364
)
6465
}

src/containers/Base.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import React, { Component } from 'react'
2+
import { configureAnchors } from 'react-scrollable-anchor'
3+
import DocumentTitle from 'react-document-title'
4+
import PropTypes from 'prop-types'
5+
import { parse as parseQuery } from 'qs'
6+
7+
import Page from '../components/Page/Page'
8+
import { getDefinition, parseDefinition } from '../lib/definitions'
9+
import '../general.scss'
10+
11+
export default class Base extends Component {
12+
state = {
13+
parserType: 'open-api-v3',
14+
definitionUrl: null,
15+
definition: null,
16+
parsedDefinition: null
17+
}
18+
19+
setDefinition = async ({ definitionUrl, parserType = this.state.parserType }) => {
20+
const definition = await getDefinition(definitionUrl)
21+
const parsedDefinition = await parseDefinition(definition, parserType)
22+
23+
this.setState({ definitionUrl, definition, parsedDefinition, parserType })
24+
}
25+
26+
async componentDidMount () {
27+
const { location } = this.props
28+
const { parserType } = this.state
29+
30+
const definitionUrl = parseQuery(location.search.split('?')[1]).url
31+
32+
await this.setDefinition({ definitionUrl, parserType })
33+
34+
configureAnchors({ offset: -10, scrollDuration: 100 })
35+
}
36+
37+
render () {
38+
const { parsedDefinition: definition, definitionUrl } = this.state
39+
const { location } = this.props
40+
41+
// TODO: add input to add a url
42+
return (
43+
<DocumentTitle title={definition ? definition.title : 'Open API v3 renderer'}>
44+
<div className='main'>
45+
{!definition && "Welcome to Temando's new Open API Renderer. Watch this space!"}
46+
{definition && <Page definition={definition} location={location} specUrl={definitionUrl} />}
47+
</div>
48+
</DocumentTitle>
49+
)
50+
}
51+
}
52+
53+
Base.contextTypes = {
54+
router: PropTypes.object
55+
}
56+
57+
Base.propTypes = {
58+
location: PropTypes.object
59+
}

src/handlers/BaseHandler.js

Lines changed: 0 additions & 58 deletions
This file was deleted.

src/index.js

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,12 @@
11
import React from 'react'
22
import ReactDOM from 'react-dom'
3-
import { Provider } from 'react-redux'
4-
import { Router, browserHistory } from 'react-router'
5-
import { syncHistoryWithStore } from 'react-router-redux'
6-
import 'babel-polyfill'
3+
import { BrowserRouter as Router } from 'react-router-dom'
74

8-
import routes from './routes'
9-
import configureStore from './store/configure'
10-
11-
// const isBrowser = typeof navigator !== 'undefined' && navigator.indexOf('Node.js') === -1;
12-
13-
const store = configureStore()
14-
const history = syncHistoryWithStore(browserHistory, store)
5+
import { routes } from './routes'
156

167
export default class App extends React.Component {
178
render () {
18-
return (
19-
<Provider store={store}>
20-
<Router history={history}>{routes}</Router>
21-
</Provider>
22-
)
9+
return <Router>{routes}</Router>
2310
}
2411
}
2512

src/lib/definitions.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import request from 'superagent'
2+
import yaml from 'js-yaml'
3+
import getParserFunction from '../parser/parserFactory'
4+
5+
export async function getDefinition (url) {
6+
if (!url) { throw new Error('Missing url') }
7+
8+
const response = await request
9+
.get(url)
10+
.timeout({
11+
response: 5000,
12+
deadline: 60000
13+
})
14+
15+
let definition = response.body
16+
17+
// TODO move this to another place
18+
if (url.endsWith('.yaml') || url.endsWith('.yml')) {
19+
definition = yaml.safeLoad(response.text)
20+
} else if (url.endsWith('json')) {
21+
definition = JSON.parse(response.text)
22+
}
23+
24+
return definition
25+
}
26+
27+
export async function parseDefinition (definition, parserType) {
28+
const parser = getParserFunction(parserType)
29+
30+
const parsedDefinition = await parser(definition)
31+
32+
return parsedDefinition
33+
}

src/routes.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import React from 'react'
2-
import { Route } from 'react-router'
3-
import BaseHandler from './handlers/BaseHandler'
2+
import { Route } from 'react-router-dom'
3+
import Base from './containers/Base'
44

5-
const routes = (
6-
<Route path='/' component={BaseHandler} />
5+
export const routes = (
6+
<Route exact path='/' component={Base} />
77
)
8-
9-
export default routes

src/store/configure.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/store/definition/actions.js

Lines changed: 0 additions & 73 deletions
This file was deleted.

src/store/definition/constants.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/store/definition/reducer.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

webpack.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ const extractSass = new ExtractTextPlugin({
1313
module.exports = {
1414
context: `${__dirname}/src`,
1515
entry: {
16-
bundle: ['core-js/es7', './index.js'],
16+
// TODO: remove babel polyfill, use transforms
17+
bundle: ['babel-polyfill', 'core-js/es7', './index.js'],
1718

1819
// Everything in the `dependencies` should be considered a `vendor` library
1920
vendor: [

0 commit comments

Comments
 (0)