From ac27f95091857cc48c87251bb1746cce26734c8e Mon Sep 17 00:00:00 2001 From: Florian Schroedl Date: Thu, 1 Aug 2024 09:41:28 +0200 Subject: [PATCH] Fix undo deleting the token on update --- common/src/app/common/files/changes_builder.cljc | 10 +++++----- frontend/src/app/main/data/tokens.cljs | 12 ++++++++---- frontend/src/app/main/ui/workspace/tokens/form.cljs | 12 ++++++------ 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/common/src/app/common/files/changes_builder.cljc b/common/src/app/common/files/changes_builder.cljc index c5f28cb9b..831e47433 100644 --- a/common/src/app/common/files/changes_builder.cljc +++ b/common/src/app/common/files/changes_builder.cljc @@ -672,11 +672,11 @@ (apply-changes-local))) (defn update-token - [changes token] - (let [token-id (:id token)] - (-> changes - (update :redo-changes conj {:type :mod-token :id token-id :token token}) - (apply-changes-local)))) + [changes {:keys [id] :as token} prev-token] + (-> changes + (update :redo-changes conj {:type :mod-token :id id :token token}) + (update :undo-changes conj {:type :mod-token :id id :token (or prev-token token)}) + (apply-changes-local))) (defn delete-token [changes token-id] diff --git a/frontend/src/app/main/data/tokens.cljs b/frontend/src/app/main/data/tokens.cljs index e0b827518..f83ce1507 100644 --- a/frontend/src/app/main/data/tokens.cljs +++ b/frontend/src/app/main/data/tokens.cljs @@ -95,14 +95,18 @@ (let [workspace-data (deref refs/workspace-data)] (get (:tokens workspace-data) id))) -(defn add-token +(defn update-create-token [token] (let [token (update token :id #(or % (uuid/next)))] (ptk/reify ::add-token ptk/WatchEvent (watch [it _ _] - (let [changes (-> (pcb/empty-changes it) - (pcb/add-token token))] + (let [prev-token (get-token-data-from-token-id (:id token)) + changes (if prev-token + (-> (pcb/empty-changes it) + (pcb/update-token token prev-token)) + (-> (pcb/empty-changes it) + (pcb/add-token token)))] (rx/of (dch/commit-changes changes))))))) (defn delete-token @@ -122,7 +126,7 @@ (let [new-token (-> (get-token-data-from-token-id id) (dissoc :id) (update :name #(str/concat % "-copy")))] - (add-token new-token))) + (update-create-token new-token))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; TEMP (Move to test) diff --git a/frontend/src/app/main/ui/workspace/tokens/form.cljs b/frontend/src/app/main/ui/workspace/tokens/form.cljs index 7db3cd1d8..73b50a8fd 100644 --- a/frontend/src/app/main/ui/workspace/tokens/form.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/form.cljs @@ -241,12 +241,12 @@ Token names should only contain letters and digits separated by . characters.")} ;; The result should be a vector of all resolved validations ;; We do not handle the error case as it will be handled by the components validations (when (and (seq result) (not err)) - (let [token (cond-> {:name final-name - :type (or (:type token) token-type) - :value final-value} - final-description (assoc :description final-description) - (:id token) (assoc :id (:id token)))] - (st/emit! (dt/add-token token)) + (let [new-token (cond-> {:name final-name + :type (or (:type token) token-type) + :value final-value} + final-description (assoc :description final-description) + (:id token) (assoc :id (:id token)))] + (st/emit! (dt/update-create-token new-token)) (st/emit! (wtu/update-workspace-tokens-event)) (modal/hide!)))))))))] [:form