This repository has been archived by the owner on Nov 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #236 from jamesplease/serialize-search
Serialize Icons catalog search into URL
- Loading branch information
Showing
4 changed files
with
94 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// This code is from the qhistory library: | ||
// https://github.com/pshrmn/qhistory | ||
// It didn't seem like big enough code to justify a new dep | ||
|
||
/* eslint-disable */ | ||
|
||
export default function historyWithQuery(history, stringify, parse) { | ||
const addSearch = location => { | ||
if (typeof location === 'object') { | ||
location.search = location.query | ||
? stringify(location.query) | ||
: location.search || ''; | ||
} | ||
}; | ||
|
||
const addQuery = location => { | ||
const { search } = location; | ||
if (search) { | ||
location.query = parse( | ||
search.charAt(0) === '?' ? search.substring(1) : search | ||
); | ||
} else { | ||
location.query = {}; | ||
} | ||
}; | ||
|
||
const updateProperties = (history, queryHistory) => { | ||
const properties = ['location', 'length', 'entries', 'index', 'action']; | ||
properties.forEach(prop => { | ||
if (history.hasOwnProperty(prop)) { | ||
queryHistory[prop] = history[prop]; | ||
} | ||
}); | ||
}; | ||
|
||
// make sure that the initial location has query support | ||
addQuery(history.location); | ||
|
||
const queryHistory = { | ||
...history, | ||
push: (location, state) => { | ||
addSearch(location); | ||
history.push(location, state); | ||
}, | ||
replace: (location, state) => { | ||
addSearch(location); | ||
history.replace(location, state); | ||
}, | ||
createHref: location => { | ||
addSearch(location); | ||
return history.createHref(location); | ||
}, | ||
}; | ||
|
||
// This relies on being the first listener called by | ||
// the actual history instance. If you register a | ||
// listener on the history instance before modifying | ||
// it with qhistory, the location object will not have | ||
// the query property set on it when that listener | ||
// is called. | ||
history.listen(location => { | ||
addQuery(location); | ||
updateProperties(history, queryHistory); | ||
}); | ||
|
||
return queryHistory; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters