mirror of
https://github.com/chanzuckerberg/cellxgene.git
synced 2026-10-06 05:18:11 +08:00
on url change util, to be moved to middleware
This commit is contained in:
+13
-20
@@ -1,4 +1,3 @@
|
|||||||
import queryString from "query-string";
|
|
||||||
import URI from "urijs";
|
import URI from "urijs";
|
||||||
|
|
||||||
export const updateCategoricalQueryParams = (category, value) => {
|
export const updateCategoricalQueryParams = (category, value) => {
|
||||||
@@ -9,33 +8,27 @@ export const updateCategoricalQueryParams = (category, value) => {
|
|||||||
there are four cases
|
there are four cases
|
||||||
1. there are no query params
|
1. there are no query params
|
||||||
2. there are query params, but our category isn't there yet
|
2. there are query params, but our category isn't there yet
|
||||||
3. there are query params and this is not the first entry to the given category
|
3. there are query params and this is not the first entry to the given category (urijs may handle this with 'normalize')
|
||||||
4. there are query params and ours already exists (remove it)
|
4. there are query params and ours already exists (remove it)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
const uri = new URI(window.location.href)
|
||||||
|
|
||||||
if (window.location.search === "") {
|
if (window.location.search === "") {
|
||||||
/* #1 */
|
newURL = uri.addQuery(category, value).toString(); /* #1 */
|
||||||
newURL = `${window.location.href}?${queryString.stringify({category: [value]})}`
|
} else if (uri.hasQuery(category, value) || uri.hasQuery(category, value, true)) { /* true param here means check arrays as well http://medialize.github.io/URI.js/docs.html#search-has */
|
||||||
window.history.pushState("", "", newUrl)
|
newURL = uri.removeQuery(category, value).toString(); /* #4 */
|
||||||
} else {
|
} else {
|
||||||
|
newURL = uri.addQuery(category, value).toString(); /* #2 & #3 are handled by URI */
|
||||||
const existingParams = queryString.parse(window.location.search);
|
|
||||||
const categoryAlreadyExists = existingParams[category];
|
|
||||||
const valueAlreadyExists = existingParams[category] && categoryAlreadyExists[value];
|
|
||||||
|
|
||||||
if (valueAlreadyExists) {
|
|
||||||
/* #4 (removal) */
|
|
||||||
|
|
||||||
newURL = `${window.location.href}?${queryString.stringify({category: [value]})}`
|
|
||||||
} else {
|
|
||||||
/* #3 */
|
|
||||||
existingParams
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
window.history.pushState("", "", newURL)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const updateContinuousParams = (category, range) => {
|
export const updateContinuousParams = (category, range) => {
|
||||||
|
/*
|
||||||
|
this is similar to updateCategoricalQueryParams, but has a different check since
|
||||||
|
in this case, same key (Unique_reads) different values (>500 vs >5000) should always replace previous range.
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user