Move user sorting to a selector

This commit is contained in:
Ken-Håvard Lieng 2017-07-06 06:46:53 +02:00
parent c005fc7cae
commit 8b44f68231
3 changed files with 50 additions and 59 deletions

File diff suppressed because one or more lines are too long

View File

@ -113,8 +113,8 @@ describe('channel reducer', () => {
srv: { srv: {
chan1: { chan1: {
users: [ users: [
{ mode: '', nick: 'nick2', renderName: 'nick2' }, { mode: '', nick: 'nick3', renderName: 'nick3' },
{ mode: '', nick: 'nick3', renderName: 'nick3' } { mode: '', nick: 'nick2', renderName: 'nick2' }
] ]
}, },
chan2: { chan2: {
@ -144,11 +144,11 @@ describe('channel reducer', () => {
srv: { srv: {
chan1: { chan1: {
users: [ users: [
{ mode: 'o', nick: 'user4', renderName: '@user4' }, { mode: '', nick: 'user3', renderName: 'user3' },
{ mode: 'v', nick: 'user5', renderName: '+user5' },
{ mode: '', nick: 'user1', renderName: 'user1' },
{ mode: '', nick: 'user2', renderName: 'user2' }, { mode: '', nick: 'user2', renderName: 'user2' },
{ mode: '', nick: 'user3', renderName: 'user3' } { mode: 'o', nick: 'user4', renderName: '@user4' },
{ mode: '', nick: 'user1', renderName: 'user1' },
{ mode: 'v', nick: 'user5', renderName: '+user5' }
] ]
} }
} }
@ -203,8 +203,8 @@ describe('channel reducer', () => {
srv: { srv: {
chan1: { chan1: {
users: [ users: [
{ mode: 'o', nick: 'nick2', renderName: '@nick2' }, { mode: 'v', nick: 'nick1', renderName: '+nick1' },
{ mode: 'v', nick: 'nick1', renderName: '+nick1' } { mode: 'o', nick: 'nick2', renderName: '@nick2' }
] ]
}, },
chan2: { chan2: {

View File

@ -97,26 +97,23 @@ export const getSelectedChannel = createSelector(
export const getSelectedChannelUsers = createSelector( export const getSelectedChannelUsers = createSelector(
getSelectedChannel, getSelectedChannel,
channel => channel.get('users', List()) channel => channel.get('users', List()).sort(compareUsers)
); );
export default createReducer(Map(), { export default createReducer(Map(), {
[actions.PART](state, action) { [actions.PART](state, { server, channels }) {
const { channels, server } = action;
return state.withMutations(s => { return state.withMutations(s => {
channels.forEach(channel => s.deleteIn([server, channel])); channels.forEach(channel => s.deleteIn([server, channel]));
}); });
}, },
[actions.socket.JOIN](state, action) { [actions.socket.JOIN](state, { server, channels, user }) {
const { server, channels, user } = action;
return state.updateIn([server, channels[0], 'users'], List(), users => return state.updateIn([server, channels[0], 'users'], List(), users =>
users.push(createUser(user)).sort(compareUsers) users.push(createUser(user))
); );
}, },
[actions.socket.PART](state, action) { [actions.socket.PART](state, { server, channel, user }) {
const { server, channel, user } = action;
if (state.hasIn([server, channel])) { if (state.hasIn([server, channel])) {
return state.updateIn([server, channel, 'users'], users => return state.updateIn([server, channel, 'users'], users =>
users.filter(u => u.nick !== user) users.filter(u => u.nick !== user)
@ -125,8 +122,7 @@ export default createReducer(Map(), {
return state; return state;
}, },
[actions.socket.QUIT](state, action) { [actions.socket.QUIT](state, { server, user }) {
const { server, user } = action;
return state.withMutations(s => { return state.withMutations(s => {
s.get(server).forEach((v, channel) => { s.get(server).forEach((v, channel) => {
s.updateIn([server, channel, 'users'], users => users.filter(u => u.nick !== user)); s.updateIn([server, channel, 'users'], users => users.filter(u => u.nick !== user));
@ -145,26 +141,23 @@ export default createReducer(Map(), {
return users.update(i, return users.update(i,
user => updateRenderName(user.set('nick', newNick)) user => updateRenderName(user.set('nick', newNick))
).sort(compareUsers); );
}); });
}); });
}); });
}, },
[actions.socket.USERS](state, action) { [actions.socket.USERS](state, { server, channel, users }) {
const { server, channel, users } = action;
return state.setIn([server, channel, 'users'], return state.setIn([server, channel, 'users'],
List(users.map(user => loadUser(user)).sort(compareUsers))); List(users.map(user => loadUser(user)))
);
}, },
[actions.socket.TOPIC](state, action) { [actions.socket.TOPIC](state, { server, channel, topic }) {
const { server, channel, topic } = action;
return state.setIn([server, channel, 'topic'], topic); return state.setIn([server, channel, 'topic'], topic);
}, },
[actions.socket.MODE](state, action) { [actions.socket.MODE](state, { server, channel, user, remove, add }) {
const { server, channel, user, remove, add } = action;
return state.updateIn([server, channel, 'users'], users => { return state.updateIn([server, channel, 'users'], users => {
const i = users.findIndex(u => u.nick === user); const i = users.findIndex(u => u.nick === user);
if (i < 0) { if (i < 0) {
@ -179,17 +172,17 @@ export default createReducer(Map(), {
} }
return updateRenderName(u.set('mode', mode + add)); return updateRenderName(u.set('mode', mode + add));
}).sort(compareUsers); });
}); });
}, },
[actions.socket.CHANNELS](state, action) { [actions.socket.CHANNELS](state, { data }) {
if (!action.data) { if (!data) {
return state; return state;
} }
return state.withMutations(s => { return state.withMutations(s => {
action.data.forEach(channel => { data.forEach(channel => {
s.setIn([channel.server, channel.name], Map({ s.setIn([channel.server, channel.name], Map({
users: List(), users: List(),
topic: channel.topic topic: channel.topic
@ -198,23 +191,21 @@ export default createReducer(Map(), {
}); });
}, },
[actions.socket.SERVERS](state, action) { [actions.socket.SERVERS](state, { data }) {
if (!action.data) { if (!data) {
return state; return state;
} }
return state.withMutations(s => { return state.withMutations(s => {
action.data.forEach(server => { data.forEach(server => {
if (!state.has(server.host)) { if (!s.has(server.host)) {
s.set(server.host, Map()); s.set(server.host, Map());
} }
}); });
}); });
}, },
[actions.CONNECT](state, action) { [actions.CONNECT](state, { host }) {
const { host } = action;
if (!state.has(host)) { if (!state.has(host)) {
return state.set(host, Map()); return state.set(host, Map());
} }
@ -222,8 +213,8 @@ export default createReducer(Map(), {
return state; return state;
}, },
[actions.DISCONNECT](state, action) { [actions.DISCONNECT](state, { server }) {
return state.delete(action.server); return state.delete(server);
} }
}); });