Skip to content

Commit

Permalink
persisted-value: always read from ?field
Browse files Browse the repository at this point in the history
  • Loading branch information
mhuebert committed Dec 24, 2023
1 parent b8494b6 commit c8b5f33
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 70 deletions.
22 changes: 11 additions & 11 deletions src/sb/app/board/admin_ui.cljc
@@ -1,7 +1,7 @@
(ns sb.app.board.admin-ui
(:require [inside-out.forms :as io]
[sb.app.board.data :as data]
[sb.app.entity.ui :as entity.ui :refer [use-persisted-field view-field]]
[sb.app.entity.ui :as entity.ui :refer [use-persisted-attr view-field]]
[sb.app.views.header :as header]
[sb.app.views.radix :as radix]
[sb.app.views.ui :as ui]
Expand All @@ -27,24 +27,24 @@
[:div.field-label (t :tr/basic-settings)]
[:div.flex-v.gap-4

(use-persisted-field board :entity/title)
(use-persisted-field board :entity/description)
(use-persisted-field board :entity/domain-name)
(use-persisted-field board :image/avatar {:label (t :tr/image.logo)})]
(use-persisted-attr board :entity/title)
(use-persisted-attr board :entity/description)
(use-persisted-attr board :entity/domain-name)
(use-persisted-attr board :image/avatar {:label (t :tr/image.logo)})]


[:div.field-label (t :tr/projects-and-members)]
[:div.flex-v.gap-4
(use-persisted-field board :board/member-fields #_{:wrap u/prune})
(use-persisted-field board :board/project-fields #_{:wrap u/prune})]
(use-persisted-attr board :board/member-fields #_{:wrap u/prune})
(use-persisted-attr board :board/project-fields #_{:wrap u/prune})]


[:div.field-label (t :tr/registration)]
[:div.flex-v.gap-4
(use-persisted-field board :board/registration-open?)
(use-persisted-field board :board/registration-url-override)
(use-persisted-field board :board/registration-page-message)
(use-persisted-field board :board/invite-email-text)]]
(use-persisted-attr board :board/registration-open?)
(use-persisted-attr board :board/registration-url-override)
(use-persisted-attr board :board/registration-page-message)
(use-persisted-attr board :board/invite-email-text)]]



Expand Down
6 changes: 6 additions & 0 deletions src/sb/app/entity/data.cljc
Expand Up @@ -106,6 +106,12 @@

{:txs txs}))

(defn persisted-value [?field]
(if-let [{:keys [entity attribute wrap]} (:field/persistence ?field)]
(wrap (get entity attribute))
(:init ?field)
#_(throw-no-persistence! ?field)))

(q/defx save-attribute!
{:prepare [az/with-account-id!]}
[ctx e a v]
Expand Down
10 changes: 3 additions & 7 deletions src/sb/app/entity/ui.cljc
Expand Up @@ -33,11 +33,7 @@
(take-while identity)
(map :sym))})))

(defn persisted-value [?field]
(if-let [{:keys [entity attribute wrap]} (:field/persistence ?field)]
(wrap (get entity attribute))
(:init ?field)
#_(throw-no-persistence! ?field)))
(def persisted-value data/persisted-value)

(defn save-field [?field & {:as props}]
(if-let [{:keys [entity attribute wrap]} (io/closest ?field :field/persistence)]
Expand All @@ -56,7 +52,7 @@
:attribute (:attribute ?field)})))
{:keys [entity attribute]} ?field]
[view ?field (merge (:props ?field)
{:persisted-value (get entity attribute)
{:persisted-value (persisted-value ?field)
:on-save (partial save-field ?field props)}
(dissoc props :view))]))

Expand All @@ -68,7 +64,7 @@
(when-some [init (:init m)] (reset! ?field init))
?field)

(defn use-persisted-field [e a & {:as props}]
(defn use-persisted-attr [e a & {:as props}]
#?(:cljs
(let [persisted-value (get e a)
make-field (or (:make-field props)
Expand Down
27 changes: 13 additions & 14 deletions src/sb/app/field/admin_ui.cljc
Expand Up @@ -116,7 +116,11 @@
:color (color/contrasting-text-color @?color)}}]
[:div.relative.w-10.focus-within-ring.rounded.overflow-hidden.self-stretch
[field.ui/color-field ?color {:on-save on-save
:style {:top -10 :left -10 :width 100 :height 100 :position "absolute"}}]]
:style {:top -10
:left -10
:width 100
:height 100
:position "absolute"}}]]
[radix/dropdown-menu {:id :field-option
:trigger [:button.p-1.relative.icon-gray.cursor-default
[icons/ellipsis-horizontal "w-4 h-4"]]
Expand All @@ -129,15 +133,13 @@
(radix/close-alert!)))}))}
(t :tr/remove)]]}]]))

