Transactions are converted!

This commit is contained in:
Bryce Covert
2020-08-21 08:21:47 -07:00
parent 57895d9ee2
commit e8301b071d
5 changed files with 145 additions and 90 deletions

View File

@@ -80,8 +80,14 @@
(re-frame/reg-fx (re-frame/reg-fx
:http :http
(fn [{:keys [method uri on-success on-error body headers token]}] (fn [{:keys [method uri on-success on-error body headers owns-state token]}]
(go (go
(when (:multi owns-state)
(re-frame/dispatch-sync [::status/loading-multi (:multi owns-state) (:which owns-state)]))
(when (:single owns-state)
(re-frame/dispatch-sync [::status/loading (:single owns-state)]))
(let [headers (if token (let [headers (if token
(assoc headers "Authorization" (str "Token " token)) (assoc headers "Authorization" (str "Token " token))
headers) headers)
@@ -90,23 +96,37 @@
:headers headers :headers headers
:url uri}))] :url uri}))]
(if (>= (:status response) 400) (if (>= (:status response) 400)
(when on-error (do
(when (:multi owns-state)
(re-frame/dispatch [::status/error-multi (:multi owns-state) (:which owns-state) [(:body response)]]))
(when (:single owns-state)
(re-frame/dispatch [::status/error (:single owns-state) [(:body response)]]))
(when on-error
(->> response
:body
(dates->date-times)
(conj on-error)
(re-frame/dispatch))))
(do
(when (:multi owns-state)
(re-frame/dispatch [::status/completed-multi (:multi owns-state) (:which owns-state)]))
(when (:single owns-state)
(re-frame/dispatch [::status/completed (:single owns-state)]))
(->> response (->> response
:body :body
(dates->date-times) (dates->date-times)
(conj on-error) (conj on-success)
(re-frame/dispatch))) (re-frame/dispatch))))))))
(->> response
:body
(dates->date-times)
(conj on-success)
(re-frame/dispatch)))))))
(re-frame/reg-fx (re-frame/reg-fx
:https :https
(fn [{:keys [requests on-success on-failure]}] (fn [{:keys [requests on-success on-failure owns-state]}]
(go (go
(when (:multi owns-state)
(re-frame/dispatch-sync [::status/loading-multi (:multi owns-state) (:which owns-state)]))
(when (:single owns-state)
(re-frame/dispatch-sync [::status/loading (:single owns-state)]))
(let [results (->> (let [results (->>
(for [{:keys [method body headers uri token]} requests] (for [{:keys [method body headers uri token]} requests]
(go (go
@@ -123,9 +143,21 @@
(async/merge) (async/merge)
(async/reduce conj []) (async/reduce conj [])
(async/<!))] (async/<!))]
(println "DONE")
(if (some #{:error} results) (if (some #{:error} results)
(re-frame/dispatch on-failure) (do
(re-frame/dispatch on-success)))))) (when (:multi owns-state)
(re-frame/dispatch [::status/error-multi (:multi owns-state) (:which owns-state) results]))
(when (:single owns-state)
(re-frame/dispatch [::status/error (:single owns-state) results]))
(re-frame/dispatch on-failure))
(do
(when (:multi owns-state)
(re-frame/dispatch [::status/completed-multi (:multi owns-state) (:which owns-state)]))
(when (:single owns-state)
(re-frame/dispatch [::status/completed (:single owns-state)]))
(re-frame/dispatch on-success)))))))
(defn kebab->snake [s] (defn kebab->snake [s]
(str/replace s #"-" "_")) (str/replace s #"-" "_"))

View File

@@ -89,6 +89,12 @@
(assoc db single {:state :loading (assoc db single {:state :loading
:error nil}))) :error nil})))
(re-frame/reg-event-db
::dispose-single
[(re-frame/path [::status]) ]
(fn [db [_ single]]
(dissoc db single )))
(re-frame/reg-event-db (re-frame/reg-event-db
::completed ::completed
[(re-frame/path [::status]) ] [(re-frame/path [::status]) ]
@@ -101,17 +107,33 @@
[(re-frame/path [::status]) ] [(re-frame/path [::status]) ]
(fn [db [_ single error]] (fn [db [_ single error]]
(assoc db single {:state :error (assoc db single {:state :error
:info nil
:error error}))) :error error})))
(re-frame/reg-event-db
::info
[(re-frame/path [::status]) ]
(fn [db [_ single info]]
(assoc db single {:info info})))
(defn status-notification [{:keys [statuses]}] (defn status-notification [{:keys [statuses]}]
(let [states (let [states (mapv #(deref (re-frame/subscribe %)) statuses)
(->> statuses error-states
(mapv #(deref (re-frame/subscribe %))) (->> states
(filter #(= :error (:state %))))] (filter #(= :error (:state %))))
(when (seq states) info-states
[:div.notification.is-warning (->> states (filter #(:info %)))]
(for [state states [:<>
state (:error state)] (if (seq error-states)
(do [:div.notification.is-warning
^{:key (:message state)} (for [state states
[:p (:message state)]))]))) state (:error state)]
(do
^{:key (:message state)}
[:p (:message state)]))])
(if (seq info-states)
[:div.notification
(for [state states]
(do
^{:key (:info state)}
[:p (:info state)]))])]))

View File

@@ -94,8 +94,7 @@
[:h1.title "Ledger"] [:h1.title "Ledger"]
[table/table {:id :ledger [table/table {:id :ledger
:ledger-page @(re-frame/subscribe [::ledger-page]) :ledger-page @(re-frame/subscribe [::ledger-page])
:status @(re-frame/subscribe [::status/single ::page])}] :status @(re-frame/subscribe [::status/single ::page])}]]))
[manual/modal {:import-completed [::manual-import-completed ]}]]))
(defn ledger-page [] (defn ledger-page []

View File

@@ -20,14 +20,6 @@
[auto-ap.status :as status])) [auto-ap.status :as status]))
(re-frame/reg-event-fx
::manual-import-completed
(fn [{:keys [db]} [_ {:keys [imported errors]}]]
{:dispatch [::params-change @(re-frame/subscribe [::data-page/params ::page])]
:db (-> db
(assoc-in [::notification :message] (str "Successfully imported " imported " transactions"))
(assoc-in [::notification :errors] errors))}))
(defn data-params->query-params [params] (defn data-params->query-params [params]
{:start (:start params 0) {:start (:start params 0)
@@ -87,9 +79,11 @@
(re-frame/reg-event-fx (re-frame/reg-event-fx
::unmounted ::unmounted
(fn [{:keys [db]} _] (fn [{:keys [db]} _]
{:dispatch [::data-page/dispose ::page] {:dispatch-n [[::data-page/dispose ::page]
[::status/dispose-single ::manual-import]]
::track/dispose {:id ::params} ::track/dispose {:id ::params}
::forward/dispose {:id ::updated}})) ::forward/dispose [{:id ::updated}
{:id ::manual-import}]}))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::mounted ::mounted
@@ -98,26 +92,24 @@
:subscription [::data-page/params ::page] :subscription [::data-page/params ::page]
:event-fn (fn [params] :event-fn (fn [params]
[::params-change params])} [::params-change params])}
::forward/register {:id ::updated ::forward/register [{:id ::updated
:events #{::edit/edited} :events #{::edit/edited}
:event-fn (fn [[_ edited-transaction]] :event-fn (fn [[_ edited-transaction]]
[::data-page/updated-entity ::page edited-transaction])}})) [::data-page/updated-entity ::page edited-transaction])}
{:id ::manual-import
:events #{::manual/import-completed}
:event-fn (fn [[_ {:keys [imported errors] :as result}]]
[::status/info ::manual-import (str "Successfully imported " imported " transactions")])}]}))
(re-frame/reg-sub
::notification
(fn [db]
(-> db ::notification)))
(defn content [] (defn content []
(let [notification (re-frame/subscribe [::notification]) (let [notification (re-frame/subscribe [::notification])
user @(re-frame/subscribe [::subs/user]) user @(re-frame/subscribe [::subs/user])
params @(re-frame/subscribe [::data-page/params ::page])] params @(re-frame/subscribe [::data-page/params ::page])]
[:div [:div
[:h1.title "Transactions"] [:h1.title "Transactions"]
[status/status-notification {:statuses [[::status/single ::unapprove-all]]}] [status/status-notification {:statuses [[::status/single ::unapprove-all]
[::status/single ::manual-import]]}]
(when (= "admin" (:user/role user)) (when (= "admin" (:user/role user))
[:div [:div
(when (:message @notification) (when (:message @notification)
@@ -155,8 +147,6 @@
{:side-bar [side-bar/side-bar {:data-page ::page}] {:side-bar [side-bar/side-bar {:data-page ::page}]
:main [:div ^{:key approval-status} :main [:div ^{:key approval-status}
[content]] [content]]
:bottom [:div
[manual/modal {:import-completed [::manual-import-completed ]}]]
:right-side-bar [appearing-side-bar :right-side-bar [appearing-side-bar
{:visible? transaction-bar-active?} {:visible? transaction-bar-active?}
[edit/form]]}]))})) [edit/form]]}]))}))

