Fix issue with transitioning between routes

This commit is contained in:
Berkeley Martinez
2015-10-27 23:46:42 -07:00
parent d633f74ff9
commit 83e82bd967
7 changed files with 34 additions and 14 deletions

View File

@ -22,6 +22,19 @@ const history = createHistory();
const appLocation = createLocation( const appLocation = createLocation(
location.pathname + location.search location.pathname + location.search
); );
function location$(history) {
return Rx.Observable.create(function(observer) {
const dispose = history.listen(function(location) {
observer.onNext(location.pathname);
});
return Rx.Disposable.create(() => {
dispose();
});
});
}
// returns an observable // returns an observable
app$({ history, location: appLocation }) app$({ history, location: appLocation })
.flatMap( .flatMap(
@ -37,18 +50,25 @@ app$({ history, location: appLocation })
({ nextLocation, props }, appCat) => ({ nextLocation, props, appCat }) ({ nextLocation, props }, appCat) => ({ nextLocation, props, appCat })
) )
.doOnNext(({ appCat }) => { .doOnNext(({ appCat }) => {
const appStore = appCat.getStore('appStore');
const appActions = appCat.getActions('appActions'); const appActions = appCat.getActions('appActions');
appStore location$(history)
.pluck('pathname')
.distinctUntilChanged() .distinctUntilChanged()
.subscribe(function({ route = appLocation.pathname }) { .doOnNext(route => debug('route change', route))
history.pushState(null, route); .subscribe(route => appActions.updateRoute(route));
});
appActions.goBack.subscribe(function() { appActions.goBack.subscribe(function() {
history.goBack(); history.goBack();
}); });
appActions
.updateRoute
.pluck('route')
.doOnNext(route => debug('update route', route))
.subscribe(function(route) {
history.pushState(null, route);
});
}) })
.flatMap(({ props, appCat }) => { .flatMap(({ props, appCat }) => {
props.history = history; props.history = history;
@ -63,7 +83,7 @@ app$({ history, location: appLocation })
debug('react rendered'); debug('react rendered');
}, },
err => { err => {
debug('an error has occured', err.stack); throw err;
}, },
() => { () => {
debug('react closed subscription'); debug('react closed subscription');

View File

@ -17,7 +17,7 @@ export default Actions({
}, },
getUser: null, getUser: null,
goTo(route) { updateRoute(route) {
return { route }; return { route };
}, },
goBack: null goBack: null

View File

@ -14,10 +14,10 @@ export default Store({
value: initValue value: initValue
}, },
init({ instance: appStore, args: [cat] }) { init({ instance: appStore, args: [cat] }) {
const { goTo, setUser, setTitle } = cat.getActions('appActions'); const { updateRoute, setUser, setTitle } = cat.getActions('appActions');
const register = createRegistrar(appStore); const register = createRegistrar(appStore);
register(setter(fromMany(setUser, setTitle, goTo))); register(setter(fromMany(setUser, setTitle, updateRoute)));
return appStore; return appStore;
} }

View File

@ -57,7 +57,7 @@ export default contain(
goToJobBoard() { goToJobBoard() {
const { appActions } = this.props; const { appActions } = this.props;
appActions.goTo('/jobs'); appActions.updateRoute('/jobs');
}, },
renderDiscount(discountAmount) { renderDiscount(discountAmount) {

View File

@ -37,7 +37,7 @@ export default contain(
return null; return null;
} }
jobActions.findJob(id); jobActions.findJob(id);
appActions.goTo(`/jobs/${id}`); appActions.updateRoute(`/jobs/${id}`);
}, },
renderList(handleJobClick, jobs) { renderList(handleJobClick, jobs) {
@ -91,7 +91,7 @@ export default contain(
bsSize='large' bsSize='large'
className='signup-btn btn-block' className='signup-btn btn-block'
onClick={ ()=> { onClick={ ()=> {
appActions.goTo('/jobs/new'); appActions.updateRoute('/jobs/new');
}}> }}>
Post a job: $200 for 30 days + weekly tweets Post a job: $200 for 30 days + weekly tweets
</Button> </Button>

View File

@ -32,7 +32,7 @@ export default contain(
const { appActions, job } = this.props; const { appActions, job } = this.props;
// redirect user in client // redirect user in client
if (!job || !job.position || !job.description) { if (!job || !job.position || !job.description) {
appActions.goTo('/jobs/new'); appActions.updateRoute('/jobs/new');
} }
}, },

View File

@ -151,7 +151,7 @@ export default Actions({
return jobActions.setError(err); return jobActions.setError(err);
} }
jobActions.setJobs({ job }); jobActions.setJobs({ job });
appActions.goTo(goTo); appActions.updateRoute(goTo);
}); });
}); });