Improve routing edge cases

This commit is contained in:
Drew DeVault 2017-12-30 14:00:42 -05:00
parent 6d509a4f29
commit c0d5c49283
3 changed files with 27 additions and 5 deletions

22
src/actions/routing.js Normal file
View File

@ -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}`))
};
}

View File

@ -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("/"));
}
}
};

View File

@ -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) {