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')
],
getInitialState() {
return {
selectedTab: selectedTabStore.getState()
};
},
componentWillMount() {
if (!window.loaded) {
var p = this.getParams();

View File

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

View File

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

View File

@ -15,15 +15,8 @@ var Search = React.createClass({
Reflux.connect(selectedTabStore, 'selectedTab')
],
getInitialState() {
return {
search: searchStore.getState(),
selectedTab: selectedTabStore.getState()
};
},
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();
}
},
@ -38,10 +31,10 @@ var Search = React.createClass({
render() {
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 (
<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')
],
getInitialState() {
return {
servers: serverStore.getState(),
channels: channelStore.getState(),
privateChats: privateChatStore.getState()
};
},
handleConnectClick() {
routeActions.navigate('connect');
},

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

File diff suppressed because one or more lines are too long