From 49a04e37c36d576bb764c3cb2dac239b24d261be Mon Sep 17 00:00:00 2001
From: Matthew Shao <me@matshao.com>
Date: Mon, 12 Jun 2017 08:41:40 +0800
Subject: [web] Update ContentLoader class name.

---
 .../js/components/ContentView/ContentLoader.jsx    | 156 +++++++++++----------
 1 file changed, 79 insertions(+), 77 deletions(-)

(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
index 4cafde28..69e4a988 100644
--- a/web/src/js/components/ContentView/ContentLoader.jsx
+++ b/web/src/js/components/ContentView/ContentLoader.jsx
@@ -2,98 +2,100 @@ import React, { Component } from 'react'
 import PropTypes from 'prop-types'
 import { MessageUtils } from '../../flow/utils.js'
 
-export default View => class extends React.Component {
+export default function withContentLoader(View) {
+    
+    return class extends React.Component {
+        static displayName = View.displayName || View.name
+        static matches = View.matches
 
-    static displayName = View.displayName || View.name
-    static matches = View.matches
-
-    static propTypes = {
-        ...View.propTypes,
-        content: PropTypes.string,  // mark as non-required
-        flow: PropTypes.object.isRequired,
-        message: PropTypes.object.isRequired,
-    }
-
-    constructor(props) {
-        super(props)
-        this.state = {
-            content: undefined,
-            request: undefined,
+        static propTypes = {
+            ...View.propTypes,
+            content: PropTypes.string,  // mark as non-required
+            flow: PropTypes.object.isRequired,
+            message: PropTypes.object.isRequired,
         }
-    }
 
-    componentWillMount() {
-        this.updateContent(this.props)
-    }
-
-    componentWillReceiveProps(nextProps) {
-        if (
-            nextProps.message.content !== this.props.message.content ||
-            nextProps.message.contentHash !== this.props.message.contentHash ||
-            nextProps.contentView !== this.props.contentView
-        ) {
-            this.updateContent(nextProps)
+        constructor(props) {
+            super(props)
+            this.state = {
+                content: undefined,
+                request: undefined,
+            }
         }
-    }
 
-    componentWillUnmount() {
-        if (this.state.request) {
-            this.state.request.abort()
+        componentWillMount() {
+            this.updateContent(this.props)
         }
-    }
 
-    updateContent(props) {
-        if (this.state.request) {
-            this.state.request.abort()
+        componentWillReceiveProps(nextProps) {
+            if (
+                nextProps.message.content !== this.props.message.content ||
+                nextProps.message.contentHash !== this.props.message.contentHash ||
+                nextProps.contentView !== this.props.contentView
+            ) {
+                this.updateContent(nextProps)
+            }
         }
-        // We have a few special cases where we do not need to make an HTTP request.
-        if(props.message.content !== undefined) {
-            return this.setState({request: undefined, content: props.message.content})
-        }
-        if(props.message.contentLength === 0 || props.message.contentLength === null){
-            return this.setState({request: undefined, content: ""})
+
+        componentWillUnmount() {
+            if (this.state.request) {
+                this.state.request.abort()
+            }
         }
 
-        let requestUrl = MessageUtils.getContentURL(props.flow, props.message, (View.name == 'ViewServer' ? props.contentView : undefined))
+        updateContent(props) {
+            if (this.state.request) {
+                this.state.request.abort()
+            }
+            // We have a few special cases where we do not need to make an HTTP request.
+            if (props.message.content !== undefined) {
+                return this.setState({request: undefined, content: props.message.content})
+            }
+            if (props.message.contentLength === 0 || props.message.contentLength === null) {
+                return this.setState({request: undefined, content: ""})
+            }
 
-        // We use XMLHttpRequest instead of fetch() because fetch() is not (yet) abortable.
-        let request = new XMLHttpRequest();
-        request.addEventListener("load", this.requestComplete.bind(this, request));
-        request.addEventListener("error", this.requestFailed.bind(this, request));
-        request.open("GET", requestUrl);
-        request.send();
-        this.setState({ request, content: undefined })
-    }
+            let requestUrl = MessageUtils.getContentURL(props.flow, props.message, (View.name == 'ViewServer' ? props.contentView : undefined))
 
-    requestComplete(request, e) {
-        if (request !== this.state.request) {
-            return // Stale request
+            // We use XMLHttpRequest instead of fetch() because fetch() is not (yet) abortable.
+            let request = new XMLHttpRequest();
+            request.addEventListener("load", this.requestComplete.bind(this, request));
+            request.addEventListener("error", this.requestFailed.bind(this, request));
+            request.open("GET", requestUrl);
+            request.send();
+            this.setState({request, content: undefined})
         }
-        this.setState({
-            content: request.responseText,
-            request: undefined
-        })
-    }
 
-    requestFailed(request, e) {
-        if (request !== this.state.request) {
-            return // Stale request
+        requestComplete(request, e) {
+            if (request !== this.state.request) {
+                return // Stale request
+            }
+            this.setState({
+                content: request.responseText,
+                request: undefined
+            })
+        }
+
+        requestFailed(request, e) {
+            if (request !== this.state.request) {
+                return // Stale request
+            }
+            console.error(e)
+            // FIXME: Better error handling
+            this.setState({
+                content: "Error getting content.",
+                request: undefined
+            })
         }
-        console.error(e)
-        // FIXME: Better error handling
-        this.setState({
-            content: "Error getting content.",
-            request: undefined
-        })
-    }
 
-    render() {
-        return this.state.content !== undefined ? (
-            <View content={this.state.content} {...this.props}/>
-        ) : (
-            <div className="text-center">
-                <i className="fa fa-spinner fa-spin"></i>
-            </div>
-        )
+        render() {
+            return this.state.content !== undefined ? (
+                <View content={this.state.content} {...this.props}/>
+            ) : (
+                <div className="text-center">
+                    <i className="fa fa-spinner fa-spin"></i>
+                </div>
+            )
+        }
     }
 };
-- 
cgit v1.2.3

