You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@whimsical.apache.org by Sam Ruby <ru...@apache.org> on 2015/12/13 16:14:54 UTC

[whimsy.git] [22/37] Commit 1478a64: switch index to be totally async

Commit 1478a64223b0c36ee221818581b60d8ba592bcc9:
    switch index to be totally async


Branch: refs/heads/secmail
Author: Sam Ruby <ru...@intertwingly.net>
Committer: Sam Ruby <ru...@intertwingly.net>
Pusher: rubys <ru...@apache.org>

------------------------------------------------------------
server.rb                                                    | + -------
views/index.html.rb                                          | + --------
views/index.js.rb                                            | ++++++++ 
views/index.json.rb                                          | +++++ ------
------------------------------------------------------------
154 changes: 71 additions, 83 deletions.
------------------------------------------------------------


diff --git a/server.rb b/server.rb
index ca9f328..52f85b1 100644
--- a/server.rb
+++ b/server.rb
@@ -13,20 +13,8 @@
 
 # list of messages
 get '/' do
-  # grab current (latest) month
-  mbox = Dir["#{ARCHIVE}/*.yml"].sort.last
-  @messages = Mailbox.new(mbox).headers
-
-  # for the first week of every month, add previous month
-  if File.mtime(mbox).day <= 7
-    mbox = Dir["#{ARCHIVE}/*.yml"].sort[-2]
-    @messages.merge! Mailbox.new(@mbox).headers
-  end
-
-  @messages = @messages.select {|id, message| message[:attachments]}
-  @messages = @messages.sort_by {|id, message| message[:time]}.reverse
-  @mbox = File.basename(mbox, '.yml')
-
+  # determine latest month for which there are messages
+  @mbox = File.basename(Dir["#{ARCHIVE}/*.yml"].sort.last, '.yml')
   _html :index
 end
 
diff --git a/views/index.html.rb b/views/index.html.rb
index fc6f73c..4d9e96e 100644
--- a/views/index.html.rb
+++ b/views/index.html.rb
@@ -10,68 +10,6 @@
 
   _script src: 'app.js'
   _.render '#index' do
-    _Index messages: @messages
-  end
-
-  _input_.btn.btn_primary type: 'submit', value: 'fetch previous month'
-
-  _script do
-    # save initial mailbox information
-    latest = @mbox
-
-    # handle button clicks
-    document.querySelector('.btn').addEventListener('click') do |event|
-      # disable button
-      event.target.disabled = true
-
-      # build JSON post XMLHttpRequest
-      xhr = XMLHttpRequest.new()
-      xhr.open 'POST', "", true
-      xhr.setRequestHeader 'Content-Type', 'application/json;charset=utf-8'
-      xhr.responseType = 'json'
-
-      # process response
-      def xhr.onreadystatechange()
-        if xhr.readyState == 4
-          response = xhr.response.json
-
-          # update latest mbox
-          latest = response.mbox if response.mbox
-
-          # add messages to table
-          tbody = document.querySelector('tbody')
-          response.messages.each do |message|
-            # create a table row
-            tr = document.createElement 'tr'
-
-            # create a column for the time
-            td = document.createElement 'td'
-            a = document.createElement 'a'
-            a.textContent = message.time
-            a.setAttribute 'href', message.href
-            td.appendChild a
-            tr.appendChild td
-
-            # create a column for the message
-            td = document.createElement 'td'
-            td.textContent = message.from
-            tr.appendChild td
-
-            # create a column for the subject
-            td = document.createElement 'td'
-            td.textContent = message.subject
-            tr.appendChild td
-
-            # append row to table
-            tbody.appendChild tr
-          end
-        end
-
-        # re-enable button
-        event.target.disabled = false
-      end
-
-      xhr.send(JSON.stringify mbox: latest)
-    end
+    _Index mbox: @mbox
   end
 end
diff --git a/views/index.js.rb b/views/index.js.rb
new file mode 100644
index 0000000..53accf2
--- /dev/null
+++ b/views/index.js.rb
@@ -0,0 +1,63 @@
+class Index < React
+  def initialize
+    @messages = []
+  end
+
+  def render
+    _table do
+      _thead do
+	_tr do
+	  _th 'Timestamp'
+	  _th 'From'
+	  _th 'Subject'
+	end
+      end
+
+      _tbody do
+	@messages.each do |messsage|
+	  _tr do
+	    _td do
+	      _a messsage.time, href: "#{messsage.href}"
+	    end 
+	    _td messsage.from
+	    _td messsage.subject
+	  end
+	end
+      end
+    end
+
+    _input.btn.btn_primary type: 'submit', value: 'fetch previous month',
+      onClick: self.fetch
+  end
+
+  def componentWillMount()
+    @latest = @@mbox
+  end
+
+  def componentDidMount()
+    self.fetch()
+  end
+
+  def fetch()
+    # build JSON post XMLHttpRequest
+    xhr = XMLHttpRequest.new()
+    xhr.open 'POST', "", true
+    xhr.setRequestHeader 'Content-Type', 'application/json;charset=utf-8'
+    xhr.responseType = 'json'
+
+    # process response
+    def xhr.onreadystatechange()
+      if xhr.readyState == 4
+	response = xhr.response.json
+
+	# update latest mbox
+	@latest = response.mbox if response.mbox
+
+	# add messages to list
+	@messages = @messages.concat(*response.messages)
+      end
+    end
+
+    xhr.send(JSON.stringify mbox: @latest)
+  end
+end
diff --git a/views/index.json.rb b/views/index.json.rb
index a61a365..b888473 100644
--- a/views/index.json.rb
+++ b/views/index.json.rb
@@ -5,13 +5,9 @@
     index = available.find_index "#{ARCHIVE}/#{@mbox}.yml"
 
     # if found and not first, process it
-    if index and index > 0
-
-      # select previous mailbox
-      mbox = available[index-1].untaint
-
+    if index
       # fetch a list of headers for all messages in the maibox with attachments
-      headers = Mailbox.new(mbox).headers.to_a.select do |id, message|
+      headers = Mailbox.new(@mbox).headers.to_a.select do |id, message|
 	message[:attachments]
       end
 
@@ -25,6 +21,9 @@
 	}
       end
 
+      # select previous mailbox
+      mbox = available[index-1].untaint
+
       # return mailbox name and messages
       {
 	mbox: File.basename(mbox, '.yml'),