From e1e0835beefc71dc91fbb1902449a3758390a298 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Thu, 12 Apr 2018 21:37:27 -0700 Subject: [PATCH] for better or worse, moving to localized events and subs. --- src/clj/auto_ap/parse.clj | 5 +- src/cljs/auto_ap/subs.cljs | 4 - .../views/components/invoice_table.cljs | 22 +++-- .../auto_ap/views/pages/import_invoices.cljs | 97 +++++++++++++------ .../auto_ap/views/pages/unpaid_invoices.cljs | 45 ++++++--- 5 files changed, 120 insertions(+), 53 deletions(-) diff --git a/src/clj/auto_ap/parse.clj b/src/clj/auto_ap/parse.clj index a2f12562..3b35b619 100644 --- a/src/clj/auto_ap/parse.clj +++ b/src/clj/auto_ap/parse.clj @@ -52,9 +52,12 @@ (excel/parse-file file filename)) (defn best-match [companies company-identifier] + (println companies) (->> companies (map (fn [company] - [company (apply min (map #(m/jaccard (.toLowerCase company-identifier) %) (:matches (:data company))))])) + (if-let [matches (:matches (:data company))] + [company (apply min (map #(m/jaccard (.toLowerCase company-identifier) %) matches))] + [company 1]))) (filter #(< (second %) 0.25)) (sort-by second) diff --git a/src/cljs/auto_ap/subs.cljs b/src/cljs/auto_ap/subs.cljs index c5bcb869..2012c492 100644 --- a/src/cljs/auto_ap/subs.cljs +++ b/src/cljs/auto_ap/subs.cljs @@ -56,10 +56,6 @@ (fn [db] (:pending (:invoices db)))) -(re-frame/reg-sub - ::unpaid-invoices - (fn [db] - (:unpaid (:invoices db)))) (re-frame/reg-sub ::status diff --git a/src/cljs/auto_ap/views/components/invoice_table.cljs b/src/cljs/auto_ap/views/components/invoice_table.cljs index e8cea8af..f371fc20 100644 --- a/src/cljs/auto_ap/views/components/invoice_table.cljs +++ b/src/cljs/auto_ap/views/components/invoice_table.cljs @@ -4,11 +4,10 @@ [reagent.core :as reagent])) (defn toggle-sort-by [params key] + (-> params (assoc :sort-by key) - (assoc :asc (if (:asc params) - false - true)))) + (update :asc not))) (defn sort-icon [which sort-by asc] (cond @@ -24,14 +23,21 @@ [:span.icon [:i.fa.fa-sort]])) -(defn invoice-table [{:keys [id invoices status on-params-change vendors]}] - (let [state (reagent/atom {})] - (fn [] +(defn query [params] + {:venia/queries [[:invoice + (assoc params + :company-id (:id @(re-frame/subscribe [::subs/company]))) + [:id :total :invoice-number :date [:vendor [:name :id]] [:company [:name :id]]]]]}) + +(defn invoice-table [{:keys [id invoices status on-params-change vendors params]}] + (let [state (reagent/atom (or @params {}))] + (fn [{:keys [id invoices status on-params-change vendors]}] (let [{:keys [sort-by asc]} @state] [:table.table.is-fullwidth [:thead [:tr - [:th {:on-click (fn [e] (on-params-change (swap! state toggle-sort-by "vendor"))) :style {:width "25%" :cursor "pointer"}} "Vendor" + [:th {:on-click (fn [e] + (on-params-change (swap! state toggle-sort-by "vendor"))) :style {:width "25%" :cursor "pointer"}} "Vendor" (sort-icon "vendor" sort-by asc)] [:th {:on-click (fn [e] (on-params-change (swap! state toggle-sort-by "company"))) :style {:width "25%" :cursor "pointer"}} "Company" (sort-icon "company" sort-by asc)] @@ -47,7 +53,7 @@ [:td {:col-span 5} [:i.fa.fa-spin.fa-spinner]]] (for [{:keys [company invoice-number date total id vendor] :as i} @invoices] - ^{:key (str company "-" invoice-number "-" date "-" total "-" id)} + ^{:key id} [:tr [:td (:name vendor)] [:td (:name company)] diff --git a/src/cljs/auto_ap/views/pages/import_invoices.cljs b/src/cljs/auto_ap/views/pages/import_invoices.cljs index 67519ca7..ab62635a 100644 --- a/src/cljs/auto_ap/views/pages/import_invoices.cljs +++ b/src/cljs/auto_ap/views/pages/import_invoices.cljs @@ -5,6 +5,7 @@ [auto-ap.subs :as subs] [auto-ap.entities.companies :as company] [auto-ap.entities.vendors :as vendor] + [auto-ap.views.components.invoice-table :refer [invoice-table] :as invoice-table] [cljsjs.dropzone :as dropzone] [cljs.reader :as edn])) (def dropzone @@ -23,7 +24,7 @@ (js/Dropzone. (reagent/dom-node this) (clj->js {:init (fn [] (.on (js-this) "success" (fn [_ files] - (re-frame/dispatch [::events/view-pending-invoices])))) + (re-frame/dispatch [::invalidated])))) :paramName "file" :headers {"Authorization" (str "Token " @token)} :url (str "/api/invoices/upload" @@ -31,10 +32,66 @@ (str "?company=" company-name))) :previewsContainer "#dz-hidden" :previewTemplate "
"})))}))) + +(re-frame/reg-sub + ::invoices + (fn [db] + (-> db ::invoices))) + +(re-frame/reg-sub + ::params + (fn [db] + (-> db (::params {})))) + +(re-frame/reg-event-fx + ::invalidated + (fn [cofx [_ params]] + {:dispatch [::params-change @(re-frame/subscribe [::params])]})) + +(re-frame/reg-event-fx + ::params-change + (fn [cofx [_ params]] + + {:db (-> (:db cofx) + (assoc-in [:status :loading] true) + (assoc-in [::params] params)) + :graphql {:token (-> cofx :db :user) + :query-obj (invoice-table/query (assoc params :imported false)) + :on-success [::received]}})) + +(re-frame/reg-event-db + ::received + (fn [db [_ data]] + (-> db + (assoc ::invoices (:invoice data)) + (assoc-in [:status :loading] false)))) + +(re-frame/reg-event-fx + ::reject-invoices + (fn [cofx [_ on-success]] + {:http {:method :post + :token (-> cofx :db :user) + :uri (str "/api/invoices/reject" + (when-let [company-id (:id @(re-frame/subscribe [::subs/company]))] + (str "?company=" company-id))) + :on-success on-success + }})) + +(re-frame/reg-event-fx + ::approve-invoices + (fn [cofx [_ on-success]] + {:http {:method :post + :token (-> cofx :db :user) + :uri (str "/api/invoices/approve" + (when-let [company-id (:id @(re-frame/subscribe [::subs/company]))] + (str "?company=" company-id))) + :on-success on-success + }})) + (def import-invoices-page (with-meta (fn [] - (let [invoices (re-frame/subscribe [::subs/pending-invoices]) + (let [invoices (re-frame/subscribe [::invoices]) status (re-frame/subscribe [::subs/status])] [:div {:class "inbox-messages"} [:h1.title "Upload invoices"] @@ -49,40 +106,26 @@ [:h1.title [:i.fa.fa-spin.fa-spinner]] (if (seq @invoices) - [:table {:class "table", :style {:width "100%"}} - [:thead - [:tr - [:th "Vendor"] - [:th "Customer"] - [:th "Invoice #"] - [:th "Date"] - [:th "Amount"] - [:th]]] - [:tbody (for [{:keys [vendor vendor-id potential-duplicate company customer-identifier invoice-number date total id] :as i} @invoices] - ^{:key id} - [:tr - [:td (:name (:vendor i))] - (if company - [:td (:name (:company i))] - [:td [:i.icon.fa.fa-warning {:title "potential duplicate"}] - (str "'" customer-identifier "' doesn't match any known company")]) - [:td invoice-number] - [:td date] - [:td total] - [:td (when potential-duplicate - [:i.icon.fa.fa-warning {:title "potential duplicate"}])]])]] + [invoice-table {:id :approved + :invoices invoices + :status (re-frame/subscribe [::subs/status]) + :params (re-frame/subscribe [::params]) + :on-params-change (fn [params] + (re-frame/dispatch [::params-change params])) }] [:span "No pending invoices"]))] (if (and (seq @invoices) (not (:loading @status))) [:div.card-footer [:a.card-footer-item {:on-click (fn [e] (.preventDefault e) - (re-frame/dispatch [::events/approve-invoices]))} + (re-frame/dispatch [::approve-invoices + [::invalidated]]))} "Accept all"] [:a.card-footer-item {:on-click (fn [e] (.preventDefault e) - (re-frame/dispatch [::events/reject-invoices]))} + (re-frame/dispatch [::reject-invoices + [::invalidated]]))} "Reject all"]])]])) {:component-will-mount (fn [] - (re-frame/dispatch [::events/view-pending-invoices]))})) + (re-frame/dispatch-sync [::invalidated]))})) diff --git a/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs b/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs index 657498ea..16a339cb 100644 --- a/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs +++ b/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs @@ -3,32 +3,51 @@ [auto-ap.entities.companies :as company] [auto-ap.entities.vendors :as vendor] [auto-ap.events :as events] - - [auto-ap.views.components.invoice-table :refer [invoice-table]] + [auto-ap.views.components.invoice-table :refer [invoice-table] :as invoice-table] [auto-ap.subs :as subs] [auto-ap.events :as events])) +(re-frame/reg-sub + ::invoices + (fn [db] + (-> db ::invoices))) + +(re-frame/reg-sub + ::params + (fn [db] + (-> db (::params {})))) + (re-frame/reg-event-fx ::params-change (fn [cofx [_ params]] - {:db (assoc-in (:db cofx) [:status :loading] true) + {:db (-> (:db cofx) + (assoc-in [:status :loading] true) + (assoc-in [::params] params)) :graphql {:token (-> cofx :db :user) - :query-obj {:venia/queries [[:invoice - {:imported true - :company_id (:id @(re-frame/subscribe [::subs/company])) - :sort-by (:sort-by params) - :asc (:asc params true)} - [:id :total :invoice-number :date [:vendor [:name :id]] [:company [:name :id]]]]]} - :on-success [::events/received-invoices :unpaid]}})) + :query-obj (invoice-table/query (assoc params :imported true)) + :on-success [::received]}})) + +(re-frame/reg-event-db + ::received + (fn [db [_ data]] + (-> db + (assoc ::invoices (:invoice data)) + (assoc-in [:status :loading] false)))) + +(re-frame/reg-event-fx + ::invalidated + (fn [cofx [_ params]] + {:dispatch [::params-change @(re-frame/subscribe [::params])]})) (def unpaid-invoices-page (with-meta (fn [] - [:div {:class "inbox-messages"} + [:div [:h1.title "Unpaid invoices"] [invoice-table {:id :unpaid - :invoices (re-frame/subscribe [::subs/unpaid-invoices]) + :params (re-frame/subscribe [::params]) + :invoices (re-frame/subscribe [::invoices]) :status (re-frame/subscribe [::subs/status]) :on-params-change (fn [params] (re-frame/dispatch [::params-change params])) }]]) - {:component-will-mount #(re-frame/dispatch-sync [::events/view-unpaid-invoices]) })) + {:component-will-mount #(re-frame/dispatch-sync [::params-change {}]) }))