Skip to content

Commit

Permalink
Add export
Browse files Browse the repository at this point in the history
  • Loading branch information
lgessler committed Jan 3, 2022
1 parent 9789168 commit f48bb33
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 38 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@material-ui/lab": "^4.0.0-alpha.56",
"fast-diff": "^1.2.0",
"highlight.js": "^10.7.3",
"json-beautify": "^1.1.1",
"karma": "^6.3.4",
"karma-chrome-launcher": "^3.1.0",
"karma-cljs-test": "^0.1.0",
Expand Down
111 changes: 73 additions & 38 deletions src/main/glam/client/ui/document/settings.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,40 @@
[com.fulcrologic.fulcro.mutations :as m]
[com.fulcrologic.fulcro.dom :as dom]
[com.fulcrologic.fulcro.mutations :as m]
[glam.client.router :as r]
[com.fulcrologic.fulcro.algorithms.form-state :as fs]
[com.fulcrologic.fulcro.ui-state-machines :as uism]
[com.fulcrologic.fulcro.data-fetch :as df]
[taoensso.timbre :as log]
[glam.client.router :as r]
[glam.models.document :as doc]
[glam.client.ui.material-ui :as mui]
[glam.client.ui.common.forms :as forms]
[glam.client.ui.document.token-editor :refer [Token ui-token]]
[com.fulcrologic.fulcro.algorithms.form-state :as fs]
[com.fulcrologic.fulcro.ui-state-machines :as uism]))
[glam.client.ui.material-ui-icon :as muic]
["json-beautify" :as beautify]))

(defsc ProjectQuery [this _]
{:query [:project/id :project/name]
:ident :project/id})

(defsc Settings [this {:document/keys [id name project] :ui/keys [busy?] :as props}]
(def data-query
[:document/id
:document/name
{:document/text-layers
[:text-layer/name
:text-layer/id
{:text-layer/text [:text/id :text/body :text/document]}
{:text-layer/token-layers
[:token-layer/name
{:token-layer/tokens [:token/id :token/text :token/begin :token/end :token/layer :token/value]}
{:token-layer/span-layers
[:span-layer/id :span-layer/name
{:span-layer/spans [:span/id :span/value :span/layer]}]}]}]}])

(defsc Settings [this {:document/keys [id name project] :>/keys [data] :ui/keys [busy?] :as props}]
{:query [:document/id :document/name fs/form-config-join :ui/busy?
{:document/project (c/get-query ProjectQuery)}]
{:document/project (c/get-query ProjectQuery)}
{:>/data data-query}]
:ident :document/id
:pre-merge (fn [{:keys [data-tree]}]
(merge {:ui/busy? false}
Expand All @@ -29,40 +47,57 @@
::forms/delete-mutation 'glam.models.document/delete-document
::forms/delete-message "Document deleted"
:form-fields #{:document/name}}
(log/info data)
(let [dirty (fs/dirty? props)]
(log/info props)
(mui/container {:maxWidth "md"}
(dom/form
{:onSubmit (fn [e]
(.preventDefault e)
(uism/trigger! this ::settings :event/save)
(c/transact! this [(fs/clear-complete! {:entity-ident [:document/id id]})]))}
(mui/vertical-grid
{:spacing 2}
(forms/text-input-with-label this :document/name "Name" "Must have 1 to 80 characters"
{:fullWidth true
:onChange (fn [e]
(m/set-string!! this :document/name :event e)
(c/transact! this [(fs/mark-complete! {:entity-ident [:document/id id]
:field :document/name})]))
:disabled busy?})
(forms/form-buttons
{:component this
:validator doc/validator
:props props
:busy? busy?
:submit-token "Save Document"
:reset-token "Discard Changes"
:on-reset (fn []
(uism/trigger! this ::settings :event/reset)
(c/transact! this [(fs/clear-complete! {:entity-ident [:document/id id]})]))
:on-delete (fn []
(uism/trigger! this ::settings :event/delete)
(r/route-to! :project {:id (:project/id project)}))
:submit-disabled (or (not dirty)
(not= :valid (doc/validator props))
(not (fs/checked? props))
busy?)
:reset-disabled (not (and dirty (not busy?)))}))))))
(mui/vertical-grid {:spacing 3}
(dom/form
{:onSubmit (fn [e]
(.preventDefault e)
(uism/trigger! this ::settings :event/save)
(c/transact! this [(fs/clear-complete! {:entity-ident [:document/id id]})]))}
(mui/vertical-grid
{:spacing 2}
(forms/text-input-with-label this :document/name "Name" "Must have 1 to 80 characters"
{:fullWidth true
:onChange (fn [e]
(m/set-string!! this :document/name :event e)
(c/transact! this [(fs/mark-complete! {:entity-ident [:document/id id]
:field :document/name})]))
:disabled busy?})
(forms/form-buttons
{:component this
:validator doc/validator
:props props
:busy? busy?
:submit-token "Save Document"
:reset-token "Discard Changes"
:on-reset (fn []
(uism/trigger! this ::settings :event/reset)
(c/transact! this [(fs/clear-complete! {:entity-ident [:document/id id]})]))
:on-delete (fn []
(uism/trigger! this ::settings :event/delete)
(r/route-to! :project {:id (:project/id project)}))
:submit-disabled (or (not dirty)
(not= :valid (doc/validator props))
(not (fs/checked? props))
busy?)
:reset-disabled (not (and dirty (not busy?)))})))
(mui/button {:type "submit"
:size "large"
:disabled busy?
:color "secondary"
:variant "contained"
:startIcon (muic/cloud-download)
:onClick (fn [e]
(.preventDefault e)
(let [data (beautify (clj->js data) nil 2 80)
blob (js/Blob. [data] #js {:type "text/json"})
link (.createElement js/document "a")]
(set! (.-download link) (str name ".json"))
(set! (.-href link) (js/window.URL.createObjectURL blob))
(.click link)
(.remove link)))}
"Export")))))

(def ui-settings (c/factory Settings))
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,11 @@ isexe@^2.0.0:
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
integrity sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==

json-beautify@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/json-beautify/-/json-beautify-1.1.1.tgz#8a1ed511ad5d52ca63ed29f7c61896c6a6ebbb9f"
integrity sha512-17j+Hk2lado0xqKtUcyAjK0AtoHnPSIgktWRsEXgdFQFG9UnaGw6CHa0J7xsvulxRpFl6CrkDFHght1p5ZJc4A==

json-parse-better-errors@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/json-parse-better-errors/-/json-parse-better-errors-1.0.2.tgz#bb867cfb3450e69107c131d1c514bab3dc8bcaa9"
Expand Down

0 comments on commit f48bb33

Please sign in to comment.