Skip to content

Commit

Permalink
save/validate/receive filter state in workspaces; json->clj util
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew committed Jun 10, 2016
1 parent 365133f commit 200c407
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
1 change: 0 additions & 1 deletion resources/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

<!-- Google Analytics -->
<script>
console.log(ulysses.config.google_analytics_code_real);
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
ga('create', ulysses.config.google_analytics_code_real, 'auto');
ga('require', 'autotrack');
Expand Down
26 changes: 18 additions & 8 deletions src/cljs/ulysses/handlers.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
parse-int-id
str->int
clj->json
json->clj
to-id-map
find-by-id
remove-by-id
Expand Down Expand Up @@ -235,7 +236,9 @@
(dispatch [:workspace-faculty-request])
(if workspace-id-maybe
(if-let [target (find-by-id workspace-id-maybe (:workspaces db))]
(assoc db :builder-workspace target)
(-> db
(assoc :builder-workspace target)
(assoc :builder-filters (:filters target)))
; TODO get default workspace
db)
; TODO get default workspace
Expand All @@ -250,14 +253,20 @@
:grant-op-id :user-id
:updated-at :created-at
:faculties])
(update :filters
(fn [jstr]
(json->clj jstr :keywords)))
(update :faculties
(fn [faculties]
(map :id faculties))))]
(when-not dont-switch?
(dispatch [:workspace-switch (:id wbb)]))
(update db :workspaces
(fn [ws]
(merge-by-id ws [wbb]))))))
(map :id faculties))))
valid-filters? (-> wbb :filters validate-builder-filters)]
(if-not valid-filters? db
(do
(when-not dont-switch?
(dispatch [:workspace-switch (:id wbb)]))
(update db :workspaces
(fn [ws]
(merge-by-id ws [wbb]))))))))

(register-handler
:workspace-save
Expand All @@ -266,7 +275,8 @@
(request db
[:workspace (:id current)]
:method :put
:params {:faculties (clj->json (or (:faculties current) []))}
:params {:faculties (clj->json (or (:faculties current) []))
:filters (clj->json (:builder-filters db))}
:handler #(dispatch [:workspace-receive %]))
db)))

Expand Down
26 changes: 26 additions & 0 deletions src/cljs/ulysses/utils.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@
[a]
(.stringify js/JSON (clj->js a)))

(defn json->clj
"json string to clojure data. include :keywords to keywordize keys"
[s & args]
(-> (.parse js/JSON s)
(js->clj)
((if (some #{:keywords} args)
keywordize-keys identity))))

(defn url-encode-component
"urlencode"
[s]
Expand Down Expand Up @@ -328,6 +336,24 @@
[response]
(-> response :user :netid string/blank? not))

(defn validate-builder-filters
[{:keys [faculty-title-set faculty-years-uconn metrics :as filters]}]
(and
; general
(map? filters)
(= (-> filters keys set) #{:faculty-title-set :faculty-years-uconn :metrics})
; faculty title set
(set? faculty-title-set)
(every? string? faculty-title-set)
; faculty years uconn
(integer? faculty-years-uconn)
; metrics
(map? metrics)
(= (-> metrics keys set)
#{:publicationCount :grantCount :grantFunds
:recentGrantCount :recentGrantFunds})
(every? (every-pred integer? (partial <= 0) (partial >= 100)) (vals metrics))))

;; ----------------------------------------------------------------------------
;; data transforms
;; ----------------------------------------------------------------------------
Expand Down

0 comments on commit 200c407

Please sign in to comment.