Created
August 22, 2019 08:53
-
-
Save aibrahim/a16155ca89b420c6d1db76dff623f31f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:progress-handler (fn [e] | |
(let [v (* 100 (/ (.-loaded e) (.-total e)))] | |
(rf/dispatch [:attachment/set-progress name v]))) | |
(rf/reg-event-db | |
:attachment/set-progress | |
(fn [db [_ file-name s]] | |
(assoc-in db [:attachments :progress file-name] s))) | |
(rf/reg-sub | |
:attachment/get-progress | |
(fn [db [_ fname]] | |
(get-in db [:attachments :progress fname]))) | |
;; view | |
(defn attachments-ui [] | |
[:div.col-lg-12 | |
[attachment-btn] | |
[attachment-form] | |
[:div {:class "row mt-4"} | |
[:hr] | |
[:table {:class "table table-responsive-md"} | |
[:thead | |
[:tr | |
[:th "Name"] | |
[:th "Size"] | |
[:th "Created at"] | |
[:th "#"]]] | |
(into | |
[:tbody] | |
(map | |
(fn [{:keys [file_name] :as t}] | |
(let [progress @(rf/subscribe [:attachment/get-progress file_name])] | |
(if (and ((comp not nil?) progress) (< progress 100)) | |
[progress-row file_name progress] | |
[attachment-row t]))) @(rf/subscribe [:attachments])))]]]) | |
(defn progress-row [file_name progress] | |
[:tr | |
[:td file_name] | |
[:td | |
(str "%" (gstring/format "%.1f" progress))] | |
[:td] | |
[:td]]) | |
(defn attachment-row [{:keys [id file_name file_url file_size created_at]}] | |
[:tr | |
[:td [:a {:href file_url} file_name]] | |
[:td file_size] | |
[:td (humanize-date created_at)] | |
[:td [:button {:class "btn btn-link btn-sm mr-3" | |
:on-click #(rf/dispatch [:delete-attachment id])} | |
[:i {:class "fas fa-times mr-2"}] "Remove"]]]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment