dispatch/client/js/state/servers.js

211 lines
4.0 KiB
JavaScript
Raw Normal View History

import { createSelector } from 'reselect';
2018-04-25 03:36:27 +00:00
import get from 'lodash/get';
import createReducer from 'utils/createReducer';
import { getSelectedTab, updateSelection } from './tab';
import * as actions from './actions';
export const getServers = state => state.servers;
export const getCurrentNick = createSelector(
getServers,
getSelectedTab,
(servers, tab) => {
2018-04-25 03:36:27 +00:00
if (!servers[tab.server]) {
return;
}
const { editedNick } = servers[tab.server];
2018-05-16 03:02:48 +00:00
if (editedNick === null) {
2018-04-25 03:36:27 +00:00
return servers[tab.server].nick;
}
return editedNick;
}
);
export const getCurrentServerName = createSelector(
getServers,
getSelectedTab,
2018-04-25 03:36:27 +00:00
(servers, tab) => get(servers, [tab.server, 'name'])
);
export const getCurrentServerStatus = createSelector(
getServers,
getSelectedTab,
2018-04-25 03:36:27 +00:00
(servers, tab) => get(servers, [tab.server, 'status'], {})
);
2018-04-25 03:36:27 +00:00
export default createReducer(
{},
{
2018-05-16 03:02:48 +00:00
[actions.CONNECT](state, { host, nick, name }) {
2018-04-25 03:36:27 +00:00
if (!state[host]) {
state[host] = {
2018-04-05 23:46:22 +00:00
nick,
2018-04-25 03:36:27 +00:00
editedNick: null,
2018-05-16 03:02:48 +00:00
name: name || host,
2018-04-25 03:36:27 +00:00
status: {
connected: false,
error: null
}
};
}
},
[actions.DISCONNECT](state, { server }) {
delete state[server];
},
[actions.SET_SERVER_NAME](state, { server, name }) {
state[server].name = name;
},
[actions.SET_NICK](state, { server, nick, editing }) {
if (editing) {
state[server].editedNick = nick;
} else if (nick === '') {
state[server].editedNick = null;
}
},
[actions.socket.NICK](state, { server, oldNick, newNick }) {
if (!oldNick || oldNick === state[server].nick) {
state[server].nick = newNick;
state[server].editedNick = null;
}
},
[actions.socket.NICK_FAIL](state, { server }) {
state[server].editedNick = null;
},
[actions.socket.SERVERS](state, { data }) {
if (data) {
data.forEach(({ host, name, nick, status }) => {
2018-05-16 03:02:48 +00:00
state[host] = { name, nick, status, editedNick: null };
2018-04-25 03:36:27 +00:00
});
}
},
[actions.socket.CONNECTION_UPDATE](state, { server, connected, error }) {
if (state[server]) {
state[server].status.connected = connected;
state[server].status.error = error;
}
}
}
2018-04-25 03:36:27 +00:00
);
2018-05-16 03:02:48 +00:00
export function connect(config) {
return {
type: actions.CONNECT,
2018-05-16 03:02:48 +00:00
...config,
socket: {
type: 'connect',
2018-05-16 03:02:48 +00:00
data: config
}
};
}
export function disconnect(server) {
return dispatch => {
dispatch({
type: actions.DISCONNECT,
server,
socket: {
type: 'quit',
data: { server }
}
});
dispatch(updateSelection());
};
}
export function reconnect(server, settings) {
return {
type: actions.RECONNECT,
server,
settings,
socket: {
type: 'reconnect',
data: {
...settings,
server
}
}
};
}
export function whois(user, server) {
return {
type: actions.WHOIS,
user,
server,
socket: {
type: 'whois',
data: { user, server }
}
};
}
export function away(message, server) {
return {
type: actions.AWAY,
message,
server,
socket: {
type: 'away',
data: { message, server }
}
};
}
export function setNick(nick, server, editing) {
nick = nick.trim().replace(' ', '');
const action = {
type: actions.SET_NICK,
nick,
server,
editing
};
if (!editing && nick !== '') {
action.socket = {
type: 'nick',
data: {
newNick: nick,
server
}
};
}
return action;
}
export function isValidServerName(name) {
return name.trim() !== '';
}
export function setServerName(name, server) {
const action = {
type: actions.SET_SERVER_NAME,
name,
server
};
if (isValidServerName(name)) {
action.socket = {
type: 'set_server_name',
data: {
name,
server
},
debounce: {
2018-10-15 06:56:17 +00:00
delay: 500,
key: `server_name:${server}`
}
};
}
return action;
}