From 27a66f11f139d0a30d222bd3ce310ba8b23e34cc Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Wed, 12 Aug 2020 10:04:53 -0700 Subject: [PATCH] invoices can be payed again --- src/cljs/auto_ap/views/components/grid.cljs | 28 +++++++++-- .../views/components/invoice_table.cljs | 27 +++-------- .../auto_ap/views/pages/unpaid_invoices.cljs | 48 ++++++++++++------- 3 files changed, 61 insertions(+), 42 deletions(-) diff --git a/src/cljs/auto_ap/views/components/grid.cljs b/src/cljs/auto_ap/views/components/grid.cljs index 2513a6cd..d4e29a6e 100644 --- a/src/cljs/auto_ap/views/components/grid.cljs +++ b/src/cljs/auto_ap/views/components/grid.cljs @@ -136,9 +136,26 @@ :class class}] (r/children (r/current-component)))) -(defn row [{:keys [class]}] - (apply r/create-element "tr" #js {:className class} - (map r/as-element (r/children (r/current-component))))) +(defn row [{:keys [class id checkable?]}] + (let [children (r/children (r/current-component))] + [:> Consumer {} + (fn [consume] + (let [{:strs [on-params-change params check-boxes? on-check-changed checked] :as consume} (js->clj consume)] + (apply r/create-element "tr" #js {:className class} + (when check-boxes? + (r/as-element [:th {:style {:width "22px"}} + [:input.checkbox (cond-> {:type "checkbox" + :checked (if (get checked id) + "checked" + "") + :on-change (fn [x e] + (let [checked (or checked #{})] + (if (get checked id) + (on-check-changed (disj checked id)) + (on-check-changed (conj checked id)))))} + (boolean? checkable?) (assoc :disabled (not checkable?))) ]])) + + (map r/as-element children))))])) (defn button-cell [params] (apply r/create-element "td" #js {"style" #js {"overflow" "visible"}} @@ -196,9 +213,12 @@ children) (sort-icon sort-key (:sort params))))))])) -(defn grid [{:keys [on-params-change params status column-count]}] +(defn grid [{:keys [on-params-change on-check-changed checked params status column-count check-boxes?]}] (r/create-element Provider #js {:value #js {:on-params-change on-params-change + :on-check-changed on-check-changed + :check-boxes? check-boxes? + :checked checked :params params :status status :column-count column-count}} diff --git a/src/cljs/auto_ap/views/components/invoice_table.cljs b/src/cljs/auto_ap/views/components/invoice_table.cljs index a525bb35..fdf354f4 100644 --- a/src/cljs/auto_ap/views/components/invoice_table.cljs +++ b/src/cljs/auto_ap/views/components/invoice_table.cljs @@ -92,20 +92,11 @@ (fn [{:keys [db]} [_ invoice]] {:db db})) -(defn row [{:keys [invoice check-boxes checked on-check-changed selected-client overrides expense-event ]}] +(defn row [{:keys [invoice check-boxes checked selected-client overrides expense-event ]}] (let [{:keys [client payments expense-accounts invoice-number date due total outstanding-balance id vendor checkable?] :as i} invoice accounts-by-id @(re-frame/subscribe [::subs/accounts-by-id client]) account->name #(:name (accounts-by-id (:id %)))] - [grid/row {:class (:class i)} - (when check-boxes - [grid/cell {} - [:input.checkbox (cond-> {:type "checkbox" - :checked (if (get checked id) - "checked" - "") - :on-change (fn [x e] (when on-check-changed - (on-check-changed id i)))} - (boolean? checkable?) (assoc :disabled (not checkable?))) ]]) + [grid/row {:class (:class i) :id id :checkable? checkable?} (when-not selected-client [grid/cell {} (if-let [client-override (:client overrides)] @@ -184,7 +175,7 @@ [buttons/fa-icon {:icon "fa-undo" :event [::unvoid-invoice i]}])]]])) -(defn invoice-table [{:keys [id invoice-page status vendors check-boxes checked on-check-changed expense-event overrides]}] +(defn invoice-table [{:keys [id invoice-page status vendors check-boxes on-check-changed expense-event overrides]}] (let [selected-client @(re-frame/subscribe [::subs/client]) {:keys [sort]} @(re-frame/subscribe [::table-params]) {:keys [invoices outstanding]} invoice-page @@ -207,14 +198,14 @@ [[] nil] (:invoices invoice-page)) [[(:invoices invoice-page)]])] - ^{:key (str @(re-frame/subscribe [::table-params]))} [grid/grid {:on-params-change (fn [p] - (re-frame/dispatch [::params-changed p])) + :on-check-changed on-check-changed :params @(re-frame/subscribe [::table-params]) + :checked (:checked invoice-page) :status status - ;; TODO checkboxes - :column-count (if selected-client 8 9)} + :check-boxes? check-boxes + :column-count (if selected-client 7 8)} [grid/controls invoice-page [:div.level-item "Outstanding " (nf outstanding)]] @@ -223,8 +214,6 @@ [grid/table {:fullwidth true} [grid/header {} [grid/row {} - (when check-boxes - [grid/header-cell {:style {:width "22px"}}]) (when-not selected-client [grid/sortable-header-cell {:sort-key "client" :sort-name "Client"} "Client"]) [grid/sortable-header-cell {:sort-key "vendor" :sort-name "Vendor"} @@ -246,8 +235,6 @@ (for [{:keys [client payments expense-accounts invoice-number date due total outstanding-balance id vendor] :as i} invoices] ^{:key id} [row {:invoice i - :check-boxes check-boxes - :checked checked :selected-client selected-client :overrides overrides :expense-event expense-event}])]])])) diff --git a/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs b/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs index 5cbec39d..d346d160 100644 --- a/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs +++ b/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs @@ -135,13 +135,22 @@ (re-frame/reg-event-db ::toggle-check - (fn [db [_ id invoice]] - (-> db - (update-in [::invoice-page :checked] (fn [x] - (let [x (or x {})] - (if (x id) - (dissoc x id) - (assoc x id invoice)))))))) + [(re-frame/path [::invoice-page :checked])] + (fn [db [_ new]] + new)) + +(re-frame/reg-event-db + ::remove-check + [(re-frame/path [::invoice-page :checked])] + (fn [db [_ to-remove]] + (let [db (or db #{})] + (disj db to-remove)))) + +(re-frame/reg-sub + ::checked-invoices + :<- [::invoice-page] + (fn [page] + (filter (comp #(get (:checked page) %) :id) (:invoices page)))) @@ -238,10 +247,10 @@ :graphql {:token (-> db :user) - :query-obj (print-checks-query (map (fn [[id invoice]] + :query-obj (print-checks-query (map (fn [{:keys [id outstanding-balance] }] {:invoice-id id - :amount (:outstanding-balance invoice)}) - (get-in db [::invoice-page :checked])) + :amount outstanding-balance}) + @(re-frame/subscribe [::checked-invoices])) bank-account-id type (:client db)) @@ -542,8 +551,9 @@ -(defn pay-button [{:keys [print-checks-shown? checked-invoices print-checks-loading?]}] - (let [current-client @(re-frame/subscribe [::subs/client])] +(defn pay-button [{:keys [print-checks-shown? print-checks-loading?]}] + (let [current-client @(re-frame/subscribe [::subs/client]) + checked-invoices @(re-frame/subscribe [::checked-invoices])] [:div [:div.is-pulled-right [:div.buttons @@ -564,7 +574,6 @@ (count checked-invoices) " invoices " "(" (->> checked-invoices - vals (map (comp js/parseFloat :outstanding-balance)) (reduce + 0) (gstring/format "$%.2f" )) @@ -583,11 +592,14 @@ ^{:key (str id "-debit")} [:a.dropdown-item {:on-click (dispatch-event [::print-checks id :debit])} "Debit from " name]))) ^{:key "advanced-divider"} [:hr.dropdown-divider] - (when (= 1 (count (set (map (comp :id :vendor) (vals checked-invoices))))) + (when (= 1 (count (set (map (comp :id :vendor) checked-invoices)))) ^{:key "handwritten"} [:a.dropdown-item {:on-click (dispatch-event [::handwrite-checks])} "Handwritten Check..."]) ^{:key "advanced"} [:a.dropdown-item {:on-click (dispatch-event [::advanced-print-checks])} "Advanced..."])]])]] [:div.is-pulled-right {:style {:margin-right "0.5rem"}} - (into [:div.tags ] (map (fn [[id invoice]] [:span.tag.is-medium (:invoice-number invoice) [:button.delete.is-small {:on-click (dispatch-event [::toggle-check id invoice])}]]) checked-invoices))]] + (into [:div.tags ] (map (fn [{:keys [id invoice-number]}] + [:span.tag.is-medium invoice-number + [:button.delete.is-small {:on-click + (dispatch-event [::remove-check id])}]]) checked-invoices))]] )) (defn check-results-dialog [] @@ -615,15 +627,15 @@ [:div.notification action-notification]) (when (= status :unpaid) - [pay-button {:print-checks-shown? print-checks-shown? :checked-invoices checked :print-checks-loading? print-checks-loading?}]) + [pay-button {:print-checks-shown? print-checks-shown? :print-checks-loading? print-checks-loading?}]) [table/invoice-table {:id :unpaid :invoice-page @(re-frame/subscribe [::invoice-page]) :status @(re-frame/subscribe [::status/single ::page]) :check-boxes (= status :unpaid) :checked checked - :on-check-changed (fn [which invoice] - (re-frame/dispatch [::toggle-check which invoice])) + :on-check-changed (fn [new] + (re-frame/dispatch [::toggle-check new ])) :expense-event [::expense-accounts-dialog/change-expense-accounts]}]])) (defn unpaid-invoices-page [params]