View File

@@ -1,59 +1,71 @@
(ns auto-ap.views.pages.transactions.manual (ns auto-ap.views.pages.transactions.manual
(:require [auto-ap.events :as events] (:require [auto-ap.events :as events]
[auto-ap.forms :as forms]
[auto-ap.status :as status]
[auto-ap.subs :as subs] [auto-ap.subs :as subs]
[auto-ap.views.components.modal :refer [action-modal]] [auto-ap.views.components.modal :as modal]
[auto-ap.views.utils :refer [bind-field]] [auto-ap.views.utils :refer [dispatch-event with-user]]
[re-frame.core :as re-frame])) [re-frame.core :as re-frame]))
(re-frame/reg-sub
::can-submit
:<- [::forms/form ::form]
(fn [{ {:keys [data]} :data}]
(not-empty data)))
(def import-form (forms/vertical-form {:submit-event [::save]
:change-event [::forms/change ::form]
:can-submit [::can-submit]
:id ::form}))
(defn form [{import-completed-event :import-completed}]
(let [{:keys [data active? error id]} @(re-frame/subscribe [::forms/form ::form])
{:keys [form-inline horizontal-field field raw-field error-notification submit-button]} import-form]
(form-inline {}
[:div.field
[:label.label
"Yodlee manual import table"]
[:div.control
[raw-field
[:textarea.textarea {:field [:data]}]]]])))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::opening ::opening
(fn [{:keys [db]} _] (fn [{:keys [db]} _]
{:dispatch [::events/modal-status ::import {:visible? true}] {:dispatch [::modal/modal-requested {:title "Import Transactions"
:db (assoc-in db [::import] {:client-id (:id @(re-frame/subscribe [::subs/client])) :body [form]
:data ""})})) :confirm {:value "Import"
:status-from [::status/single ::form]
:class "is-primary"
:on-click (dispatch-event [::save])
:can-submit [::can-submit]
:close-event [::status/completed ::form]}}]
:db (-> db
(forms/start-form ::form
{:client-id (:id @(re-frame/subscribe [::subs/client]))
:data ""}))}))
(re-frame/reg-sub
::import
(fn [db]
(-> db ::import)))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::import-completed ::import-completed
(fn [{:keys [db]} [_ import-completed-event {:keys [imported errors] :as result}]] (fn [{:keys [db]} [_ {:keys [imported errors] :as result}]]
{:dispatch-n [[::events/modal-completed ::import] {:dispatch [::modal/modal-closed ]}))
(conj import-completed-event result)]}))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::import-error ::save
(fn [{:keys [db]} [_ result]] [with-user (forms/in-form ::form) ]
{:dispatch [::events/modal-failed ::import "Import failed"]})) (fn [{:keys [db user]}]
(let [import (:data db)]
(re-frame/reg-event-fx {:http {:token user
::import-started :owns-state {:single ::form}
(fn [{:keys [db]} [_ import-completed-event]]
(let [import (::import db)]
{:http {:token (:user db)
:method :post :method :post
:body (pr-str import) :body (pr-str import)
:headers {"Content-Type" "application/edn"} :headers {"Content-Type" "application/edn"}
:uri (str "/api/transactions/batch-upload") :uri (str "/api/transactions/batch-upload")
:on-success [::import-completed import-completed-event] :on-success [::import-completed]}})))
:on-error [::import-error]}})))
(defn modal [{import-completed-event :import-completed}]
(let [data @(re-frame/subscribe [::import])
change-event [::events/change-form [::import]]]
[action-modal {:id ::import
:title "Manual Yodlee Import"
:action-text "Import"
:save-event [::import-started import-completed-event]}
[:div.field
[:label.label
"Yodlee manual import table"]
[:div.control
[bind-field
[:textarea.textarea {:field [:data]
:event change-event
:subscription data}]]]]]))