Skip to content

Commit

Permalink
Don't use custom history API (carbon-app#485)
Browse files Browse the repository at this point in the history
* Add shallowEquals check for onUpdate

* Using Next.js Router instead of custom history API

Fixes vercel/next.js#4994

* Remove urlObject code
  • Loading branch information
timneutkens authored and Michael Fix committed Aug 21, 2018
1 parent d41f05d commit 2e945fc
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 25 deletions.
7 changes: 5 additions & 2 deletions components/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { DragDropContext } from 'react-dnd'
import domtoimage from 'dom-to-image'
import ReadFileDropContainer, { DATA_URL, TEXT } from 'dropperx'
import Spinner from 'react-spinner'
import shallowEquals from '../lib/shallow-equals'

// Ours
import Button from './Button'
Expand Down Expand Up @@ -113,8 +114,10 @@ class Editor extends React.Component {
window.removeEventListener('online', this.setOnline)
}

componentDidUpdate() {
this.props.onUpdate(this.state)
componentDidUpdate(prevProps, prevState) {
if(!shallowEquals(this.state, prevState)) {
this.props.onUpdate(this.state)
}
}

getCarbonImage({ format, type } = { format: 'png' }) {
Expand Down
29 changes: 9 additions & 20 deletions lib/routing.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import Morph from 'morphmorph'

let createHistory
let history
if (typeof window !== 'undefined') {
createHistory = require('history').createBrowserHistory
history = createHistory()
}

const mapper = new Morph({
types: {
bool: v => {
Expand Down Expand Up @@ -68,11 +61,6 @@ export const deserializeState = serializedState => {
return JSON.parse(decodeURIComponent(stateString))
}

const keysToQuery = keys =>
`?${Object.keys(keys)
.map(key => `${key}=${keys[key]}`)
.join('&')}`

export const getQueryStringState = query => {
if (query.state) {
return deserializeState(query.state)
Expand All @@ -88,15 +76,16 @@ export const getQueryStringState = query => {
return state
}

export const updateQueryString = state => {
if (history.location.search.indexOf('react_perf') < 0) {
const mappedState = mapper.map(reverseMappings, state)
serializeCode(mappedState)
export const updateQueryString = (router, state) => {
const mappedState = mapper.map(reverseMappings, state)
serializeCode(mappedState)

history.replace({
search: encodeURI(keysToQuery(mappedState))
})
}
router.replace({
pathname: router.pathname
}, {
pathname: router.pathname,
query: mappedState
}, {shallow: true})
}

// private
Expand Down
25 changes: 25 additions & 0 deletions lib/shallow-equals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export default function shallowEquals (a, b) {
for (const i in a) {
// NaN === NaN is false in Javascript
if(isNaN(b[i]) && isNaN(a[i])) {
continue
}
if (b[i] !== a[i]) {
console.log('DID NOT MATCH', i, a[i], b[i])
return false
}
}

for (const i in b) {
// NaN === NaN is false in Javascript
if(isNaN(b[i]) && isNaN(a[i])) {
continue
}

if (b[i] !== a[i]) {
return false
}
}

return true
}
7 changes: 4 additions & 3 deletions pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ import { saveState } from '../lib/util'

class Index extends React.Component {
render() {
const {router} = this.props
return (
<Page enableHeroText={true}>
<Editor {...this.props.router} onUpdate={onEditorUpdate} api={api} />
<Editor {...router} onUpdate={(state) => onEditorUpdate(router, state)} api={api} />
</Page>
)
}
}

function onEditorUpdate(state) {
updateQueryString(state)
function onEditorUpdate(router, state) {
updateQueryString(router, state)
const s = { ...state }
delete s.code
delete s.backgroundImage
Expand Down

0 comments on commit 2e945fc

Please sign in to comment.