Skip to content
This repository was archived by the owner on Sep 8, 2021. It is now read-only.

fix: dont dispatch resolved promise action if view has changed #8

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
294 changes: 181 additions & 113 deletions bin/main.js

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion src/components/ProfileFavorites.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ const store = require('../store');
class ProfileFavorites extends Profile {
componentWillMount() {
store.dispatch({
type: 'PROFILE_PAGE_LOADED',
type: 'PROFILE_FAVORITES_PAGE_LOADED',
payload: Promise.all([
agent.Profile.get(this.props.params.username),
agent.Articles.favoritedBy(this.props.params.username)
])
});
}

componentWillUnmount() {
store.dispatch({ type: 'PROFILE_FAVORITES_PAGE_UNLOADED' });
}

renderTabs() {
return (
<ul className="nav nav-pills outline-active">
Expand Down
13 changes: 11 additions & 2 deletions src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,31 @@ const agent = require('./agent');
exports.promiseMiddleware = store => next => action => {
if (isPromise(action.payload)) {
store.dispatch({ type: 'ASYNC_START', subtype: action.type });
const initialView = store.getState().viewChangeCounter;
action.payload.then(
res => {
// The view might have changed mid-promise, so if the view unloaded,
// don't dispatch the action.
const finalView = store.getState().viewChangeCounter;
if (finalView !== initialView) {
return;
}
console.log('RESULT', res);
action.payload = res;
store.dispatch(action);
},
error => {
const finalView = store.getState().viewChangeCounter;
if (finalView !== initialView) {
return;
}
console.log('ERROR', error);
action.error = true;
action.payload = error.response.body;
store.dispatch(action);
}
);

store.dispatch({ type: 'LOADING' });

return;
}

Expand Down
9 changes: 7 additions & 2 deletions src/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ const auth = require('./reducers/auth');
const editor = require('./reducers/editor');
const home = require('./reducers/home');
const profile = require('./reducers/profile');
const profileFavorites = require('./reducers/profileFavorites');
const settings = require('./reducers/settings');

const defaultState = {
appName: 'Conduit',
token: null
token: null,
viewChangeCounter: 0
};

module.exports = (state = defaultState, action) => {
Expand All @@ -18,6 +20,7 @@ module.exports = (state = defaultState, action) => {
state = editor(state, action);
state = home(state, action);
state = profile(state, action);
state = profileFavorites(state, action);
state = settings(state, action);
switch (action.type) {
case 'APP_LOAD':
Expand Down Expand Up @@ -46,7 +49,9 @@ module.exports = (state = defaultState, action) => {
state = Object.assign({}, state, { redirectTo: '/' });
break;
case 'ARTICLE_PAGE_UNLOADED':
state = Object.assign({}, state);
state = Object.assign({}, state, {
viewChangeCounter: state.viewChangeCounter + 1
});
delete state.article;
delete state.comments;
delete state.commentErrors;
Expand Down
4 changes: 3 additions & 1 deletion src/reducers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ module.exports = (state, action) => {
break;
case 'LOGIN_PAGE_UNLOADED':
case 'REGISTER_PAGE_UNLOADED':
state = Object.assign({}, state);
state = Object.assign({}, state, {
viewChangeCounter: state.viewChangeCounter + 1
});
const props = ['errors', 'username', 'email', 'password', 'inProgress']
for (const key of props) {
delete state[key];
Expand Down
4 changes: 3 additions & 1 deletion src/reducers/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ module.exports = (state, action) => {
}
break;
case 'EDITOR_PAGE_UNLOADED':
state = Object.assign({}, state);
state = Object.assign({}, state, {
viewChangeCounter: state.viewChangeCounter + 1
});
const keys = [
'title',
'description',
Expand Down
4 changes: 3 additions & 1 deletion src/reducers/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ module.exports = (state, action) => {
});
break;
case 'HOME_PAGE_UNLOADED':
state = Object.assign({}, state);
state = Object.assign({}, state, {
viewChangeCounter: state.viewChangeCounter + 1
});
delete state.articles;
delete state.tags;
delete state.tab;
Expand Down
4 changes: 3 additions & 1 deletion src/reducers/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ module.exports = (state = defaultState, action) => {
});
break;
case 'PROFILE_PAGE_UNLOADED':
state = Object.assign({}, state);
state = Object.assign({}, state, {
viewChangeCounter: state.viewChangeCounter + 1
});
delete state.profile;
delete state.articles;
delete state.articlesCount;
Expand Down
12 changes: 12 additions & 0 deletions src/reducers/profileFavorites.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

module.exports = (state = defaultState, action) => {
switch (action.type) {
case 'PROFILE_FAVORITES_PAGE_UNLOADED':
return Object.assign({}, state, {
viewChangeCounter: state.viewChangeCounter + 1
});
}

return state;
};
4 changes: 3 additions & 1 deletion src/reducers/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ module.exports = (state, action) => {
}
break;
case 'SETTINGS_PAGE_UNLOADED':
state = Object.assign({}, state);
state = Object.assign({}, state, {
viewChangeCounter: state.viewChangeCounter + 1
});
for (const key of ['errors', 'inProgress']) {
delete state[key];
}
Expand Down