// @flow

import React from 'react'
import sigma from '../sigma/main'
import '../sigma/parsers.json.js'
import { embedProps } from './tools'

type State = {
    loaded: boolean
};
type Props = {
	path: string,
    onGraphLoaded?: () => void,
    children?: mixed,
    sigma?: sigma
};


// TODO: make a superclass for loaders??

/**

LoadJSON component, interface for parsers.json sigma plugin. Can be used within Sigma component.
Can be composed with other plugins: on load it mounts all child components (e.g. other sigma plugins). 
Child's componentWillMount should be used to enable plugins on loaded graph.

 @param {string} path   path to the JSON file
 @param {Function} onGraphLoaded        Optional callback for graph update

[see sigma plugin page for more details](https://github.com/jacomyal/sigma.js/tree/master/plugins/sigma.neo4j.cypher)

**/


class LoadJSON extends React.PureComponent {
    state: State;
	props: Props;
    onLoad: () => void;

    constructor(props: Props) {
        super(props)
        this.state = {loaded:false}
        this.onLoad = this._onLoad.bind(this)
    }

	componentDidMount() {
		this._load(this.props.path)
	}

	componentWillReceiveProps(props: Props) {
		// reload only if path changes
		if(this.props.path !== props.path) {
            this.setState({loaded:false})
			this._load(props.path)
        }
	}

	render() {
        if(!this.state.loaded)
            return null
        return <div>{ embedProps(this.props.children, {sigma: this.props.sigma}) }</div>
    }


    _load(url: string) {
        sigma.parsers.json(
                this.props.path ,
                this.props.sigma ,
                this.onLoad
        )
    }

    _onLoad() {
        if(this.props.sigma)
            this.props.sigma.refresh()
        this.setState({loaded:true})
        if(this.props.onGraphLoaded)
            return this.props.onGraphLoaded()
    }

}

export default LoadJSON;


@@ -150,18 +150,16 @@
 ate = %7B%0A
-  
   loaded
@@ -201,18 +201,16 @@
 string,%0A
-  
   onGrap
@@ -231,18 +231,16 @@
 %3E void,%0A
-  
   childr
@@ -251,18 +251,16 @@
  mixed,%0A
-  
   sigma?
@@ -518,17 +518,16 @@
 lugins).
- 
 %0AChild's
@@ -889,18 +889,16 @@
 onent %7B%0A
-  
   state:
@@ -920,18 +920,16 @@
  Props;%0A
-  
   onLoad
@@ -945,18 +945,16 @@
 oid;%0A%0A  
- 
- 
 construc
@@ -969,28 +969,24 @@
 s: Props) %7B%0A
-    
     super(pr
@@ -990,20 +990,16 @@
 (props)%0A
-    
     this
@@ -1022,20 +1022,16 @@
 :false%7D%0A
-    
     this
@@ -1064,18 +1064,16 @@
 d(this)%0A
-  
   %7D%0A%0A%09co
@@ -1241,22 +1241,16 @@
 path) %7B%0A
-      
       th
@@ -1303,20 +1303,16 @@
 s.path)%0A
-    
     %7D%0A%09%7D
@@ -1321,28 +1321,24 @@
 %09render() %7B%0A
-    
     if(!this
@@ -1348,30 +1348,24 @@
 ate.loaded)%0A
-      
       return
@@ -1370,28 +1370,24 @@
 rn null%0A    
- 
-   
 return %3Cdiv%3E
@@ -1459,18 +1459,14 @@
 iv%3E%0A
-  
   %7D%0A%0A%0A
-  
   _l
@@ -1480,28 +1480,24 @@
 : string) %7B%0A
-    
     sigma.pa
@@ -1508,26 +1508,16 @@
 s.json(%0A
-          
       th
@@ -1532,26 +1532,16 @@
 .path ,%0A
-          
       th
@@ -1557,26 +1557,16 @@
 sigma ,%0A
-          
       th
@@ -1583,23 +1583,15 @@
     
-   
- 
 )%0A
-  
   %7D%0A%0A
-  
   _o
@@ -1600,28 +1600,24 @@
 oad() %7B%0A    
- 
-   
 if(this.prop
@@ -1631,22 +1631,16 @@
 )%0A      
-  
-    
 this.pro
@@ -1662,20 +1662,16 @@
 h()%0A    
-  
-  
 this.set
@@ -1695,20 +1695,16 @@
 e%7D)%0A    
-   
- 
 if(this.
@@ -1730,22 +1730,16 @@
 )%0A      
-     
- 
 return t
@@ -1766,20 +1766,17 @@
 ded()%0A  
-  %7D%0A
+%7D
 %0A%7D%0A%0Aexpo
@@ -1796,9 +1796,8 @@
 adJSON;%0A
-%0A

format spaces