standardized users.

This commit is contained in:
Bryce Covert
2020-08-04 08:19:00 -07:00
parent e49200e6dc
commit c7fd0aafc0
2 changed files with 67 additions and 60 deletions

View File

@@ -169,7 +169,8 @@
(.preventDefault e)) (.preventDefault e))
(when can-submit (when can-submit
(re-frame/dispatch-sync (vec (conj submit-event params)))))} (re-frame/dispatch-sync (vec (conj submit-event params)))))}
[:h1.title.is-2 title] (when title
[:h1.title.is-2 title])
children])) children]))
:raw-field (fn [control] :raw-field (fn [control]
(let [{:keys [data]} @(re-frame/subscribe [::form id])] (let [{:keys [data]} @(re-frame/subscribe [::form id])]

View File

@@ -8,56 +8,58 @@
[auto-ap.views.components.address :refer [address-field]] [auto-ap.views.components.address :refer [address-field]]
[auto-ap.views.components.admin.side-bar :refer [admin-side-bar]] [auto-ap.views.components.admin.side-bar :refer [admin-side-bar]]
[auto-ap.views.components.layouts :refer [side-bar-layout]] [auto-ap.views.components.layouts :refer [side-bar-layout]]
[auto-ap.views.utils :refer [login-url dispatch-value-change bind-field horizontal-field dispatch-event]] [auto-ap.views.utils :refer [login-url dispatch-value-change bind-field horizontal-field dispatch-event with-user]]
[auto-ap.views.components.modal :refer [modal action-modal]] [auto-ap.views.components.modal :refer [modal action-modal]]
[auto-ap.views.components.grid :as grid] [auto-ap.views.components.grid :as grid]
[auto-ap.utils :refer [by replace-if]] [auto-ap.utils :refer [by replace-if]]
[cljs.reader :as edn] [cljs.reader :as edn]
[auto-ap.routes :as routes] [auto-ap.routes :as routes]
[bidi.bidi :as bidi] [bidi.bidi :as bidi]
[auto-ap.status :as status])) [auto-ap.status :as status]
[auto-ap.forms :as forms]))
(re-frame/reg-sub (re-frame/reg-sub
::editing ::can-submit
(fn [db] (fn [db]
(-> db ::editing))) true))
(re-frame/reg-event-db (re-frame/reg-event-db
::change ::changed
(fn [db [_ path value]] (forms/change-handler ::form
(assoc-in db (concat [::editing] path) value))) (fn [data field value]
[])))
(re-frame/reg-event-db (re-frame/reg-event-db
::editing ::editing
(fn [db [_ d]] (fn [db [_ d]]
(re-frame/dispatch [::events/modal-status ::edit-user {:visible? true}]) (re-frame/dispatch [::events/modal-status ::edit-user {:visible? true}])
(assoc-in db [::editing] d))) (-> db
(forms/start-form ::form d))))
(re-frame/reg-event-db (re-frame/reg-event-db
::add-client ::add-client
(fn [db [_ d]] [(forms/in-form ::form)]
(fn [form [_ d]]
(let [client (get @(re-frame/subscribe [::subs/clients-by-id]) (let [client (get @(re-frame/subscribe [::subs/clients-by-id])
(get-in db [::editing :adding-client]))] (get-in form [:data :adding-client]))]
(update-in db [::editing :clients] conj client)))) (update-in form [:data :clients] conj client ))))
(re-frame/reg-event-db (re-frame/reg-event-db
::remove-client ::remove-client
(fn [db [_ d]] [(forms/in-form ::form)]
(update-in db [::editing :clients] #(filter (fn [c] (not= (:id c) d)) %)))) (fn [form [_ d]]
(update-in form [:data :clients] #(filter (fn [c] (not= (:id c) d)) %))))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::save ::saving
(fn [{:keys [db]} [_]] [with-user (forms/triggers-loading ::form) (forms/in-form ::form)]
{:db (-> db (fn [{:keys [db user]} [_]]
(assoc-in [::editing :saving?] true )) {:graphql
:graphql {:token user
{:token (-> db :user)
:query-obj {:venia/operation {:operation/type :mutation :query-obj {:venia/operation {:operation/type :mutation
:operation/name "EditUser"} :operation/name "EditUser"}
:venia/queries [{:query/data [:edit-user :venia/queries [{:query/data [:edit-user
{:edit-user (-> (get db ::editing) {:edit-user (-> (:data db)
(update :clients #(map :id %)) (update :clients #(map :id %))
(select-keys #{:id :name :clients :role}))} (select-keys #{:id :name :clients :role}))}
[:id :name :role [:clients [:id :name]]]]}]} [:id :name :role [:clients [:id :name]]]]}]}
@@ -65,60 +67,64 @@
(re-frame/reg-event-fx (re-frame/reg-event-fx
::saved ::saved
(forms/triggers-stop ::form)
(fn [{:keys [db]} [_ {:keys [edit-user]}]] (fn [{:keys [db]} [_ {:keys [edit-user]}]]
(re-frame/dispatch [::events/modal-completed ::edit-user]))) (re-frame/dispatch [::events/modal-completed ::edit-user])))
(def user-form (forms/vertical-form {:submit-event [::saving]
:change-event [::changed]
:can-submit [::can-submit]
:id ::form}))
(defn form [] (defn form []
(let [editing @(re-frame/subscribe [::editing])] (let [{:keys [data active? error id]} @(re-frame/subscribe [::forms/form ::form])
[action-modal {:title (str "Edit " (:name editing)) {:keys [form-inline field raw-field error-notification submit-button]} user-form]
[action-modal {:title (str "Edit " (:name data))
:id ::edit-user :id ::edit-user
:action-text "Save" :action-text "Save"
:save-event [::save]} :save-event [::saving]
[horizontal-field :status-from ::form}
[:label.label "Name"] (form-inline {}
[bind-field [:<>
[:input.input {:type "text" (field "Name"
:field [:name] [:input.input {:type "text"
:spec ::entity/name :field [:name]
:event ::change :spec ::entity/name}])
:subscription editing}]]]
[horizontal-field
[:label.label "Role"]
[:div.control
[:div.select
[bind-field
[:select {:type "select"
:field [:role]
:spec ::entity/name
:event ::change
:subscription editing}
[:option {:value ":none"} "None"]
[:option {:value ":user"} "User"]
[:option {:value ":manager"} "Manager"]
[:option {:value ":admin"} "Admin"]]]]]]
(when (#{":user" ":manager"} (:role editing)) [:div.field
[horizontal-field [:p.help "Role"]
[:label.label "Clients"]
[:div.control [:div.control
[:div.select
[raw-field
[:select {:type "select"
:field [:role]}
[:option {:value ":none"} "None"]
[:option {:value ":user"} "User"]
[:option {:value ":manager"} "Manager"]
[:option {:value ":admin"} "Admin"]]]]]]])
(when (#{":user" ":manager"} (:role data))
[:div.field
[:p.help "Clients"]
[:div.control
[:div.field.has-addons [:div.field.has-addons
[:p.control [:div.control
[:div.select [:div.select
[bind-field [raw-field
[:select {:type "select" [:select {:type "select"
:field [:adding-client] :field [:adding-client]}
:event ::change
:subscription editing}
[:option] [:option]
(let [used-clients (set (map :id (:clients editing)))] (let [used-clients (set (map :id (:clients data)))]
(for [{:keys [id name]} @(re-frame/subscribe [::subs/clients]) (for [{:keys [id name] :as client} @(re-frame/subscribe [::subs/clients])
:when (not (used-clients id))] :when (not (used-clients id))]
^{:key id} [:option {:value id} name]))]]]] ^{:key id} [:option {:value id} name]))]]]]
[:p.control [:p.control
[:button.button.is-primary {:on-click (dispatch-event [::add-client])} "Add"]]] [:button.button.is-primary {:on-click (dispatch-event [::add-client])} "Add"]]]
[:ul [:ul
(for [{:keys [id name]} (:clients editing)] (for [{:keys [id name]} (:clients data)]
^{:key id} [:li name [:a.icon {:on-click (dispatch-event [::remove-client id])} [:i.fa.fa-times ]]])]]])])) ^{:key id} [:li name [:a.icon {:on-click (dispatch-event [::remove-client id])} [:i.fa.fa-times ]]])]]])]))