Skip to content

Instantly share code, notes, and snippets.

@realgenekim
Last active July 19, 2019 01:28
Show Gist options
  • Save realgenekim/4375188bba08300612b1e00b62f6555e to your computer and use it in GitHub Desktop.
Save realgenekim/4375188bba08300612b1e00b62f6555e to your computer and use it in GitHub Desktop.
(ns trello-workflow.trello-interfaces
(:require [re-frame.core :as re-frame]
[ajax.core :as ajax]
[trello-workflow.io :as io]
[goog.string :as gstring]
[goog.string.format]))
(defprotocol TrelloInterface
" interface to loading boards, lists, cards
{:command :load-board
...
"
(invoke [this opts]))
(declare servertrello faketrello trello)
(deftype ServerTrello []
TrelloInterface
; 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.
:init
(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})))
:load-boards
(io/http-async-get-boards re-frame/dispatch
(:callback opts))
:load-lists
(io/http-async-get-board-lists re-frame/dispatch
(:board-id opts)
(:callback opts))
:load-list-card-counts
(io/http-async-get-list-count re-frame/dispatch
(:list-id opts)
(:callback opts)))))
;{:http-xhrio
; {
; :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))
;"5d0970462e03af7956e850ae")
:name (gstring/format "list-%d" (rand-int 100))
:closed false
:idBoard (gstring/format "board-%d" 100)
;"595a82896010c31f0ef7692e"
:pos 8192
:subscribed false
:softLimit nil})
(deftype FakeTrello []
TrelloInterface
; implements everything below
(invoke [this opts]
(println "FakeTrello: " opts)
(case (:command opts)
:init
(let [retval [(first (:on-success opts))
{:username "fake"
:fullname "fake fake"}]
_ (println "LOGIN: " retval)]
(re-frame/dispatch retval))
:load-boards
(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))
:load-lists
(let [retval [(:callback opts)
(vec (for [x (range 10)]
(a-list)))]]
(println "load-lists: " retval)
(re-frame/dispatch retval))
:load-list-card-counts
(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
[fake?]
(println "set-trello-fake: " fake?)
(def trello (if fake?
faketrello
servertrello)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment