diff --git a/src/actions/tweets.js b/src/actions/tweets.js index c67f933..7a4a833 100644 --- a/src/actions/tweets.js +++ b/src/actions/tweets.js @@ -1,5 +1,5 @@ import TwitterClient from '../utils/twitter-client'; -import { clearText } from './texts'; +import { clearText, setSearchQuery } from './texts'; export const ADD_TWEET_TO_TAB = 'ADD_TWEET_TO_TAB'; export const CLEAR_AND_SET_TWEETS = 'CLEAR_AND_SET_TWEETS'; @@ -99,3 +99,18 @@ export const loadList = (listId, account, reset = false) => { }); } } + +export const loadSearch = (query, account, reset = false) => { + return dispatch => { + const client = new TwitterClient(account); + client.searchTweets(query, 50, (tweets) => { + if (reset) { + dispatch(clearAndSetTweets(tweets, account, 'search')); + } else { + for (let tweet of tweets) { + dispatch(addTweetToTab(tweet, account, 'search')); + } + } + }); + } +} diff --git a/src/containers/search-container.js b/src/containers/search-container.js index 37cb166..a46dd45 100644 --- a/src/containers/search-container.js +++ b/src/containers/search-container.js @@ -1,7 +1,6 @@ import * as Keycode from '../utils/keycode'; import Actions from '../actions'; import Search from '../components/search'; -import TwitterClient from '../utils/twitter-client'; import { connect } from 'react-redux'; const mapStateToProps = (state, props) => { @@ -15,13 +14,8 @@ const mapDispatchToProps = (dispatch, props) => { onSearchFieldKeyDown: (event) => { if (event.keyCode === Keycode.ENTER) { event.preventDefault(); - - let query = event.target.value; - let client = new TwitterClient(props.account); - client.searchTweets(query, 50, (tweets) => { - dispatch(Actions.setSearchQuery(query, props.account)); - dispatch(Actions.clearAndSetTweets(tweets, props.account, 'search')); - }); + dispatch(Actions.setSearchQuery(event.target.value, props.account)); + dispatch(Actions.loadSearch(event.target.value, props.account, true)) } } } diff --git a/src/utils/ipc-action.js b/src/utils/ipc-action.js index 836a7f1..a09b6eb 100644 --- a/src/utils/ipc-action.js +++ b/src/utils/ipc-action.js @@ -59,19 +59,11 @@ export default class IpcAction { } }); - let listId = this.state.activeListId(); - if (listId) { - this.dispatch(Actions.loadList(listId, this.state.activeAccount())); - } + const listId = this.state.activeListId(); + if (listId) this.dispatch(Actions.loadList(listId, this.state.activeAccount())); - let query = this.state.activeSearchQuery(); - if (query) { - this.client().searchTweets(query, 50, (tweets) => { - for (let tweet of tweets) { - this.addTweetToTab(tweet, this.state.activeAccount(), 'search'); - } - }); - } + const query = this.state.activeSearchQuery(); + if (query) this.dispatch(Actions.loadSearch(query, this.state.activeAccount())); this.dispatch(Actions.reconnectStreaming(this.state.activeAccount())); });