import React from 'react';

var CollectedImage = React.createClass({
	displayName: 'CollectedImage',
	getInitialState: function getInitialState() {
		return {
			html: null
		};
	},
	componentWillMount: function componentWillMount() {
		window.promiseCollectedImageURL(this.props).then(this.onLoad, this.onError);
	},
	onLoad: function onLoad(result) {
		this.setState(result);
	},
	onError: function onError(error) {
		this.setState({ error: error });
	},
	render: function render() {
		var text = this.props.text;
		var _state = this.state,
		    html = _state.html,
		    error = _state.error;


		if (html) {
			return React.createElement('div', { dangerouslySetInnerHTML: { __html: html } });
		} else if (error) {
			return React.createElement('div', { children: 'Error: ' + error.message });
		}

		return text;
	}
});

export default CollectedImage;