flux icon indicating copy to clipboard operation
flux copied to clipboard

[Question] How to restore the message in MessageComposer if some error occurred during saving?

Open anatoliyarkhipov opened this issue 11 years ago • 4 comments

The _onKeyDown handler in MessageComposer has a naive implementation:

_onKeyDown: function(event) {
    if (event.keyCode === ENTER_KEY_CODE) {
      event.preventDefault();
      var text = this.state.text.trim();
      if (text) {
        ChatMessageActionCreators.createMessage(text, this.props.threadID);
      }
      this.setState({text: ''});
    }
  }

This implementation assumes that the message will be 100% saved and clears the input immediately. But often we do not want clear a form until we are not sure that AJAX call was successful. How to improve this method to restore the text if saving fails?

anatoliyarkhipov avatar Jun 05 '15 08:06 anatoliyarkhipov

We need to add two actions, one of which will be called when the XHR completes: MESSAGE_SAVED_SUCCESS and MESSAGED_SAVED_ERROR. Based on these actions, the store should either rollback the message (and probably provide some UX around retrying) or mark/clear the message record as confirmed.

fisherwebdev avatar Jun 05 '15 17:06 fisherwebdev

The MESSAGED_SAVED_ERROR action must be called with the same text which was received in createMessage, I correctly understand? Something like this:

createMessage(text) {
    ApiUtils.createMessage(text)
        .then(() => this.dispatchSuccessAction())
        .fail(() => this.dispatchFailAction(text))
}

anatoliyarkhipov avatar Jun 06 '15 10:06 anatoliyarkhipov

todomv Why EventEmitter just register one event, then in callback use case..

cycgit avatar Sep 24 '15 00:09 cycgit

Yeah generally you need to associate the result of the WebAPIUtils actions with the initial input. Generally an ID, text could work too.

kyldvs avatar Nov 18 '15 20:11 kyldvs