[Question] How to restore the message in MessageComposer if some error occurred during saving?
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?
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.
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))
}
todomv Why EventEmitter just register one event, then in callback use case..
Yeah generally you need to associate the result of the WebAPIUtils actions with the initial input. Generally an ID, text could work too.