Skip to content

Commit

Permalink
Prefer reg-event-db in re-frame event handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
neenaoffline committed Dec 19, 2023
1 parent 2b01fac commit b22d037
Showing 1 changed file with 35 additions and 35 deletions.
70 changes: 35 additions & 35 deletions src/bean/ui/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,37 @@
(fn [_ _]
(db/initial-app-db)))

;; TODO: think most of these should be reg-event-db instead of reg-event-fx
(rf/reg-event-fx
(rf/reg-event-db
::update-code
(fn update-code [{:keys [db]} [_ code]]
{:db (update-in db [:sheet] #(-> %
(code/set-code code)
(assoc-in [:ui :code-evaluation-state] :pending)))}))
(fn update-code [db [_ code]]
(update-in db [:sheet] #(-> %
(code/set-code code)
(assoc-in [:ui :code-evaluation-state] :pending)))))

(rf/reg-event-fx
(rf/reg-event-db
::evaluate-code
(fn evaluate-code [{:keys [db]} _]
{:db (-> db
(update-in [:sheet] code/reevaluate)
(assoc-in [:sheet :ui :code-evaluation-state]
(if (code-errors/get-error (:sheet db))
:error
:evaluated)))}))
(fn evaluate-code [db _]
(-> db
(update-in [:sheet] code/reevaluate)
(assoc-in [:sheet :ui :code-evaluation-state]
(if (code-errors/get-error (:sheet db))
:error
:evaluated)))))

(rf/reg-event-fx
(rf/reg-event-db
::update-cell
(fn update-cell [{:keys [db]} [_ address content]]
{:db (update-in db [:sheet] #(grid/eval-cell address % content))}))
(fn update-cell [db [_ address content]]
(update-in db [:sheet] #(grid/eval-cell address % content))))

(rf/reg-event-fx
(rf/reg-event-db
::resize-row
(fn resize-row [{:keys [db]} [_ row height]]
{:db (assoc-in db [:sheet :ui :row-heights row] height)}))
(fn resize-row [db [_ row height]]
(assoc-in db [:sheet :ui :row-heights row] height)))

(rf/reg-event-fx
(rf/reg-event-db
::resize-col
(fn resize-col [{:keys [db]} [_ col width]]
{:db (assoc-in db [:sheet :ui :col-widths col] width)}))
(fn resize-col [db [_ col width]]
(assoc-in db [:sheet :ui :col-widths col] width)))

(rf/reg-fx
::focus-cell
Expand All @@ -55,13 +54,14 @@
(rf/reg-event-fx
::set-mode
(fn set-mode [{:keys [db]} [_ [r c] mode]]
(merge
{:db (assoc-in db [:sheet :grid r c :mode] mode)}
(when (= mode :edit) {::focus-cell [r c]}))))
(case mode
:view {:db (assoc-in db [:sheet :grid r c :mode] mode)}
:edit {:db (assoc-in db [:sheet :grid r c :mode] mode)
::focus-cell [r c]})))

(rf/reg-event-fx
::edit-mode
(fn edit-mode [{:keys [db]} [_ [r c]]]
(fn edit-mode [_ [_ [r c]]]
{:fx [[:dispatch [::set-mode [r c] :edit]]]}))

(rf/reg-event-db
Expand All @@ -84,14 +84,14 @@
(fn [db [_]]
(assoc-in db [:ui :selections] [])))

(rf/reg-event-fx
(rf/reg-event-db
::explain
(fn explain [{:keys [db]} [_ expression]]
{:db (assoc-in db
[:ui :provenance]
(provenance/sentence-proof expression (:sheet db)))}))
(fn explain [db [_ expression]]
(assoc-in db
[:ui :provenance]
(provenance/sentence-proof expression (:sheet db)))))

(rf/reg-event-fx
(rf/reg-event-db
::display-help
(fn display-help [{:keys [db]} [_ flag]]
{:db (assoc-in db [:ui :help-display] flag)}))
(fn display-help [db [_ flag]]
(assoc-in db [:ui :help-display] flag)))

0 comments on commit b22d037

Please sign in to comment.