PromptContainer.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import React from "react";
  2. import Prompt from "../components/Prompt";
  3. class PromptContainer extends React.Component {
  4. constructor(props, context, updater) {
  5. super(props, context, updater);
  6. this.state = {
  7. username: ""
  8. };
  9. // No autobind with ES6 classes without ES7 autobind decorators
  10. this.handleSubmitUser = this.handleSubmitUser.bind(this);
  11. this.handleUpdateUser = this.handleUpdateUser.bind(this);
  12. }
  13. handleSubmitUser(e) {
  14. e.preventDefault();
  15. // console.log("oSU", this);
  16. let username = this.state.username;
  17. this.setState({
  18. username: ""
  19. });
  20. if (this.props.routeParams.playerOne) {
  21. // Go to battle
  22. this.context.router.push({
  23. pathname: "battle",
  24. query: {
  25. playerOne: this.props.routeParams.playerOne,
  26. playerTwo: this.state.username
  27. }
  28. });
  29. return;
  30. }
  31. this.context.router.push(`playerTwo/${this.state.username}`);
  32. }
  33. handleUpdateUser(e) {
  34. // console.log("oUU", this);
  35. this.setState({
  36. username: e.target.value
  37. });
  38. }
  39. render() {
  40. return (
  41. <Prompt
  42. header={this.props.route.header}
  43. onSubmitUser={this.handleSubmitUser}
  44. onUpdateUser={this.handleUpdateUser}
  45. username={this.state.username}
  46. />
  47. );
  48. }
  49. }
  50. // Initializing via static properties is es7 only.
  51. PromptContainer.contextTypes = {
  52. router: React.PropTypes.object.isRequired
  53. };
  54. PromptContainer.defaultProps = {};
  55. PromptContainer.propTypes = {};
  56. export default PromptContainer;