(ui/defview options-editor [?options {:keys [on-save
persisted-value]}]
(ui/defview options-editor [?options {:keys [on-save]}]
[:div.col-span-2.flex-v.gap-3
[:label.field-label (t :tr/options)]
(when (:loading? ?options)
[:div.loading-bar.absolute.h-1.top-0.left-0.right-0])
(into [:div.flex-v]
(map #(show-option % {:on-save on-save
:persisted-value (entity.ui/persisted-value %)}) ?options))
(map #(show-option % {:on-save on-save}) ?options))

(let [?new (h/use-memo #(io/field :init ""))]
[:form.flex.gap-2 {:on-submit (fn [^js e]
Expand Down Expand Up @@ -193,8 +195,7 @@
{:key (fn [{:syms [?id]} _] @?id)}
[?field {:keys [expanded?
toggle-expand!
on-save
persisted-value]}]
on-save]}]
(let [{:syms [?type ?label]} ?field
{:keys [icon]} (data/field-types @?type)
{:keys [drag-handle-props
Expand Down Expand Up @@ -223,8 +224,7 @@
[:div.flex.items-center.group-hover:text-black.text-gray-500.pl-2
[icons/chevron-down:mini (str "w-4" (when expanded? " rotate-180"))]]]
(when expanded?
(field-row-detail ?field {:on-save on-save
:persisted-value persisted-value}))]))
(field-row-detail ?field {:on-save on-save}))]))

(ui/defview fields-editor [{:as ?fields :keys [label]} props]
(let [!new-field (h/use-state nil)
Expand All @@ -251,11 +251,10 @@
(->> ?fields
(map (fn [{:as ?field :syms [?id]}]
(field-row ?field
(merge {:expanded? (= expanded @?id)
:toggle-expand! #(expand! (fn [old]
(u/guard @?id (partial not= old))))
:on-save on-save
:persisted-value (:init ?field)}))))
(merge {:expanded? (= expanded @?id)
:toggle-expand! #(expand! (fn [old]
(u/guard @?id (partial not= old))))
:on-save on-save}))))
doall)]
(when-let [{:as ?new-field
:syms [?type ?label]} @!new-field]
Expand Down
14 changes: 7 additions & 7 deletions src/sb/app/field/ui.cljc
Expand Up @@ -5,6 +5,7 @@
[inside-out.forms :as forms]
[sb.app.asset.data :as asset.data]
[sb.app.asset.ui :as asset.ui]
[sb.app.entity.data :as entity.data]
[sb.app.field.data :as data]
[sb.app.form.ui :as form.ui]
[sb.app.views.radix :as radix]
Expand Down Expand Up @@ -90,7 +91,7 @@
(defn show-postfix [?field props]
(when-let [postfix (or (:postfix props)
(:postfix (meta ?field))
(and (some-> (:persisted-value props)
(and (some-> (entity.data/persisted-value ?field)
(not= (:value props)))
[icons/pencil-outline "w-4 h-4 text-txt/40"]))]
[:div.pointer-events-none.absolute.inset-y-0.right-0.top-0.bottom-0.flex.items-center.p-2 postfix]))
Expand All @@ -105,13 +106,12 @@
wrap
unwrap
wrapper-class
on-save
persisted-value]
on-save]
:or {wrap identity
unwrap identity}} (merge props (:props (meta ?field)))
blur! (fn [e] (j/call-in e [:target :blur]))
cancel! (fn [e]
(reset! ?field persisted-value)
(reset! ?field (entity.data/persisted-value ?field))
(blur! e))
props (v/merge-props props
(form.ui/?field-props ?field
Expand Down Expand Up @@ -233,9 +233,9 @@
(let [get-value (j/get-in [:target :value])]
[:input.default-ring.default-ring-hover.rounded
(-> (v/merge-props
(form.ui/pass-props props)
(form.ui/?field-props ?field get-value (merge props {:save-on-change? true}))
{:type "color"})
(form.ui/pass-props )
(update :value #(or % "#ffffff")))]))

(ui/defview image-field [?field props]
Expand Down Expand Up @@ -306,7 +306,8 @@
{:key (comp :entity/id :field)}
[{:keys [parent field entry can-edit?]}]
(let [value (data/entry-value field entry)
?field (h/use-memo #(forms/field :init (data/entry-value field entry) :label (:field/label field)))
?field (h/use-memo #(forms/field :init (data/entry-value field entry) :label (:field/label field))
[(str value)])
props {:label (:label field)
:can-edit? can-edit?
:on-save (partial data/save-entry! nil (:entity/id parent) (:entity/id field))}]
Expand All @@ -315,7 +316,6 @@
:field.type/select [select-field ?field (merge props
{:wrap (fn [x] {:select/value x})
:unwrap :select/value
:persisted-value value
:options (:field/options field)})]
:field.type/link-list [ui/pprinted value props]
:field.type/image-list [images-field ?field props]
Expand Down
52 changes: 25 additions & 27 deletions src/sb/app/form/ui.cljc
@@ -1,13 +1,14 @@
(ns sb.app.form.ui
(:require [applied-science.js-interop :as j]
[inside-out.forms :as forms]
[sb.app.views.ui :as ui]
[inside-out.forms]
[inside-out.forms :as io]
[yawn.view :as v]
[sb.i18n :refer [t tr*]]
[sb.util :as u]
[sb.app.entity.data :as entity.data]
[sb.app.views.ui :as ui]
[sb.color :as color]
[sb.i18n :refer [t]]
[sb.icons :as icons]
[sb.color :as color]))
[sb.util :as u]
[yawn.view :as v]))


(defn field-id [?field]
Expand All @@ -17,8 +18,8 @@
(str "field-" (:sym ?field))))

(defn maybe-save-field [?field props value]
(if-let [on-save (and (not= value (:persisted-value props))
(:on-save props))]
(if-let [on-save (and (not= value (entity.data/persisted-value ?field))
(:on-save props))]
(do
(prn :saving value)
(reset! ?field value)
Expand All @@ -28,7 +29,7 @@

(defn pass-props [props] (dissoc props
:multi-line :postfix :wrapper-class
:persisted-value :on-save :on-change-value
:on-save :on-change-value
:wrap :unwrap
:inline?
:can-edit?
Expand All @@ -48,26 +49,23 @@
unwrap
on-change-value
on-change
persisted-value
save-on-change?]
:or {wrap identity unwrap identity}}]
(cond-> {:id (field-id ?field)
:value (unwrap @?field)
:on-change (fn [e]
(let [new-value (wrap (get-value e))]
(reset! ?field new-value)
(when on-change-value
(pass-props (on-change-value new-value)))
(when on-change
(on-change e))
(when save-on-change?
(maybe-save-field ?field props new-value))))
:on-blur (fn [e]
(maybe-save-field ?field props (wrap (get-value e)))
((io/blur-handler ?field) e))
:on-focus (io/focus-handler ?field)}
persisted-value
(assoc :persisted-value (unwrap persisted-value))))
{:id (field-id ?field)
:value (unwrap @?field)
:on-change (fn [e]
(let [new-value (wrap (get-value e))]
(reset! ?field new-value)
(when on-change-value
(pass-props (on-change-value new-value)))
(when on-change
(on-change e))
(when save-on-change?
(maybe-save-field ?field props new-value))))
:on-blur (fn [e]
(maybe-save-field ?field props (wrap (get-value e)))
((io/blur-handler ?field) e))
:on-focus (io/focus-handler ?field)})

(def email-validator (fn [v _]
(when v
Expand Down
8 changes: 4 additions & 4 deletions src/sb/app/org/admin_ui.cljc
Expand Up @@ -12,10 +12,10 @@
[:<>
(header/entity org (list (entity.ui/settings-button org)))
[:div {:class form.ui/form-classes}
(entity.ui/use-persisted-field org :entity/title )
(entity.ui/use-persisted-field org :entity/description)
(entity.ui/use-persisted-field org :entity/domain-name)
(entity.ui/use-persisted-attr org :entity/title)
(entity.ui/use-persisted-attr org :entity/description)
(entity.ui/use-persisted-attr org :entity/domain-name)
;; TODO - uploading an image does not work
(entity.ui/use-persisted-field org :image/avatar)
(entity.ui/use-persisted-attr org :image/avatar)

]]))

0 comments on commit c8b5f33

Please sign in to comment.