Skip to content

Commit

Permalink
show and edit tags on member profile
Browse files Browse the repository at this point in the history
  • Loading branch information
mhuebert committed Jan 10, 2024
1 parent bc8685c commit 8fc6c4b
Show file tree
Hide file tree
Showing 20 changed files with 285 additions and 191 deletions.
2 changes: 1 addition & 1 deletion deps.edn
Expand Up @@ -5,7 +5,7 @@

;; v2
datalevin/datalevin {:mvn/version "0.8.21"}
io.github.mhuebert/re-db {:git/sha "d9d0196403e346261e3007ab0f4d5e32e8a8e7a3"}
io.github.mhuebert/re-db {:git/sha "631618c03eaf3f89422497493705114a386e2b4d"}
io.github.mhuebert/yawn {:git/sha "68285f6c132f26a2ff3cc2f7dffc3fe68c9856d9"}
io.github.mhuebert/inside-out {:git/sha "fe175565b5840455e9ca6cd410b6c84a554c3003"}

Expand Down
1 change: 1 addition & 0 deletions src/sb/app.cljc
Expand Up @@ -34,6 +34,7 @@
:boolean {:view field.ui/checkbox-field}
:project/badges {:view field.ui/badges-field}
:prose/as-map {:view field.ui/prose-field}
:entity/tags {:view field.ui/tags-field}
:account/email {:props {:type "email"
:placeholder (t :tr/email)}
:validators [form.ui/email-validator]}
Expand Down
12 changes: 4 additions & 8 deletions src/sb/app/board/data.cljc
Expand Up @@ -101,16 +101,12 @@
;; create membership
)

#?(:clj
(defn membership-id [entity-id account-id]
(dl/entity [:member/entity+account [(dl/resolve-id entity-id)
(dl/resolve-id account-id)]])))

