Advertisement
Foshy

React App function

Apr 23rd, 2018
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class JsonFetcher extends Component {
  2.     constructor(props) {
  3.         super(props);
  4.         this.child = React.createRef();
  5.         console.log('Constructor was called')
  6.         this.state = {
  7.             data: [],
  8.         }
  9.     }
  10.  
  11.     componentDidMount() {  
  12.         console.log('CompDidMount was called')
  13.         this.updateContent(this.props.mainUrl)
  14.     }
  15.  
  16.     updateContent(mainUrl){
  17.         console.log("updateContent was called")
  18.         fetch(mainUrl)
  19.             .then((responseJsonAnyUrl) => responseJsonAnyUrl.json())
  20.             .then((responseJsonAnyUrl) => {
  21.                 this.setState({
  22.                     mainUrl: mainUrl,
  23.                     jsonObject: responseJsonAnyUrl
  24.                 },
  25.                 function testName(){
  26.                     if(this.child.current){    
  27.                         let interval = 20000
  28.                         let ind = this.child.current.getCurrentIndex()
  29.                         let zlengths = this.child.current.getZoneLengths()
  30.                         let option = 1
  31.                         if (ind !== zlengths[0]-1){
  32.                             //didn't iterate trough all content, can't update
  33.                             option=2
  34.                             interval = 5000
  35.                         }  
  36.                         console.log('ind', ind)
  37.                         console.log('len', zlengths[0]-1)
  38.                         console.log('interval', interval)
  39.                         this.timeout = setTimeout(
  40.                             function(){
  41.                                 if (option===1){
  42.                                     this.updateContent(this.state.mainUrl)
  43.                                 }else if (option===2){
  44.                                     testName()
  45.                                 }
  46.                             }.bind(this, this.state.mainUrl)
  47.                             ,
  48.                             interval)
  49.                     }    
  50.                 })
  51.             })
  52.     }
  53.  
  54.  
  55.     interpretJson() {
  56.         console.log('interpretJson was called')
  57.         var contentListArray = []
  58.         if (this.state.jsonObject !== undefined) {
  59.             this.state.jsonObject.zones.map((mainzoneObj, index) => {
  60.                 Object.values(mainzoneObj).map((zone, index2) => {   /*( {*/
  61.                     var contentList= []
  62.                     zone.map((zoneObj, index3) => {
  63.                         contentList.push({type: "clear",
  64.                                             url: "",
  65.                                             duration: 0})
  66.                                            
  67.                         contentList.push({type: zoneObj.type,
  68.                                             url: zoneObj.url,
  69.                                             duration:zoneObj.duration})
  70.                     })
  71.                     console.log(contentList)
  72.                     contentListArray.push(contentList)
  73.                 })
  74.             })    
  75.         }
  76.        
  77.         return(
  78.             <Display content={contentListArray} ref={this.child}/>
  79.         )
  80.     }
  81.  
  82.        
  83.     render() {
  84.         console.log('Render was called')
  85.         if(this.state.jsonObject){
  86.             return (
  87.                 <div>
  88.                     <div>
  89.                         { //no point calling this before JsonObject is defined
  90.                             this.interpretJson()
  91.                         }
  92.                     </div>
  93.                 </div>
  94.             )
  95.         }else
  96.             return(
  97.                 null
  98.             )                
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement