From 2e8a7b46482b7b484b6be5a75219cf292548499f Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Sun, 5 Aug 2018 19:51:18 -0700 Subject: [PATCH] allows adding --- resources/public/index.html | 3 + src/clj/auto_ap/db/companies.clj | 9 + src/clj/auto_ap/graphql.clj | 2 + src/clj/auto_ap/routes/companies.clj | 17 +- src/cljc/auto_ap/entities/companies.cljc | 2 +- src/cljs/auto_ap/events.cljs | 3 +- .../auto_ap/views/components/address.cljs | 1 + .../auto_ap/views/pages/admin/companies.cljs | 171 ++++++++++++++++-- 8 files changed, 183 insertions(+), 25 deletions(-) diff --git a/resources/public/index.html b/resources/public/index.html index 0c71916b..6c0f150c 100644 --- a/resources/public/index.html +++ b/resources/public/index.html @@ -340,6 +340,9 @@ /* background-clip: padding-box; */ } + .modal-card { + overflow: auto; + } .typeahead-suggestion { display: block; diff --git a/src/clj/auto_ap/db/companies.clj b/src/clj/auto_ap/db/companies.clj index 175d9fdd..f3d1ccba 100644 --- a/src/clj/auto_ap/db/companies.clj +++ b/src/clj/auto_ap/db/companies.clj @@ -45,4 +45,13 @@ (Integer/parseInt id)))) +(defn add-bank-account [id bank-account] + (let [company (get-by-id id) + company (update company :bank-accounts + (fn [ba] + (let [next-id (inc (apply max (conj (map :id ba) 0)))] + (conj (vec ba) (assoc bank-account :id next-id)))))] + (upsert id company))) + + diff --git a/src/clj/auto_ap/graphql.clj b/src/clj/auto_ap/graphql.clj index fc8cd550..02c88bcf 100644 --- a/src/clj/auto_ap/graphql.clj +++ b/src/clj/auto_ap/graphql.clj @@ -36,6 +36,7 @@ {:fields {:id {:type 'Int} :name {:type 'String} :email {:type 'String} + :address {:type :address} :locations {:type '(list String)} :bank_accounts {:type '(list :bank_account)}}} @@ -49,6 +50,7 @@ :bank_name {:type 'String}}} :address {:fields {:street1 {:type 'String} + :street2 {:type 'String} :city {:type 'String} :state {:type 'String} :zip {:type 'String}}} diff --git a/src/clj/auto_ap/routes/companies.clj b/src/clj/auto_ap/routes/companies.clj index 8a45b089..8dcd94af 100644 --- a/src/clj/auto_ap/routes/companies.clj +++ b/src/clj/auto_ap/routes/companies.clj @@ -15,10 +15,19 @@ :body (pr-str (filter #(can-see-company? (:identity r) (:id %)) (companies/get-all))) :headers {"Content-Type" "application/edn"}}) (wrap-spec - (PUT "/:id" {:keys [edn-params] {:keys [id]} :route-params :as r} + (PUT "/:id" {{:keys [address email locations new-bank-accounts]} :edn-params :keys [edn-params] {:keys [id ]} :route-params :as r} (assert-can-see-company (:identity r) id) - {:status 200 - :body (pr-str (companies/upsert id edn-params)) - :headers {"Content-Type" "application/edn"}}) + (let [id (Integer/parseInt id) + company (companies/get-by-id id) + updated-company (merge company {:address address + :email email + :locations locations})] + (companies/upsert id updated-company) + (doseq [bank-account new-bank-accounts] + (companies/add-bank-account id bank-account)) + + {:status 200 + :body (pr-str (companies/get-by-id id)) + :headers {"Content-Type" "application/edn"}})) ::entity/company)) wrap-secure)) diff --git a/src/cljc/auto_ap/entities/companies.cljc b/src/cljc/auto_ap/entities/companies.cljc index 8c4ded79..7e128df1 100644 --- a/src/cljc/auto_ap/entities/companies.cljc +++ b/src/cljc/auto_ap/entities/companies.cljc @@ -8,7 +8,7 @@ (s/def ::id int) (s/def ::name ::shared/required-identifier) -(s/def ::address ::address/address) +(s/def ::address (s/nilable ::address/address)) (s/def ::location string?) (s/def ::locations (s/coll-of ::location)) diff --git a/src/cljs/auto_ap/events.cljs b/src/cljs/auto_ap/events.cljs index 2e3ca5e4..f14a2e72 100644 --- a/src/cljs/auto_ap/events.cljs +++ b/src/cljs/auto_ap/events.cljs @@ -37,7 +37,8 @@ :graphql {:token token :query-obj {:venia/queries [[:company - [:id :name :locations [:bank-accounts [:id :number :check-number :name :type] ]]] + [:id :name :locations [:bank-accounts [:id :number :check-number :name :type] ] + [:address [:street1 :street2 :city :state :zip]]]] [:vendor [:id :name :default-expense-account :primary-contact :primary-email :primary-phone :secondary-contact :secondary-email :secondary-phone :print-as :invoice-reminder-schedule :code]]]} diff --git a/src/cljs/auto_ap/views/components/address.cljs b/src/cljs/auto_ap/views/components/address.cljs index 5d2c4e2f..64de7364 100644 --- a/src/cljs/auto_ap/views/components/address.cljs +++ b/src/cljs/auto_ap/views/components/address.cljs @@ -3,6 +3,7 @@ [auto-ap.views.utils :refer [dispatch-value-change dispatch-event bind-field horizontal-field]])) (defn address-field [{:keys [event field subscription]}] + (println "sub" subscription) [:span [horizontal-field nil diff --git a/src/cljs/auto_ap/views/pages/admin/companies.cljs b/src/cljs/auto_ap/views/pages/admin/companies.cljs index 8470bcdd..24d896cc 100644 --- a/src/cljs/auto_ap/views/pages/admin/companies.cljs +++ b/src/cljs/auto_ap/views/pages/admin/companies.cljs @@ -3,7 +3,7 @@ (:require [re-frame.core :as re-frame] [reagent.core :as reagent] [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-event dispatch-value-change bind-field horizontal-field]] @@ -12,6 +12,51 @@ [auto-ap.routes :as routes] [bidi.bidi :as bidi])) +(re-frame/reg-event-fx + ::edit + (fn [{:keys [db]} [_ company-id]] + {:dispatch [::events/modal-status :auto-ap.views.pages.admin.companies/edit {:visible? true}] + :db (assoc-in db [:admin :company] + (get (:companies db) company-id))})) + +(re-frame/reg-event-fx + ::save + (fn [{:keys [db]} _] + (let [edited-company (-> (get-in db [:admin :company]) + (dissoc :location) + (dissoc :new-account))] + + {:db (assoc-in db [:admin :company :saving?] true) + :http {:method :put + :token (:user db) + :body (pr-str edited-company) + :headers {"Content-Type" "application/edn"} + :uri (str "/api/companies/" (:id edited-company)) + :on-success [::save-complete] + :on-error [::save-error]}}))) + +(re-frame/reg-event-fx + ::save-complete + (fn [{:keys [db]} [_ company]] + {:dispatch [::events/modal-completed :auto-ap.views.pages.admin.companies/edit] + :db (-> db + + (assoc-in [:admin :company] nil) + (assoc-in [:companies (:id company)] company))})) + +(re-frame/reg-event-db + ::save-error + (fn [db [_ company]] + (-> db + (assoc-in [:admin :company :saving?] false) + (assoc-in [:admin :company :error] true)))) + +(re-frame/reg-event-db + ::change + (fn [db [_ path value]] + (assoc-in db (concat [:admin :company] path) + value))) + (re-frame/reg-event-fx ::add-location (fn [{:keys [db]} _] @@ -20,6 +65,28 @@ (update-in [:admin :company :locations] conj (:location company)) (update-in [:admin :company] dissoc :location))}))) +(re-frame/reg-event-fx + ::add-new-bank-account + (fn [{:keys [db]} _] + (let [company (:company @(re-frame/subscribe [::subs/admin])) + new-bank-account (:new-account company) + new-bank-account (-> new-bank-account + (update :check-number #(if (seq %) (js/parseInt %) nil)) + (update :yodlee-account-id #(if (seq %) (js/parseInt %) nil)) + (assoc :is-new? true))] + {:db (-> db + (update-in [:admin :company :new-bank-accounts] (fn [bank-accounts] + (conj bank-accounts new-bank-account))) + (update-in [:admin :company] dissoc :new-account))}))) + +(re-frame/reg-event-db + ::remove-new-bank-account + (fn [db [_ index]] + (update-in db [:admin :company :new-bank-accounts] + (fn [bas] + (vec (concat (take index bas) + (drop (inc index) bas))))))) + (defn companies-table [] (let [companies (re-frame/subscribe [::subs/companies]) editing-company (:company @(re-frame/subscribe [::subs/admin]))] @@ -30,7 +97,7 @@ [:th "Email"]]] [:tbody (for [{:keys [id name email] :as c} @companies] ^{:key (str name "-" id )} - [:tr {:on-click (fn [] (re-frame/dispatch [::events/edit id])) + [:tr {:on-click (fn [] (re-frame/dispatch [::edit id])) :style {"cursor" "pointer"}} [:td name] [:td email]])]])) @@ -47,7 +114,7 @@ [action-modal {:id ::edit :title (str "Edit " (:name editing-company)) :action-text "Save" - :save-event [::events/save]} + :save-event [::save]} [horizontal-field [:label.label "Name"] [:div.control @@ -55,7 +122,7 @@ [:input.input {:type "text" :field :name :spec ::entity/name - :event ::events/change + :event ::change :subscription editing-company}]]]] [horizontal-field @@ -65,22 +132,9 @@ [:input.input {:type "email" :field :email :spec ::entity/name - :event ::events/change + :event ::change :subscription editing-company}]]]] - [:h2.subtitle "Address"] - - [address-field {:field [:address] - :event ::events/change - :subscription editing-company}] - - [horizontal-field - [:label.label "Bank Accounts"] - [:div.control - [:ul - (for [{:keys [number check-number id]} (:bank-accounts editing-company)] - ^{:key id} [:li number " - " check-number])]]] - [horizontal-field [:label.label "Locations"] [:div.control @@ -89,12 +143,91 @@ [bind-field [:input.input {:type "text" :field :location - :event ::events/change + :event ::change :subscription editing-company}]]] [:p.control [:button.button.is-primary {:on-click (dispatch-event [::add-location])} "Add"]]] [:ul (for [location (:locations editing-company)] ^{:key location} [:li location ])]]] + [:h2.subtitle "Address"] + [address-field {:field [:address] + :event ::change + :subscription editing-company}] + [:h2.subtitle "Add account"] + + [horizontal-field + [:label.label "Bank"] + [:div.control + + [bind-field + [:input.input {:placeholder "Bank Name" + :type "text" + :field [:new-account :bank-name] + :event ::change + :subscription editing-company}]]] + [:div.control + + [bind-field + [:input.input {:placeholder "Routing" + :type "text" + :field [:new-account :routing] + :event ::change + :subscription editing-company}]]] + [:div.control + + [bind-field + [:input.input {:placeholder "Code" + :type "text" + :field [:new-account :bank-code] + :event ::change + :subscription editing-company}]]]] + [horizontal-field + [:label.label "Account"] + [:div.control + [bind-field + [:input.input {:placeholder "Nickname" + :type "text" + :field [:new-account :name] + :event ::change + :subscription editing-company}]]] + [:div.control + [bind-field + [:input.input {:placeholder "Acct #" + :type "text" + :field [:new-account :number] + :event ::change + :subscription editing-company}]]] + [:div.control + [bind-field + [:input.input {:placeholder "Check #" + :type "text" + :field [:new-account :check-number] + :event ::change + :subscription editing-company}]]]] + [horizontal-field + [:label.label "Yodlee Account"] + [:div.control + [bind-field + [:input.input {:placeholder "Yodlee Account #" + :type "text" + :field [:new-account :yodlee-account-id] + :event ::change + :subscription editing-company}]]] + [:div.control + [:button.button.is-primary.is-pulled-right {:on-click (dispatch-event [::add-new-bank-account])} "Add"]]] + + + + [:h2.subtitle "Bank Accounts"] + [horizontal-field + nil + [:div.control + [:ul + + (for [{:keys [name number check-number id]} (:bank-accounts editing-company)] + ^{:key id} [:li name]) + (for [[index {:keys [name number check-number]}] (map vector (range) (:new-bank-accounts editing-company))] + ^{:key index} [:li [:strong "* " name] [:button.button {:on-click (dispatch-event [::remove-new-bank-account index])} [:span.icon [:i.fa.fa-times]]]])]]] (when (:saving? editing-company) [:div.is-overlay {:style {"backgroundColor" "rgba(150,150,150, 0.5)"}}])]])])