Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

restoreLastLocation: use localforage, don't redir outside Calypso #14196

Merged
merged 3 commits into from
May 31, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 31 additions & 18 deletions client/state/routing/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,48 @@
*/
import debugFactory from 'debug';
import page from 'page';
import store from 'store';

/**
* Internal dependencies
*/
import localforage from 'lib/localforage';
import { isOutsideCalypso } from 'lib/url';
import { ROUTE_SET } from 'state/action-types';

const debug = debugFactory( 'calypso:restore-last-location' );
const LAST_PATH = 'last_path';

let hasInitialized = false;
export const restoreLastLocation = () => {
let hasInitialized = false;

export const restoreLastLocation = () => ( next ) => ( action ) => {
if ( action.type === ROUTE_SET && action.path ) {
const lastPath = store.get( 'last_path' );

if ( ! hasInitialized && lastPath && lastPath !== '/' && action.path === '/' ) {
debug( 'redir to', action.path );
page( lastPath );
} else {
debug( 'saving', action.path );
store.set( 'last_path', action.path );
}

if ( ! hasInitialized ) {
hasInitialized = true;
return ( next ) => ( action ) => {
if ( action.type !== ROUTE_SET || ! action.path ) {
return next( action );
}
}

return next( action );
localforage.getItem( LAST_PATH ).then(
( lastPath ) => {
if ( ! hasInitialized &&
lastPath && lastPath !== '/' &&
action.path === '/' &&
! isOutsideCalypso( lastPath ) ) {
debug( 'redir to', lastPath );
page( lastPath );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still seeing a flash of the reader. Perhaps we need to return early here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. I think we'll play with this in another PR.

} else if ( action.path !== lastPath &&
! isOutsideCalypso( action.path ) ) {
debug( 'saving', action.path );
localforage.setItem( LAST_PATH, action.path );
}

if ( ! hasInitialized ) {
hasInitialized = true;
}

return next( action );
},
() => next( action )
);
};
};

export default restoreLastLocation;