From 07f0df6af428a56a4abedde841a0813e2802cd2b Mon Sep 17 00:00:00 2001 From: hmhealey Date: Wed, 2 Dec 2015 09:21:58 -0500 Subject: Moved action creation for suggestions --- .../components/suggestion/command_provider.jsx | 25 ++---------------- web/react/components/suggestion/suggestion_box.jsx | 23 ++++------------- .../components/suggestion/suggestion_list.jsx | 8 ++---- web/react/dispatcher/event_helpers.jsx | 30 ++++++++++++++++++++++ web/react/utils/async_client.jsx | 24 +++++++++++++++++ 5 files changed, 63 insertions(+), 47 deletions(-) (limited to 'web/react') diff --git a/web/react/components/suggestion/command_provider.jsx b/web/react/components/suggestion/command_provider.jsx index 34de3bfd1..a1f7901ef 100644 --- a/web/react/components/suggestion/command_provider.jsx +++ b/web/react/components/suggestion/command_provider.jsx @@ -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 - }); - } } diff --git a/web/react/components/suggestion/suggestion_box.jsx b/web/react/components/suggestion/suggestion_box.jsx index 22dbf1497..d8493f92a 100644 --- a/web/react/components/suggestion/suggestion_box.jsx +++ b/web/react/components/suggestion/suggestion_box.jsx @@ -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); diff --git a/web/react/components/suggestion/suggestion_list.jsx b/web/react/components/suggestion/suggestion_list.jsx index cd75f36c5..290e0eec6 100644 --- a/web/react/components/suggestion/suggestion_list.jsx +++ b/web/react/components/suggestion/suggestion_list.jsx @@ -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(); } diff --git a/web/react/dispatcher/event_helpers.jsx b/web/react/dispatcher/event_helpers.jsx index 856eec2f1..47d6b150e 100644 --- a/web/react/dispatcher/event_helpers.jsx +++ b/web/react/dispatcher/event_helpers.jsx @@ -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 + }); +} diff --git a/web/react/utils/async_client.jsx b/web/react/utils/async_client.jsx index 8cf111d55..edd26b4d1 100644 --- a/web/react/utils/async_client.jsx +++ b/web/react/utils/async_client.jsx @@ -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'); + } + ); +} -- cgit v1.2.3-1-g7c22