Advertisement
portarul23

Untitled

Feb 27th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import './App.css';
  3. import GreenHouse from './Components/GreenHouse.js';
  4. import uuidv1 from 'uuid/v1';
  5. import 'bootstrap/dist/css/bootstrap.min.css';
  6.  
  7. //this will contain the content of the application, will be the main handler
  8. class App extends Component {
  9. constructor(props) {
  10. super(props);
  11. this.state = {isAuth:false, username:'', password:''};
  12. this.handleClick = this.handleClick.bind(this);
  13. this.handleChange = this.handleChange.bind(this);
  14. this.handleSignOut = this.handleSignOut.bind(this);
  15. }
  16. handleChange(e) {
  17. this.setState({[e.target.name]:e.target.value});
  18. }
  19.  
  20. handleClick(e){
  21. //logic to login the user
  22. this.setState({isAuth:true})
  23. }
  24.  
  25. handleSignOut() {
  26. this.setState({isAuth:false, username:'', password:'', showLogin:true})
  27. }
  28.  
  29. render() {
  30. return(
  31. <div>
  32. <Navbar username = {this.state.username} isAuth = {this.state.isAuth} handleChange={this.handleChange} handleSignOut ={this.handleSignOut}/>
  33. {!this.state.isAuth && this.state.showLogin && <Login handleClick = {this.handleClick} handleChange={this.handleChange} />}
  34. {this.state.username && this.state.isAuth && <p>{this.state.username}</p>}
  35. {this.state.password && this.state.isAuth && <p>{this.state.password}</p>}
  36. </div>
  37. );
  38.  
  39. }
  40. }
  41.  
  42. class Login extends Component {
  43.  
  44. render() {
  45. return(
  46. <div>
  47. <form>
  48. <input type='text' name='username' onChange = {this.props.handleChange} />
  49. <input type='password' name='password' onChange = {this.props.handleChange} />
  50. <button type='button' onClick = {this.props.handleClick} >
  51. click me to change parent state
  52. </button>
  53. </form>
  54. </div>
  55. );
  56. }
  57. }
  58.  
  59. class Navbar extends Component{
  60. render() {
  61. return(
  62. <div>
  63. <p>Greenhouse Controller</p>
  64. {this.props.username && this.props.isAuth && <p>{this.props.username}</p>}
  65. {!this.props.isAuth && <button onClick={this.props.handleChange} name='showLogin' value={false}>Sign in</button>}
  66. {!this.props.isAuth && <button >Sign up</button>}
  67. {this.props.isAuth && <button onClick={this.props.handleSignOut}>Sign out</button>}
  68. </div>
  69. );
  70. }
  71. }
  72.  
  73.  
  74.  
  75. export default App;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement