Skip to content

Commit

Permalink
Fix handling of active-editor value (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
katauber committed Dec 16, 2021
1 parent 43c8413 commit 1437a6a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
39 changes: 22 additions & 17 deletions src/cljs/metafacture_playground/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@
(mapv
(fn [sub-sub-key]
{:name (->storage-key [k sub-key sub-sub-key])
:value (get-in db [k sub-key sub-sub-key])})
:value (let [value (get-in db [k sub-key sub-sub-key])]
(if (keyword? value)
(name value)
value))})
(-> (get rest-db sub-key) keys)))
(keys rest-db))))
[]
Expand Down Expand Up @@ -162,7 +165,7 @@
(fn [editor]
[::edit-input-value editor (get sample editor "")])
[:data :flux :fix :morph])
[::switch-editor active-editor])}))
[::switch-editor (:active-editor sample)])})

(re-frame/reg-event-fx
::load-sample
Expand Down Expand Up @@ -230,16 +233,18 @@

;;; Share links

(defn- get-used-params [flux fix morph]
(defn- get-used-params [flux fix morph data]
(if flux
(let [flux (-> flux
(clj-str/replace "\n|" "|")
(clj-str/replace "\\s?\\|\\s?" "|"))
fix-in-flux? (re-find #"\|fix\|" flux)
morph-in-flux? (re-find #"\|morph\|" flux)]
(merge
(when fix-in-flux? {:fix fix})
(when morph-in-flux? {:morph morph})))
morph-in-flux? (re-find #"\|morph\|" flux)
data-in-flux? (re-find #"PG_DATA" flux)]
(cond-> {}
fix-in-flux? (merge {:fix fix})
morph-in-flux? (merge {:morph morph})
data-in-flux? (merge {:data data})))
{}))

(defn- count-query-string [params]
Expand Down Expand Up @@ -271,14 +276,13 @@
[{db :db} [_ url data flux fix morph active-editor]]
(let [max-query-string 1536
max-url-string 2048
api-call-params (merge (when data {:data data})
(when flux {:flux flux})
(get-used-params flux fix morph))
workflow-params (merge (when data {:data data})
(when flux {:flux flux})
(when fix {:fix fix})
(when morph {:morph morph})
{:active-editor (name active-editor)})
api-call-params (when flux
(merge
{:flux flux}
(get-used-params flux fix morph data)))
workflow-params (merge api-call-params
(when active-editor
{:active-editor (name active-editor)}))
api-call-query-string-too-long? (query-string-too-long? api-call-params max-query-string)
workflow-query-string-too-long? (query-string-too-long? workflow-params max-query-string)
api-call-link (when (and api-call-params (not api-call-query-string-too-long?))
Expand Down Expand Up @@ -343,8 +347,9 @@

(defn process
[{db :db} [_ data flux fix morph active-editor]]
(let [active-editor-in-flux? (re-find (re-pattern (str "\\|(\\s|\\n)*" (name active-editor) "(\\s|\\n)*\\|")) (or flux ""))
message (when-not active-editor-in-flux?
(let [active-editor-in-flux? (when (and active-editor flux)
(re-find (re-pattern (str "\\|(\\s|\\n)*" (name active-editor) "(\\s|\\n)*\\|")) flux))
message (when (and active-editor (not active-editor-in-flux?))
(str "Flux does not use selected " (name active-editor) "."))]
{:fetch {:method :post
:url "process"
Expand Down
8 changes: 7 additions & 1 deletion src/cljs/metafacture_playground/utils.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@
[lambdaisland.uri :refer [uri query-string->map]]))

(defn parse-url [href]
(-> href uri :query query-string->map))
(let [query-params (-> href
uri
:query
query-string->map)]
(if (:active-editor query-params)
(update query-params :active-editor keyword)
query-params)))
15 changes: 7 additions & 8 deletions test/cljs/metafacture_playground/event_handler_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
(def empty-db
(-> (events/initialize-db {:db {}
:event [::events/initialize-db]})
(dissoc :dispatch)))
(dissoc :dispatch :dispatch-n :storage/set :metafacture-playground.effects/unset-url-query-params)))

; db with one input-field not empty
(def db1
Expand All @@ -43,9 +43,6 @@
(events/edit-value [:edit-value :flux (:flux sample-data)])
(events/edit-value [:edit-value :fix (:fix sample-data)])))

(def db-with-sample
{:db {:input-fields sample-fields}})

(deftest initialize-db
(testing "Test initializing of db without values"
(is (= empty-db {:db db/default-db})))
Expand Down Expand Up @@ -161,7 +158,9 @@
(deftest generate-links-test
(testing "Test generating share links"
(let [db' (-> empty-db
(events/load-sample [:load-sample sample-data]))
(events/edit-value [:edit-value :data (:data sample-data)])
(events/edit-value [:edit-value :fix (:fix sample-data)])
(events/edit-value [:edit-value :flux (:flux sample-data)]))
data (get-in db' [:db :input-fields :data :content])
fix (get-in db' [:db :input-fields :fix :content])
flux (get-in db' [:db :input-fields :flux :content])
Expand All @@ -182,10 +181,10 @@

(testing "Test not generating links if parameters are too long"
(let [db' (-> empty-db
(events/edit-value [:edit-value :data (generate-random-string 1537)]))
data (get-in db' [:db :input-fields :data :content])
(events/edit-value [:edit-value :flux (generate-random-string 1537)]))
flux (get-in db' [:db :input-fields :flux :content])
db'' (-> db'
(events/generate-links [:generate-links "http://test.metafacture.org/playground/" data nil nil nil :fix])
(events/generate-links [:generate-links "http://test.metafacture.org/playground/" nil flux nil nil :fix])
:db)]
(and (is (= (get-in db'' [:message :content]) "Share links for large workflows are not supported yet"))
(is (nil? (get-in db'' [:links :api-call])))
Expand Down

0 comments on commit 1437a6a

Please sign in to comment.