New 'update address' view

* show web3.eth.accounts in dropdown in case web3 available, otherwise
  edit box
This commit is contained in:
Teemu Patja 2017-02-18 22:43:11 +02:00
parent d35b794ca4
commit 354c262301
No known key found for this signature in database
GPG Key ID: F5B7035E6580FD4C
4 changed files with 69 additions and 31 deletions

View File

@ -1,31 +1,21 @@
(ns commiteth.common
(:require [reagent.core :as reagent]
(:require [reagent.core :as r]
[re-frame.core :as rf]))
(defn input [{:keys [value-path] :as props}]
(let [init-val @(rf/subscribe [:get-in value-path])
props-clean (dissoc props :value-path)
val (reagent/atom init-val)
save #(let [v (-> @val str clojure.string/trim)]
(when (seq v)
(rf/dispatch [:assoc-in value-path v])))]
(fn []
[:input.form-control
(merge props-clean {:type "text"
:value @val
:on-blur save
:on-change #(reset! val (-> % .-target .-value))})])))
(defn input [val-ratom props]
(fn []
[:input
(merge props {:type "text"
:value @val-ratom
:on-change #(reset! val-ratom (-> % .-target .-value))})]))
(defn checkbox [{:keys [value-path on-change]}]
(let [init-val @(rf/subscribe [:get-in value-path])
val (reagent/atom init-val)]
(fn [props]
(let [props-clean (dissoc props :value-path)]
[:input.form-control
(merge {:type "checkbox"
:checked @val
:on-change #(let [new-val (not @val)]
(on-change)
(rf/dispatch [:assoc-in value-path
(reset! val new-val)]))}
props-clean)]))))
(defn dropdown [title val-ratom items]
(fn []
(if (= 1 (count items))
(reset! val-ratom (first items)))
[:select.ui.basic.selection.dropdown
{:on-change #(reset! val-ratom (-> % .-target .-value))}
(doall (for [item items]
^{:key item} [:option
{:value item}
item]))]))

View File

@ -12,9 +12,10 @@
[commiteth.activity :refer [activity-page]]
[commiteth.repos :refer [repos-page]]
[commiteth.bounties :refer [bounties-page]]
[commiteth.update-address :refer [update-address-page]]
[commiteth.manage :refer [manage-page]]
[commiteth.issues :refer [issues-page]]
[commiteth.common :refer [input checkbox]]
[commiteth.common :refer [input]]
[commiteth.config :as config]
[commiteth.svg :as svg]
[clojure.set :refer [rename-keys]]
@ -125,14 +126,15 @@
(def pages
{:activity #'activity-page
:repos #'repos-page
:bounties #'bounties-page})
:bounties #'bounties-page
:update-address #'update-address-page})
(defn page []
(fn []
[:div.ui.pusher
[page-header]
;; [error-pane]
[:div.ui.vertical.segment.foo
[:div.ui.vertical.segment
[:div.page-content
[(pages @(rf/subscribe [:page]))]]]]))

View File

@ -170,11 +170,20 @@
:enabled (:enabled repo)} ))))
(reg-event-fx
:update-address
(fn [{:keys [db]} [_]]
{:db db
:dispatch [:set-active-page :update-address]}))
(reg-event-fx
:save-user-address
(fn [{:keys [db]} [_ user-id address]]
(prn "save-user-address" user-id address)
{:db db
:http {:method POST
:url "/api/user/address"
:on-success #(println %)
:on-success #(println "on-success" %)
:on-error #(println "on-error" %)
:params {:user-id user-id :address address}}}))

View File

@ -0,0 +1,37 @@
(ns commiteth.update-address
(:require [re-frame.core :as rf]
[commiteth.common :refer [input dropdown]]
[reagent.core :as r]
[reagent.crypt :as crypt]))
(defn update-address-page []
(let [user (rf/subscribe [:user])
web3 (.-web3 js/window)
web3-accounts (into [] (when-not (nil? web3) (-> web3
.-eth
.-accounts
js->clj)))
address (r/atom @(rf/subscribe [:get-in [:user :address]]))]
(fn []
(println "web3-accounts" web3-accounts)
[:div.ui.container.grid
[:div.ui.form.eight.wide.column
[:h3 "Update address"]
[:p "Placeholder text for explaining what an Etheum address is."]
[:div.field
(if-not (empty? web3-accounts)
[dropdown "Select address"
address
(into []
(for [acc web3-accounts]
acc))]
[:div.ui.input
[input address {:placeholder "0x0000000000000000000000000000000000000000"
:auto-complete "off"
:max-length 42}]])]
[:button.ui.button {:on-click
#(rf/dispatch [:save-user-address
(:id @user)
@address])}
"Update"]]])))