From 3c4413b160af025056e8554e0f012ed18212b5ff Mon Sep 17 00:00:00 2001 From: Vitaliy Vlasov Date: Mon, 29 Jan 2018 19:07:54 +0200 Subject: [PATCH 1/6] [FIX #187] Add dummy value to Metamask dropdown; do not automatically preselect first item in the list; convert addresses to lower-case --- src/cljs/commiteth/common.cljs | 17 +++++++++-------- src/cljs/commiteth/update_address.cljs | 14 ++++++++------ 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/cljs/commiteth/common.cljs b/src/cljs/commiteth/common.cljs index 1213395..bc44638 100644 --- a/src/cljs/commiteth/common.cljs +++ b/src/cljs/commiteth/common.cljs @@ -10,18 +10,19 @@ (merge props {:type "text" :value @val-ratom :on-change #(reset! val-ratom (-> % .-target .-value))})])) - (defn dropdown [props title val-ratom items] (fn [] - (if (= 1 (count items)) - (reset! val-ratom (first items))) [:select.ui.basic.selection.dropdown (merge props {:on-change - #(reset! val-ratom (-> % .-target .-value))}) - (doall (for [item items] - ^{:key item} [:option - {:value item} - item]))])) + #(let [selected-value (-> % .-target .-value)] + (when (not= selected-value title) + (reset! val-ratom selected-value))) + :default-value (if (contains? (set items) @val-ratom) + @val-ratom + title)}) + (conj (doall (for [item items] + ^{:key item} [:option {:value item} item])) + ^{:key title} [:option {:value title :disabled true} title])])) (defn moment-timestamp [time] (let [now (.now js/Date.) diff --git a/src/cljs/commiteth/update_address.cljs b/src/cljs/commiteth/update_address.cljs index 6134bde..41364c6 100644 --- a/src/cljs/commiteth/update_address.cljs +++ b/src/cljs/commiteth/update_address.cljs @@ -3,6 +3,7 @@ [commiteth.common :refer [input dropdown]] [reagent.core :as r] [reagent.crypt :as crypt] + [clojure.string :as str] [cljs-web3.eth :as web3-eth])) @@ -10,7 +11,9 @@ (let [db (rf/subscribe [:db]) user (rf/subscribe [:user]) updating-address (rf/subscribe [:get-in [:updating-address]]) - address (r/atom @(rf/subscribe [:get-in [:user :address]]))] + address (-> @(rf/subscribe [:get-in [:user :address]]) + str/lower-case + r/atom)] (fn [] (let [web3 (:web3 @db) web3-accounts (when web3 @@ -22,13 +25,12 @@ [:p "Insert your Ethereum address in hex format."] [:div.field (if-not (empty? web3-accounts) - [dropdown {:class "address-input"} "Select address" + [dropdown {:class "address-input"} + "Select address" address - (vec - (for [acc web3-accounts] - acc))] + (map str/lower-case web3-accounts)] [:div.ui.input.address-input - [input address {:placeholder "0x0000000000000000000000000000000000000000" + [input address {:placeholder "0x0000000000000000000000000000000000000000" :auto-complete "off" :auto-correct "off" :spell-check "false" From c5fdad5b8e86f980d271e5f57595f5361ac12733 Mon Sep 17 00:00:00 2001 From: Vitaliy Vlasov Date: Mon, 29 Jan 2018 19:36:49 +0200 Subject: [PATCH 2/6] Fix assoc-in in save-user-address handler --- src/cljs/commiteth/handlers.cljs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cljs/commiteth/handlers.cljs b/src/cljs/commiteth/handlers.cljs index 077d280..382d7fb 100644 --- a/src/cljs/commiteth/handlers.cljs +++ b/src/cljs/commiteth/handlers.cljs @@ -324,7 +324,7 @@ :http {:method POST :url "/api/user/address" :on-success #(do - (dispatch [:assoc-in [:user [:address] address]]) + (dispatch [:assoc-in [:user :address] address]) (dispatch [:set-flash-message :success "Address saved"])) From a52f5f2b20e446670c414f033979457528df65ca Mon Sep 17 00:00:00 2001 From: Vitaliy Vlasov Date: Mon, 29 Jan 2018 20:18:16 +0200 Subject: [PATCH 3/6] Move selection behaviour and option list population to dropdown component --- src/cljs/commiteth/common.cljs | 31 ++++++++++++++++---------- src/cljs/commiteth/update_address.cljs | 5 ++--- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/cljs/commiteth/common.cljs b/src/cljs/commiteth/common.cljs index bc44638..a613925 100644 --- a/src/cljs/commiteth/common.cljs +++ b/src/cljs/commiteth/common.cljs @@ -10,19 +10,26 @@ (merge props {:type "text" :value @val-ratom :on-change #(reset! val-ratom (-> % .-target .-value))})])) + (defn dropdown [props title val-ratom items] - (fn [] - [:select.ui.basic.selection.dropdown - (merge props {:on-change - #(let [selected-value (-> % .-target .-value)] - (when (not= selected-value title) - (reset! val-ratom selected-value))) - :default-value (if (contains? (set items) @val-ratom) - @val-ratom - title)}) - (conj (doall (for [item items] - ^{:key item} [:option {:value item} item])) - ^{:key title} [:option {:value title :disabled true} title])])) + "If val-ratom is set, preselect it in the dropdown. + Add value of val-ratom if it's missing from items list. + Otherwise, prepend title as a disabled option" + (let [items (cond-> items + (and @val-ratom + (not (contains? (set items) @val-ratom))) + (conj @val-ratom) + (not @val-ratom) + (conj title))] + (fn [] + [:select.ui.basic.selection.dropdown + (merge props {:on-change + #(reset! val-ratom (-> % .-target .-value)) + :default-value (or @val-ratom title)}) + (doall (for [item items] + ^{:key item} [:option {:value item + :disabled (= item title)} + item]))]))) (defn moment-timestamp [time] (let [now (.now js/Date.) diff --git a/src/cljs/commiteth/update_address.cljs b/src/cljs/commiteth/update_address.cljs index 41364c6..9261671 100644 --- a/src/cljs/commiteth/update_address.cljs +++ b/src/cljs/commiteth/update_address.cljs @@ -11,9 +11,8 @@ (let [db (rf/subscribe [:db]) user (rf/subscribe [:user]) updating-address (rf/subscribe [:get-in [:updating-address]]) - address (-> @(rf/subscribe [:get-in [:user :address]]) - str/lower-case - r/atom)] + address (r/atom (some-> @(rf/subscribe [:get-in [:user :address]]) + str/lower-case))] (fn [] (let [web3 (:web3 @db) web3-accounts (when web3 From 9e9d0f6d4d9a4e5e0fc09451b7dac651b46ea223 Mon Sep 17 00:00:00 2001 From: Vitaliy Vlasov Date: Wed, 31 Jan 2018 17:48:26 +0200 Subject: [PATCH 4/6] Change dropdown component: remove doall, use into instead of conj --- src/cljs/commiteth/common.cljs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/cljs/commiteth/common.cljs b/src/cljs/commiteth/common.cljs index a613925..0b0898f 100644 --- a/src/cljs/commiteth/common.cljs +++ b/src/cljs/commiteth/common.cljs @@ -15,21 +15,21 @@ "If val-ratom is set, preselect it in the dropdown. Add value of val-ratom if it's missing from items list. Otherwise, prepend title as a disabled option" - (let [items (cond-> items + (let [items (cond->> items (and @val-ratom (not (contains? (set items) @val-ratom))) - (conj @val-ratom) + (into [@val-ratom]) (not @val-ratom) - (conj title))] + (into [title]))] (fn [] [:select.ui.basic.selection.dropdown (merge props {:on-change #(reset! val-ratom (-> % .-target .-value)) :default-value (or @val-ratom title)}) - (doall (for [item items] + (for [item items] ^{:key item} [:option {:value item :disabled (= item title)} - item]))]))) + item])]))) (defn moment-timestamp [time] (let [now (.now js/Date.) From 50036b10dd6f296aa17da7dd93d5f25cc2f40c86 Mon Sep 17 00:00:00 2001 From: Vitaliy Vlasov Date: Thu, 1 Feb 2018 15:40:54 +0200 Subject: [PATCH 5/6] Address dropdown: move items list construction to invocation point --- src/cljs/commiteth/common.cljs | 27 ++++++++++---------------- src/cljs/commiteth/update_address.cljs | 19 ++++++++++++++---- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/cljs/commiteth/common.cljs b/src/cljs/commiteth/common.cljs index 0b0898f..4399156 100644 --- a/src/cljs/commiteth/common.cljs +++ b/src/cljs/commiteth/common.cljs @@ -13,23 +13,16 @@ (defn dropdown [props title val-ratom items] "If val-ratom is set, preselect it in the dropdown. - Add value of val-ratom if it's missing from items list. - Otherwise, prepend title as a disabled option" - (let [items (cond->> items - (and @val-ratom - (not (contains? (set items) @val-ratom))) - (into [@val-ratom]) - (not @val-ratom) - (into [title]))] - (fn [] - [:select.ui.basic.selection.dropdown - (merge props {:on-change - #(reset! val-ratom (-> % .-target .-value)) - :default-value (or @val-ratom title)}) - (for [item items] - ^{:key item} [:option {:value item - :disabled (= item title)} - item])]))) + Otherwise, prepend title as a disabled option." + (fn [] + [:select.ui.basic.selection.dropdown + (merge props {:on-change + #(reset! val-ratom (-> % .-target .-value)) + :default-value (or @val-ratom title)}) + (for [item items] + ^{:key item} [:option {:value item + :disabled (= item title)} + item])])) (defn moment-timestamp [time] (let [now (.now js/Date.) diff --git a/src/cljs/commiteth/update_address.cljs b/src/cljs/commiteth/update_address.cljs index 9261671..60e298f 100644 --- a/src/cljs/commiteth/update_address.cljs +++ b/src/cljs/commiteth/update_address.cljs @@ -24,10 +24,21 @@ [:p "Insert your Ethereum address in hex format."] [:div.field (if-not (empty? web3-accounts) - [dropdown {:class "address-input"} - "Select address" - address - (map str/lower-case web3-accounts)] + ; Add value of address if it's missing from items list. + ; If address is empty, add title + (let [accounts (map str/lower-case web3-accounts) + addr @address + title "Select address" + items (cond->> accounts + (and addr + (not (contains? (set accounts) addr))) + (into [addr]) + (not addr) + (into [title]))] + [dropdown {:class "address-input"} + title + address + items]) [:div.ui.input.address-input [input address {:placeholder "0x0000000000000000000000000000000000000000" :auto-complete "off" From 8a75d5d68c6bf778a10702af60e7badc28eca4f5 Mon Sep 17 00:00:00 2001 From: Vitaliy Vlasov Date: Thu, 1 Feb 2018 16:32:10 +0200 Subject: [PATCH 6/6] Address dropdown: Apply case-insensitive comparison only during component setup --- src/cljs/commiteth/update_address.cljs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/cljs/commiteth/update_address.cljs b/src/cljs/commiteth/update_address.cljs index 60e298f..14ceaa1 100644 --- a/src/cljs/commiteth/update_address.cljs +++ b/src/cljs/commiteth/update_address.cljs @@ -11,8 +11,7 @@ (let [db (rf/subscribe [:db]) user (rf/subscribe [:user]) updating-address (rf/subscribe [:get-in [:updating-address]]) - address (r/atom (some-> @(rf/subscribe [:get-in [:user :address]]) - str/lower-case))] + address (r/atom @(rf/subscribe [:get-in [:user :address]]))] (fn [] (let [web3 (:web3 @db) web3-accounts (when web3 @@ -29,12 +28,14 @@ (let [accounts (map str/lower-case web3-accounts) addr @address title "Select address" - items (cond->> accounts - (and addr - (not (contains? (set accounts) addr))) - (into [addr]) - (not addr) - (into [title]))] + addr-not-in-web3? (and addr (as-> web3-accounts acc + (map str/lower-case acc) + (set acc) + (contains? acc addr) + (not acc))) + items (cond->> web3-accounts + addr-not-in-web3? (into [addr]) + (not addr) (into [title]))] [dropdown {:class "address-input"} title address