Skip to content

Commit

Permalink
integrate with bil
Browse files Browse the repository at this point in the history
  • Loading branch information
maeda committed Aug 19, 2020
1 parent 3605cbd commit a3d5d46
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 13 deletions.
1 change: 1 addition & 0 deletions resources/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
<div id="app"></div>
<script src="/js/compiled/app.js"></script>
<script src="/js/aii-jslib.js"></script>
<script src="/js/bil-jslib.js"></script>
</body>
</html>
41 changes: 41 additions & 0 deletions src/cljs/tsca_webapp/bil/effects.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
(ns tsca-webapp.bil.effects
(:require
[re-frame.core :as re-frame]
[cljs.core.match :refer-macros [match]]
[day8.re-frame.tracing :refer-macros [fn-traced]]
[tsca-webapp.task.effects :as task]
["../common/mock.js" :as mock])
(:require-macros [tsca-webapp.aii :refer [defcommand]]))

(declare bil)
(def loading-interval 250)

(defn- initialize []
(js/Promise.
(fn [resolve reject]
(letfn [(trial [] (if js/window.TSCABookappInterface
(do (def bil js/window.TSCABookappInterface)
(resolve bil))
(js/setTimeout trial loading-interval)))]
(trial)))))

(re-frame/reg-fx
:bil-initialize
(fn [callback-ids]
(task/callback callback-ids (initialize))))

(defn- parse-period [source]
(-> (.split source ";")
second))

(defn process-single [_]
(let [avatar (-> (bil.avatars) first bil.avatarInfo)]
{:fund-amount (.-balance avatar)
:original-fund-amount (.-amount (bil.genesisInfo))
:frozen-until (parse-period (.-storage avatar))}))

(re-frame/reg-fx
:bil
(fn [{:keys [commands] :as callback-ids}]
(let [promise (js/Promise.all (map process-single commands))]
(task/callback callback-ids (.then promise #(mock/sleep 1000 %))))))
32 changes: 28 additions & 4 deletions src/cljs/tsca_webapp/book_app/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,35 @@
[re-frame.core :as re-frame]
[day8.re-frame.tracing :refer-macros [fn-traced]]))

(re-frame/reg-event-db
(re-frame/reg-event-fx
::open
(fn-traced
[db _]
db))
(fn-traced [{:keys [db]} _]
{:db (-> db
(assoc-in [:book-app] {:status :loading-bil}))
:bil-initialize {:success-id ::bil-ready
:error-id ::bil-loading-error}}))

(re-frame/reg-event-fx
::bil-ready
(fn-traced [{:keys [db]} _]
{:db (-> db
(assoc-in [:book-app] {:status :loading-value}))
:bil {:commands [{:type :values}]
:success-id ::load-values-done
:error-id ::bil-loading-error}}))

(re-frame/reg-event-db
::load-values-done
(fn-traced [db [_ [values]]]
(-> db
(assoc-in [:book-app] {:status :done
:values values}))))

(re-frame/reg-event-db
::bil-loading-error
(fn-traced [db _]
(-> db
(assoc-in [:book-app] {:status :error}))))

(re-frame/reg-event-fx
::change-iframe-url
Expand Down
26 changes: 21 additions & 5 deletions src/cljs/tsca_webapp/book_app/subs.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,31 @@
[cljs.core.match :refer-macros [match]]
[tsca-webapp.common.subs-parts :as common]))

(re/reg-sub
::book-app
(fn [{:keys [book-app]}]
book-app))

(re/reg-sub
::status
:<- [::book-app]
(fn [{:keys [status]}]
(or (#{:done :error} status)
:loading)))

(re/reg-sub
::parameters
:<- [::common/routing-params]
(fn [{:keys [bahash]}]
:<- [::book-app]
(fn [[{:keys [bahash]} {:keys [values]}]]
(match [bahash]
["MOCK_bookhash_proto0_funny"] [{:title "Initial Balance"}]
["MOCK_bookhash_proto0_frozen"] [{:title "Fund Amount"}
{:title "Unfrozen Timestamp"}
{:title "Fund Owner"}]
["MOCK_bookhash_proto0_funny"]
[{:title "Initial Balance" :value ":smile:"}]

["MOCK_bookhash_proto0_frozen"]
[{:title "Fund Balance" :value (:fund-amount values)}
{:title "Frozen Until" :value (:frozen-until values)}
{:title "Original Fund Amount" :value (:original-fund-amount values)}]
:else [])))

(re/reg-sub
Expand Down
11 changes: 7 additions & 4 deletions src/cljs/tsca_webapp/book_app/views.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
(defn- main-page [modal-atom]
[:div.card
[:div.card-body
(map-indexed (fn [i {:keys [title]}]
[:div.columns {:keys (str "p-" i)}
(map-indexed (fn [i {:keys [title value]}]
[:div.columns {:key (str "p-" i)}
[:div.col-4 title]
[:div.col-4 "???"]])
[:div.col-4 value]])
@(re-frame/subscribe [::subs/parameters]))
[:div.gap]
[:button.btn
Expand All @@ -49,5 +49,8 @@
(defn top []
(let [modal-atom (reagent/atom {:show false :url nil})]
[:div
[main-page modal-atom]
(case @(re-frame/subscribe [::subs/status])
:loading [:h4 "Loading..."]
:error [:h4.text-error "loading ERROR!"]
[main-page modal-atom])
[assistnt-modal modal-atom]]))
1 change: 1 addition & 0 deletions src/cljs/tsca_webapp/effects.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
[tsca-webapp.ledger.effects]
[tsca-webapp.task.effects]
[tsca-webapp.aii.effects]
[tsca-webapp.bil.effects]
[tsca-webapp.dom.effects]))

0 comments on commit a3d5d46

Please sign in to comment.