renamed company to client.

This commit is contained in:
Bryce Covert
2019-01-18 07:44:12 -08:00
parent 583752d740
commit 775150131e
38 changed files with 250 additions and 306 deletions

View File

@@ -0,0 +1,233 @@
(ns auto-ap.views.pages.admin.clients
(:require-macros [cljs.core.async.macros :refer [go]])
(:require [re-frame.core :as re-frame]
[reagent.core :as reagent]
[auto-ap.subs :as subs]
[auto-ap.events :as events]
[auto-ap.entities.clients :as entity]
[auto-ap.views.components.address :refer [address-field]]
[auto-ap.views.utils :refer [login-url dispatch-event dispatch-value-change bind-field horizontal-field]]
[auto-ap.views.components.modal :refer [action-modal]]
[cljs.reader :as edn]
[auto-ap.routes :as routes]
[bidi.bidi :as bidi]))
(re-frame/reg-event-fx
::edit
(fn [{:keys [db]} [_ client-id]]
{:dispatch [::events/modal-status :auto-ap.views.pages.admin.clients/edit {:visible? true}]
:db (assoc-in db [:admin :client]
(get (:clients db) client-id))}))
(re-frame/reg-event-fx
::save
(fn [{:keys [db]} _]
(let [edited-client (-> (get-in db [:admin :client])
(dissoc :location)
(dissoc :new-account))]
{:db (assoc-in db [:admin :client :saving?] true)
:http {:method :put
:token (:user db)
:body (pr-str edited-client)
:headers {"Content-Type" "application/edn"}
:uri (str "/api/clients/" (:id edited-client))
:on-success [::save-complete]
:on-error [::save-error]}})))
(re-frame/reg-event-fx
::save-complete
(fn [{:keys [db]} [_ client]]
{:dispatch [::events/modal-completed :auto-ap.views.pages.admin.clients/edit]
:db (-> db
(assoc-in [:admin :client] nil)
(assoc-in [:clients (:id client)] client))}))
(re-frame/reg-event-db
::save-error
(fn [db [_ client]]
(-> db
(assoc-in [:admin :client :saving?] false)
(assoc-in [:admin :client :error] true))))
(re-frame/reg-event-db
::change
(fn [db [_ path value]]
(assoc-in db (concat [:admin :client] path)
value)))
(re-frame/reg-event-fx
::add-location
(fn [{:keys [db]} _]
(let [client (:client @(re-frame/subscribe [::subs/admin]))]
{:db (-> db
(update-in [:admin :client :locations] conj (:location client))
(update-in [:admin :client] dissoc :location))})))
(re-frame/reg-event-fx
::add-new-bank-account
(fn [{:keys [db]} _]
(let [client (:client @(re-frame/subscribe [::subs/admin]))
new-bank-account (:new-account client)
new-bank-account (-> new-bank-account
(update :check-number #(if (seq %) (js/parseInt %) nil))
(update :yodlee-account-id #(if (seq %) (js/parseInt %) nil))
(assoc :is-new? true))]
{:db (-> db
(update-in [:admin :client :new-bank-accounts] (fn [bank-accounts]
(conj bank-accounts new-bank-account)))
(update-in [:admin :client] dissoc :new-account))})))
(re-frame/reg-event-db
::remove-new-bank-account
(fn [db [_ index]]
(update-in db [:admin :client :new-bank-accounts]
(fn [bas]
(vec (concat (take index bas)
(drop (inc index) bas)))))))
(defn clients-table []
(let [clients (re-frame/subscribe [::subs/clients])
editing-client (:client @(re-frame/subscribe [::subs/admin]))]
[:table {:class "table", :style {:width "100%"}}
[:thead
[:tr
[:th "Name"]
[:th "Email"]]]
[:tbody (for [{:keys [id name email] :as c} @clients]
^{:key (str name "-" id )}
[:tr {:on-click (fn [] (re-frame/dispatch [::edit id]))
:style {"cursor" "pointer"}}
[:td name]
[:td email]])]]))
(defn admin-clients-page []
[:div
(let [clients (re-frame/subscribe [::subs/clients])
editing-client (:client @(re-frame/subscribe [::subs/admin]))]
[:div
[:h1.title "Clients"]
[clients-table]
[action-modal {:id ::edit
:title (str "Edit " (:name editing-client))
:action-text "Save"
:save-event [::save]}
[horizontal-field
[:label.label "Name"]
[:div.control
[bind-field
[:input.input {:type "text"
:field :name
:spec ::entity/name
:event ::change
:subscription editing-client}]]]]
[horizontal-field
[:label.label "Email"]
[:div.control
[bind-field
[:input.input {:type "email"
:field :email
:spec ::entity/name
:event ::change
:subscription editing-client}]]]]
[horizontal-field
[:label.label "Locations"]
[:div.control
[:div.field.has-addons
[:p.control
[bind-field
[:input.input {:type "text"
:field :location
:event ::change
:subscription editing-client}]]]
[:p.control [:button.button.is-primary {:on-click (dispatch-event [::add-location])} "Add"]]]
[:ul
(for [location (:locations editing-client)]
^{:key location} [:li location ])]]]
[:h2.subtitle "Address"]
[address-field {:field [:address]
:event ::change
:subscription editing-client}]
[:h2.subtitle "Add account"]
[horizontal-field
[:label.label "Bank"]
[:div.control
[bind-field
[:input.input {:placeholder "Bank Name"
:type "text"
:field [:new-account :bank-name]
:event ::change
:subscription editing-client}]]]
[:div.control
[bind-field
[:input.input {:placeholder "Routing"
:type "text"
:field [:new-account :routing]
:event ::change
:subscription editing-client}]]]
[:div.control
[bind-field
[:input.input {:placeholder "Code"
:type "text"
:field [:new-account :bank-code]
:event ::change
:subscription editing-client}]]]]
[horizontal-field
[:label.label "Account"]
[:div.control
[bind-field
[:input.input {:placeholder "Nickname"
:type "text"
:field [:new-account :name]
:event ::change
:subscription editing-client}]]]
[:div.control
[bind-field
[:input.input {:placeholder "Acct #"
:type "text"
:field [:new-account :number]
:event ::change
:subscription editing-client}]]]
[:div.control
[bind-field
[:input.input {:placeholder "Check #"
:type "text"
:field [:new-account :check-number]
:event ::change
:subscription editing-client}]]]]
[horizontal-field
[:label.label "Yodlee Account"]
[:div.control
[bind-field
[:input.input {:placeholder "Yodlee Account #"
:type "text"
:field [:new-account :yodlee-account-id]
:event ::change
:subscription editing-client}]]]
[:div.control
[:button.button.is-primary.is-pulled-right {:on-click (dispatch-event [::add-new-bank-account])} "Add"]]]
[:h2.subtitle "Bank Accounts"]
[horizontal-field
nil
[:div.control
[:ul
(for [{:keys [name number check-number id]} (:bank-accounts editing-client)]
^{:key id} [:li name])
(for [[index {:keys [name number check-number]}] (map vector (range) (:new-bank-accounts editing-client))]
^{:key index} [:li [:strong "* " name] [:button.button {:on-click (dispatch-event [::remove-new-bank-account index])} [:span.icon [:i.fa.fa-times]]]])]]]
(when (:saving? editing-client) [:div.is-overlay {:style {"backgroundColor" "rgba(150,150,150, 0.5)"}}])]])])