 (ns salava.page.ui.edit
   (:require [reagent.core :refer [atom cursor create-class]]
+            [reagent-modals.modals :as m]
             [salava.core.ui.ajax-utils :as ajax]
             [cljs-uuid-utils.core :refer [make-random-uuid uuid-string]]
             [salava.core.ui.layout :as layout]

(defn content [state]
  (let [{:keys [id name]} (:page @state)]
    [:div {:id "page-edit"}
     [ph/edit-page-header (t :page/Editpage ": " name)]
     [ph/edit-page-buttons id :content (fn [next-url] (save-page (:page @state) next-url))]
     [page-form state]]))

    (if file
      (update-block-value block-atom :files (conj (vec (:files @block-atom)) (assoc file :key (random-key)))))))

(defn send-file [files-atom block-atom]
  (let [file (-> (.querySelector js/document (str "#upload-file-" (:key @block-atom)))
                 .-files
                 (.item 0))
        form-data (doto
                    (js/FormData.)
                    (.append "file" file (.-name file)))]
    (m/modal! (file/upload-modal nil (t :file/Uploadingfile) (t :file/Uploadinprogress)))
    (ajax/POST
      "/obpv1/file/upload"
      {:body    form-data
       :handler (fn [{:keys [status message reason data]} response]
                  (when (= status "success")
                    (reset! files-atom (conj @files-atom data))
                    (select-file block-atom @files-atom (:id data)))
                  (m/modal! (file/upload-modal status message reason)))})))

(defn remove-file [files-atom file]
  (reset! files-atom (vec (remove #(= % file) @files-atom))))

