|
| 1 | +(ns status-im.contexts.profile.contact.add-nickname.view |
| 2 | + (:require [clojure.string :as string] |
| 3 | + [quo.components.info.info-message :as info-message] |
| 4 | + [quo.core :as quo] |
| 5 | + [react-native.core :as rn] |
| 6 | + [status-im.common.validation.profile :as profile-validator] |
| 7 | + [status-im.constants :as constants] |
| 8 | + [status-im.contexts.profile.contact.add-nickname.style :as style] |
| 9 | + [status-im.contexts.profile.utils :as profile.utils] |
| 10 | + [utils.debounce :as debounce] |
| 11 | + [utils.i18n :as i18n] |
| 12 | + [utils.re-frame :as rf])) |
| 13 | + |
| 14 | +(defn view |
| 15 | + [] |
| 16 | + (let [{:keys [public-key primary-name nickname customization-color] |
| 17 | + :as profile} (rf/sub [:contacts/current-contact]) |
| 18 | + ;; TODO: remove :blue when #18733 merged. |
| 19 | + customization-color (or customization-color :blue) |
| 20 | + full-name (profile.utils/displayed-name profile) |
| 21 | + profile-picture (profile.utils/photo profile) |
| 22 | + [unsaved-nickname set-unsaved-nickname] (rn/use-state nickname) |
| 23 | + [error-msg set-error-msg] (rn/use-state nil) |
| 24 | + validate-nickname (rn/use-callback |
| 25 | + (debounce/debounce |
| 26 | + (fn [name] |
| 27 | + (set-error-msg |
| 28 | + (profile-validator/validation-nickname name))) |
| 29 | + 300)) |
| 30 | + on-cancel (rn/use-callback #(rf/dispatch [:hide-bottom-sheet])) |
| 31 | + on-nickname-change (rn/use-callback (fn [text] |
| 32 | + (set-unsaved-nickname text) |
| 33 | + (validate-nickname text))) |
| 34 | + on-nickname-submit (rn/use-callback |
| 35 | + (fn [] |
| 36 | + (rf/dispatch [:hide-bottom-sheet]) |
| 37 | + (rf/dispatch [:toasts/upsert |
| 38 | + {:id :add-nickname |
| 39 | + :type :positive |
| 40 | + :text (i18n/label |
| 41 | + (if (string/blank? nickname) |
| 42 | + :t/nickname-added |
| 43 | + :t/nickname-updated) |
| 44 | + {:primary-name primary-name})}]) |
| 45 | + (rf/dispatch [:contacts/update-nickname public-key |
| 46 | + (string/trim unsaved-nickname)])) |
| 47 | + [public-key unsaved-nickname])] |
| 48 | + [:<> |
| 49 | + [quo/drawer-top |
| 50 | + {:type :context-tag |
| 51 | + :context-tag-type :default |
| 52 | + :title (i18n/label :t/add-nickname-title) |
| 53 | + :full-name full-name |
| 54 | + :profile-picture profile-picture |
| 55 | + :customization-color customization-color}] |
| 56 | + [rn/view {:style style/input-wrapper} |
| 57 | + [quo/input |
| 58 | + {:type :text |
| 59 | + :char-limit constants/profile-name-max-length |
| 60 | + :max-length constants/profile-name-max-length |
| 61 | + :auto-focus true |
| 62 | + :default-value unsaved-nickname |
| 63 | + :error? (not (string/blank? error-msg)) |
| 64 | + :label (i18n/label :t/nickname) |
| 65 | + :on-change-text on-nickname-change} |
| 66 | + :on-submit-editing on-nickname-submit] |
| 67 | + [info-message/info-message |
| 68 | + {:icon :i/info |
| 69 | + :size :default |
| 70 | + :type (if (not (string/blank? error-msg)) :error :default)} |
| 71 | + (if (not (string/blank? error-msg)) |
| 72 | + error-msg |
| 73 | + (i18n/label :t/nickname-visible-to-you))]] |
| 74 | + [quo/bottom-actions |
| 75 | + {:actions :two-actions |
| 76 | + :button-one-label (i18n/label :t/add-nickname-title) |
| 77 | + :button-one-props {:disabled? (or (string/blank? unsaved-nickname) |
| 78 | + (not (string/blank? error-msg))) |
| 79 | + :on-press on-nickname-submit} |
| 80 | + :button-two-label (i18n/label :t/cancel) |
| 81 | + :button-two-props {:type :grey |
| 82 | + :on-press on-cancel}}]])) |
0 commit comments