🚧 More work on data and forms validation.

This commit is contained in:
Andrey Antukh 2019-09-09 12:34:31 +02:00
parent 2477b289e2
commit a009961a58
15 changed files with 464 additions and 314 deletions

View file

@ -8,20 +8,22 @@
(ns uxbox.main.ui.auth.login
(:require
[rumext.alpha :as mf]
[struct.alpha :as s]
[cljs.spec.alpha :as s]
[uxbox.builtins.icons :as i]
[uxbox.config :as cfg]
[uxbox.main.data.auth :as da]
[uxbox.main.store :as st]
[uxbox.main.ui.messages :refer [messages-widget]]
[uxbox.util.dom :as dom]
[uxbox.util.forms :as fm]
[uxbox.util.forms2 :as fm2]
[uxbox.util.i18n :refer [tr]]
[uxbox.util.router :as rt]))
(s/defs ::login-form
(s/dict :username (s/&& ::s/string ::fm/not-empty-string)
:password (s/&& ::s/string ::fm/not-empty-string)))
(s/def ::username ::fm2/not-empty-string)
(s/def ::password ::fm2/not-empty-string)
(s/def ::login-form
(s/keys :req-un [::username ::password]))
(defn- on-submit
[event form]
@ -42,7 +44,8 @@
(mf/defc login-form
[]
(let [{:keys [data] :as form} (fm/use-form ::login-form {})]
(let [{:keys [data] :as form} (fm2/use-form ::login-form {})]
(prn "login-form" form)
[:form {:on-submit #(on-submit % form)}
[:div.login-content
(when cfg/isdemo
@ -52,16 +55,18 @@
{:name "username"
:tab-index "2"
:value (:username data "")
:on-blur (fm/on-input-blur form :username)
:on-change (fm/on-input-change form :username)
:class (fm2/error-class form :username)
:on-blur (fm2/on-input-blur form :username)
:on-change (fm2/on-input-change form :username)
:placeholder (tr "auth.email-or-username")
:type "text"}]
[:input.input-text
{:name "password"
:tab-index "3"
:value (:password data "")
:on-blur (fm/on-input-blur form :password)
:on-change (fm/on-input-change form :password)
:class (fm2/error-class form :password)
:on-blur (fm2/on-input-blur form :password)
:on-change (fm2/on-input-change form :password)
:placeholder (tr "auth.password")
:type "password"}]
[:input.btn-primary

View file

@ -53,7 +53,6 @@
(mf/defc register-form
[props]
(let [{:keys [data] :as form} (fm/use-form ::register-form {})]
(prn "register-form" form)
[:form {:on-submit #(on-submit % form)}
[:div.login-content
[:input.input-text

View file

@ -8,38 +8,40 @@
(ns uxbox.main.ui.settings.password
(:require
[rumext.alpha :as mf]
[struct.alpha :as s]
[cljs.spec.alpha :as s]
[uxbox.builtins.icons :as i]
[uxbox.main.data.users :as udu]
[uxbox.main.store :as st]
[uxbox.util.dom :as dom]
[uxbox.util.forms :as fm]
[uxbox.util.forms2 :as fm]
[uxbox.util.i18n :refer [tr]]
[uxbox.util.messages :as um]))
(defn- on-error
[form error]
(case (:code error)
:uxbox.services.users/old-password-not-match
(swap! form assoc-in [:errors :password-old]
{:type ::api :message "settings.password.wrong-old-password"})
:else (throw (ex-info "unexpected" {:error error}))))
(defn- on-submit
[event form]
(letfn [(on-error [error]
(case (:code error)
:uxbox.services.users/old-password-not-match
(swap! form assoc-in [:errors :password-old]
{:type ::api :message "settings.password.wrong-old-password"})
(dom/prevent-default event)
(let [data (:clean-data form)
opts {:on-success #(st/emit! (um/info (tr "settings.password.password-saved")))
:on-error #(on-error form %)}]
(st/emit! (udu/update-password data opts))))
:else (throw (ex-info "unexpected" {:error error}))))
(s/def ::password-1 ::fm/not-empty-string)
(s/def ::password-2 ::fm/not-empty-string)
(s/def ::password-old ::fm/not-empty-string)
(on-success [_]
(st/emit! (um/info (tr "settings.password.password-saved"))))]
(dom/prevent-default event)
(let [data (:clean-data form)
opts {:on-success on-success
:on-error on-error}]
(st/emit! (udu/update-password data opts)))))
(s/defs ::password-form
(s/dict :password-1 (s/&& ::s/string ::fm/not-empty-string)
:password-2 (s/&& ::s/string ::fm/not-empty-string)
:password-old (s/&& ::s/string ::fm/not-empty-string)))
(s/def ::password-form
(s/keys :req-un [::password-1
::password-2
::password-old]))
(mf/defc password-form
[props]
@ -54,7 +56,8 @@
:on-blur (fm/on-input-blur form :password-old)
:on-change (fm/on-input-change form :password-old)
:placeholder (tr "settings.password.old-password")}]
[:& fm/field-error {:form form :field :password-old}]
[:& fm/field-error {:form form :field :password-old :type ::api}]
[:input.input-text
{:type "password"
@ -64,7 +67,7 @@
:on-blur (fm/on-input-blur form :password-1)
:on-change (fm/on-input-change form :password-1)
:placeholder (tr "settings.password.new-password")}]
[:& fm/field-error {:form form :field :password-1}]
;; [:& fm/field-error {:form form :field :password-1}]
[:input.input-text
{:type "password"
@ -74,7 +77,7 @@
:on-blur (fm/on-input-blur form :password-2)
:on-change (fm/on-input-change form :password-2)
:placeholder (tr "settings.password.confirm-password")}]
[:& fm/field-error {:form form :field :password-2}]
;; [:& fm/field-error {:form form :field :password-2}]
[:input.btn-primary
{:type "submit"

View file

@ -7,16 +7,16 @@
(ns uxbox.main.ui.settings.profile
(:require
[cljs.spec.alpha :as s]
[cuerdas.core :as str]
[lentes.core :as l]
[rumext.alpha :as mf]
[struct.alpha :as s]
[uxbox.builtins.icons :as i]
[uxbox.main.data.users :as udu]
[uxbox.main.store :as st]
[uxbox.util.data :refer [read-string]]
[uxbox.util.dom :as dom]
[uxbox.util.forms :as fm]
[uxbox.util.forms2 :as fm]
[uxbox.util.i18n :as i18n :refer [tr]]
[uxbox.util.interop :refer [iterable->seq]]
[uxbox.util.messages :as um]))
@ -32,11 +32,16 @@
(-> (l/key :profile)
(l/derive st/state)))
(s/defs ::profile-form
(s/dict :fullname (s/&& ::s/string ::fm/not-empty-string)
:username (s/&& ::s/string ::fm/not-empty-string)
:language (s/&& ::s/string ::fm/not-empty-string)
:email ::s/email))
(s/def ::fullname ::fm/not-empty-string)
(s/def ::username ::fm/not-empty-string)
(s/def ::language ::fm/not-empty-string)
(s/def ::email ::fm/email)
(s/def ::profile-form
(s/keys :req-un [::fullname
::username
::language
::email]))
(defn- on-error
[error form]
@ -58,7 +63,6 @@
(defn- on-submit
[event form]
(prn "on-submit" form)
(dom/prevent-default event)
(let [data (:clean-data form)
on-success #(st/emit! (um/info (tr "settings.profile.profile-saved")))

View file

@ -41,14 +41,12 @@
(watch [_ state stream]
(let [pid (get-in state [:workspace :current])
selected (get-in state [:workspace pid :selected])]
(prn "start-move-selected" selected)
(rx/from-coll (map start-move selected))))))
(defn on-mouse-down
[event {:keys [id type] :as shape} selected]
(let [selected? (contains? selected id)
drawing? @refs/selected-drawing-tool]
(prn "on-mouse-down" id type selected? (= type :canvas))
(when-not (:blocked shape)
(cond
drawing?

View file

@ -29,7 +29,6 @@
(common/on-mouse-down event shape selected))
(on-double-click [event]
(when selected?
(prn "path-component$on-double-click")
(st/emit! (dw/start-edition-mode (:id shape)))))]
[:g.shape {:class (when selected? "selected")
:on-double-click on-double-click