Guest User

Untitled

a guest
May 29th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. import React, {Component} from 'react';
  2. import logo from './logo.svg';
  3. import './App.css';
  4. import {BrowserRouter as Router} from 'react-router-dom';
  5. import Navbar from "./components/nav";
  6. import firebase from './firebase';
  7. import Questions from './components/questions';
  8. import Stuff from './components/stuff';
  9. class App extends Component {
  10. constructor(p) {
  11. super(p);
  12. this.state = {user: null}
  13. }
  14.  
  15. render() {
  16. return (
  17. <div className="App">
  18. <Navbar/>
  19. {this.state.user ? <Questions/> : <Stuff/>}
  20. </div>
  21. );
  22. }
  23. }
  24.  
  25. export default App;
  26.  
  27. import React from 'react';
  28. import {Navbar as Bar, Nav, NavItem, Modal, Button, FormControl} from 'react-bootstrap';
  29. import {BrowserRouter as Router, Link, Route} from 'react-router-dom';
  30. import firebase, {auth} from '../firebase';
  31.  
  32. class Navbar extends React.Component {
  33. constructor(props) {
  34. super(props);
  35. this.state = {};
  36. this.login = this.login.bind(this);
  37. this.logout = this.logout.bind(this);
  38. this.openLogin = this.openLogin.bind(this);
  39. this.handleClose = this.handleClose.bind(this);
  40. }
  41.  
  42. componentDidMount() {
  43. auth.onAuthStateChanged((user) => {
  44. if (user) {
  45. this.setState({user});
  46. }
  47. });
  48. }
  49.  
  50. logout() {
  51. auth.signOut()
  52. .then(() => {
  53. this.setState({
  54. user: null
  55. });
  56. });
  57. }
  58.  
  59. login() {
  60. var email = document.getElementById('email').value;
  61. var password = document.getElementById('password').value;
  62. auth.signInWithEmailAndPassword(email, password)
  63. .then(result => {
  64. const user = result.user;
  65. this.setState({user});
  66. document.getElementById('close').click();
  67. }
  68. ).catch(e => console.log(e));
  69. }
  70.  
  71. openLogin() {
  72. this.setState({show: true});
  73. }
  74.  
  75. handleClose() {
  76. this.setState({show: false});
  77. }
  78.  
  79. render() {
  80. return (
  81. <React.Fragment>
  82. <Router>
  83. <Bar>
  84. <Bar.Header>
  85. <Bar.Brand>
  86. <a href="/home">UczIchApp</a>
  87. {/*<Route path='path' component=""/>*/}
  88. </Bar.Brand>
  89. <Bar.Brand>
  90.  
  91. </Bar.Brand>
  92. </Bar.Header>
  93. <Nav>
  94. {
  95. this.state.user ?
  96. <NavItem onClick={this.logout}>Wyloguj się</NavItem>
  97. :
  98. <NavItem onClick={this.openLogin}>Zaloguj się</NavItem>
  99. }
  100. </Nav>
  101. </Bar>
  102. </Router>
  103. <Modal show={this.state.show} onHide={this.handleClose}>
  104. <Modal.Header closeButton>
  105. <Modal.Title>Modal heading</Modal.Title>
  106. </Modal.Header>
  107. <Modal.Body>
  108. <form>
  109. <FormControl
  110. id="email"
  111. type="email"
  112. label="Email address"
  113. placeholder="Enter email"
  114. />
  115. <FormControl id="password" label="Password" type="password"/>
  116. <Button onClick={this.login}>Zaloguj</Button>
  117. </form>
  118. </Modal.Body>
  119. <Modal.Footer>
  120. <Button id="close" onClick={this.handleClose}>Close</Button>
  121. </Modal.Footer>
  122. </Modal>
  123. </React.Fragment>
  124. )
  125. }
  126. }
  127.  
  128. export default Navbar;
  129.  
  130. constructor(p) {
  131. super(p);
  132. this.state = {user: null}
  133. this.checkUserState= this.checkUserState.bind(this);
  134. }
  135.  
  136. checkUserState(user){
  137. this.seState({user});
  138. }
  139.  
  140. <Navbar {...this}/>
  141.  
  142. this.setState({
  143. user: null
  144. },function(){
  145. this.props.checkUserState(this.state.user)
  146. });
Add Comment
Please, Sign In to add comment