Fix history logic #504

Closed
btzr-io wants to merge 5 commits from patch-2 into master
5 changed files with 78 additions and 84 deletions

View file

@ -32,6 +32,7 @@ Web UI version numbers should always match the corresponding version of LBRY App
* Support claims no longer show up on Published page (#384) * Support claims no longer show up on Published page (#384)
* Fixed rendering of small prices (#461) * Fixed rendering of small prices (#461)
* Fixed incorrect URI in Downloads/Published page (#460) * Fixed incorrect URI in Downloads/Published page (#460)
* Fixed backward / forward logic of history (#499)
* Fixed menu bug (#503) * Fixed menu bug (#503)
### Deprecated ### Deprecated

View file

@ -29,8 +29,6 @@ export function doNavigate(path, params = {}, options = {}) {
let url = path; let url = path;
if (params) url = `${url}?${toQueryString(params)}`; if (params) url = `${url}?${toQueryString(params)}`;
dispatch(doChangePath(url));
const state = getState(); const state = getState();
const pageTitle = selectPageTitle(state); const pageTitle = selectPageTitle(state);
const historyState = history.state; const historyState = history.state;
@ -38,6 +36,8 @@ export function doNavigate(path, params = {}, options = {}) {
dispatch( dispatch(
doHistoryPush({ params, page: historyState.page + 1 }, pageTitle, url) doHistoryPush({ params, page: historyState.page + 1 }, pageTitle, url)
); );
dispatch(doChangePath(url));
}; };
} }
@ -60,7 +60,7 @@ export function doChangePath(path, options = {}) {
dispatch({ dispatch({
type: types.CHANGE_PATH, type: types.CHANGE_PATH,
data: { data: {
path, path: path.replace(/^#/, ""),
}, },
}); });
@ -83,17 +83,17 @@ export function doChangePath(path, options = {}) {
export function doHistoryBack() { export function doHistoryBack() {
return function(dispatch, getState) { return function(dispatch, getState) {
// Get back history from stack // Get backward history from stack
const back = selectHistoryBack(getState()); const back = selectHistoryBack(getState());
if (back) { if (back) {
// Set location
dispatch(doChangePath(back.location));
dispatch({ dispatch({
type: types.HISTORY_NAVIGATE, type: types.HISTORY_BACKWARD,
data: { page: back }, data: { location: back },
}); });
// Set location
dispatch(doChangePath(back));
} }
}; };
} }
@ -104,13 +104,13 @@ export function doHistoryForward() {
const forward = selectHistoryForward(getState()); const forward = selectHistoryForward(getState());
if (forward) { if (forward) {
// Set location
dispatch(doChangePath(forward.location));
dispatch({ dispatch({
type: types.HISTORY_NAVIGATE, type: types.HISTORY_FORWARD,
data: { page: forward }, data: { location: forward },
}); });
// Set location
dispatch(doChangePath(forward));
} }
}; };
} }
@ -303,23 +303,16 @@ export function doDaemonReady() {
return function(dispatch, getState) { return function(dispatch, getState) {
const path = window.location.hash || "#/discover"; const path = window.location.hash || "#/discover";
const params = parseQueryParams(path.split("?")[1] || ""); const params = parseQueryParams(path.split("?")[1] || "");
const location = path.replace(/^#/, "");
// Get first page
const page = {
index: 0,
location: path.replace(/^#/, ""),
};
history.replaceState( history.replaceState(
{ params, is_first_page: true, page: 1 }, { params, is_first_page: true, page: 1 },
document.title, document.title,
`${path}` `${path}`
); );
dispatch(doAuthenticate()); dispatch(doAuthenticate());
dispatch({ dispatch({ type: types.DAEMON_READY, data: { location } });
type: types.DAEMON_READY,
data: { page },
});
dispatch(doFetchDaemonSettings()); dispatch(doFetchDaemonSettings());
dispatch(doFileList()); dispatch(doFileList());
}; };

View file

@ -2,6 +2,8 @@ export const CHANGE_PATH = "CHANGE_PATH";
export const OPEN_MODAL = "OPEN_MODAL"; export const OPEN_MODAL = "OPEN_MODAL";
export const CLOSE_MODAL = "CLOSE_MODAL"; export const CLOSE_MODAL = "CLOSE_MODAL";
export const HISTORY_NAVIGATE = "HISTORY_NAVIGATE"; export const HISTORY_NAVIGATE = "HISTORY_NAVIGATE";
export const HISTORY_BACKWARD = "HISTORY_BACKWARD";
export const HISTORY_FORWARD = "HISTORY_FORWARD";
export const SHOW_SNACKBAR = "SHOW_SNACKBAR"; export const SHOW_SNACKBAR = "SHOW_SNACKBAR";
export const REMOVE_SNACKBAR_SNACK = "REMOVE_SNACKBAR_SNACK"; export const REMOVE_SNACKBAR_SNACK = "REMOVE_SNACKBAR_SNACK";
export const WINDOW_FOCUSED = "WINDOW_FOCUSED"; export const WINDOW_FOCUSED = "WINDOW_FOCUSED";

View file

@ -24,19 +24,17 @@ const defaultState = {
daemonReady: false, daemonReady: false,
hasSignature: false, hasSignature: false,
badgeNumber: 0, badgeNumber: 0,
history: { index: 0, stack: [] }, history: {
stack: { backward: [], forward: [] },
currentPage: "/discover",
},
volume: sessionStorage.getItem("volume") || 1, volume: sessionStorage.getItem("volume") || 1,
}; };
reducers[types.DAEMON_READY] = function(state, action) { reducers[types.DAEMON_READY] = function(state, action) {
const { history } = state; const { history } = state;
const { page } = action.data; history.currentPage = action.data.location.replace(/[?]$/, "");
history.stack.push(page); return Object.assign({}, state, { daemonReady: true, history });
return Object.assign({}, state, {
daemonReady: true,
history,
});
}; };
reducers[types.DAEMON_VERSION_MATCH] = function(state, action) { reducers[types.DAEMON_VERSION_MATCH] = function(state, action) {
@ -172,51 +170,63 @@ reducers[types.WINDOW_FOCUSED] = function(state, action) {
}; };
reducers[types.HISTORY_NAVIGATE] = (state, action) => { reducers[types.HISTORY_NAVIGATE] = (state, action) => {
let page = false;
let location = false;
// Get history from state // Get history from state
const { history } = state; const { history } = state;
const { currentPage } = history;
if (action.data.page) { const location = action.data.location.replace(/[?]$/, "");
// Get page
page = action.data.page;
} else if (action.data.location) {
// Get new location
location = action.data.location;
}
// Add new location to stack
if (location) {
const lastItem = history.stack.length - 1;
// Check for duplicated // Check for duplicated
let is_duplicate = lastItem > -1 let is_duplicate = currentPage === location;
? history.stack[lastItem].location === location
: false;
if (!is_duplicate) { if (!is_duplicate) {
// Create new page history.stack.backward.push(currentPage);
page = { history.stack.forward = [];
index: history.stack.length, history.currentPage = location;
location,
};
// Update index
history.index = history.stack.length;
// Add to stack
history.stack.push(page);
}
} else if (page) {
// Update index
history.index = page.index;
} }
return Object.assign({}, state, { return Object.assign({}, state, {
history, history,
isBackDisabled: history.index === 0, // First page isBackDisabled: history.stack.backward.length === 0, // First page
isForwardDisabled: history.index === history.stack.length - 1, // Last page isForwardDisabled: history.stack.forward.length === 0, // Last page });
});
};
reducers[types.HISTORY_BACKWARD] = (state, action) => {
// Get history from state
const { history } = state;
const { currentPage } = history;
let { location } = action.data;
// Update history
history.stack.forward.push(currentPage);
//history.stack.backward.pop();
history.currentPage = location;
return Object.assign({}, state, {
history,
isBackDisabled: history.stack.backward.length === 0, // First page
isForwardDisabled: history.stack.forward.length === 0, // Last page });
});
};
reducers[types.HISTORY_FORWARD] = (state, action) => {
// Get history from state
const { history } = state;
const { currentPage } = history;
let { location } = action.data;
// Update history
history.stack.backward.push(currentPage);
// history.stack.forward.pop();
history.currentPage = location;
return Object.assign({}, state, {
history,
isBackDisabled: history.stack.backward.length === 0, // First page
isForwardDisabled: history.stack.forward.length === 0, // Last page });
}); });
}; };

View file

@ -239,25 +239,13 @@ export const selectIsForwardDisabled = createSelector(
); );
export const selectHistoryBack = createSelector(_selectState, state => { export const selectHistoryBack = createSelector(_selectState, state => {
const { history } = state; const { stack } = state.history;
const index = history.index - 1; return stack.backward.pop();
// Check if page exists
if (index > -1) {
// Get back history
return history.stack[index];
}
}); });
export const selectHistoryForward = createSelector(_selectState, state => { export const selectHistoryForward = createSelector(_selectState, state => {
const { history } = state; const { stack } = state.history;
const index = history.index + 1; return stack.forward.pop();
// Check if page exists
if (index <= history.stack.length) {
// Get forward history
return history.stack[index];
}
}); });
export const selectVolume = createSelector(_selectState, state => state.volume); export const selectVolume = createSelector(_selectState, state => state.volume);