(q/defquery show
{:prepare [(member.data/member:log-visit! :board-id)
(az/with-roles :board-id)]}
[{:keys [board-id member/roles]}]
(if-let [board (db/pull `[~@entity.data/entity-keys
:entity/member-tags
:entity/member-fields
:entity/project-fields
:board/registration-open?
Expand All @@ -124,12 +120,12 @@
:entity/kind
{:image/avatar [:entity/id]}
:account/display-name]}
{:member/tags [:entity/id
{:entity/tags [:entity/id
:tag/label
:tag/color]}
:entity/field-entries
{:member/entity [:entity/id]}
{:member/custom-tags [:tag/label]}
{:entity/custom-tags [:tag/label]}
:member/roles])

(q/defquery members
Expand Down Expand Up @@ -181,7 +177,7 @@
{:prepare [az/with-account-id!
(az/with-roles :board-id)
(fn [_ {:as params :keys [board-id account-id]}]
(validate/assert-can-edit! account-id board-id)
(validate/assert-can-edit! account-id (dl/entity board-id))
params)]}
[{:keys [board-id member/roles]}]
(some->
Expand Down
6 changes: 4 additions & 2 deletions src/sb/app/entity/data.cljc
Expand Up @@ -3,6 +3,7 @@
[re-db.api :as db]
[sb.authorize :as az]
[sb.query :as q]
[sb.server.datalevin :as dl]
[sb.schema :as sch :refer [? s- unique-uuid]]
[sb.validate :as validate]
[inside-out.forms :as io]))
Expand Down Expand Up @@ -122,11 +123,12 @@
{:prepare [az/with-account-id!]}
[{:keys [account-id]} e m]
(let [e (sch/wrap-id e)
_ (validate/assert-can-edit! account-id e)
entity (dl/entity e)
_ (validate/assert-can-edit! account-id entity)
txs (-> (assoc m :db/id e)
retract-nils)]

(let [parent-schema (-> (keyword (name (:entity/kind (db/entity e))) "as-map")
(let [parent-schema (-> (keyword (name (:entity/kind entity)) "as-map")
(@sch/!malli-registry))
without-nils (ignore-optional-nils parent-schema m)]
(validate/assert without-nils (mu/select-keys parent-schema (keys without-nils))))
Expand Down
9 changes: 5 additions & 4 deletions src/sb/app/field/data.cljc
Expand Up @@ -204,9 +204,10 @@
(q/defx add-field
{:prepare [az/with-account-id!]}
[{:keys [account-id]} e a new-field]
(validate/assert-can-edit! account-id e)
(let [e (sch/wrap-id e)
existing-fields (a (db/entity e))
entity (dl/entity e)
_ (validate/assert-can-edit! account-id entity)
existing-fields (a entity)
field (assoc new-field :field/id (dl/new-uuid :field))]
(validate/assert field :field/as-map)
(db/transact! [[:db/add e a (conj existing-fields field)]])
Expand All @@ -215,8 +216,8 @@
(q/defx remove-field
{:prepare [az/with-account-id!]}
[{:keys [account-id]} parent-id a field-id]
(validate/assert-can-edit! account-id parent-id)
(let [parent (db/entity (sch/wrap-id parent-id))]
(validate/assert-can-edit! account-id parent)
(db/transact! [[:db/add (:db/id parent) a (->> (get parent a)
(remove (comp #{field-id} :field/id))
vec)]])
Expand Down Expand Up @@ -248,9 +249,9 @@
:prose/format (:prose/format entry)}))

(q/defx save-entry! [{:keys [account-id]} parent-id field-id entry]
(validate/assert-can-edit! account-id parent-id)
(let [field (db/entity (sch/wrap-id field-id))
parent (db/entity (sch/wrap-id parent-id))
_ (validate/assert-can-edit! account-id parent)
entries (assoc (get parent :entity/field-entries) field-id entry)]
(validate/assert (db/touch field) :field/as-map)
(validate/assert entry :field-entry/as-map)
Expand Down
63 changes: 58 additions & 5 deletions src/sb/app/field/ui.cljc
Expand Up @@ -21,7 +21,8 @@
[yawn.view :as v]
[sb.color :as color]
[sb.i18n :refer [t]]
[promesa.core :as p]))
[promesa.core :as p]
[re-db.api :as db]))

#?(:cljs
(defn parse-video-url [url]
Expand Down Expand Up @@ -90,7 +91,7 @@
[:div.h-5.w-5.inline-flex.items-center.justify-center.absolute
[ui/loading:spinner "h-3 w-3"]])
[:input.h-5.w-5.rounded.border-gray-300.text-primary
(form.ui/pass-props props)]
(u/dissoc-qualified props)]
[:div.flex-v.gap-1.ml-2
(when-let [label (form.ui/get-label ?field (:field/label props))]
[:div.flex.items-center.h-5 label])
Expand Down Expand Up @@ -174,7 +175,7 @@
(form.ui/show-label ?field (:field/label props) (:label classes))
[:div.flex-v.relative
(with-messages-popover ?field
[auto-size (-> (form.ui/pass-props props)
[auto-size (-> (u/dissoc-qualified props)
(v/merge-props
data-props
{:disabled (not can-edit?)
Expand Down Expand Up @@ -302,7 +303,7 @@
:position "absolute"}} props)
(assoc :type "color")
(update :value #(or % "#ffffff"))
(form.ui/pass-props))])
(u/dissoc-qualified))])

(ui/defview plural-item-form [{:as props
:keys [?items
Expand Down Expand Up @@ -670,4 +671,56 @@
(doall (for [?entry (seq ?entries)
:when (or can-edit?
(data/entry-value @?entry))]
(show-entry-field ?entry props))))
(show-entry-field ?entry props))))

(ui/defview tags-field
{:make-?field (fn [init _props]
(io/field :many {:tag/id ?id}
:init init))}
[?tags {:as props :keys [field/can-edit?]}]
(let [all-tags (-> (io/closest ?tags :db/id)
db/entity
:member/entity
:entity/member-tags)
by-id (reduce #(assoc %1 (:tag/id %2) %2) {} all-tags)
selected (into #{} (map :tag/id) @?tags)
[editing? edit!] (h/use-state (empty? selected))
to-add (seq (remove (comp selected :tag/id) all-tags))]
[:div.flex-v.gap-1
[:div.flex.flex-wrap.gap-2
(doall (for [{:as ?tag :syms [?id]} ?tags
:let [{:tag/keys [id label color]} (by-id @?id)]]
[:div.tag-md.cursor-default.gap-1.group
{:key id
:style (color/color-pair color)
:on-click (when editing?
#(do (io/remove-many! ?tag)
(entity.data/maybe-save-field ?tags)))}
label
(when editing?
[icons/x-mark "w-4 h-4 -mr-1 opacity-50 group-hover:opacity-100"])]))]
(when to-add
[:div.flex-v.gap-2
(when-not editing?
[:div.tag-md.px-1.hover:bg-gray-100.text-gray-400.hover:text-gray-700.cursor-default {:on-click #(edit! not)}
(t :tr/edit-tags)])
(when editing?
[:div.bg-gray-100.rounded-lg.border-gray-400.p-3.flex.items-center.gap-2
[:div.flex.flex-wrap.gap-2
(for [{:tag/keys [id label color]} to-add]
[:div.tag-md.cursor-default.group
{:key id
:style (color/color-pair color)
:on-click #(do (io/add-many! ?tags {:tag/id id})
(entity.data/maybe-save-field ?tags))}
label
[icons/plus-thick "w-4 h-4 -mr-1 opacity-50 group-hover:opacity-100"]])]
[:div.hover:bg-gray-200.p-2.rounded {:on-click #(edit! not)} [icons/checkmark "flex-none"]]])
[form.ui/show-field-messages ?tags]])]
)
;; pass in a list of tags (from parent) to show. group-by (set @?tags).
;; show each tag,
;; if can-edit,
;; - show an "x" for removing each tag,
;; - show unused tags, click-to-add
)
29 changes: 19 additions & 10 deletions src/sb/app/form/ui.cljc
Expand Up @@ -3,11 +3,14 @@
[inside-out.forms]
[inside-out.forms :as io]
[sb.app.entity.data :as entity.data]
[sb.app.views.radix :as radix]
[sb.app.views.ui :as ui]
[sb.authorize :as az]
[sb.color :as color]
[sb.i18n :refer [t]]
[sb.icons :as icons]
[sb.util :as u]
[yawn.hooks :as h]
[yawn.view :as v]))


Expand All @@ -17,15 +20,6 @@
:clj
(str "field-" (:sym ?field))))


(defn pass-props [props]
(reduce (fn [m k]
(cond-> m
(qualified-keyword? k)
(dissoc k)))
props
(keys props)))

(defn attribute-label [a]
(sb.i18n/tr* (keyword "tr" (name a))))

Expand Down Expand Up @@ -88,4 +82,19 @@
[:button.btn.btn-primary
{:type "submit"
:disabled (not (io/submittable? ?form))}
label]])
label]])

#?(:cljs
(defn use-dev-panel [entity role-map default]
(let [!roles (h/use-state default)
roles (role-map @!roles)]
[(az/editor-role? roles)
roles
(when (ui/dev?)
[radix/select-menu {:value @!roles
:on-value-change #(reset! !roles %)
:field/classes {:trigger "flex items-center px-2 icon-gray text-sm self-start focus-visible:ring-0"
:content (str radix/menu-content-classes " text-sm")}
:field/can-edit? true
:field/options (->> (keys role-map)
(mapv (fn [k] {:value k :text k})))}])])))
19 changes: 12 additions & 7 deletions src/sb/app/member/data.cljc
Expand Up @@ -28,8 +28,8 @@
:member/entity (sch/ref :one)
:member/account (sch/ref :one)

:member/tags (sch/ref :many :tag/as-map)
:member/custom-tags {s- [:sequential [:map {:closed true} :tag/label]]}
:entity/tags {s- [:sequential [:map {:closed true} :tag/id]]}
:entity/custom-tags {s- [:sequential [:map {:closed true} :tag/label]]}

;; TODO: move/remove. this should simply be a field that an organizer adds.
:member/newsletter-subscription? {s- :boolean},
Expand Down Expand Up @@ -59,9 +59,9 @@

(? :member/inactive?)
(? :member/email-frequency)
(? :member/custom-tags)
(? :entity/custom-tags)
(? :member/newsletter-subscription?)
(? :member/tags)
(? :entity/tags)
(? :member/roles)
(? :member/last-visited)

Expand All @@ -76,11 +76,16 @@
(q/defquery show
{:prepare az/require-account!}
[params]
(dissoc (q/pull `[{:member/tags [:*]}
(dissoc (q/pull `[~@entity.data/entity-keys
:entity/tags
:member/field-entries
{:member/entity [:entity/id
:entity/kind
:entity/member-tags
:entity/member-fields]}
{:member/account [~@entity.data/entity-keys
:account/display-name]}]
(:member-id params))
:member/password))
(:member-id params))))

#?(:clj
(defn membership-id [account-id entity-id]
Expand Down

0 comments on commit 8fc6c4b

Please sign in to comment.