Implement getInitialState in stores

This commit is contained in:
Ken-Håvard Lieng 2015-05-23 00:40:16 +02:00
parent 6a25c1d6a5
commit 6a6aedc5da
14 changed files with 70 additions and 79 deletions

View File

@ -18,12 +18,6 @@ var Chat = React.createClass({
Reflux.connect(selectedTabStore, 'selectedTab') Reflux.connect(selectedTabStore, 'selectedTab')
], ],
getInitialState() {
return {
selectedTab: selectedTabStore.getState()
};
},
componentWillMount() { componentWillMount() {
if (!window.loaded) { if (!window.loaded) {
var p = this.getParams(); var p = this.getParams();

View File

@ -20,8 +20,6 @@ var MessageBox = React.createClass({
getInitialState() { getInitialState() {
return { return {
messages: messageLineStore.getState(),
selectedTab: selectedTabStore.getState(),
height: window.innerHeight - 100 height: window.innerHeight - 100
}; };
}, },

View File

@ -17,7 +17,6 @@ var MessageInput = React.createClass({
getInitialState() { getInitialState() {
return { return {
history: inputHistoryStore.getState(),
value: '' value: ''
}; };
}, },

View File

@ -15,15 +15,8 @@ var Search = React.createClass({
Reflux.connect(selectedTabStore, 'selectedTab') Reflux.connect(selectedTabStore, 'selectedTab')
], ],
getInitialState() {
return {
search: searchStore.getState(),
selectedTab: selectedTabStore.getState()
};
},
componentDidUpdate(prevProps, prevState) { componentDidUpdate(prevProps, prevState) {
if (!prevState.search.get('show') && this.state.search.get('show')) { if (!prevState.search.show && this.state.search.show) {
this.refs.input.getDOMNode().focus(); this.refs.input.getDOMNode().focus();
} }
}, },
@ -38,10 +31,10 @@ var Search = React.createClass({
render() { render() {
var style = { var style = {
display: this.state.search.get('show') ? 'block' : 'none' display: this.state.search.show ? 'block' : 'none'
}; };
var results = this.state.search.get('results').map(result => { var results = this.state.search.results.map(result => {
return ( return (
<p key={result.id}>{util.timestamp(new Date(result.time * 1000))} {result.from} {result.content}</p> <p key={result.id}>{util.timestamp(new Date(result.time * 1000))} {result.from} {result.content}</p>
); );

View File

@ -17,14 +17,6 @@ var TabList = React.createClass({
Reflux.connect(privateChatStore, 'privateChats') Reflux.connect(privateChatStore, 'privateChats')
], ],
getInitialState() {
return {
servers: serverStore.getState(),
channels: channelStore.getState(),
privateChats: privateChatStore.getState()
};
},
handleConnectClick() { handleConnectClick() {
routeActions.navigate('connect'); routeActions.navigate('connect');
}, },

View File

@ -11,12 +11,6 @@ var TabListItem = React.createClass({
Reflux.connect(selectedTabStore, 'tab') Reflux.connect(selectedTabStore, 'tab')
], ],
getInitialState() {
return {
tab: selectedTabStore.getState()
};
},
handleClick() { handleClick() {
tabActions.select(this.props.server, this.props.channel); tabActions.select(this.props.server, this.props.channel);
}, },

View File

@ -50,7 +50,7 @@ function updateRenderName(user) {
return user.set('renderName', name); return user.set('renderName', name);
} }
function sortUsers(a, b) { function compareUsers(a, b) {
a = a.renderName.toLowerCase(); a = a.renderName.toLowerCase();
b = b.renderName.toLowerCase(); b = b.renderName.toLowerCase();
@ -75,6 +75,10 @@ function sortUsers(a, b) {
return 0; return 0;
} }
function getState() {
return channels;
}
var channelStore = Reflux.createStore({ var channelStore = Reflux.createStore({
init() { init() {
this.listenToMany(actions); this.listenToMany(actions);
@ -92,7 +96,7 @@ var channelStore = Reflux.createStore({
addUser(user, server, channel) { addUser(user, server, channel) {
channels = channels.updateIn([server, channel, 'users'], empty, users => { channels = channels.updateIn([server, channel, 'users'], empty, users => {
return users.push(createUser(user)).sort(sortUsers); return users.push(createUser(user)).sort(compareUsers);
}); });
this.trigger(channels); this.trigger(channels);
}, },
@ -117,14 +121,14 @@ var channelStore = Reflux.createStore({
var i = users.findIndex(user => user.nick === oldNick); var i = users.findIndex(user => user.nick === oldNick);
return users.update(i, user => { return users.update(i, user => {
return updateRenderName(user.set('nick', newNick)); return updateRenderName(user.set('nick', newNick));
}).sort(sortUsers); }).sort(compareUsers);
}); });
}); });
this.trigger(channels); this.trigger(channels);
}, },
setUsers(users, server, channel) { setUsers(users, server, channel) {
users = _.map(users, user => loadUser(user)).sort(sortUsers); users = _.map(users, user => loadUser(user)).sort(compareUsers);
channels = channels.setIn([server, channel, 'users'], Immutable.List(users)); channels = channels.setIn([server, channel, 'users'], Immutable.List(users));
this.trigger(channels); this.trigger(channels);
}, },
@ -146,7 +150,7 @@ var channelStore = Reflux.createStore({
return updateRenderName(user); return updateRenderName(user);
}); });
channels = channels.updateIn([mode.server, mode.channel, 'users'], users => users.sort(sortUsers)); channels = channels.updateIn([mode.server, mode.channel, 'users'], users => users.sort(compareUsers));
this.trigger(channels); this.trigger(channels);
}, },
@ -193,9 +197,8 @@ var channelStore = Reflux.createStore({
return channels.getIn([server, channel, 'topic']); return channels.getIn([server, channel, 'topic']);
}, },
getState() { getInitialState: getState,
return channels; getState
}
}); });
module.exports = channelStore; module.exports = channelStore;

View File

@ -13,6 +13,13 @@ if (stored) {
history = JSON.parse(stored); history = JSON.parse(stored);
} }
function getState() {
if (index !== -1) {
return history[index];
}
return null;
}
var inputHistoryStore = Reflux.createStore({ var inputHistoryStore = Reflux.createStore({
init() { init() {
this.listenToMany(actions); this.listenToMany(actions);
@ -51,12 +58,8 @@ var inputHistoryStore = Reflux.createStore({
} }
}, },
getState() { getInitialState: getState,
if (index !== -1) { getState
return history[index];
}
return null;
}
}); });
module.exports = inputHistoryStore; module.exports = inputHistoryStore;

View File

@ -26,6 +26,10 @@ function wrap() {
return false; return false;
} }
function getState() {
return messages;
}
var messageLineStore = Reflux.createStore({ var messageLineStore = Reflux.createStore({
init() { init() {
updateCharWidth(); updateCharWidth();
@ -59,9 +63,8 @@ var messageLineStore = Reflux.createStore({
} }
}, },
getState() { getInitialState: getState,
return messages; getState
}
}); });
module.exports = messageLineStore; module.exports = messageLineStore;

View File

@ -8,6 +8,10 @@ var serverActions = require('../actions/server');
var privateChats = Immutable.Map(); var privateChats = Immutable.Map();
var empty = Immutable.List(); var empty = Immutable.List();
function getState() {
return privateChats;
}
var privateChatStore = Reflux.createStore({ var privateChatStore = Reflux.createStore({
init() { init() {
this.listenToMany(actions); this.listenToMany(actions);
@ -36,9 +40,8 @@ var privateChatStore = Reflux.createStore({
this.trigger(privateChats); this.trigger(privateChats);
}, },
getState() { getInitialState: getState,
return privateChats; getState
}
}); });
module.exports = privateChatStore; module.exports = privateChatStore;

View File

@ -10,6 +10,10 @@ var Search = Immutable.Record({
var search = new Search(); var search = new Search();
function getState() {
return search;
}
var searchStore = Reflux.createStore({ var searchStore = Reflux.createStore({
init() { init() {
this.listenToMany(actions); this.listenToMany(actions);
@ -25,9 +29,8 @@ var searchStore = Reflux.createStore({
this.trigger(search); this.trigger(search);
}, },
getState() { getInitialState: getState,
return search; getState
}
}); });
module.exports = searchStore; module.exports = searchStore;

View File

@ -34,6 +34,10 @@ function updateChannelName(name) {
history[history.length - 1] = selectedTab; history[history.length - 1] = selectedTab;
} }
function getState() {
return selectedTab;
}
var selectedTabStore = Reflux.createStore({ var selectedTabStore = Reflux.createStore({
init() { init() {
this.listenToMany(actions); this.listenToMany(actions);
@ -148,9 +152,8 @@ var selectedTabStore = Reflux.createStore({
return selectedTab.channel; return selectedTab.channel;
}, },
getState() { getInitialState: getState,
return selectedTab; getState
}
}); });
selectedTabStore.listen(selectedTab => { selectedTabStore.listen(selectedTab => {
@ -165,7 +168,7 @@ selectedTabStore.listen(selectedTab => {
} else { } else {
routeActions.navigate('/' + selectedTab.server); routeActions.navigate('/' + selectedTab.server);
} }
} else if (_.size(serverStore.getState()) === 0) { } else if (serverStore.getState().size === 0) {
routeActions.navigate('connect'); routeActions.navigate('connect');
} }

View File

@ -11,6 +11,10 @@ var Server = Immutable.Record({
name: null name: null
}); });
function getState() {
return servers;
}
var serverStore = Reflux.createStore({ var serverStore = Reflux.createStore({
init() { init() {
this.listenToMany(actions); this.listenToMany(actions);
@ -56,9 +60,8 @@ var serverStore = Reflux.createStore({
return servers.getIn([server, 'name']); return servers.getIn([server, 'name']);
}, },
getState() { getInitialState: getState,
return servers; getState
}
}); });
module.exports = serverStore; module.exports = serverStore;

File diff suppressed because one or more lines are too long