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