diff --git a/src/cljs/auto_ap/subs.cljs b/src/cljs/auto_ap/subs.cljs index 8c114fe3..81f42325 100644 --- a/src/cljs/auto_ap/subs.cljs +++ b/src/cljs/auto_ap/subs.cljs @@ -1,5 +1,6 @@ (ns auto-ap.subs (:require [re-frame.core :as re-frame] + [auto-ap.utils :refer [by]] [clojure.string :as str] [goog.crypt.base64 :as base64])) @@ -33,6 +34,11 @@ (fn [db client] (:accounts db))) +(re-frame/reg-sub + ::accounts-for-client-by-id + (fn [db client] + (by :id (:accounts db)))) + (re-frame/reg-sub ::accounts-for-current-client (fn [db] diff --git a/src/cljs/auto_ap/views/components/layouts.cljs b/src/cljs/auto_ap/views/components/layouts.cljs index b9eb4860..b35c8e61 100644 --- a/src/cljs/auto_ap/views/components/layouts.cljs +++ b/src/cljs/auto_ap/views/components/layouts.cljs @@ -109,7 +109,7 @@ (defn appearing-side-bar [{:keys [visible?]} & children ] [appearing {:visible? visible? :enter-class "slide-in-right" :exit-class "slide-out-right" :timeout 500} - [:aside {:class "column is-3 aside menu" :style {:height "calc(100vh - 46px)" :overflow "auto"}} + [:aside {:class "column is-4 aside menu" :style {:height "calc(100vh - 46px)" :overflow "auto"}} [:div.sub-main {} children ]]]) (defn side-bar-layout [{:keys [side-bar main ap bottom right-side-bar]}] diff --git a/src/cljs/auto_ap/views/pages/invoices/form.cljs b/src/cljs/auto_ap/views/pages/invoices/form.cljs index 735f5c4c..95f8a1c5 100644 --- a/src/cljs/auto_ap/views/pages/invoices/form.cljs +++ b/src/cljs/auto_ap/views/pages/invoices/form.cljs @@ -1,5 +1,6 @@ (ns auto-ap.views.pages.invoices.form (:require [auto-ap.entities.invoice :as invoice] + [auto-ap.utils :refer [by]] [auto-ap.events :as events] [auto-ap.forms :as forms] [auto-ap.subs :as subs] @@ -12,12 +13,31 @@ [cljs-time.core :as c] [clojure.spec.alpha :as s] [re-frame.core :as re-frame] - [clojure.string :as str])) + [clojure.string :as str] + [goog.string :as gstring])) ;; SUBS (re-frame/reg-sub - ::can-submit-edit-invoice + ::form :<- [::forms/form ::form] + :<- [::subs/accounts-for-client-by-id] + (fn [[form accounts]] + (update-in form [:data :expense-accounts] (fn [expense-accounts] + (mapv + (fn [ea] + (let [account (accounts (:id (:account ea)))] + (cond-> ea + (:location account) (assoc ea :location (:location account)) + (:amount ea) (assoc :title (str (:name account) " - " + (or (:location account) + (:location ea)) ": " + (gstring/format "$%.2f" (:amount ea 0) ))) + (not account) (assoc :title "New expense account")))) + expense-accounts))))) + +(re-frame/reg-sub + ::can-submit-edit-invoice + :<- [::form] (fn [{:keys [data status]} _] (let [min-total (if (= (:total (:original data)) (:outstanding-balance (:original data))) nil @@ -32,7 +52,7 @@ command)) (defmethod submit-query :create [db] - (let [{:keys [data] {:keys [id invoice-number date location total expense-accounts vendor-id client-id]} :data} @(re-frame/subscribe [::forms/form ::form])] + (let [{:keys [data] {:keys [id invoice-number date location total expense-accounts vendor-id client-id]} :data} @(re-frame/subscribe [::form])] {:venia/operation {:operation/type :mutation :operation/name "AddInvoice"} :venia/queries [{:query/data [:add-invoice @@ -52,7 +72,7 @@ invoice-read]}]})) (defmethod submit-query :edit [db] - (let [{:keys [data] {:keys [id invoice-number date location total expense-accounts vendor-id client-id]} :data} @(re-frame/subscribe [::forms/form ::form])] + (let [{:keys [data] {:keys [id invoice-number date location total expense-accounts vendor-id client-id]} :data} @(re-frame/subscribe [::form])] {:venia/operation {:operation/type :mutation :operation/name "EditInvoice"} :venia/queries [{:query/data [:edit-invoice @@ -70,7 +90,7 @@ invoice-read]}]})) (defmethod submit-query :add-and-print [db [_ _ bank-account-id type]] - (let [{:keys [data] {:keys [id invoice-number date location total expense-accounts vendor-id client-id]} :data} @(re-frame/subscribe [::forms/form ::form])] + (let [{:keys [data] {:keys [id invoice-number date location total expense-accounts vendor-id client-id]} :data} @(re-frame/subscribe [::form])] {:venia/operation {:operation/type :mutation :operation/name "AddAndPrintInvoice"} :venia/queries [{:query/data [:add-and-print-invoice @@ -152,7 +172,7 @@ ::submitted (fn [{:keys [db]} [_ params command bank-account-id type]] (when @(re-frame/subscribe [::can-submit-edit-invoice]) - (let [{:keys [data]} @(re-frame/subscribe [::forms/form ::form])] + (let [{:keys [data]} @(re-frame/subscribe [::form])] {:db (forms/loading db ::form) :graphql {:token (-> db :user) @@ -187,7 +207,7 @@ (defn form [{:keys [can-change-amount?] :as params}] [forms/side-bar-form {:form ::form } - (let [{:keys [data active? error id]} @(re-frame/subscribe [::forms/form ::form]) + (let [{:keys [data active? error id]} @(re-frame/subscribe [::form]) exists? (:id data) current-client @(re-frame/subscribe [::subs/client]) can-change-amount? (#{:unpaid ":unpaid"} (:status data)) @@ -303,12 +323,12 @@ - (for [[index {:keys [id location] :as expense-account {account-id :id account-numeric-code :numeric-code account-name :name} :account}] (map vector (range) (:expense-accounts data))] + (for [[index {:keys [title account id location] :as expense-account {account-id :id account-numeric-code :numeric-code account-name :name} :account}] (map vector (range) (:expense-accounts data))] ^{:key id} [:div.box [:div.columns [:div.column - [:h1.subtitle.is-5 "Ex"]] + [:h1.subtitle.is-6 title]] [:div.column.is-narrow [:a.button {:on-click (dispatch-event [::remove-expense-account id])} [:span.icon [:i.fa.fa-times]]]]] [:div.field @@ -328,11 +348,11 @@ [:div.control (if-let [forced-location (:location @(re-frame/subscribe [::subs/account (get-in data [:expense-accounts index :account :id])]))] [:div.select - [:select {:disabled "disabled" :style {:width "4em"} :value forced-location} [:option {:value forced-location} forced-location]]] + [:select {:disabled "disabled" :style {:width "5em"} :value forced-location} [:option {:value forced-location} forced-location]]] [:div.select [bind-field [:select {:type "select" - :style {:width "4em"} + :style {:width "5em"} :field [:expense-accounts index :location] :allow-nil? true :spec (set locations) diff --git a/src/cljs/auto_ap/views/utils.cljs b/src/cljs/auto_ap/views/utils.cljs index a806dd1b..05da273e 100644 --- a/src/cljs/auto_ap/views/utils.cljs +++ b/src/cljs/auto_ap/views/utils.cljs @@ -110,14 +110,14 @@ keys (dissoc keys :field :subscription :event :spec)] (into [dom keys] (with-keys rest)))) -(defmethod do-bind "typeahead" [dom {:keys [field text-field event subscription class spec] :as keys} & rest] +(defmethod do-bind "typeahead" [dom {:keys [field text-field event text-event subscription class spec] :as keys} & rest] (let [field (if (keyword? field) [field] field) event (if (keyword? event) [event] event) keys (assoc keys :on-change (fn [selected text-description text-value] (re-frame/dispatch (conj (conj event field) selected)) (when text-field - (re-frame/dispatch (conj (conj event text-field) text-value)))) + (re-frame/dispatch (conj (conj (or text-event event) text-field) text-value)))) :value (get-in subscription field) :class (str class (when (and spec (not (s/valid? spec (get-in subscription field))))