diff --git a/src/clj/auto_ap/db/users.clj b/src/clj/auto_ap/db/users.clj index ac124c02..b92e154b 100644 --- a/src/clj/auto_ap/db/users.clj +++ b/src/clj/auto_ap/db/users.clj @@ -25,6 +25,10 @@ (defn get-all [] (map data->fields (query base-query))) +(defn get-by-id [id] + (first (map data->fields (query (-> base-query + (helpers/merge-where [:= :id id])))))) + (defn find-or-insert! [row] (let [user (-> base-query (helpers/merge-where [:and [:= :provider-id (:provider-id row)] @@ -42,3 +46,10 @@ first db->clj data->fields)))) + + +(defn update! [row] + (j/update! (get-conn) + :users + (-> row (fields->data) (clj->db)) + ["id = ?" (:id row)])) diff --git a/src/clj/auto_ap/graphql.clj b/src/clj/auto_ap/graphql.clj index a871cafc..c9975a39 100644 --- a/src/clj/auto_ap/graphql.clj +++ b/src/clj/auto_ap/graphql.clj @@ -12,6 +12,7 @@ [auto-ap.db.users :as users] [auto-ap.db.checks :as checks] [auto-ap.routes.checks :as rchecks] + [auto-ap.graphql.users :as gq-users] [auto-ap.db.reminders :as reminders] [auto-ap.db.invoices-checks :as invoices-checks] [auto-ap.db.utils :as utils] @@ -69,7 +70,8 @@ {:fields {:id {:type 'Int} :name {:type 'String} :role {:type 'String} - :companies {:type '(list :company)}}} + :companies {:type '(list :company) + :resolve :get-user-companies}}} :invoice {:fields {:id {:type 'Int} @@ -133,6 +135,12 @@ { :invoice_payment {:fields {:invoice_id {:type 'Int} :amount {:type 'Float}}} + + :edit_user + {:fields {:id {:type 'Int} + :name {:type 'String} + :role {:type 'String} + :companies {:type '(list Int)}}} } :mutations @@ -140,7 +148,10 @@ :args {:invoice_payments {:type '(list :invoice_payment)} :bank_account_id {:type 'Int} :company_id {:type 'Int}} - :resolve :mutation/print-checks}}}) + :resolve :mutation/print-checks} + :edit_user {:type :user + :args {:edit_user {:type :edit_user}} + :resolve :mutation/edit-user}}}) @@ -233,6 +244,12 @@ (company-cache (:company_id value)) (companies/get-by-id (:company_id value))))) +(defn get-user-companies [context args value] + (->graphql + (if-let [company-cache (:company-cache context)] + (map company-cache (:companies value)) + (map companies/get-by-id (:companies value))))) + (defn get-company [context args value] (->graphql (companies/get-all))) @@ -246,12 +263,11 @@ (defn get-user [context args value] (let [users (users/get-all) - users (cond-> users - (executor/selects-field? context :user/companies) - (join-companies) - )] - (println users) - (->graphql users))) + + extra-context (cond-> context + (executor/selects-field? context :user/companies) (assoc :company-cache (by :id (companies/get-all))))] + (resolve/with-context + (->graphql users) extra-context))) (defn get-vendor [context args value] (->graphql @@ -265,6 +281,10 @@ (:company_id args) (:bank_account_id args)))) +(defn edit-user [context args value] + (->graphql + (gq-users/edit-user (:edit_user args)))) + (def schema (-> integreat-schema (attach-resolvers {:get-invoice-page get-invoice-page @@ -275,7 +295,9 @@ :get-check-by-id get-check-by-id :get-company get-company :get-user get-user + :get-user-companies get-user-companies :mutation/print-checks print-checks + :mutation/edit-user edit-user :get-vendor get-vendor}) schema/compile)) diff --git a/src/clj/auto_ap/graphql/users.clj b/src/clj/auto_ap/graphql/users.clj new file mode 100644 index 00000000..c46e3615 --- /dev/null +++ b/src/clj/auto_ap/graphql/users.clj @@ -0,0 +1,8 @@ +(ns auto-ap.graphql.users + (:require [auto-ap.db.users :as users])) + +(defn edit-user [user] + (users/update! user) + (users/get-by-id (:id user))) + + diff --git a/src/cljc/auto_ap/utils.cljc b/src/cljc/auto_ap/utils.cljc index 87a3d9a9..32fdadba 100644 --- a/src/cljc/auto_ap/utils.cljc +++ b/src/cljc/auto_ap/utils.cljc @@ -8,3 +8,12 @@ #(assoc %1 (f %2) (fv %2)) {} xs))) + +(defn replace-if [f candidate existing] + (reduce + (fn [xs x] + (if (f x candidate) + (conj xs candidate) + (conj xs x))) + [] + existing)) diff --git a/src/cljs/auto_ap/views/pages/admin/users.cljs b/src/cljs/auto_ap/views/pages/admin/users.cljs index 640774af..608f413c 100644 --- a/src/cljs/auto_ap/views/pages/admin/users.cljs +++ b/src/cljs/auto_ap/views/pages/admin/users.cljs @@ -2,13 +2,14 @@ (:require-macros [cljs.core.async.macros :refer [go]]) (:require [re-frame.core :as re-frame] [reagent.core :as reagent] + [clojure.string :as str] [auto-ap.subs :as subs] [auto-ap.events.admin.companies :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.utils :refer [by]] + [auto-ap.utils :refer [by replace-if]] [cljs.reader :as edn] [auto-ap.routes :as routes] [bidi.bidi :as bidi])) @@ -62,9 +63,35 @@ (re-frame/reg-event-db ::remove-company (fn [db [_ d]] - (println "remove compnay " d) (update-in db [::editing :user :companies] #(filter (fn [c] (not= (:id c) d)) %)))) +(re-frame/reg-event-fx + ::save + (fn [{:keys [db]} [_]] + {:db (-> db + (assoc-in [::editing :saving?] true )) + :graphql + {:token (-> db :user) + + :query-obj {:venia/operation {:operation/type :mutation + :operation/name "EditUser"} + + :venia/queries [{:query/data [:edit-user + {:edit-user (update (get-in db [::editing :user]) :companies #(map :id %))} + [:id :name :role [:companies [:id :name]]]]}]} + :on-success [::saved]}})) + +(re-frame/reg-event-fx + ::saved + (fn [{:keys [db]} [_ {:keys [edit-user]}]] + {:db (-> db + (assoc-in [::editing :saving?] false ) + (dissoc ::editing) + (update ::users + (fn [us] + (replace-if #(= (:id %1) (:id %2)) edit-user us) + )))})) + (defn users-table [] (let [users (re-frame/subscribe [::users])] [:table {:class "table", :style {:width "100%"}} @@ -79,14 +106,13 @@ :style {"cursor" "pointer"}} [:td name] [:td role] - [:td]])]])) + [: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])] - (println editing) [:div [:h1.title "Users"] @@ -94,11 +120,11 @@ (when editing [modal {:title (str "Edit " (:name (:user editing))) - :foot [:a.button.is-primary {:on-click (fn [] (re-frame/dispatch [::events/save]))} - [:span "Save"] - (when (:saving? editing) - [:span.icon - [:i.fa.fa-spin.fa-spinner]])] + :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"] diff --git a/src/cljs/auto_ap/views/pages/needs_activation.cljs b/src/cljs/auto_ap/views/pages/needs_activation.cljs index 3faa9ae5..a6780034 100644 --- a/src/cljs/auto_ap/views/pages/needs_activation.cljs +++ b/src/cljs/auto_ap/views/pages/needs_activation.cljs @@ -4,14 +4,26 @@ [reagent.core :as reagent] [auto-ap.subs :as subs] [auto-ap.events :as events] - [auto-ap.views.utils :refer [login-url]] + [auto-ap.views.utils :refer [login-url dispatch-event]] [cljs.reader :as edn] [auto-ap.routes :as routes] [bidi.bidi :as bidi] [goog.string :as gstring])) +(re-frame/reg-event-fx + ::relogin + (fn [{:keys [db]} _] + { + :db (assoc db :user nil)} + )) (defn needs-activation-page [] [:div - [:h2 "Sorry, your user is not activated yet. Please have Ben Skinner enable your account."]]) + [:h2 "Sorry, your user is not activated yet. Please have Ben Skinner enable your account. Click " + [:a {:on-click (fn [] + (re-frame/dispatch-sync [::relogin]) + true) + :href login-url} + "here"] + " to try again."]])