just some refactoring for action-dialogue.

This commit is contained in:
Bryce Covert
2018-05-22 21:11:10 -07:00
parent 2c42146a3d
commit 6bb4c6e928
7 changed files with 110 additions and 65 deletions

View File

@@ -1,5 +1,5 @@
1) Aligning checks 1) Aligning checks
2) popup blocker -- disable 2) popup blocker -- disable X
3) Splitting accounts 3) Splitting accounts
4) Permissions per location 4) Permissions per location
5) List of payments 5) List of payments

View File

@@ -282,6 +282,7 @@
(:bank_account_id args)))) (:bank_account_id args))))
(defn edit-user [context args value] (defn edit-user [context args value]
(Thread/sleep 1000)
(->graphql (->graphql
(gq-users/edit-user (:edit_user args)))) (gq-users/edit-user (:edit_user args))))

View File

@@ -69,6 +69,16 @@
(println company) (println company)
(assoc db :company (:id company)))) (assoc db :company (:id company))))
(re-frame/reg-event-db
::modal-status
(fn [db [_ id state]]
(update-in db [:modal-state id] #(merge % state))))
(re-frame/reg-event-db
::modal-completed
(fn [db [_ id state]]
(update-in db [:modal-state] #(dissoc % id))))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::set-active-page ::set-active-page
(fn [{:keys [db]} [_ handler]] (fn [{:keys [db]} [_ handler]]

View File

@@ -25,6 +25,12 @@
(fn [db] (fn [db]
(:menu db))) (:menu db)))
(re-frame/reg-sub
::modal-state
(fn [db [_ id]]
(println "ID" id)
(get (:modal-state db) id)))
(re-frame/reg-sub (re-frame/reg-sub
::token ::token
(fn [db] (fn [db]

View File

@@ -1,5 +1,8 @@
(ns auto-ap.views.components.modal (ns auto-ap.views.components.modal
(:require [re-frame.core :as re-frame])) (:require [re-frame.core :as re-frame]
[auto-ap.events :as events]
[auto-ap.subs :as subs]
[auto-ap.views.utils :refer [with-keys]]))
(defn modal [{:keys [title foot hide-event]} & body] (defn modal [{:keys [title foot hide-event]} & body]
[:div.modal.is-active [:div.modal.is-active
@@ -11,8 +14,25 @@
title] title]
[:button.delete {:on-click (fn [] (re-frame/dispatch hide-event))}]] [:button.delete {:on-click (fn [] (re-frame/dispatch hide-event))}]]
[:section.modal-card-body [:section.modal-card-body
body] (with-keys body)]
(when foot (when foot
[:footer.modal-card-foot [:footer.modal-card-foot
foot])]]) foot])]])
(defn action-modal [{:keys [title action-text id save-event]} & rest]
(let [{:keys [visible? saving?]} @(re-frame/subscribe [::subs/modal-state id])]
(when visible?
[modal {:title title
:foot [:a.button.is-primary {:on-click (fn []
(re-frame/dispatch [::events/modal-status id {:saving? true}])
(re-frame/dispatch save-event))
:class (when saving?
"is-loading")}
[:span action-text]]
:id id
:hide-event [::events/modal-status id {:visible? false}]}
(with-keys rest)
(when saving? [:div.is-overlay {:style {"backgroundColor" "rgba(150,150,150, 0.5)"}}])])))

View File

@@ -4,11 +4,11 @@
[reagent.core :as reagent] [reagent.core :as reagent]
[clojure.string :as str] [clojure.string :as str]
[auto-ap.subs :as subs] [auto-ap.subs :as subs]
[auto-ap.events.admin.companies :as events] [auto-ap.events :as events]
[auto-ap.entities.companies :as entity] [auto-ap.entities.companies :as entity]
[auto-ap.views.components.address :refer [address-field]] [auto-ap.views.components.address :refer [address-field]]
[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]]
[auto-ap.views.components.modal :refer [modal]] [auto-ap.views.components.modal :refer [modal action-modal]]
[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]
@@ -49,6 +49,7 @@
(re-frame/reg-event-db (re-frame/reg-event-db
::edit ::edit
(fn [db [_ d]] (fn [db [_ d]]
(re-frame/dispatch [::events/modal-status ::edit-user {:visible? true}])
(if-let [user (get (by :id (::users db)) d)] (if-let [user (get (by :id (::users db)) d)]
(assoc-in db [::editing :user] user) (assoc-in db [::editing :user] user)
(dissoc db ::editing)))) (dissoc db ::editing))))
@@ -84,6 +85,7 @@
(re-frame/reg-event-fx (re-frame/reg-event-fx
::saved ::saved
(fn [{:keys [db]} [_ {:keys [edit-user]}]] (fn [{:keys [db]} [_ {:keys [edit-user]}]]
(re-frame/dispatch [::events/modal-completed ::edit-user])
{:db (-> db {:db (-> db
(assoc-in [::editing :saving?] false ) (assoc-in [::editing :saving?] false )
(dissoc ::editing) (dissoc ::editing)
@@ -107,75 +109,71 @@
[:td name] [:td name]
[:td role] [:td role]
[:td (str/join ", " (map :name companies))]])]])) [:td (str/join ", " (map :name companies))]])]]))
(def admin-users-page (def admin-users-page
(with-meta (with-meta
(fn [] (fn []
[:div [:div
(let [companies (re-frame/subscribe [::users]) (let [companies (re-frame/subscribe [::users])
editing @(re-frame/subscribe [::editing])] editing @(re-frame/subscribe [::editing])]
[:div [:div
[:h1.title "Users"] [:h1.title "Users"]
[users-table] [users-table]
(when editing [action-modal {:title (str "Edit " (:name (:user editing)))
[modal {:title (str "Edit " (:name (:user editing))) :id ::edit-user
:foot [:a.button.is-primary {:on-click (fn [] (re-frame/dispatch [::save])) :action-text "Save"
:class (when (:saving? editing) :save-event [::save]
"is-loading" :editing editing}
)} [horizontal-field
[:span "Save"]] [:label.label "Name"]
:hide-event [::edit nil]} [bind-field
[horizontal-field [:input.input {:type "text"
[:label.label "Name"] :field [:user :name]
[:div.control :spec ::entity/name
[bind-field :event ::change
[:input.input {:type "text" :subscription editing}]]]
:field [:user :name]
:spec ::entity/name
:event ::change
:subscription editing}]]]]
[horizontal-field [horizontal-field
[:label.label "Role"] [:label.label "Role"]
[:div.control [:div.control
[bind-field [bind-field
[:select.select {:type "select" [:select.select {:type "select"
:field [:user :role] :field [:user :role]
:spec ::entity/name :spec ::entity/name
:event ::change
:subscription editing}
[:option {:value "none"} "None"]
[:option {:value "user"} "User"]
[:option {:value "admin"} "Admin"]]]]]
(when (= "user" (:role (:user editing)))
[horizontal-field
[:label.label "Companies"]
[:div.control
[:div.field.has-addons
[:p.control
[:div.select
[bind-field
[:select {:type "select"
:field [:adding-company]
:event ::change :event ::change
:subscription editing} :subscription editing}
[:option] [:option {:value "none"} "None"]
(let [used-companies (set (map :id (:companies (:user editing))))] [:option {:value "user"} "User"]
(for [{:keys [id name]} @(re-frame/subscribe [::subs/companies]) [:option {:value "admin"} "Admin"]]]]]
:when (not (used-companies id))]
^{:key id} [:option {:value id} name]))]]]]
[:p.control
[:button.button.is-primary {:on-click (dispatch-event [::add-company])} "Add"]]]
[:ul
(for [{:keys [id name]} (:companies (:user editing))]
^{:key id} [:li name [:a.icon {:on-click (dispatch-event [::remove-company id])} [:i.fa.fa-times ]]])]]])
(when (:saving? editing) [:div.is-overlay {:style {"backgroundColor" "rgba(150,150,150, 0.5)"}}])])])]) (when (= "user" (:role (:user editing)))
[horizontal-field
[:label.label "Companies"]
[:div.control
[:div.field.has-addons
[:p.control
[:div.select
[bind-field
[:select {:type "select"
:field [:adding-company]
:event ::change
:subscription editing}
[:option]
(let [used-companies (set (map :id (:companies (:user editing))))]
(for [{:keys [id name]} @(re-frame/subscribe [::subs/companies])
:when (not (used-companies id))]
^{:key id} [:option {:value id} name]))]]]]
[:p.control
[:button.button.is-primary {:on-click (dispatch-event [::add-company])} "Add"]]]
[:ul
(for [{:keys [id name]} (:companies (:user editing))]
^{:key id} [:li name [:a.icon {:on-click (dispatch-event [::remove-company id])} [:i.fa.fa-times ]]])]]])]])])
{:component-will-mount #(re-frame/dispatch-sync [::users-mounted {}]) })) {:component-will-mount #(re-frame/dispatch-sync [::users-mounted {}]) }))

View File

@@ -1,6 +1,8 @@
(ns auto-ap.views.utils (ns auto-ap.views.utils
(:require [re-frame.core :as re-frame] (:require [re-frame.core :as re-frame]
[clojure.spec.alpha :as s] [clojure.spec.alpha :as s]
[auto-ap.events :as events]
[auto-ap.subs :as subs]
[cljs-time.format :as format])) [cljs-time.format :as format]))
(defn active-when= [active-page candidate] (defn active-when= [active-page candidate]
@@ -37,6 +39,9 @@
(defmulti do-bind (fn [a {:keys [type] :as x}] (defmulti do-bind (fn [a {:keys [type] :as x}]
type)) type))
(defn with-keys [children]
(map-indexed (fn [i c] ^{:key i} c) children))
(defmethod do-bind "select" [dom {:keys [field subscription event class value spec] :as keys} & rest] (defmethod do-bind "select" [dom {:keys [field subscription event class value spec] :as keys} & rest]
(let [field (if (keyword? field) [field] field) (let [field (if (keyword? field) [field] field)
event (if (keyword? event) [event] event) event (if (keyword? event) [event] event)
@@ -48,7 +53,7 @@
(when (and spec (not (s/valid? spec (get-in subscription field)))) (when (and spec (not (s/valid? spec (get-in subscription field))))
" is-danger"))) " is-danger")))
keys (dissoc keys :field :subscription :event :spec)] keys (dissoc keys :field :subscription :event :spec)]
(vec (concat [dom keys] rest)))) (into [dom keys] (with-keys rest))))
(defmethod do-bind "radio" [dom {:keys [field subscription event class value spec] :as keys} & rest] (defmethod do-bind "radio" [dom {:keys [field subscription event class value spec] :as keys} & rest]
@@ -61,7 +66,7 @@
(when (and spec (not (s/valid? spec (get-in subscription field )))) (when (and spec (not (s/valid? spec (get-in subscription field ))))
" is-danger"))) " is-danger")))
keys (dissoc keys :field :subscription :event :spec)] keys (dissoc keys :field :subscription :event :spec)]
(vec (concat [dom keys] rest)))) (into [dom keys] (with-keys rest))))
(defmethod do-bind :default [dom {:keys [field event subscription class spec] :as keys} & rest] (defmethod do-bind :default [dom {:keys [field event subscription class spec] :as keys} & rest]
@@ -75,10 +80,12 @@
(when (and spec (not (s/valid? spec (get-in subscription field)))) (when (and spec (not (s/valid? spec (get-in subscription field))))
" is-danger"))) " is-danger")))
keys (dissoc keys :field :subscription :event :spec)] keys (dissoc keys :field :subscription :event :spec)]
(vec (concat [dom keys] rest)))) (into [dom keys] (with-keys rest))))
(defn bind-field [all] (defn bind-field [all]
(apply do-bind all)) (apply do-bind all))
(defn horizontal-field [label & controls] (defn horizontal-field [label & controls]
[:div.field.is-horizontal [:div.field.is-horizontal
@@ -87,4 +94,7 @@
] ]
(into (into
[:div.field-body] [:div.field-body]
(map (fn [c] [:div.field c]) controls))]) (with-keys (map (fn [x] [:div.field x]) controls)))])