Moved action creation for suggestions

This commit is contained in:
hmhealey
2015-12-02 09:21:58 -05:00
parent 06e0919bca
commit 07f0df6af4
5 changed files with 63 additions and 47 deletions

View File

@@ -1,8 +1,7 @@
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import AppDispatcher from '../../dispatcher/app_dispatcher.jsx';
import * as Client from '../../utils/client.jsx';
import * as AsyncClient from '../../utils/async_client.jsx';
import Constants from '../../utils/constants.jsx';
import SuggestionStore from '../../stores/suggestion_store.jsx';
@@ -42,27 +41,7 @@ export default class CommandProvider {
if (pretext.startsWith('/')) {
SuggestionStore.setMatchedPretext(suggestionId, pretext);
Client.executeCommand(
'',
pretext,
true,
(data) => {
this.handleCommandsReceived(suggestionId, pretext, data.suggestions);
}
);
AsyncClient.getSuggestedCommands(pretext, suggestionId, CommandSuggestion);
}
}
handleCommandsReceived(suggestionId, matchedPretext, commandSuggestions) {
const terms = commandSuggestions.map(({suggestion}) => suggestion);
AppDispatcher.handleServerAction({
type: Constants.ActionTypes.SUGGESTION_RECEIVED_SUGGESTIONS,
id: suggestionId,
matchedPretext,
terms,
items: commandSuggestions,
component: CommandSuggestion
});
}
}

View File

@@ -1,8 +1,8 @@
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import AppDispatcher from '../../dispatcher/app_dispatcher.jsx';
import Constants from '../../utils/constants.jsx';
import * as EventHelpers from '../../dispatcher/event_helpers.jsx';
import SuggestionStore from '../../stores/suggestion_store.jsx';
import * as Utils from '../../utils/utils.jsx';
@@ -79,11 +79,7 @@ export default class SuggestionBox extends React.Component {
const caret = Utils.getCaretPosition(textbox);
const pretext = textbox.value.substring(0, caret);
AppDispatcher.handleViewAction({
type: ActionTypes.SUGGESTION_PRETEXT_CHANGED,
id: this.suggestionId,
pretext
});
EventHelpers.emitSuggestionPretextChanged(this.suggestionId, pretext);
if (this.props.onUserInput) {
this.props.onUserInput(textbox.value);
@@ -115,22 +111,13 @@ export default class SuggestionBox extends React.Component {
handleKeyDown(e) {
if (SuggestionStore.hasSuggestions(this.suggestionId)) {
if (e.which === KeyCodes.UP) {
AppDispatcher.handleViewAction({
type: ActionTypes.SUGGESTION_SELECT_PREVIOUS,
id: this.suggestionId
});
EventHelpers.emitSelectPreviousSuggestion(this.suggestionId);
e.preventDefault();
} else if (e.which === KeyCodes.DOWN) {
AppDispatcher.handleViewAction({
type: ActionTypes.SUGGESTION_SELECT_NEXT,
id: this.suggestionId
});
EventHelpers.emitSelectNextSuggestion(this.suggestionId);
e.preventDefault();
} else if (e.which === KeyCodes.SPACE || e.which === KeyCodes.ENTER) {
AppDispatcher.handleViewAction({
type: ActionTypes.SUGGESTION_COMPLETE_WORD,
id: this.suggestionId
});
EventHelpers.emitCompleteWordSuggestion(this.suggestionId);
e.preventDefault();
} else if (this.props.onKeyDown) {
this.props.onKeyDown(e);

View File

@@ -1,8 +1,8 @@
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import AppDispatcher from '../../dispatcher/app_dispatcher.jsx';
import Constants from '../../utils/constants.jsx';
import * as EventHelpers from '../../dispatcher/event_helpers.jsx';
import SuggestionStore from '../../stores/suggestion_store.jsx';
export default class SuggestionList extends React.Component {
@@ -37,11 +37,7 @@ export default class SuggestionList extends React.Component {
}
handleItemClick(term, e) {
AppDispatcher.handleViewAction({
type: Constants.ActionTypes.SUGGESTION_COMPLETE_WORD,
id: this.props.suggestionId,
term
});
EventHelpers.emitCompleteWordSuggestion(this.props.suggestionId, term);
e.preventDefault();
}

View File

@@ -111,3 +111,33 @@ export function showRegisterAppModal() {
value: true
});
}
export function emitSuggestionPretextChanged(suggestionId, pretext) {
AppDispatcher.handleViewAction({
type: ActionTypes.SUGGESTION_PRETEXT_CHANGED,
id: suggestionId,
pretext
});
}
export function emitSelectNextSuggestion(suggestionId) {
AppDispatcher.handleViewAction({
type: ActionTypes.SUGGESTION_SELECT_NEXT,
id: suggestionId
});
}
export function emitSelectPreviousSuggestion(suggestionId) {
AppDispatcher.handleViewAction({
type: ActionTypes.SUGGESTION_SELECT_PREVIOUS,
id: suggestionId
});
}
export function emitCompleteWordSuggestion(suggestionId, term = '') {
AppDispatcher.handleViewAction({
type: Constants.ActionTypes.SUGGESTION_COMPLETE_WORD,
id: suggestionId,
term
});
}

View File

@@ -731,3 +731,27 @@ export function savePreferences(preferences, success, error) {
}
);
}
export function getSuggestedCommands(command, suggestionId, component) {
client.executeCommand(
'',
command,
true,
(data) => {
// pull out the suggested commands from the returned data
const terms = data.suggestions.map((suggestion) => suggestion.suggestion);
AppDispatcher.handleServerAction({
type: ActionTypes.SUGGESTION_RECEIVED_SUGGESTIONS,
id: suggestionId,
matchedPretext: command,
terms,
items: data.suggestions,
component
});
},
(err) => {
dispatchError(err, 'getCommandSuggestions');
}
);
}