Advertisement
Foshy

React App

Apr 4th, 2018
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, {Component} from 'react';
  2. import * as localForage from 'localforage';
  3. import './App.css'
  4.  
  5. const urlAny = "http://demo0046512.mockable.io/stream/anycontent"
  6. //const urlAny = "http://192.168.43.88:5000/screen"
  7.  
  8. // for display purposes a constant (stateless component) is enough, when some functionality is needed it's better to create a class
  9. const DisplayImage = (props) => (
  10.     <div>
  11.         <img src={props.urlForImage} className='fullscreenVideo'>
  12.         </img>
  13.     </div>
  14. )
  15.  
  16. const DisplayVideo = (props) => (
  17.     <div>
  18.         {props.urlForVideo
  19.             ?
  20.             <video className='fullscreenVideo' preload="auto" loop autoplay='true' controls>
  21.                 <source src={props.urlForVideo} type="video/mp4"/>
  22.             </video>
  23.             : null}
  24.     </div>
  25. )
  26.  
  27. class DisplayText extends Component {
  28.     constructor(props) {
  29.         super(props);
  30.         this.state = {
  31.             dataText: ''
  32.         }
  33.     }
  34.  
  35.     componentDidMount() {
  36.         fetch(this.props.urlForText)
  37.             .then(responseText => responseText.text())
  38.             .then(responseText => {
  39.                 this.setState({
  40.                     dataText: responseText
  41.                 })
  42.             })
  43.     }
  44.  
  45.     render() {
  46.         return (
  47.             <div>
  48.                 <div dangerouslySetInnerHTML={{__html: this.state.dataText}}></div>
  49.             </div>
  50.         )
  51.     }
  52. }
  53.  
  54. /* ----------------------Fetch & Display-----------------*/
  55. class JsonFetcher extends Component {
  56.     constructor(props) {
  57.         super(props);
  58.  
  59.         this.state = {
  60.             data: [],
  61.         }
  62.     }
  63.  
  64.     componentDidMount() {
  65.         fetch(this.props.mainUrl)
  66.             .then((responseJsonAnyUrl) => responseJsonAnyUrl.json())
  67.             .then((responseJsonAnyUrl) => {
  68.                 this.setState({
  69.                     jsonObject: responseJsonAnyUrl
  70.                 })
  71.             })
  72.     }
  73.  
  74.     interpretJson() {
  75.         if (this.state.jsonObject !== undefined) {
  76.             return this.state.jsonObject.zones.map((mainzoneObj, index) => (
  77.                 //console.log(this.state.jsonObject.zone),
  78.                 Object.values(mainzoneObj).map((zone, index2) => (
  79.                     zone.map((zoneObj, index3) => {
  80.                     //console.log(zoneObj)
  81.                         switch (zoneObj.type) {
  82.                             case 'text':
  83.                                 return(
  84.                                     <DisplayText urlForText={zoneObj.url} key={index3}/>
  85.                                     //sleep here during zoneObj.duration miliseconds
  86.                                 )
  87.                               break;
  88.                             case 'image':
  89.                                 return (
  90.                                     <DisplayImage urlForImage={zoneObj.url} key={index3}/>
  91.                                     //sleep here during zoneObj.duration miliseconds
  92.                                 )    
  93.                               break;
  94.                             case 'video':
  95.                                 return(
  96.                                     <DisplayVideo urlForVideo={zoneObj.url} key={index3}/>
  97.                                     //sleep here during zoneObj.duration miliseconds
  98.                                 )
  99.                               break;
  100.                             default:
  101.                                 return <p>nothing found</p>
  102.                         }
  103.                     })
  104.                 ))    
  105.             ))
  106.         }
  107.     }
  108.     render() {
  109.         return (
  110.          
  111.             <div>
  112.                 <div>
  113.                     {
  114.                         this.interpretJson()
  115.                     }
  116.                 </div>
  117.                 <div>
  118.                     <PersistData fetchedObject={this.state.jsonObject}/>    
  119.                 </div>
  120.                
  121.             </div>
  122.         )
  123.     }
  124. }
  125.  
  126.  
  127.  
  128. /*----------------------- Store & Get persisted data-----------*/
  129. class PersistData extends Component{
  130.     constructor(props){
  131.         super(props);
  132.         this.state = {
  133.             data: [],
  134.         }
  135.     }
  136.  
  137.  
  138. /*[WIP]
  139.     storeData(jsonObj){
  140.         localForage.setItem('key', storableJsonObject )
  141.     }
  142.  
  143.     getStoredData(){
  144.         localForage.getItem('key')
  145.             .then((promisedResult) => {
  146.                this.setState({
  147.                 storedData: promisedResult
  148.                })
  149.                 //console.log(this.state.storedData)
  150.             })        
  151.     }
  152. */
  153.  
  154.     render(){
  155.         return(
  156.             <div>
  157.                 {console.log(this.props.fetchedObject)}
  158.             </div>
  159.         )
  160.     }
  161. }
  162.  
  163. /*----------------------- Main Component ------------------*/
  164. class App extends Component {
  165.     constructor() {
  166.         super();
  167.         this.state = {
  168.             data: [],
  169.         }
  170.     }
  171.  
  172.     componentDidMount() {
  173.  
  174.     }
  175.  
  176.     render() {
  177.         return (
  178.             <div>
  179.                 <JsonFetcher mainUrl={urlAny}/>
  180.             </div>
  181.         )
  182.     }
  183. }
  184. export default App;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement