Made all interactions much better

This commit is contained in:
Bryce Covert
2020-08-17 19:43:31 -07:00
parent dbb671b5d9
commit 6a8bb77e95
10 changed files with 234 additions and 284 deletions

View File

@@ -21,6 +21,7 @@
(re-frame/reg-fx (re-frame/reg-fx
:set-uri-params :set-uri-params
(fn [uri-params] (fn [uri-params]
(println "HERE?")
(pushy/set-token! p/history (pushy/set-token! p/history
(str (.-protocol (.-location js/window)) "//" (.-host (.-location js/window)) (.-pathname (.-location js/window)) (str (.-protocol (.-location js/window)) "//" (.-host (.-location js/window)) (.-pathname (.-location js/window))
"?" "?"

View File

@@ -109,7 +109,7 @@
(mapv #(deref (re-frame/subscribe %))) (mapv #(deref (re-frame/subscribe %)))
(filter #(= :error (:state %))))] (filter #(= :error (:state %))))]
(when (seq states) (when (seq states)
[:div.notification [:div.notification.is-warning
(for [state states (for [state states
state (:error state)] state (:error state)]
(do (do

View File

@@ -1,7 +1,9 @@
(ns auto-ap.views.components.grid (ns auto-ap.views.components.grid
(:require [reagent.core :as r] (:require [reagent.core :as r]
[auto-ap.views.utils :refer [appearing]] [auto-ap.views.utils :refer [appearing]]
[react :as react])) [react :as react]
[re-frame.core :as re-frame]
[auto-ap.views.pages.data-page :as data-page]))
(defonce grid-context (react/createContext "default")) (defonce grid-context (react/createContext "default"))
(def Provider (.-Provider grid-context)) (def Provider (.-Provider grid-context))
@@ -105,6 +107,7 @@
[:> Consumer {} [:> Consumer {}
(fn [consume] (fn [consume]
(let [{:strs [on-params-change params] :as consume} (js->clj consume)] (let [{:strs [on-params-change params] :as consume} (js->clj consume)]
(println "PARAMS" params)
(r/as-element (into (r/as-element (into
[:div {:style {:margin-bottom "1rem"}} [:div {:style {:margin-bottom "1rem"}}
[:div.level [:div.level
@@ -215,19 +218,35 @@
children) children)
(sort-icon sort-key (:sort params))))))])) (sort-icon sort-key (:sort params))))))]))
(defn grid [{:keys [on-params-change on-check-changed checked params status column-count check-boxes?]}] (defn grid [{:keys [on-params-change on-check-changed checked params status column-count check-boxes? data-page]}]
(r/create-element Provider (if data-page
#js {:value #js {:on-params-change on-params-change (let [page @(re-frame/subscribe [::data-page/page data-page])]
:on-check-changed on-check-changed (r/create-element Provider
:check-boxes? check-boxes? #js {:value #js {:on-params-change (fn [p]
:checked checked (re-frame/dispatch [::data-page/table-params-changed data-page p]))
:params params :on-check-changed (fn [new]
:status status (re-frame/dispatch [::data-page/toggle-check data-page new]))
:column-count column-count}} :check-boxes? check-boxes?
(r/as-element :checked (:checked page)
(into :params (:params page)
[:<> ] :status (:status page)
(r/children (r/current-component)))))) :column-count column-count}}
(r/as-element
(into
[:<> ]
(r/children (r/current-component))))))
(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}}
(r/as-element
(into
[:<> ]
(r/children (r/current-component)))))))
(defn virtual-paginate [start xs ] (defn virtual-paginate [start xs ]
(take 100 (drop (or start 0) xs))) (take 100 (drop (or start 0) xs)))

View File

@@ -16,13 +16,29 @@
[clojure.string :as str] [clojure.string :as str]
[goog.string :as gstring] [goog.string :as gstring]
[re-frame.core :as re-frame] [re-frame.core :as re-frame]
[auto-ap.views.components.expense-accounts-dialog :as expense-accounts-dialog])) [auto-ap.views.components.expense-accounts-dialog :as expense-accounts-dialog]
[auto-ap.views.pages.data-page :as data-page]))
(defn query [params] (defn query [params]
{:venia/queries [[:invoice_page {:venia/queries [[:invoice_page
(-> params {
(assoc :start (:start params 0)
:client-id (:id @(re-frame/subscribe [::subs/client])))) :sort (:sort params)
:vendor-id (:id (:vendor params))
:date-range (:date-range params)
:due-range (:due-range params)
:amount-gte (:amount-gte (:amount-range params))
:amount-lte (:amount-lte (:amount-range params))
:invoice-number-like (:invoice-number-like params)
:client-id (:id @(re-frame/subscribe [::subs/client]))
:import-status (:import-status params)
:status (condp = @(re-frame/subscribe [::subs/active-page])
:invoices nil
:import-invoices nil
:unpaid-invoices :unpaid
:paid-invoices :paid
:voided-invoices :voided)}
[[:invoices [:id :total :outstanding-balance :invoice-number :date :due :status :client-identifier :automatically-paid-when-due [[:invoices [:id :total :outstanding-balance :invoice-number :date :due :status :client-identifier :automatically-paid-when-due
[:vendor [:name :id]] [:vendor [:name :id]]
[:expense_accounts [:amount :id :location [:expense_accounts [:amount :id :location
@@ -35,24 +51,7 @@
:start :start
:end]]]}) :end]]]})
(re-frame/reg-sub
::specific-table-params
(fn [db]
(::table-params db)))
(re-frame/reg-sub
::table-params
:<- [::specific-table-params]
:<- [::subs/query-params]
(fn [[specific-table-params query-params]]
(update (merge (select-keys query-params #{:start :sort}) specific-table-params )
:sort seq)))
(re-frame/reg-event-fx
::params-changed
[(re-frame/path [::table-params])]
(fn [{table-params :db} [_ params :as z]]
{:db (merge table-params params)}))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::void-invoice ::void-invoice
@@ -92,7 +91,7 @@
(fn [{:keys [db]} [_ invoice]] (fn [{:keys [db]} [_ invoice]]
{:db db})) {:db db}))
(defn row [{:keys [invoice check-boxes checked selected-client overrides expense-event actions]}] (defn row [{:keys [invoice check-boxes selected-client overrides expense-event actions]}]
(let [{:keys [client payments expense-accounts invoice-number date due total outstanding-balance id vendor checkable?] :as i} invoice (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]) accounts-by-id @(re-frame/subscribe [::subs/accounts-by-id client])
account->name #(:name (accounts-by-id (:id %)))] account->name #(:name (accounts-by-id (:id %)))]
@@ -180,12 +179,13 @@
[buttons/fa-icon {:icon "fa-undo" [buttons/fa-icon {:icon "fa-undo"
:event [::unvoid-invoice i]}])]]])) :event [::unvoid-invoice i]}])]]]))
(defn invoice-table [{:keys [id data checked status check-boxes on-check-changed overrides actions]}] (defn invoice-table [{:keys [id check-boxes overrides actions data-page]}]
(let [selected-client @(re-frame/subscribe [::subs/client]) (let [selected-client @(re-frame/subscribe [::subs/client])
{:keys [sort]} @(re-frame/subscribe [::table-params]) {:keys [data status table-params]} @(re-frame/subscribe [::data-page/page data-page])
selected-client @(re-frame/subscribe [::subs/client]) selected-client @(re-frame/subscribe [::subs/client])
is-loading? (= :loading (:state status)) is-loading? (= :loading (:state status))
is-sorted-by-vendor? (and (= "vendor" (:sort-key (first sort))) is-sorted-by-vendor? (and (= "vendor" (:sort-key (first (:sort table-params))))
(not is-loading?) (not is-loading?)
(or (apply <= (map (comp :name :vendor) (:data data))) (or (apply <= (map (comp :name :vendor) (:data data)))
(apply >= (map (comp :name :vendor) (:data data))))) (apply >= (map (comp :name :vendor) (:data data)))))
@@ -202,12 +202,7 @@
[[] nil] [[] nil]
(:data data)) (:data data))
[[(:data data)]])] [[(:data data)]])]
[grid/grid {:on-params-change (fn [p] [grid/grid {:data-page data-page
(re-frame/dispatch [::params-changed p]))
:on-check-changed on-check-changed
:params @(re-frame/subscribe [::table-params])
:checked checked
:status status
:check-boxes? check-boxes :check-boxes? check-boxes
:column-count (if selected-client 8 9)} :column-count (if selected-client 8 9)}
[grid/controls data [grid/controls data

View File

@@ -13,93 +13,16 @@
[auto-ap.views.components.typeahead :refer [typeahead-entity]] [auto-ap.views.components.typeahead :refer [typeahead-entity]]
[auto-ap.views.utils :refer [active-when dispatch-event bind-field horizontal-field date->str str->date pretty standard query-params dispatch-value-change]] [auto-ap.views.utils :refer [active-when dispatch-event bind-field horizontal-field date->str str->date pretty standard query-params dispatch-value-change]]
[auto-ap.subs :as subs] [auto-ap.subs :as subs]
[auto-ap.events :as events])) [auto-ap.events :as events]
[auto-ap.views.pages.data-page :as data-page]))
(re-frame/reg-sub (defn invoice-number-filter [{:keys [data-page]}]
::specific-filters
(fn [db ]
(::filters db nil)))
(re-frame/reg-sub
::filters
:<- [::specific-filters]
:<- [::subs/vendors-by-id]
:<- [::subs/query-params]
(fn [[specific-filters vendors-by-id query-params] ]
(let [url-filters (-> query-params
(select-keys #{:vendor-id
:date-range
:due-range
:invoice-number-like
:amount-gte
:amount-lte
:status}))
url-filters {:vendor (when-let [vendor-id (:vendor-id url-filters)]
{:id (str vendor-id)
:name (get-in vendors-by-id [(str vendor-id) :name] "Loading...")})
:date-range (:date-range url-filters)
:due-range (:due-range url-filters)
:amount-range {:amount-gte (:amount-gte url-filters)
:amount-lte (:amount-lte url-filters)}
:invoice-number-like (str (:invoice-number-like url-filters))}]
(deep-merge url-filters (or specific-filters {}) ))))
(re-frame/reg-sub
::filter
:<- [::filters]
(fn [filters [_ which]]
(get filters which)))
(re-frame/reg-sub
::settled-filters
(fn [db ]
(::settled-filters db)))
(re-frame/reg-sub
::filter-params
:<- [::settled-filters]
:<- [::filters]
:<- [::subs/active-page]
(fn [[settled-filters filters ap ]]
(let [filters (or settled-filters filters)]
{:vendor-id (:id (:vendor filters))
:date-range (:date-range filters)
:due-range (:due-range filters)
:amount-gte (:amount-gte (:amount-range filters))
:amount-lte (:amount-lte (:amount-range filters))
:invoice-number-like (:invoice-number-like filters)
:status (condp = ap
:invoices nil
:unpaid-invoices :unpaid
:paid-invoices :paid
:voided-invoices :voided)})))
(re-frame/reg-event-fx
::filters-settled
(fn [{:keys [db]} [_ & params]]
{:db (assoc db ::settled-filters @(re-frame/subscribe [::filters]))}))
(re-frame/reg-event-fx
::filter-changed
(fn [{:keys [db]} [_ & params]]
(let [[a b c] params
[which val] (if (= 3 (count params))
[(into [a] b) c]
[[a] b])]
{:db (assoc-in db (into [::filters] which) val)
:dispatch-debounce
{:event [::filters-settled]
:time 800
:key ::filters}})))
(defn invoice-number-filter []
[:div.field [:div.field
[:div.control [:input.input {:placeholder "AP-123" [:div.control [:input.input {:placeholder "AP-123"
:value @(re-frame/subscribe [::filter :invoice-number-like]) :value @(re-frame/subscribe [::data-page/filter data-page :invoice-number-like])
:on-change (dispatch-value-change [::filter-changed :invoice-number-like ])} ]]]) :on-change (dispatch-value-change [::data-page/filter-changed data-page :invoice-number-like ])} ]]])
(defn invoices-side-bar [params] (defn invoices-side-bar [{:keys [data-page] :as params}]
(let [ap @(re-frame/subscribe [::subs/active-page]) (let [ap @(re-frame/subscribe [::subs/active-page])
user @(re-frame/subscribe [::subs/user])] user @(re-frame/subscribe [::subs/user])]
[:div [:div
@@ -142,28 +65,29 @@
[:p.menu-label "Vendor"] [:p.menu-label "Vendor"]
[:div [:div
[typeahead-entity {:matches @(re-frame/subscribe [::subs/vendors]) [typeahead-entity {:matches @(re-frame/subscribe [::subs/vendors])
:on-change #(re-frame/dispatch [::filter-changed :vendor %]) :on-change #(re-frame/dispatch [::data-page/filter-changed data-page :vendor %])
:match->text :name :match->text :name
:type "typeahead-entity" :type "typeahead-entity"
:value @(re-frame/subscribe [::filter :vendor])}]] :value @(re-frame/subscribe [::data-page/filter data-page :vendor])
:include-keys [:name :id]}]]
[:p.menu-label "Date Range"] [:p.menu-label "Date Range"]
[:div [:div
[date-range-filter [date-range-filter
{:on-change-event [::filter-changed :date-range] {:on-change-event [::data-page/filter-changed data-page :date-range]
:value @(re-frame/subscribe [::filter :date-range])}]] :value @(re-frame/subscribe [::data-page/filter data-page :date-range])}]]
[:p.menu-label "Due Range"] [:p.menu-label "Due Range"]
[:div [:div
[date-range-filter [date-range-filter
{:on-change-event [::filter-changed :due-range] {:on-change-event [::data-page/filter-changed data-page :due-range]
:value @(re-frame/subscribe [::filter :due-range])}]] :value @(re-frame/subscribe [::data-page/filter data-page :due-range])}]]
[:p.menu-label "Amount"] [:p.menu-label "Amount"]
[:div [:div
[number-filter [number-filter
{:on-change-event [::filter-changed :amount-range] {:on-change-event [::data-page/filter-changed data-page :amount-range]
:value @(re-frame/subscribe [::filter :amount-range])}]] :value @(re-frame/subscribe [::data-page/filter data-page :amount-range])}]]
[:p.menu-label "Invoice #"] [:p.menu-label "Invoice #"]
[:div [:div
[invoice-number-filter]]])])) [invoice-number-filter params]]])]))

