Colocate reducers, actions and selectors

This commit is contained in:
Ken-Håvard Lieng 2017-05-26 08:20:00 +02:00
parent 1e7d4c3fe4
commit 889e3b88b7
53 changed files with 1031 additions and 914 deletions

View file

@ -1,66 +0,0 @@
import { Record, List } from 'immutable';
import { LOCATION_CHANGED } from '../util/router';
import createReducer from '../util/createReducer';
import * as actions from '../actions';
const TabRecord = Record({
server: null,
name: null
});
class Tab extends TabRecord {
isChannel() {
return this.name && this.name.charAt(0) === '#';
}
toString() {
let str = this.server;
if (this.name) {
str += `:${this.name}`;
}
return str;
}
}
const State = Record({
selected: new Tab(),
history: List()
});
function selectTab(state, action) {
const tab = new Tab(action);
return state
.set('selected', tab)
.update('history', history => history.push(tab));
}
export const getSelectedTab = state => state.tab.selected;
export default createReducer(new State(), {
[actions.SELECT_TAB]: selectTab,
[actions.PART](state, action) {
return state.set('history', state.history.filter(tab =>
!(tab.server === action.server && tab.name === action.channels[0])
));
},
[actions.CLOSE_PRIVATE_CHAT](state, action) {
return state.set('history', state.history.filter(tab =>
!(tab.server === action.server && tab.name === action.nick)
));
},
[actions.DISCONNECT](state, action) {
return state.set('history', state.history.filter(tab => tab.server !== action.server));
},
[LOCATION_CHANGED](state, action) {
const { route, params } = action;
if (route === 'chat') {
return selectTab(state, params);
}
return state.set('selected', new Tab());
}
});