Skip to content
This repository has been archived by the owner on Apr 7, 2023. It is now read-only.

Commit

Permalink
Unify the logic for search
Browse files Browse the repository at this point in the history
  • Loading branch information
Takashi Kokubun committed Mar 27, 2016
1 parent ca0288f commit 9566027
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
17 changes: 16 additions & 1 deletion src/actions/tweets.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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'));
}
}
});
}
}
10 changes: 2 additions & 8 deletions src/containers/search-container.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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))
}
}
}
Expand Down
16 changes: 4 additions & 12 deletions src/utils/ipc-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
});
Expand Down

0 comments on commit 9566027

Please sign in to comment.