Last active
May 16, 2017 14:53
-
-
Save arqex/cde9f7a1b541567f1767e26e7e7fd2db to your computer and use it in GitHub Desktop.
Returning reaction
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// We can return the promise from our reaction | |
freezer.on('message:send', text => { | |
freezer.get().set({status: 'SENDING'}); | |
// Return the promise | |
return Ajax.post('/messages', {text: text} ) | |
.then( msg => { | |
freezer.get() | |
.set({status: 'READY'}) | |
.messages.push(msg) | |
; | |
}) | |
; | |
}); | |
// in a different file, our component will trigger the message sending | |
// and show the success message when it finish | |
class MessageEditor extends React.Component { | |
render(){ | |
return ( | |
<div> | |
<textarea onChange={ e => this.setState({msg: e.target.value}) /> | |
<button onClick={ () => this.send() }>Send</button> | |
</div> | |
); | |
} | |
send(){ | |
// Receive the promise from the reaction, so we can handle the message | |
// displaying in the Component itself | |
freezer.emit('message:send', this.state.msg) | |
.then( () => this.showToast('Your message has been sent successfully!') ) | |
; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment