Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
(ns trello-workflow.trello-interfaces
(:require [re-frame.core :as re-frame]
[ajax.core :as ajax]
[ :as io]
[goog.string :as gstring]
(defprotocol TrelloInterface
" interface to loading boards, lists, cards
{:command :load-board
(invoke [this opts]))
(declare servertrello faketrello trello)
(deftype ServerTrello []
; implements everything below
(invoke [this opts]
(println "ServerTrello: " opts)
(case (:command opts)
; initial login
; :on-success [::load-initial-state-success]
; :on-failure [::load-initial-state-success]}]
; cljs-ajax
; :handler - A function that takes a single argument [ok result].
; The result will be the response if true and the error response if false.
(let [handler (fn [[ok retval]]
(println "ServerTrello: whoami handler: callback: " ok retval)
(println "opts: " opts)
(if ok
(re-frame/dispatch [(first (:on-success opts)) retval])
(re-frame/dispatch [(first (:on-failure opts)) retval])))]
(ajax/ajax-request (merge opts
{:handler handler})))
(io/http-async-get-boards re-frame/dispatch
(:callback opts))
(io/http-async-get-board-lists re-frame/dispatch
(:board-id opts)
(:callback opts))
(io/http-async-get-list-count re-frame/dispatch
(:list-id opts)
(:callback opts)))))
; {
; :method :get
; :uri "/whoami"
; :timeout 30000
; :format (ajax/json-request-format)
; :response-format (ajax/json-response-format {:keywords? true})
; :on-success [::load-initial-state-success]
; :on-failure [::load-initial-state-success]}}
(defn a-list
"random board"
{:id (gstring/format "list-%d" (rand-int 10000))
:name (gstring/format "list-%d" (rand-int 100))
:closed false
:idBoard (gstring/format "board-%d" 100)
:pos 8192
:subscribed false
:softLimit nil})
(deftype FakeTrello []
; implements everything below
(invoke [this opts]
(println "FakeTrello: " opts)
(case (:command opts)
(let [retval [(first (:on-success opts))
{:username "fake"
:fullname "fake fake"}]
_ (println "LOGIN: " retval)]
(re-frame/dispatch retval))
(let [retval [(:callback opts)
[{:id "abc"
:name "Gene's board"
:starred false}
{:id "def"
:name "MVK's board"
:starred false}]]
_ (println "load-board: " retval)]
(re-frame/dispatch retval))
(let [retval [(:callback opts)
(vec (for [x (range 10)]
(println "load-lists: " retval)
(re-frame/dispatch retval))
(let [list-id (:list-id opts)
retval [(:callback opts)
[list-id (rand-int 20)]]]
(println "load-list-card-counts: " retval)
(re-frame/dispatch retval)))))
(def faketrello (FakeTrello.))
(def servertrello (ServerTrello.))
;(def trello faketrello)
(defn call-trello [opts]
(invoke trello opts))
(defn set-trello-fake
(println "set-trello-fake: " fake?)
(def trello (if fake?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment