Skip to content

Commit

Permalink
finish all the logic, polishing pending
Browse files Browse the repository at this point in the history
  • Loading branch information
briansztamfater committed May 9, 2024
1 parent 7435066 commit 505e486
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 92 deletions.
12 changes: 12 additions & 0 deletions src/status_im/contexts/wallet/common/utils/networks.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
[status-im.constants :as constants]
[utils.number]))

(def ^:private last-comma-followed-by-text-to-end-regex #",\s(?=[^,]+$)")

(def id->network
{constants/ethereum-mainnet-chain-id constants/mainnet-network-name
constants/ethereum-goerli-chain-id constants/mainnet-network-name
Expand Down Expand Up @@ -92,3 +94,13 @@
{:network %
:testnet-enabled? testnet-enabled?
:goerli-enabled? goerli-enabled?})))))

(defn network-ids->formatted-text
[network-ids]
(let [network-names (->> network-ids
(map id->network)
(map name)
(map string/capitalize)
(string/join ", "))
formatted-text (string/replace network-names last-comma-followed-by-text-to-end-regex " and ")]
formatted-text))
18 changes: 18 additions & 0 deletions src/status_im/contexts/wallet/common/utils/networks_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,21 @@
constants/arbitrum-mainnet-chain-id))
(is (= (utils/network->chain-id {:network :arbitrum :testnet-enabled? true :goerli-enabled? false})
constants/arbitrum-sepolia-chain-id))))

(deftest test-network-ids->formatted-text
(testing "Empty network-ids should return an empty string"
(is (= "" (utils/network-ids->formatted-text []))))

(testing "Single network-id should return the capitalized name of that network"
(is (= "Mainnet" (utils/network-ids->formatted-text [constants/ethereum-mainnet-chain-id]))))

(testing "Two network-ids should return a comma-separated string with 'and' for the last item"
(is (= "Mainnet and Optimism"
(utils/network-ids->formatted-text [constants/ethereum-mainnet-chain-id
constants/optimism-mainnet-chain-id]))))

(testing "Multiple network-ids should return a comma-separated string with 'and' for the last item"
(is (= "Mainnet, Optimism and Arbitrum"
(utils/network-ids->formatted-text [constants/ethereum-mainnet-chain-id
constants/optimism-mainnet-chain-id
constants/arbitrum-mainnet-chain-id])))))
20 changes: 18 additions & 2 deletions src/status_im/contexts/wallet/send/input_amount/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
[status-im.contexts.wallet.send.input-amount.style :as style]
[status-im.contexts.wallet.send.routes.view :as routes]
[status-im.contexts.wallet.send.utils :as send-utils]
[status-im.contexts.wallet.sheets.unpreferred-networks-alert.view :as unpreferred-networks-alert]
[utils.address :as address]
[utils.i18n :as i18n]
[utils.money :as money]
Expand Down Expand Up @@ -100,6 +101,13 @@
:on-press add-token-networks}
(i18n/label :t/add-networks-token-can-be-sent-to {:token-symbol token-symbol})]]]))

(defn- show-unpreferred-networks-alert
[on-confirm]
(rf/dispatch
[:show-bottom-sheet
{:content (fn []
[unpreferred-networks-alert/view
{:on-confirm on-confirm}])}]))
(defn view
;; crypto-decimals, limit-crypto and initial-crypto-currency? args are needed
;; for component tests only
Expand Down Expand Up @@ -214,7 +222,11 @@
(not (nil? routes))
(not loading-routes?)
(not token-not-supported-in-receiver-networks?))
receiver-networks (rf/sub [:wallet/wallet-send-receiver-networks])]
receiver-networks (rf/sub [:wallet/wallet-send-receiver-networks])
receiver-preferred-networks (rf/sub
[:wallet/wallet-send-receiver-preferred-networks])
sending-to-unpreferred-networks? (not= (set receiver-networks)
(set receiver-preferred-networks))]
(rn/use-mount
(fn []
(let [dismiss-keyboard-fn #(when (= % "active") (rn/dismiss-keyboard!))
Expand Down Expand Up @@ -277,10 +289,14 @@
button-one-label)
:button-one-props (merge button-one-props
{:disabled? (and (not no-routes-found?) confirm-disabled?)
:on-press (if no-routes-found?
:on-press (cond
no-routes-found?
#(rf/dispatch [:wallet/get-suggested-routes
{:amount (controlled-input/input-value
input-state)}])
sending-to-unpreferred-networks?
#(show-unpreferred-networks-alert on-confirm)
:else
on-confirm)}
(when no-routes-found?
{:type :grey}))}]
Expand Down
83 changes: 3 additions & 80 deletions src/status_im/contexts/wallet/send/routes/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,6 @@
(def network-link-1x-height 56)
(def network-link-2x-height 111)

(defn- make-network-item
[{:keys [network-name chain-id] :as _network}
{:keys [title color on-change network-preferences] :as _options}]
{:title (or title (string/capitalize (name network-name)))
:image :icon-avatar
:image-props {:icon (resources/get-network network-name)
:size :size-20}
:action :selector
:action-props {:type :checkbox
:customization-color color
:checked? (some #(= % chain-id) @network-preferences)
:on-change on-change}})

(defn fetch-routes
[amount valid-input? bounce-duration-ms]
(if valid-input?
Expand All @@ -41,63 +28,6 @@
bounce-duration-ms)
(rf/dispatch [:wallet/clean-suggested-routes])))

(defn networks-drawer
[{:keys [on-save theme]}]
(let [network-details (rf/sub [:wallet/network-details])
{:keys [color]} (rf/sub [:wallet/current-viewing-account])
selected-networks (rf/sub [:wallet/wallet-send-receiver-networks])
prefix (rf/sub [:wallet/wallet-send-address-prefix])
prefix-seq (string/split prefix #":")
grouped-details (group-by #(contains? (set prefix-seq) (:short-name %)) network-details)
preferred (get grouped-details true [])
not-preferred (get grouped-details false [])
network-preferences (reagent/atom selected-networks)
toggle-network (fn [{:keys [chain-id]}]
(swap! network-preferences
(fn [preferences]
(if (some #(= % chain-id) preferences)
(vec (remove #(= % chain-id) preferences))
(conj preferences chain-id)))))]
(fn []
[rn/view
[quo/drawer-top {:title (i18n/label :t/edit-receiver-networks)}]
[quo/category
{:list-type :settings
:label (i18n/label :t/preferred-by-receiver)
:data (mapv (fn [network]
(make-network-item network
{:color color
:network-preferences network-preferences
:on-change #(toggle-network network)}))
preferred)}]
(when (pos? (count not-preferred))
[quo/category
{:list-type :settings
:label (i18n/label :t/not-preferred-by-receiver)
:data (mapv (fn [network]
(make-network-item network
{:color color
:network-preferences network-preferences
:on-change #(toggle-network network)}))
not-preferred)}])
(when (not= selected-networks @network-preferences)
[rn/view {:style (style/warning-container color theme)}
[quo/icon :i/info {:color (colors/resolve-color color theme)}]
[quo/text
{:size :paragraph-2
:style style/warning-text} (i18n/label :t/receiver-networks-warning)]])
[quo/bottom-actions
{:actions :one-action
:button-one-label (i18n/label :t/apply-changes)
:button-one-props {:disabled? (or (= selected-networks @network-preferences)
(empty? @network-preferences))
:on-press (fn []
(rf/dispatch [:wallet/update-receiver-networks
@network-preferences])
(rf/dispatch [:hide-bottom-sheet])
(on-save))
:customization-color color}}]])))

(defn- open-preferences
[]
(let [receiver-networks (rf/sub [:wallet/wallet-send-receiver-networks])
Expand All @@ -113,15 +43,8 @@
(i18n/label
:t/token-not-available-on-networks
{:token-symbol token-symbol
:networks (->> not-available-tokens
(map network-utils/id->network)
(map name)
(map string/capitalize)
(string/join ", ")
(fn [s]
(string/replace s
#",\s([^,]+)$"
" and $1")))}))]
:networks (network-utils/network-ids->formatted-text
not-available-tokens)}))]
(rf/dispatch
[:show-bottom-sheet
{:content (fn []
Expand All @@ -131,7 +54,7 @@
:second-section-label (i18n/label :t/not-preferred-by-receiver)
:selected-networks (set (map network-utils/id->network receiver-networks))
:receiver-preferred-networks receiver-preferred-networks
:button-label (i18n/label :t/apply)
:button-label (i18n/label :t/apply-changes)
:warning-label warning-label
:on-save
(fn [chain-ids]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
{:margin-horizontal 20
:margin-vertical 8})

(def warning-container
{:flex-direction :row
:margin-horizontal 20
:align-items :center})

(defn sending-to-unpreferred-networks-alert-container
[theme]
{:height 76
Expand All @@ -24,10 +29,7 @@
:margin-horizontal 20
:padding 10})

(def sending-to-unpreferred-networks-content-container
{:margin-left 8
:align-items :flex-start})

(def sending-to-unpreferred-networks-text
{:flex 1
:height 54.6
:margin-left 8})
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@
first-section-networks)}]
(when warning-label
[rn/view
{:style {:flex-direction :row
:margin-horizontal 20}}
{:style style/warning-container}
[quo/icon :i/alert
{:size 16
:color colors/danger-50}]
Expand All @@ -136,9 +135,9 @@
[rn/view {:style (style/sending-to-unpreferred-networks-alert-container theme)}
[rn/view
[quo/icon :i/alert
{:size 16
:color (colors/resolve-color :blue theme)
:style {:margin-top 2}}]]
{:size 16
:color (colors/resolve-color :blue theme)
:container-style {:margin-top 2}}]]
[quo/text
{:style style/sending-to-unpreferred-networks-text
:size :paragraph-2}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(ns status-im.contexts.wallet.sheets.unpreferred-networks-alert.style)

(def sending-to-unpreferred-networks-title
{:margin-horizontal 20})

(def sending-to-unpreferred-networks-text
{:flex 1
:height 66
:margin-horizontal 20
:margin-vertical 4})
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
(ns status-im.contexts.wallet.sheets.unpreferred-networks-alert.view
(:require [quo.core :as quo]
[re-frame.core :as rf]
[status-im.contexts.wallet.sheets.unpreferred-networks-alert.style :as style]
[utils.i18n :as i18n]))

(defn view
[{:keys [on-confirm]}]
[:<>
[quo/text
{:style style/sending-to-unpreferred-networks-title
:size :heading-2
:weight :semi-bold}
(i18n/label :t/sending-to-unpreferred-networks)]
[quo/text
{:style style/sending-to-unpreferred-networks-text
:size :paragraph-1}
(i18n/label :t/sending-to-networks-the-receiver-does-not-prefer)]
[quo/bottom-actions
{:actions :two-actions
:button-two-label (i18n/label :t/cancel)
:button-two-props {:on-press #(rf/dispatch [:hide-bottom-sheet])
:type :grey}
:button-one-label (i18n/label :t/proceed-anyway)
:button-one-props {:on-press (fn []
(rf/dispatch [:hide-bottom-sheet])
(when on-confirm (on-confirm)))
:customization-color :danger}}]])
4 changes: 3 additions & 1 deletion translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2617,5 +2617,7 @@
"add-networks-token-can-be-sent-to": "Add networks {{token-symbol}} can be sent to",
"not-available": "Not available",
"token-not-available-on-networks": "{{token-symbol}} is not available on {{networks}}.",
"sending-to-networks-the-receiver-does-not-prefer": "Sending to networks the receiver does not prefer may result in recipient having difficulty accessing the sent tokens."
"sending-to-networks-the-receiver-does-not-prefer": "Sending to networks the receiver does not prefer may result in recipient having difficulty accessing the sent tokens.",
"proceed-anyway": "Proceed anyway",
"sending-to-unpreferred-networks": "Sending to unpreferred networks"
}

0 comments on commit 505e486

Please sign in to comment.