var React = require('react');
var moment = require('moment');

var DateSpan =  React.createClass({
    propTypes: {
        date: React.PropTypes.oneOfType([
            React.PropTypes.number,
            React.PropTypes.string,
            React.PropTypes.instanceOf(Date)
        ]).isRequired,

        refreshInterval: React.PropTypes.number,
        format:          React.PropTypes.string
    },

    getDefaultProps: function() {
        return {
            format:          '',
            refreshInterval: 10*1000
        };
    },

    getInitialState: function() {
        return {
            currentDate: Date.now()
        };
    },

    tick: function() {
        // Update "fake" internal state to trigger re-rendering
        this.setState({
            currentDate: Date.now()
        });
    },

    componentDidMount: function() {
        if (!this.props.format) {
            this.interval = setInterval(this.tick, this.props.refreshInterval);
        }
    },

    componentWillUnmount: function() {
        if (!this.props.format) {
            clearInterval(this.interval);
        }
    },

    render: function() {
        var date   = this.props.date;
        var format = this.props.format;

        // Display interval by default
        var displayDate = moment(date).fromNow();
        // Apply formating if provided
        if (Boolean(format)) {
            displayDate = moment(date).format(format);
        }

        return <span>{displayDate}</span>;
    }
});

module.exports = DateSpan;

@@ -1,11 +1,13 @@
-var
+const
  React =
@@ -25,19 +25,21 @@
 eact');%0A
-var
+const
  moment 
@@ -64,74 +64,25 @@
 );%0A%0A
-var DateSpan =  React.createClass(%7B%0A    propTypes: %7B%0A        date:
+const dateShape =
  Rea
@@ -106,24 +106,16 @@
 fType(%5B%0A
-        
     Reac
@@ -134,24 +134,16 @@
 number,%0A
-        
     Reac
@@ -162,24 +162,16 @@
 string,%0A
-        
     Reac
@@ -203,18 +203,102 @@
 te)%0A
-        %5D)
+%5D);%0A%0Aconst DateSpan =  React.createClass(%7B%0A    propTypes: %7B%0A        date:            dateShape
 .isR
@@ -306,17 +306,16 @@
 quired,%0A
-%0A
         
@@ -407,24 +407,75 @@
 ing%0A    %7D,%0A%0A
+    contextTypes: %7B%0A        now: dateShape%0A    %7D,%0A%0A
     getDefau
@@ -665,39 +665,23 @@
         
-currentDate: Date.now()
+tick: 0
 %0A       
@@ -775,24 +775,60 @@
 e-rendering%0A
+        let %7B tick %7D = this.state;%0A%0A
         this
@@ -855,31 +855,22 @@
     
-currentDate: Date.now()
+tick: tick + 1
 %0A   
@@ -926,33 +926,32 @@
 ) %7B%0A        if (
-!
 this.props.forma
@@ -951,32 +951,59 @@
 s.format) %7B%0A    
+        return;%0A        %7D%0A%0A
         this.int
@@ -1058,34 +1058,24 @@
 hInterval);%0A
-        %7D%0A
     %7D,%0A%0A    
@@ -1125,34 +1125,29 @@
 if (
-!
 this.
-props.format
+interval
 ) %7B%0A    
     
@@ -1134,32 +1134,59 @@
 interval) %7B%0A    
+        return;%0A        %7D%0A%0A
         clearInt
@@ -1207,26 +1207,16 @@
 erval);%0A
-        %7D%0A
     %7D,%0A%0A
@@ -1252,18 +1252,28 @@
     
-var date  
+let %7B date, format %7D
  = t
@@ -1285,13 +1285,8 @@
 rops
-.date
 ;%0A  
@@ -1295,49 +1295,44 @@
     
-var format = this.props.forma
+let %7B now %7D = this.contex
 t;%0A
-%0A
         
 // D
@@ -1331,88 +1331,25 @@
     
-// Display interval by default%0A        var displayDate = moment(date).fromNow();
+let displayDate;%0A
 %0A   
@@ -1399,24 +1399,16 @@
  if 
-(Boolean
 (format)
 ) %7B%0A
@@ -1403,17 +1403,16 @@
 (format)
-)
  %7B%0A     
@@ -1474,87 +1474,613 @@
    %7D
-%0A%0A        return %3Cspan%3E%7BdisplayDate%7D%3C/span%3E;%0A    %7D%0A%7D);%0A%0Amodule.exports = DateSpan
+ else %7B%0A            displayDate = moment(date).from(now);%0A        %7D%0A%0A        return %3Cspan%3E%7BdisplayDate%7D%3C/span%3E;%0A    %7D%0A%7D);%0A%0Aconst DateContext =  React.createClass(%7B%0A    propTypes: %7B%0A        children: React.PropTypes.node,%0A        now:      dateShape%0A    %7D,%0A%0A    childContextTypes: %7B%0A        now: dateShape%0A    %7D,%0A%0A    getChildContext: function() %7B%0A        return %7B%0A            now: this.props.now%0A        %7D;%0A    %7D,%0A%0A    render: function() %7B%0A        return (React.Children.only(this.props.children));%0A    %7D%0A%7D);%0A%0Amodule.exports = DateSpan;%0Amodule.exports.shape = dateShape;%0Amodule.exports.Context = DateContext
 ;%0A

Fix #15: fix rendering of DateSpan on server