(ns auto-ap.events (:require [re-frame.core :as re-frame] [auto-ap.db :as db] [auto-ap.routes :as routes] [auto-ap.effects :as effects] [bidi.bidi :as bidi])) (re-frame/reg-event-fx ::initialize-db (fn [{:keys [db]} [_ token]] (let [handler (:handler (bidi/match-route routes/routes (.. js/window -location -pathname)))] (if (and (not= :login handler) (not token)) {:redirect "/login" :db (assoc db/default-db :active-page :login :user token)} {:db (assoc db/default-db :active-page handler :user token) :http {:method :get :token token :uri (str "/api/companies") :on-success [::received-companies]}})))) (re-frame/reg-event-db ::toggle-menu (fn [db [_ which]] (update-in db [:menu which :active?] #(not %)))) (re-frame/reg-event-fx ::logged-in (fn [{:keys [db]} [_ token user]] {:http {:method :get :token token :uri (str "/api/companies") :on-success [::received-companies]} :db (assoc db :user (assoc user :token token))})) (re-frame/reg-event-db ::received-companies (fn [db [_ companies]] (assoc db :companies companies))) (re-frame/reg-event-db ::swap-company (fn [db [_ company]] (assoc db :company company))) (re-frame/reg-event-fx ::set-active-page (fn [{:keys [db]} [_ handler]] (if (and (not= :login handler) (not (:user db))) {:redirect "/login" :db (assoc db :active-page :login)} {:db (assoc db :active-page handler)}))) (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 :token (-> cofx :db :user) :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 :token (-> cofx :db :user) :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 :token (-> cofx :db :user) :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 :token (-> cofx :db :user) :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 ::logout (fn [{:keys [db]} [_]] {:db (assoc db :user nil :menu {:company {:active? false} :account {:active? false}}) :redirect (bidi/path-for routes/routes :login) :set-local-storage ["jwt" nil]})) (re-frame/reg-event-fx ::submit-new-invoice (fn [{:keys [db]} [_ invoice]] {:http {:method :post :token (-> db :user) :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)))