                                 {:id :user/isConsumer :label "Consumer"}]
                       :model (or (:permissions @new-user) #{})
                       :on-change #(swap! new-user assoc :permissions %)]]
-                    [button #(do (re-frame/dispatch [:add-user @new-user])
+                    [button #(do (>evt [:add-user @new-user])
                                  (reset! new-user {}))
                      "Add User" "Create a new user"]]]))))
 

   :disabled? true])

(defn history-panel []
  (let [user (re-frame/subscribe [:user])
        history (re-frame/subscribe [:history])
        csv (re-frame/subscribe [:history-csv])]
    (fn []
      (if @user
        (let [consumer? (:user/isConsumer @user)
              editor? (:user/isEditor @user)
              admin? (:user/isAdmin @user)]
          [re-com/v-box
           :gap title-gap
           :children [(panel-title "History")
                      [re-com/scroller
                       :scroll :auto
                       :child [history-table @history]]
                      (when admin?
                        [history-csv @csv])
                      [re-com/h-box
                       :children [(button [:get-history] "Refresh History" "Load history from server")]]]])
        [login-panel]))))

(defn formatted-schema [title schema-part only-dynamic?]
  [re-com/v-box

  (struct/valid? receipt complete-receipt))

(defn receipt-page []
  (let [receipt (<sub [:current-receipt])
        validation-errors (first (validate-receipt receipt))]
    [re-com/v-box
     :gap tight-gap
     :children
     [[labelled "Paid by"
       (:purchase/paymentMethod validation-errors)
       [dropdown :multiple? false
        :field-key :purchase/paymentMethod
        :subs-key :payment-methods
        :schema-id-key :paymentMethod/abbrev
        :schema-label-key :paymentMethod/name]]
      [labelled "Date"
       (:purchase/date validation-errors)
       [date-time-picker
        :model (or (:purchase/date receipt) (time/now))
        :on-change #(>evt [:edit-current-receipt :purchase/date %])]]
      [labelled "Price"
       (:purchase/price validation-errors)
       [re-com/input-text
        :width field-width
        :model (or (:purchase/price receipt) "0.00")
        :on-change #(>evt [:edit-current-receipt :purchase/price %])
        :change-on-blur? false
        :attr {:type "number"
               :step "0.01"}]]
      [labelled "Category"
       (:purchase/category validation-errors)
       [dropdown :multiple? false
        :field-key :purchase/category
        :subs-key :categories
        :schema-key :category/name]]
      [labelled "Vendor"
       (:purchase/vendor validation-errors)
       [dropdown :multiple? false
        :field-key :purchase/vendor
        :subs-key :vendors
        :schema-key :vendor/name
        :filter-fn #(some #{(:purchase/category receipt)} (:vendor/category %))]]
      [labelled "Comment"
       (:purchase/comment validation-errors)
       [re-com/input-text
        :width field-width
        :model (or (:purchase/comment receipt) "")
        :on-change #(>evt [:edit-current-receipt :purchase/comment %])
        :attr {:type "text"}]]
      [labelled "For Whom"
       (:purchase/forWhom validation-errors)
       [dropdown :multiple? true
        :field-key :purchase/forWhom
        :subs-key :users
        :filter-fn :user/isConsumer
        :schema-label-key :user/name
        :schema-id-key :user/abbrev]]
      [re-com/gap :size std-gap]
      [re-com/h-box
       :children
       [[re-com/button
         :disabled? (not (valid-receipt? receipt))
         :on-click #(>evt [:submit-receipt receipt])
         :label "Submit Receipt"]]]]]))

(defn home-panel []
  (if (:user/isEditor (<sub [:user]))
    [re-com/v-box
     :gap title-gap
     :children [(panel-title "New Receipt")
                [receipt-page]]]
    [login-panel]))

(defn add-user []
  (let [new-user (reagent/atom {})]
