Guest User

Untitled

a guest
May 21st, 2018
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. class Home extends Component {
  2. state = {
  3. isLogined: false,
  4. redirect: false,
  5. username: "",
  6. avatar: "",
  7. name: "",
  8. messages: [],
  9. feeds: [],
  10. activechat: {},
  11. currentRoom: ""
  12. };
  13.  
  14. componentDidMount() {
  15. var config = {
  16. //config data
  17. };
  18. Firebase.initializeApp(config);
  19.  
  20. Firebase.auth().onAuthStateChanged(user => {
  21. if (user) {
  22. console.log("User logined");
  23. this.setState({
  24. isLogined: true
  25. });
  26. console.log(this.state.isLogined);
  27. this.getUserData(user);
  28. } else {
  29. console.log("User isn't logined");
  30. this.setState({
  31. isLogined: false,
  32. redirect: true
  33. });
  34. console.log(this.state.isLogined);
  35. }
  36. });
  37. }
  38.  
  39. async getUserData(user) {
  40. var db = Firebase.database();
  41. var uid = user.uid;
  42. await db
  43. .ref(`users/${uid}`)
  44. .once("value")
  45. .then(s => {
  46. this.setState({
  47. username: s.child("username").val()
  48. });
  49. });
  50. await db
  51. .ref(`userdata/${this.state.username}`)
  52. .once("value")
  53. .then(s => {
  54. this.setState({
  55. avatar: s.child("pic100").val(),
  56. name: s.child("name").val()
  57. });
  58. });
  59.  
  60. db
  61. .ref(`messages/users/${this.state.username}`)
  62. .orderByChild("timestamp")
  63. .limitToLast(30)
  64. .on("value", s => {
  65. var new_messages = [];
  66. s.forEach(element => {
  67. var message = element.val();
  68. new_messages.push(message);
  69. });
  70. new_messages = new_messages.reverse();
  71. this.setState({
  72. messages: new_messages
  73. });
  74. });
  75. var topics = [];
  76.  
  77. db
  78. .ref(`home/feed`)
  79. .orderByChild("time")
  80. .limitToLast(30)
  81. .on("child_added", s => {
  82. topics.push(s.val());
  83. var reversedtopics = topics.reverse();
  84. this.setState({
  85. feeds: reversedtopics
  86. });
  87. });
  88. }
  89.  
  90. loadChatBox(chat) {
  91. var id = chat.id;
  92. var name = chat.name;
  93. var type = chat.room_type;
  94. console.log(id + " " + type);
  95. this.setState({
  96. activechat: {
  97. name: name,
  98. id: id,
  99. type: type
  100. }
  101. });
  102. }
  103.  
  104. getChatBox(chat) {
  105. if (chat.id) {
  106. console.log("Get Chat Box called");
  107. return <ChatBox chat={chat} />;
  108. } else {
  109. return <div>Please select a chat</div>;
  110. }
  111. }
  112.  
  113. render() {
  114. var topics = this.state.feeds.reverse();
  115. const { redirect } = this.state;
  116.  
  117. if (redirect) {
  118. return <Redirect to="/login" />;
  119. }
  120. return (
  121. <div className={"container"}>
  122. <HomeBar avatar={this.state.avatar} />
  123. <div className={"columnContainer"}>
  124. <Card className={"leftContainer"}>
  125. <div style={{ overflowY: "scroll", height: "100%" }}>
  126. <ChatList
  127. messages={this.state.messages}
  128. onPress={message => {
  129. if (message) {
  130. this.loadChatBox(message);
  131. message = null;
  132. }
  133. }}
  134. />
  135. </div>
  136. </Card>
  137. <Card className={"middleContainer"}>
  138. {this.getChatBox(this.state.activechat)}
  139. </Card>
  140.  
  141. <Card className={"rightContainer"}>
  142. <HomeFeed feeds={this.state.feeds} />
  143. </Card>
  144. </div>
  145. </div>
  146. );
  147. }
  148. }
  149.  
  150. export default Home;
  151.  
  152.  
  153.  
  154.  
  155. HomeBar
  156.  
  157. const HomeBar = props => {
  158. console.log(props);
  159. return (
  160. <div style={styles.root}>
  161. <AppBar position="static" style={{ backgroundColor: "#fff" }}>
  162. <Toolbar>
  163. <IconButton
  164. style={styles.menuButton}
  165. color="inherit"
  166. aria-label="Menu"
  167. >
  168. <MenuIcon />
  169. </IconButton>
  170. <Typography style={styles.title} variant="title" color="inherit">
  171. AppName
  172. </Typography>
  173. <Button style={styles.menuItem}>Home</Button>
  174. <Button style={styles.menuItem}>Topics</Button>
  175. <Button style={styles.menuItem}>Rooms</Button>
  176. <Button style={styles.menuItem}>Leaderboard</Button>
  177.  
  178. <IconButton color="inherit">
  179. <Avatar alt="Remy Sharp" src={props.avatar} />
  180. </IconButton>
  181. </Toolbar>
  182. </AppBar>
  183. </div>
  184. );
  185. };
  186.  
  187. export default HomeBar;
Add Comment
Please, Sign In to add comment