From c0d5c49283948d76824a2d027b407839fa68735b Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sat, 30 Dec 2017 14:00:42 -0500 Subject: [PATCH] Improve routing edge cases --- src/actions/routing.js | 22 ++++++++++++++++++++++ src/actions/selection.js | 6 +++--- src/ui/navigation.js | 4 ++-- 3 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 src/actions/routing.js diff --git a/src/actions/routing.js b/src/actions/routing.js new file mode 100644 index 0000000..60f3924 --- /dev/null +++ b/src/actions/routing.js @@ -0,0 +1,22 @@ +import { push } from 'react-router-redux'; + +// Pushes but preserves the query string +export function push_path(to) { + return (dispatch, getState) => { + const { router } = getState(); + if (router.location.search !== "") { + dispatch(push(`${to}${router.location.search}`)); + } else { + dispatch(push(to)); + } + }; +} + +export function push_query(to) { + return (dispatch, getState) => { + const { router } = getState(); + dispatch(push(`${ + router.location.pathname === "/" ? "" : router.location.pathname + }${to}`)) + }; +} diff --git a/src/actions/selection.js b/src/actions/selection.js index f6595da..895ce42 100644 --- a/src/actions/selection.js +++ b/src/actions/selection.js @@ -1,6 +1,6 @@ import { filter_subscribe, filter_unsubscribe } from './filter_subscribe'; import { unsubscribe } from './subscribe'; -import { push } from 'react-router-redux'; +import { push_path } from './routing'; export const UNION = 'UNION'; export const SUBTRACT = 'SUBTRACT'; @@ -55,9 +55,9 @@ export default function selectTorrent(ids, action, navigate=true) { if (navigate) { const url_torrents = state.selection.slice(0, 3); if (url_torrents.length > 0) { - dispatch(push(`/torrents/${url_torrents}`)); + dispatch(push_path(`/torrents/${url_torrents}`)); } else { - dispatch(push("/")); + dispatch(push_path("/")); } } }; diff --git a/src/ui/navigation.js b/src/ui/navigation.js index b3097be..835d59a 100644 --- a/src/ui/navigation.js +++ b/src/ui/navigation.js @@ -2,7 +2,7 @@ import React from 'react'; import { connect } from 'react-redux'; import { Link, NavLink } from 'react-router-dom'; import { filter_subscribe } from '../actions/filter_subscribe'; -import { push } from 'react-router-redux'; +import { push_query } from '../actions/routing'; import query from 'query-string'; import search_criteria from '../search'; @@ -21,7 +21,7 @@ function update_filter(text, fs, location, dispatch) { const tfilter = fs.filter(fs => fs.kind === "torrent")[0]; const criteria = search_criteria(text); dispatch(filter_subscribe("torrent", criteria, tfilter.serial)); - dispatch(push(search_qs(text))); + dispatch(push_query(search_qs(text))); } function render(props) {