Commit 70085955 authored by Joachim Schunk's avatar Joachim Schunk

make modal-state-atom more local

parent f67e5df4
Pipeline #47832 passed with stages
in 2 minutes and 43 seconds
......@@ -23,8 +23,6 @@
[lernmeister.components.ui :as ui]
[reagent.core :as reagent]))
(def modal-state-atom (reagent/atom {}))
(defn get-new-calculation [with-units]
(let [base-object {:id (get-unique-id "ae")
:name nil
......@@ -51,7 +49,7 @@
:correct int-part
:incorrect (str "-" int-part))))))
(defn show-calculation [{:keys [exercise-core index calculation change-fn delete-fn]}]
(defn show-calculation [{:keys [exercise-core index calculation change-fn delete-fn modal-state-atom]}]
(let [calculations (:calculations exercise-core)
base-path [:core :calculations index]
answer (into {}
......@@ -101,7 +99,7 @@
:do-render true
:modal-state-atom modal-state-atom}]]]]))
(defn new-exercise-form [{:keys [exercise-atom change-fn]}]
(defn new-exercise-form [{:keys [exercise-atom change-fn modal-state-atom]}]
(let [exercise @exercise-atom
exercise-core (:core exercise)
with-units (:with-units exercise-core)
......@@ -121,7 +119,8 @@
:index index
:calculation calculation
:change-fn change-fn
:delete-fn #(change-fn [:core :calculations] (vec-remove calculations %))}])
:delete-fn #(change-fn [:core :calculations] (vec-remove calculations %))
:modal-state-atom modal-state-atom}])
calculations)]]
[ui/modal-panel
{:state (reagent/cursor modal-state-atom [:modal-visible?])
......@@ -198,11 +197,13 @@
exercise-scheme-version)))))}))
(defn additional-forms [exercise-atom]
(when (= :current (get-in @exercise-atom [:authoring :migration-status]))
[new-exercise-form {:exercise-atom exercise-atom
:change-fn (partial calc-change-fn
(fn [exercise] (reset! exercise-atom exercise))
@exercise-atom)}]))
(reagent/with-let [modal-state-atom (reagent/atom {})]
(when (= :current (get-in @exercise-atom [:authoring :migration-status]))
[new-exercise-form {:exercise-atom exercise-atom
:change-fn (partial calc-change-fn
(fn [exercise] (reset! exercise-atom exercise))
@exercise-atom)
:modal-state-atom modal-state-atom}])))
(defmethod e-edit/show-settings :calculation [_ _]
(fn [exercise-atom _]
......
......@@ -13,10 +13,12 @@
[lernmeister.components.exercise-types.calchelper
:refer
[build-latex-string calc-change-fn]]
[lernmeister.components.exercise-types.calculation.check
:refer
[get-calculation-ids]]
[lernmeister.components.exercise-types.calculation.migrate-answer
:refer
[answer-scheme-version migrate-answer]]
[lernmeister.components.exercise-types.calculation.check :refer [get-calculation-ids]]
[lernmeister.components.exercise-types.multistep-calculation.check
:refer
[number-obj]]
......@@ -29,8 +31,6 @@
[lernmeister.components.ui :as ui]
[reagent.core :as reagent]))
(def modal-state-atom (reagent/atom {}))
(defn get-new-calculation [with-units]
(let [base-object {:number number-obj}]
(if with-units (assoc base-object :units [])
......@@ -93,7 +93,7 @@
[:span.icon [:i.fas.fa-edit]]
[:span "Einheit"]]])]])]]))
(defn calculations [{:keys [exercise-core result answer change-fn]}]
(defn calculations [{:keys [exercise-core result answer change-fn modal-state-atom]}]
(let [calculations (:calculations exercise-core)]
(if (pos? (count calculations))
[:<>
......@@ -111,7 +111,7 @@
calculations)]
[:span "No calculations available"])))
(defn print-calculation-exercise [{:keys [answer exercise change-fn result]}]
(defn print-calculation-exercise [{:keys [answer exercise change-fn result modal-state-atom]}]
[:<>
[ui/card
{:title (str (:title exercise))}
......@@ -121,11 +121,11 @@
[calculations {:exercise-core (:core exercise)
:result result
:answer answer
:change-fn change-fn}]
:change-fn change-fn
:modal-state-atom modal-state-atom}]
(when result
[:div.has-text-right {:class (get-result-color-class result)}
[point-result {:result result}]])]]
[ui/modal-panel
{:state (reagent/cursor modal-state-atom [:modal-visible?])
:title [modal-renderers/editor-heading {:text "Größe editieren"
......@@ -145,16 +145,18 @@
:collats-fn (:collats-fn @modal-state-atom)}]]]])
(defn exercise-renderer [exercise {:keys [result answer on-change]}]
(let [calculation-ids (get-calculation-ids exercise)
ans-obj (or answer (fresh-answer-obj calculation-ids (get-in exercise [:core :with-units])))]
(if-let [migrated-answer (migrate-if-necessary ans-obj :answer-scheme "calculation" answer-scheme-version
(partial migrate-answer calculation-ids))]
[print-calculation-exercise
{:answer migrated-answer
:exercise exercise
:change-fn (partial calc-change-fn on-change migrated-answer)
:result result}]
[unmigrateable-answer])))
(reagent/with-let [modal-state-atom (reagent/atom {})]
(let [calculation-ids (get-calculation-ids exercise)
ans-obj (or answer (fresh-answer-obj calculation-ids (get-in exercise [:core :with-units])))]
(if-let [migrated-answer (migrate-if-necessary ans-obj :answer-scheme "calculation" answer-scheme-version
(partial migrate-answer calculation-ids))]
[print-calculation-exercise
{:answer migrated-answer
:exercise exercise
:change-fn (partial calc-change-fn on-change migrated-answer)
:result result
:modal-state-atom modal-state-atom}]
[unmigrateable-answer]))))
(defmethod ce-ex/render-exercise :calculation [_ _]
(fn [exercise data]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment