Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.23 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import fire from './config/Fire';
  3.  
  4. class Popular extends Component{
  5. constructor(props){
  6. super(props);
  7. this.state = {
  8. categories: [],
  9. top_posts: [],
  10. test_arr: ['hey', 'yo'],
  11. loadedCategories: false,
  12. loadedTopPosts: false,
  13. loadedTopPosts2: false,
  14. indexer: 0,
  15. }
  16. }
  17.  
  18. loadCategories = () =>{
  19. let collectionRef = fire.firestore().collection('categories').orderBy("totalviews", "desc").limit(5);
  20. let x = this;
  21. collectionRef.get().then(function(collection){
  22. let i=0;
  23. collection.forEach(function(doc){
  24. if (x.state.categories.length === 5){
  25. x.setState({loadedCategories: true})
  26. }
  27. else{
  28. let newArray = x.state.categories.concat(doc.id);
  29. x.setState({categories: newArray})
  30.  
  31. // let postsCollectionRef = fire.firestore().collection("posts").where("category", "==", doc.id).orderBy("views", "desc").limit(3);
  32. // postsCollectionRef.get().then(function(collection){
  33. // collection.forEach(function(doc){
  34. // let array = x.state.categories[i].concat(doc.id);
  35. // x.setState({categories:array});
  36. // })
  37. // });
  38. }
  39. i = i+1;
  40. })
  41.  
  42. //console.log("LOAD CATEGORIES", x.state.categories);
  43. })
  44. }
  45.  
  46. loadTopPosts = () =>{
  47. this.setState({loadedTopPosts: true}) //call this right away to ensure that loadTopPosts() is only called once.
  48. let x = this;
  49. let categoryArray = this.state.categories;
  50.  
  51. categoryArray.forEach(function(category){
  52. console.log("CATEGORY: ", category);
  53. let collectionRef = fire.firestore().collection('posts').where("category", "==", category).orderBy("views", "desc").limit(2);
  54.  
  55. collectionRef.get().then(function(collection){
  56. console.log("Collection: ", collection);
  57. if (collection.size === 1){
  58. collection.forEach(function(data){
  59. let tempArray = [category, data.id];
  60. // counter+=1;
  61. let newArray = x.state.top_posts;
  62. newArray.push(tempArray);
  63. x.setState({top_posts:newArray})
  64. //console.log(x.state.top_posts);
  65. console.log("go here?3");
  66. })
  67. }
  68. else if (collection.size === 2){
  69. console.log("go here?4");
  70. let innerArray = [category];
  71. // counter+=1;
  72. collection.forEach(function(data){
  73. innerArray.push(data.id);
  74. if (Object.keys(innerArray).length === 2){
  75. let newArray = x.state.top_posts;
  76. newArray.push(innerArray);
  77. x.setState({top_posts:newArray})
  78. console.log("go here?5");
  79. }
  80. })
  81. }
  82. else{ //size is 0
  83. let tempArray = [category];
  84. // counter+=1;
  85. let newArray = x.state.top_posts;
  86. newArray.push(tempArray);
  87. x.setState({top_posts:newArray})
  88. }
  89. x.setState({loadedTopPosts2: true})
  90.  
  91. console.log("TOP POSTS: ",x.state.top_posts);
  92. })
  93. })
  94. // counter =0;
  95. }
  96.  
  97. handleClick = (e) =>{
  98. let title = e.target.id;
  99. this.props.updateDisplayPosts(title);
  100. }
  101.  
  102. parseTopPosts = () =>{
  103. let indexer = this.state.indexer;
  104. this.setState({indexer: indexer+1}) //increment indexer
  105. if (this.state.indexer === 5){
  106. this.setState({indexer: 0, loadedTopPosts2:false});
  107. return;
  108. }
  109. let indexOf = this.state.top_posts.indexOf(indexer);
  110. console.log(indexOf);
  111. if (typeof this.state.top_posts[indexOf+1] === "string" && indexOf !==-1){
  112. console.log("go here1");
  113. if (typeof this.state.top_posts[indexOf+2] === "string"){
  114. console.log("go here2");
  115. return (<div><li><a>{this.state.top_posts[indexOf+1]}</a></li>
  116. <li><a>{this.state.top_posts[indexOf+2]}</a></li></div>);
  117. }
  118. else{
  119. console.log("go here3");
  120. return <li><a>{this.state.top_posts[indexOf+1]}</a></li>;
  121. }
  122. }
  123. else{
  124. console.log("go here4");
  125. //no top posts...
  126. return;
  127. }
  128. }
  129.  
  130. increaseIndexer = () =>{
  131. this.setState({indexer: this.state.indexer+1})
  132. return this.state.indexer;
  133. }
  134.  
  135. render(){
  136. let indexer = this.increaseIndexer();
  137. return(
  138. <div>
  139. {!this.state.loadedCategories ? this.loadCategories(): null}
  140. {(this.state.loadedCategories && !this.state.loadedTopPosts) ? this.loadTopPosts():null}
  141. <h2>Popular Right Now</h2>
  142.  
  143. {(this.state.loadedCategories && this.state.loadedTopPosts2) ? this.state.categories.map(i =>{
  144. return (<div>
  145. <h3>{i}</h3>
  146. <ul>
  147. {/* {(this.state.loadedCategories && this.state.loadedTopPosts2) ? */}
  148. {this.state.top_posts[indexer]}
  149. {/* {(this.state.loadedCategories && this.state.loadedTopPosts2) ? this.increaseIndexer(): null} */}
  150. {/* // [2].map(j=>{
  151. // return(<li><a>{j}</a></li>)
  152. // })} */}
  153. {/* : null} */}
  154. </ul></div>
  155. )}): null}
  156.  
  157. </div>
  158. )
  159. }
  160. }
  161.  
  162. export default Popular;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement