From e5bf1e930a5b6ba0b3300b02daf792d65d795202 Mon Sep 17 00:00:00 2001
From: Jason <jason.daurus@gmail.com>
Date: Tue, 14 Jun 2016 23:52:00 +0800
Subject: [web] FlowView and ContentView

---
 .../js/components/ContentView/ContentLoader.jsx    | 67 ++++++++++++++++++++++
 1 file changed, 67 insertions(+)
 create mode 100644 web/src/js/components/ContentView/ContentLoader.jsx

(limited to 'web/src/js/components/ContentView/ContentLoader.jsx')

diff --git a/web/src/js/components/ContentView/ContentLoader.jsx b/web/src/js/components/ContentView/ContentLoader.jsx
new file mode 100644
index 00000000..f346dc01
--- /dev/null
+++ b/web/src/js/components/ContentView/ContentLoader.jsx
@@ -0,0 +1,67 @@
+import React, { Component, PropTypes } from 'react'
+import { MessageUtils } from '../../flow/utils.js'
+
+export default class ContentLoader extends Component {
+
+    static propTypes = {
+        flow: PropTypes.object.isRequired,
+        message: PropTypes.object.isRequired,
+    }
+
+    constructor(props, context) {
+        super(props, context)
+        this.state = { content: null, request: null }
+    }
+
+    requestContent(nextProps) {
+        if (this.state.request) {
+            this.state.request.abort()
+        }
+
+        const request = MessageUtils.getContent(nextProps.flow, nextProps.message)
+
+        this.setState({ content: null, request })
+
+        request
+            .done(content => {
+                this.setState({ content })
+            })
+            .fail((xhr, textStatus, errorThrown) => {
+                if (textStatus === 'abort') {
+                    return
+                }
+                this.setState({ content: `AJAX Error: ${textStatus}\r\n${errorThrown}` })
+            })
+            .always(() => {
+                this.setState({ request: null })
+            })
+    }
+
+    componentWillMount() {
+        this.requestContent(this.props)
+    }
+
+    componentWillReceiveProps(nextProps) {
+        if (nextProps.message !== this.props.message) {
+            this.requestContent(nextProps)
+        }
+    }
+
+    componentWillUnmount() {
+        if (this.state.request) {
+            this.state.request.abort()
+        }
+    }
+
+    render() {
+        return this.state.content ? (
+            React.cloneElement(this.props.children, {
+                content: this.state.content
+            })
+        ) : (
+            <div className="text-center">
+                <i className="fa fa-spinner fa-spin"></i>
+            </div>
+        )
+    }
+}
-- 
cgit v1.2.3

