(ns auto-ap.events (:require [re-frame.core :as re-frame] [auto-ap.db :as db] [auto-ap.routes :as routes] [bidi.bidi :as bidi])) (re-frame/reg-event-db ::initialize-db (fn [_ _] (assoc db/default-db :active-page (:handler (bidi/match-route routes/routes (.. js/window -location -pathname)))))) (re-frame/reg-event-db ::toggle-company-menu (fn [db [_ active-page]] (update-in db [:menu :active?] #(not %)))) (re-frame/reg-event-db ::swap-company (fn [db [_ company]] (assoc db :company company))) (re-frame/reg-event-db ::set-active-page (fn [db [_ active-page]] (assoc db :active-page active-page))) (re-frame/reg-event-db ::imported-invoices (fn [db [_ new-invoices]] (assoc-in db [:invoices] new-invoices))) (re-frame/reg-event-fx ::approve-invoices (fn [cofx [_]] {:http {:method :post :uri (str "/api/invoices/approve" (when-let [company-name (-> cofx :db :company :name)] (str "?company=" company-name))) :on-success [::received-invoices :pending] }})) (re-frame/reg-event-fx ::view-pending-invoices (fn [cofx []] {:db (assoc-in (:db cofx) [:status :loading] true) :http {:method :get :uri (str "/api/invoices/pending" (when-let [company-name (-> cofx :db :company :name)] (str "?company=" company-name))) :on-success [::received-invoices :pending]}})) (re-frame/reg-event-fx ::view-unpaid-invoices (fn [cofx []] {:db (assoc-in (:db cofx) [:status :loading] true) :http {:method :get :uri (str "/api/invoices/unpaid" (when-let [company-name (-> cofx :db :company :name)] (str "?company=" company-name))) :on-success [::received-invoices :unpaid]}})) (re-frame/reg-event-fx ::reject-invoices (fn [cofx [_]] {:http {:method :post :uri (str "/api/invoices/reject" (when-let [company-name (-> cofx :db :company :name)] (str "?company=" company-name))) :on-success [::received-invoices :pending] }})) (re-frame/reg-event-db ::submitted-new-invoice (fn [db [_ invoice]] (assoc db :new-invoice {}))) (re-frame/reg-event-fx ::submit-new-invoice (fn [{:keys [db]} [_ invoice]] {:http {:method :post :uri "/api/invoices" :body (pr-str {:rows [(assoc invoice :imported true)]}) :headers {"Content-Type" "application/edn"} :on-success [::submitted-new-invoice]} :db (assoc-in db [:new-invoice :loading?] true)})) (re-frame/reg-event-db ::received-invoices (fn [db [_ type new-invoices]] (-> db (assoc-in [:invoices type] new-invoices) (assoc-in [:status :loading] false) ))) (re-frame/reg-event-db ::change-form-state (fn [db [_ target value]] (assoc-in db target value)))