From a3d5d46243d1133915d246f3a443cd235cef0ca1 Mon Sep 17 00:00:00 2001 From: maeda Date: Mon, 17 Aug 2020 16:07:47 +0900 Subject: [PATCH] integrate with bil --- resources/public/index.html | 1 + src/cljs/tsca_webapp/bil/effects.cljs | 41 +++++++++++++++++++++++ src/cljs/tsca_webapp/book_app/events.cljs | 32 +++++++++++++++--- src/cljs/tsca_webapp/book_app/subs.cljs | 26 +++++++++++--- src/cljs/tsca_webapp/book_app/views.cljs | 11 +++--- src/cljs/tsca_webapp/effects.cljs | 1 + 6 files changed, 99 insertions(+), 13 deletions(-) create mode 100644 src/cljs/tsca_webapp/bil/effects.cljs diff --git a/resources/public/index.html b/resources/public/index.html index 8ca4ca8..30d2da8 100644 --- a/resources/public/index.html +++ b/resources/public/index.html @@ -15,5 +15,6 @@
+ diff --git a/src/cljs/tsca_webapp/bil/effects.cljs b/src/cljs/tsca_webapp/bil/effects.cljs new file mode 100644 index 0000000..c6f5a88 --- /dev/null +++ b/src/cljs/tsca_webapp/bil/effects.cljs @@ -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 %)))))) diff --git a/src/cljs/tsca_webapp/book_app/events.cljs b/src/cljs/tsca_webapp/book_app/events.cljs index 636f85a..55c35b1 100644 --- a/src/cljs/tsca_webapp/book_app/events.cljs +++ b/src/cljs/tsca_webapp/book_app/events.cljs @@ -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 diff --git a/src/cljs/tsca_webapp/book_app/subs.cljs b/src/cljs/tsca_webapp/book_app/subs.cljs index cae05c3..64a36ae 100644 --- a/src/cljs/tsca_webapp/book_app/subs.cljs +++ b/src/cljs/tsca_webapp/book_app/subs.cljs @@ -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 diff --git a/src/cljs/tsca_webapp/book_app/views.cljs b/src/cljs/tsca_webapp/book_app/views.cljs index 523c9e4..6bfd154 100644 --- a/src/cljs/tsca_webapp/book_app/views.cljs +++ b/src/cljs/tsca_webapp/book_app/views.cljs @@ -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 @@ -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]])) diff --git a/src/cljs/tsca_webapp/effects.cljs b/src/cljs/tsca_webapp/effects.cljs index 0cba0ff..66639d2 100644 --- a/src/cljs/tsca_webapp/effects.cljs +++ b/src/cljs/tsca_webapp/effects.cljs @@ -4,5 +4,6 @@ [tsca-webapp.ledger.effects] [tsca-webapp.task.effects] [tsca-webapp.aii.effects] + [tsca-webapp.bil.effects] [tsca-webapp.dom.effects]))