View File

@@ -1,7 +1,8 @@
(ns auto-ap.views.components.typeahead (ns auto-ap.views.components.typeahead
(:require [reagent.core :as r] (:require [reagent.core :as r]
[reagent.ratom :as ra] [reagent.ratom :as ra]
[clojure.string :as str])) [clojure.string :as str]
[clojure.set :as set]))
(defn get-valid-matches [matches not-found-description not-found-value text] (defn get-valid-matches [matches not-found-description not-found-value text]
(let [valid-matches (take 15 (for [[[id t :as match] i] (map vector matches (range)) (let [valid-matches (take 15 (for [[[id t :as match] i] (map vector matches (range))
@@ -124,17 +125,18 @@
valid-matches)] valid-matches)]
valid-matches)) valid-matches))
(defn typeahead-entity [{:keys [matches on-change field value class not-found-description (defn typeahead-entity [{:keys [matches on-change field value class not-found-description
disabled not-found-value auto-focus name]}] disabled not-found-value auto-focus name include-keys]}]
(let [text (r/atom (or (second (first (filter #(= (first %) value) matches))) "")) (let [text (r/atom (or (second (first (filter #(= (first %) value) matches))) ""))
highlighted (r/atom nil) highlighted (r/atom nil)
] ]
(r/create-class (r/create-class
{:reagent-render (fn [{:keys [matches on-change disabled match->text field value class not-found-description]}] {:reagent-render (fn [{:keys [matches on-change disabled match->text field value class not-found-description include-keys]}]
(let [select (fn [entity] (let [select (fn [entity]
(when on-change (when on-change
(if (= :not-found entity) (if (= :not-found entity)
(on-change nil) (on-change nil)
(on-change entity)))) (on-change (cond-> entity
(and include-keys entity) (select-keys include-keys))))))
text-atom text text-atom text
text (or (when value (match->text value)) @text) text (or (when value (match->text value)) @text)
valid-matches (get-valid-entity-matches matches not-found-description not-found-value text match->text)] valid-matches (get-valid-entity-matches matches not-found-description not-found-value text match->text)]

View File

@@ -31,20 +31,25 @@
(defmulti page (fn [active-page] active-page)) (defmulti page (fn [active-page] active-page))
(defmethod page :unpaid-invoices [_] (defmethod page :unpaid-invoices [_]
(unpaid-invoices-page {:status "unpaid"})) ^{:key :voided}
[unpaid-invoices-page {:status :unpaid}])
(defmethod page :import-invoices [_] (defmethod page :import-invoices [_]
(import-invoices-page )) (import-invoices-page ))
(defmethod page :paid-invoices [_] (defmethod page :paid-invoices [_]
(unpaid-invoices-page {:status "paid"})) ^{:key :voided}
[unpaid-invoices-page {:status :paid}]
)
(defmethod page :voided-invoices [_] (defmethod page :voided-invoices [_]
(unpaid-invoices-page {:status "voided"})) ^{:key :voided}
[unpaid-invoices-page {:status :voided}])
(defmethod page :invoices [_] (defmethod page :invoices [_]
(unpaid-invoices-page {})) ^{:key :all}
[unpaid-invoices-page {}])
(defmethod page :payments [_] (defmethod page :payments [_]
[payments-page]) [payments-page])

View File

@@ -1,6 +1,7 @@
(ns auto-ap.views.pages.data-page (ns auto-ap.views.pages.data-page
(:require [auto-ap.status :as status] (:require [auto-ap.status :as status]
[auto-ap.utils :refer [by merge-by replace-by]] [auto-ap.subs :as subs]
[auto-ap.utils :refer [deep-merge replace-by]]
[re-frame.core :as re-frame])) [re-frame.core :as re-frame]))
(re-frame/reg-sub (re-frame/reg-sub
@@ -35,10 +36,30 @@
[::data id :data] [::data id :data]
replace-by :id (update entity :class #(or % "live-added"))))) replace-by :id (update entity :class #(or % "live-added")))))
(re-frame/reg-event-db (re-frame/reg-sub
::table-params
(fn [db [_ which]]
(get-in db [::table-params which])))
(re-frame/reg-sub
::params
(fn [[_ id]]
[(re-frame/subscribe [::subs/client])
(re-frame/subscribe [::settled-filters id])
(re-frame/subscribe [::table-params id])
(re-frame/subscribe [::subs/query-params])])
(fn [[client filters table-params query-params]]
(cond-> {}
client (assoc :client-id (:id client))
(seq query-params) (merge query-params)
(seq filters) (merge filters)
(seq table-params) (merge table-params))))
(re-frame/reg-event-fx
::received ::received
(fn [db [_ id data]] (fn [{:keys [db]} [_ id data]]
(assoc-in db [::data id] data))) {:db (assoc-in db [::data id] data)
:set-uri-params (dissoc @(re-frame/subscribe [::params id]) :client-id)}))
(re-frame/reg-event-db (re-frame/reg-event-db
::dispose ::dispose
@@ -46,7 +67,9 @@
(-> db (-> db
(update ::data dissoc id) (update ::data dissoc id)
(update ::checked dissoc id) (update ::checked dissoc id)
(update ::params dissoc id)))) (update ::table-params dissoc id)
(update ::filters dissoc id)
(update ::settled-filters dissoc id))))
(re-frame/reg-sub (re-frame/reg-sub
::data ::data
@@ -58,13 +81,67 @@
(fn [[_ id]] (fn [[_ id]]
[(re-frame/subscribe [::data id]) [(re-frame/subscribe [::data id])
(re-frame/subscribe [::status/single [::page id]]) (re-frame/subscribe [::status/single [::page id]])
(re-frame/subscribe [::checked id])]) (re-frame/subscribe [::checked id])
(fn [[data status checked] [_ id]] (re-frame/subscribe [::params id])
(re-frame/subscribe [::table-params id])
(re-frame/subscribe [::filters id])])
(fn [[data status checked params table-params filters] [_ id]]
{:id id {:id id
:data data :data data
:status status :status status
:checked checked})) :checked checked
:params params
:filters filters
:table-params table-params}))
(defn in-page-entities [which] (defn in-page-entities [which]
(re-frame/path [::data which :data ] )) (re-frame/path [::data which :data ] ))
(re-frame/reg-event-fx
::table-params-changed
(fn [{:keys [db]} [_ which params :as z]]
{:db (update-in db [::table-params which] merge params)}))
(re-frame/reg-sub
::specific-filters
(fn [db [_ id]]
(get-in db [::filters id])))
(re-frame/reg-sub
::filters
(fn [[_ id]]
[(re-frame/subscribe [::specific-filters id])
(re-frame/subscribe [::subs/query-params])])
(fn [[specific-filters query-params] ]
(deep-merge query-params (or specific-filters {}) )))
(re-frame/reg-sub
::filter
(fn [[_ id]]
[(re-frame/subscribe [::filters id])])
(fn [[filters] [_ id which]]
(get filters which)))
(re-frame/reg-event-fx
::filters-settled
(fn [{:keys [db]} [_ id]]
{:db (assoc-in db [::settled-filters id] @(re-frame/subscribe [::filters id]))}))
(re-frame/reg-sub
::settled-filters
(fn [db [_ id]]
(get-in db [::settled-filters id])))
(re-frame/reg-event-fx
::filter-changed
(fn [{:keys [db]} [_ id & params]]
(let [[a b c] params
[which val] (if (= 3 (count params))
[(into [a] b) c]
[[a] b])]
{:db (assoc-in db (into [::filters id] which) val)
:dispatch-debounce
{:event [::filters-settled id]
:time 800
:key [::filters id]}})))

View File

@@ -17,7 +17,11 @@
[clojure.string :as str] [clojure.string :as str]
[vimsical.re-frame.cofx.inject :as inject] [vimsical.re-frame.cofx.inject :as inject]
[auto-ap.status :as status] [auto-ap.status :as status]
[vimsical.re-frame.fx.track :as track])) [vimsical.re-frame.fx.track :as track]
[auto-ap.views.pages.data-page :as data-page]
[clojure.set :as set]
[auto-ap.effects.forward :as forward]))
(defn dropzone [] (defn dropzone []
(let [client (re-frame/subscribe [::subs/client]) (let [client (re-frame/subscribe [::subs/client])
token (re-frame/subscribe [::subs/token]) token (re-frame/subscribe [::subs/token])
@@ -30,7 +34,7 @@
(.on (js-this) "success" (fn [_ files] (.on (js-this) "success" (fn [_ files]
(re-frame/dispatch [::invalidated]))) (re-frame/dispatch [::invalidated])))
(.on (js-this) "error" (fn [_ error] (.on (js-this) "error" (fn [_ error]
(re-frame/dispatch [::errored error])))) (re-frame/dispatch [::status/error ::import [(edn/read-string error)]]))))
:paramName "file" :paramName "file"
:headers {"Authorization" (str "Token " @token)} :headers {"Authorization" (str "Token " @token)}
:url (str "/api/invoices/upload" :url (str "/api/invoices/upload"
@@ -59,7 +63,6 @@
:value @vendor}]] :value @vendor}]]
] ]
[:div.tile.notification [:div.tile.notification
[:div.has-text-centered {:style {:padding "80px 0px" :width "100%"}} [:div.has-text-centered {:style {:padding "80px 0px" :width "100%"}}
[:span [:span
[:span {:class "icon"} [:span {:class "icon"}
@@ -68,15 +71,6 @@
)) ))
(re-frame/reg-sub
::invoice-page
(fn [db]
(-> db ::invoice-page)))
(re-frame/reg-sub
::error
(fn [db]
(-> db ::error)))
(re-frame/reg-sub (re-frame/reg-sub
@@ -86,65 +80,49 @@
(re-frame/reg-event-fx (re-frame/reg-event-fx
::invalidated ::invalidated
(fn [cofx [_ params]] (fn [{:keys [db]} [_ params]]
{:dispatch [::params-change @(re-frame/subscribe [::params])]})) {:dispatch-n [[::params-change @(re-frame/subscribe [::data-page/params :import-invoices])]
[::data-page/reset-checked :import-invoices]/]
:db (update db ::batch inc)}
))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::mounted ::mounted
(fn [cofx [_ params]] (fn [cofx [_ params]]
{::track/register {:id ::params {::track/register [{:id ::params
:subscription [::params] :subscription [::data-page/params :import-invoices]
:event-fn (fn [params] :event-fn (fn [params]
[::params-change params])}})) [::params-change params])}
]
::forward/register [{:id ::received
:events #{::data-page/received}
:event-fn (fn [[_ _ {:keys [data]}]]
[::received data])}]
:dispatch [::data-page/dispose :import-invoices]}))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::unmounted ::unmounted
(fn [cofx [_ params]] (fn [cofx [_ params]]
{::track/dispose {:id ::params}})) {::track/dispose {:id ::params}}))
(re-frame/reg-event-fx
::completed
(fn [cofx [_ params]]
(println (::batch (:db cofx)))
{:dispatch [::invalidated]
:db (-> (:db cofx)
(update ::batch inc))}))
(re-frame/reg-sub
::params
:<- [::subs/client]
:<- [::invoice-table/table-params]
(fn [[client table-params]]
(cond-> {:import-status "pending"}
client (assoc :client-id (:id client))
(seq table-params) (merge table-params))))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::params-change ::params-change
[with-user (re-frame/inject-cofx ::inject/sub [::params])] [with-user]
(fn [{:keys [db user] ::keys [params]} [_]] (fn [{:keys [user] } [_ params]]
{:db (-> db {:graphql {:token user
(dissoc ::error)) :owns-state {:single [::data-page/page :import-invoices]}
:graphql {:token user :query-obj (invoice-table/query (assoc params :import-status "pending"))
:owns-state {:single ::page} :on-success (fn [result]
:query-obj (invoice-table/query params) (let [result (set/rename-keys (first (:invoice-page result))
:on-success [::received]}})) {:invoices :data})]
(re-frame/reg-event-db [::data-page/received :import-invoices result]))}}))
(re-frame/reg-event-fx
::received ::received
(fn [db [_ data]] (fn [_ [_ data]]
(-> db {:dispatch [::data-page/toggle-check :import-invoices (set (map :id data))]}))
(assoc ::invoice-page (first (:invoice-page data)))
(update-in [::invoice-page] (fn [ip]
(assoc ip :checked (set (map :id (:invoices ip))))))
(assoc-in [:status :loading] false))))
(re-frame/reg-event-db
::errored
(fn [db [_ error]]
(assoc db ::error (or (:message (edn/read-string error))
"An unknown error has occured."))))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::reject-invoices-clicked ::reject-invoices-clicked
@@ -154,12 +132,10 @@
:owns-state {:single ::reject} :owns-state {:single ::reject}
:query-obj {:venia/operation {:operation/type :mutation :query-obj {:venia/operation {:operation/type :mutation
:operation/name "RejectInvoices"} :operation/name "RejectInvoices"}
:venia/queries [[:reject-invoices :venia/queries [[:reject-invoices
{:invoices (vec invoices)} {:invoices (vec invoices)}
[]]]} []]]}
:on-success [::completed]} :on-success [::invalidated]}}))
}))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::approve-invoices-clicked ::approve-invoices-clicked
@@ -169,12 +145,10 @@
:owns-state {:single ::approve} :owns-state {:single ::approve}
:query-obj {:venia/operation {:operation/type :mutation :query-obj {:venia/operation {:operation/type :mutation
:operation/name "ApproveInvoices"} :operation/name "ApproveInvoices"}
:venia/queries [[:approve-invoices :venia/queries [[:approve-invoices
{:invoices (vec invoices)} {:invoices (vec invoices)}
[]]]} []]]}
:on-success [::completed]} :on-success [::invalidated]}}))
}))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::approve-invoices ::approve-invoices
@@ -184,17 +158,10 @@
:uri (str "/api/invoices/approve" :uri (str "/api/invoices/approve"
(when-let [client-id (:id @(re-frame/subscribe [::subs/client]))] (when-let [client-id (:id @(re-frame/subscribe [::subs/client]))]
(str "?client=" client-id))) (str "?client=" client-id)))
:on-success on-success :on-success on-success}}))
}}))
(re-frame/reg-event-db
::toggle-check
(fn [db [_ new]]
(-> db (assoc-in [::invoice-page :checked] new))))
(defn approve-reject-button [checked] (defn approve-reject-button [checked]
[:div.is-pulled-right [:div.is-pulled-right
[:button.button.is-success {:on-click (dispatch-event [::approve-invoices-clicked checked]) [:button.button.is-success {:on-click (dispatch-event [::approve-invoices-clicked checked])
:class (status/class-for @(re-frame/subscribe [::status/single ::approve])) :class (status/class-for @(re-frame/subscribe [::status/single ::approve]))
:disabled (if (seq checked) :disabled (if (seq checked)
@@ -205,8 +172,6 @@
(str (str
(count checked) (count checked)
" invoices")) " invoices"))
[:span " "]] [:span " "]]
[:button.button.is-danger {:on-click (dispatch-event [::reject-invoices-clicked checked]) [:button.button.is-danger {:on-click (dispatch-event [::reject-invoices-clicked checked])
:class (status/class-for @(re-frame/subscribe [::status/single ::reject])) :class (status/class-for @(re-frame/subscribe [::status/single ::reject]))
@@ -218,47 +183,34 @@
(str (str
(count checked) (count checked)
" invoices")) " invoices"))
[:span " "]]])
[:span " "]
]])
(def import-invoices-content (def import-invoices-content
(with-meta (with-meta
(fn [] (fn []
(let [invoice-page (re-frame/subscribe [::invoice-page]) (let [page @(re-frame/subscribe [::data-page/page :import-invoices])
status (re-frame/subscribe [::subs/status]) batch @(re-frame/subscribe [::batch])
error (re-frame/subscribe [::error])
batch (re-frame/subscribe [::batch])
params @(re-frame/subscribe [::params])
client (:id @(re-frame/subscribe [::subs/client]))] client (:id @(re-frame/subscribe [::subs/client]))]
^{:key (str client "-" @batch)} ^{:key (str client "-" batch)}
[:div [:div
[:h1.title "Upload invoices"] [:h1.title "Upload invoices"]
[status/status-notification {:statuses [[::status/single ::approve]
^{:key (str @batch)} [::status/single ::reject]
[::status/single ::import]]}]
^{:key (str batch)}
[dropzone] [dropzone]
[:div {:class "section"}
(when @error
[:div.notification.is-warning @error])]
[:div {:class "card found-invoices",} [:div {:class "card found-invoices",}
[:div {:class "card-header"} [:div {:class "card-header"}
[:span {:class "card-header-title"} "Found Invoices"]] [:span {:class "card-header-title"} "Found Invoices"]]
[:div {:class "card-content"} [:div {:class "card-content"}
[approve-reject-button (:checked @invoice-page)] [approve-reject-button (:checked page)]
(if (seq (:invoices @invoice-page)) (if (seq (:data (:data page)))
[invoice-table {:id :approved [invoice-table {:id :approved
:invoice-page @invoice-page :data-page :import-invoices
:overrides {:client (fn [row] :overrides {:client (fn [row]
[:p (:name (:client row)) [:p (:name (:client row))
[:p [:i.is-size-7 (:client-identifier row)]]])} [:p [:i.is-size-7 (:client-identifier row)]]])}
:check-boxes true :check-boxes true}]
:checked (:checked @invoice-page)
:on-check-changed (fn [which]
(re-frame/dispatch [::toggle-check which]))
:status @(re-frame/subscribe [::status/single ::page])}]
[:span "No pending invoices"])]]])) [:span "No pending invoices"])]]]))
{:component-did-mount (fn [] {:component-did-mount (fn []
(re-frame/dispatch-sync [::mounted])) (re-frame/dispatch-sync [::mounted]))

View File

@@ -35,22 +35,6 @@
[reagent.core :as r] [reagent.core :as r]
[vimsical.re-frame.fx.track :as track])) [vimsical.re-frame.fx.track :as track]))
;; TODO: Sort out approaches for which buttons to show
;; TODO: make it so filters are basically free
(re-frame/reg-sub
::params
:<- [::subs/client]
:<- [::side-bar/filter-params]
:<- [::table/table-params]
(fn [[client filter-params table-params]]
(cond-> {:import-status "imported"}
client (assoc :client-id (:id client))
(seq filter-params) (merge filter-params)
(seq table-params) (merge table-params))))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::params-change ::params-change
[with-user] [with-user]
@@ -63,11 +47,7 @@
{:invoices :data})] {:invoices :data})]
[::data-page/received :invoices result])) [::data-page/received :invoices result]))
:on-error [::events/page-failed]} :on-error [::events/page-failed]}}))
:set-uri-params (dissoc params
:status
:client-id
:import-status)}))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::unmounted ::unmounted
@@ -82,7 +62,7 @@
::mounted ::mounted
(fn [{:keys [db]} _] (fn [{:keys [db]} _]
{::track/register [{:id ::params {::track/register [{:id ::params
:subscription [::params] :subscription [::data-page/params :invoices]
:event-fn (fn [params] :event-fn (fn [params]
[::params-change params])}] [::params-change params])}]
::forward/register [{:id ::updated ::forward/register [{:id ::updated
@@ -207,7 +187,7 @@
(dispatch-event [::data-page/remove-check :invoices id])}]]) checked-invoices))]])) (dispatch-event [::data-page/remove-check :invoices id])}]]) checked-invoices))]]))
(defn unpaid-invoices-content [{:keys [status] :as params}] (defn unpaid-invoices-content [{:keys [status]}]
(let [page @(re-frame/subscribe [::data-page/page :invoices]) (let [page @(re-frame/subscribe [::data-page/page :invoices])
current-client @(re-frame/subscribe [::subs/client])] current-client @(re-frame/subscribe [::subs/client])]
[:div [:div
@@ -216,12 +196,8 @@
(when (= status :unpaid) (when (= status :unpaid)
[pay-button]) [pay-button])
[table/invoice-table {:id (:id page) [table/invoice-table {:id (:id page)
:checked (:checked page) :data-page :invoices
:data (:data page)
:status (:status page)
:check-boxes (= status :unpaid) :check-boxes (= status :unpaid)
:on-check-changed (fn [new]
(re-frame/dispatch [::data-page/toggle-check :invoices new ]))
:actions #{:edit :void :expense-accounts}}]])) :actions #{:edit :void :expense-accounts}}]]))
(defn unpaid-invoices-page [params] (defn unpaid-invoices-page [params]
@@ -231,8 +207,7 @@
:component-did-mount #(re-frame/dispatch [::mounted]) :component-did-mount #(re-frame/dispatch [::mounted])
:reagent-render :reagent-render
(fn [] (fn []
(let [{invoice-bar-active? :active?} @(re-frame/subscribe [::forms/form ::form/form]) (let [{invoice-bar-active? :active?} @(re-frame/subscribe [::forms/form ::form/form])]
params @(re-frame/subscribe [::params])] [side-bar-layout {:side-bar [invoices-side-bar {:data-page :invoices}]
[side-bar-layout {:side-bar [invoices-side-bar {}]
:main [unpaid-invoices-content params] :main [unpaid-invoices-content params]
:right-side-bar [appearing-side-bar {:visible? invoice-bar-active?} [form/form {}]]}]))})) :right-side-bar [appearing-side-bar {:visible? invoice-bar-active?} [form/form {}]]}]))}))