diff --git a/notes.txt b/notes.txt index f448f9f3..53089561 100644 --- a/notes.txt +++ b/notes.txt @@ -1,5 +1,5 @@ 1) Aligning checks -2) popup blocker -- disable +2) popup blocker -- disable X 3) Splitting accounts 4) Permissions per location 5) List of payments diff --git a/src/clj/auto_ap/graphql.clj b/src/clj/auto_ap/graphql.clj index c9975a39..ddd9e1b9 100644 --- a/src/clj/auto_ap/graphql.clj +++ b/src/clj/auto_ap/graphql.clj @@ -282,6 +282,7 @@ (:bank_account_id args)))) (defn edit-user [context args value] + (Thread/sleep 1000) (->graphql (gq-users/edit-user (:edit_user args)))) diff --git a/src/cljs/auto_ap/events.cljs b/src/cljs/auto_ap/events.cljs index 63e7fd5e..074e21fa 100644 --- a/src/cljs/auto_ap/events.cljs +++ b/src/cljs/auto_ap/events.cljs @@ -69,6 +69,16 @@ (println 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 ::set-active-page (fn [{:keys [db]} [_ handler]] diff --git a/src/cljs/auto_ap/subs.cljs b/src/cljs/auto_ap/subs.cljs index 55dd17ba..ede5f127 100644 --- a/src/cljs/auto_ap/subs.cljs +++ b/src/cljs/auto_ap/subs.cljs @@ -25,6 +25,12 @@ (fn [db] (:menu db))) +(re-frame/reg-sub + ::modal-state + (fn [db [_ id]] + (println "ID" id) + (get (:modal-state db) id))) + (re-frame/reg-sub ::token (fn [db] diff --git a/src/cljs/auto_ap/views/components/modal.cljs b/src/cljs/auto_ap/views/components/modal.cljs index 4d734604..3fb429ef 100644 --- a/src/cljs/auto_ap/views/components/modal.cljs +++ b/src/cljs/auto_ap/views/components/modal.cljs @@ -1,5 +1,8 @@ (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] [:div.modal.is-active @@ -11,8 +14,25 @@ title] [:button.delete {:on-click (fn [] (re-frame/dispatch hide-event))}]] [:section.modal-card-body - body] + (with-keys body)] (when foot [:footer.modal-card-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)"}}])]))) diff --git a/src/cljs/auto_ap/views/pages/admin/users.cljs b/src/cljs/auto_ap/views/pages/admin/users.cljs index 608f413c..c6d34b29 100644 --- a/src/cljs/auto_ap/views/pages/admin/users.cljs +++ b/src/cljs/auto_ap/views/pages/admin/users.cljs @@ -4,11 +4,11 @@ [reagent.core :as reagent] [clojure.string :as str] [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.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.components.modal :refer [modal]] + [auto-ap.views.components.modal :refer [modal action-modal]] [auto-ap.utils :refer [by replace-if]] [cljs.reader :as edn] [auto-ap.routes :as routes] @@ -49,6 +49,7 @@ (re-frame/reg-event-db ::edit (fn [db [_ d]] + (re-frame/dispatch [::events/modal-status ::edit-user {:visible? true}]) (if-let [user (get (by :id (::users db)) d)] (assoc-in db [::editing :user] user) (dissoc db ::editing)))) @@ -84,6 +85,7 @@ (re-frame/reg-event-fx ::saved (fn [{:keys [db]} [_ {:keys [edit-user]}]] + (re-frame/dispatch [::events/modal-completed ::edit-user]) {:db (-> db (assoc-in [::editing :saving?] false ) (dissoc ::editing) @@ -107,75 +109,71 @@ [:td name] [:td role] [:td (str/join ", " (map :name companies))]])]])) + + + (def admin-users-page (with-meta (fn [] [:div (let [companies (re-frame/subscribe [::users]) editing @(re-frame/subscribe [::editing])] - [:div [:h1.title "Users"] [users-table] - (when editing - [modal {:title (str "Edit " (:name (:user editing))) - :foot [:a.button.is-primary {:on-click (fn [] (re-frame/dispatch [::save])) - :class (when (:saving? editing) - "is-loading" - )} - [:span "Save"]] - :hide-event [::edit nil]} - [horizontal-field - [:label.label "Name"] - [:div.control - [bind-field - [:input.input {:type "text" - :field [:user :name] - :spec ::entity/name - :event ::change - :subscription editing}]]]] + [action-modal {:title (str "Edit " (:name (:user editing))) + :id ::edit-user + :action-text "Save" + :save-event [::save] + :editing editing} + [horizontal-field + [:label.label "Name"] + [bind-field + [:input.input {:type "text" + :field [:user :name] + :spec ::entity/name + :event ::change + :subscription editing}]]] - [horizontal-field - [:label.label "Role"] - [:div.control - [bind-field - [:select.select {:type "select" - :field [:user :role] - :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] + [horizontal-field + [:label.label "Role"] + [:div.control + [bind-field + [:select.select {:type "select" + :field [:user :role] + :spec ::entity/name :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 ]]])]]]) + [: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 + :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"]]] - (when (:saving? editing) [:div.is-overlay {:style {"backgroundColor" "rgba(150,150,150, 0.5)"}}])])])]) + [: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 {}]) })) diff --git a/src/cljs/auto_ap/views/utils.cljs b/src/cljs/auto_ap/views/utils.cljs index c9b53eec..587e01d7 100644 --- a/src/cljs/auto_ap/views/utils.cljs +++ b/src/cljs/auto_ap/views/utils.cljs @@ -1,6 +1,8 @@ (ns auto-ap.views.utils (:require [re-frame.core :as re-frame] [clojure.spec.alpha :as s] + [auto-ap.events :as events] + [auto-ap.subs :as subs] [cljs-time.format :as format])) (defn active-when= [active-page candidate] @@ -37,6 +39,9 @@ (defmulti do-bind (fn [a {:keys [type] :as x}] 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] (let [field (if (keyword? field) [field] field) event (if (keyword? event) [event] event) @@ -48,7 +53,7 @@ (when (and spec (not (s/valid? spec (get-in subscription field)))) " is-danger"))) 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] @@ -61,7 +66,7 @@ (when (and spec (not (s/valid? spec (get-in subscription field )))) " is-danger"))) 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] @@ -75,10 +80,12 @@ (when (and spec (not (s/valid? spec (get-in subscription field)))) " is-danger"))) keys (dissoc keys :field :subscription :event :spec)] - (vec (concat [dom keys] rest)))) + (into [dom keys] (with-keys rest)))) (defn bind-field [all] - (apply do-bind all)) + (apply do-bind all)) + + (defn horizontal-field [label & controls] [:div.field.is-horizontal @@ -87,4 +94,7 @@ ] (into [:div.field-body] - (map (fn [c] [:div.field c]) controls))]) + (with-keys (map (fn [x] [:div.field x]) controls)))]) + + +