From ca29a2bc2cdbe4c5c84fb110b24b39babc4a758d Mon Sep 17 00:00:00 2001 From: defclass Date: Wed, 9 Dec 2020 10:21:33 +0800 Subject: [PATCH] refactor(publish): fetch page list from logseq server --- src/main/frontend/components/publishing.cljs | 210 +++++++++---------- src/main/frontend/db.cljs | 13 -- src/main/frontend/handler/page.cljs | 21 ++ 3 files changed, 125 insertions(+), 119 deletions(-) diff --git a/src/main/frontend/components/publishing.cljs b/src/main/frontend/components/publishing.cljs index b00fcedc85..b3fb7fc514 100644 --- a/src/main/frontend/components/publishing.cljs +++ b/src/main/frontend/components/publishing.cljs @@ -16,68 +16,68 @@ [frontend.handler.project :as project-handler])) -(defn project - [editor-state current-project pages] - (rum/with-context [[t] i18n/*tongue-context*] - (if (= :display @editor-state) - (when current-project - [:div.cp__publishing-pj - [:span.cp__publishing-pj-name current-project] - [:span.cp__publishing-edit - {:on-click - (fn [_] - (reset! editor-state :editor))} - (t :publishing/edit)]]) - [:div.flex.cp__publishing_pj_edit - [:input#cp__publishing-project-input - {:placeholder current-project - :default-value current-project}] - [:div.cp__publishing-pj-bt - (ui/button - (t :publishing/save) - :on-click (fn [e] - (util/stop e) - (let [editor (.getElementById js/document "cp__publishing-project-input") - v (.-value editor) - data {:name v}] - (-> (p/let [result (project-handler/update-project current-project data)] +(rum/defcs project + < (rum/local :display ::project-state) + [state current-project pages] + (let [editor-state (get state ::project-state)] + (rum/with-context [[t] i18n/*tongue-context*] + (if (= :display @editor-state) + (when current-project + [:div.cp__publishing-pj + [:span.cp__publishing-pj-name current-project] + [:span.cp__publishing-edit + {:on-click + (fn [_] + (reset! editor-state :editor))} + (t :publishing/edit)]]) + [:div.flex.cp__publishing_pj_edit + [:input#cp__publishing-project-input + {:placeholder current-project + :default-value current-project}] + [:div.cp__publishing-pj-bt + (ui/button + (t :publishing/save) + :on-click (fn [e] + (util/stop e) + (let [editor (.getElementById js/document "cp__publishing-project-input") + v (.-value editor) + data {:name v}] + (-> (p/let [result (project-handler/update-project current-project data)] + (when (:result result) + (state/update-current-project :name v) + (notification/show! "Updated project name successfully." :success) + (reset! editor-state :display))) + (p/catch + (fn [error] + (notification/show! "Failed to update project name." :failed)))))) + :background "green")] + + [:div.cp__publishing-pj-bt + (ui/button + (t :publishing/delete) + :on-click (fn [e] + (util/stop e) + (let [confirm-message + (util/format + "This operation will delete all the published pages under the project \"%s\", are you sure?" + current-project)] + (when (.confirm js/window confirm-message) + (p/let [result (project-handler/delete-project current-project)] (when (:result result) - (state/update-current-project :name v) - (notification/show! "Updated project name successfully." :success) - (reset! editor-state :display))) - (p/catch - (fn [error] - (notification/show! "Failed to update project name." :failed)))))) - :background "green")] + (reset! editor-state :display) + (doseq [{:keys [title]} pages] + (page-handler/page-add-properties! title {:published false})) + (state/remove-current-project) + (notification/show! "The project was deleted successfully." :success)))))) + :background "red")] - [:div.cp__publishing-pj-bt - (ui/button - (t :publishing/delete) - :on-click (fn [e] - (util/stop e) - (let [confirm-message - (util/format - "This operation will delete all the published pages under the project \"%s\", are you sure?" - current-project)] - (when (.confirm js/window confirm-message) - (p/let [result (project-handler/delete-project current-project)] - (when (:result result) - (reset! editor-state :display) - (doseq [page pages] - (let [page (first page) - page-name (:page/name page)] - (page-handler/page-add-properties! page-name {:published false}))) - (state/remove-current-project) - (notification/show! "The project was deleted successfully." :success)))))) - :background "red")] - - [:div.cp__publishing-pj-bt - (ui/button - (t :publishing/cancel) - :on-click (fn [e] - (util/stop e) - (reset! editor-state :display)) - :background "pink")]]))) + [:div.cp__publishing-pj-bt + (ui/button + (t :publishing/cancel) + :on-click (fn [e] + (util/stop e) + (reset! editor-state :display)) + :background "pink")]])))) (rum/defcs my-publishing < rum/reactive db-mixins/query @@ -85,52 +85,50 @@ [state] (let [current-repo (state/sub :git/current-repo) projects (state/sub [:me :projects]) - pages (db/get-published-pages) - editor-state (get state ::project-state) current-project (project-handler/get-current-project current-repo projects)] - (rum/with-context [[t] i18n/*tongue-context*] - [:div.flex-1 - [:h1.title (t :my-publishing)] - [:div#cp__publishing-pj-ct - [:span (t :publishing/current-project)] - (project editor-state current-project pages)] - (when current-repo - [:div#cp__publishing-pg-ct - [:div (t :publishing/pages)] - [:table.table-auto - [:thead - [:tr - [:th (t :publishing/page-name)] - [:th (t :publishing/delete-from-logseq)]]] - [:tbody - (for [page pages] - (let [page (first page) - {:keys [title permalink]} (:page/properties page) - page-name (:page/name page)] - [:tr {:key permalink} - [:td [:div.flex {} - [:span [:a {:on-click (fn [e] (util/stop e)) - :href (rfe/href :page {:name title})} - page-name]] - [:span [:a {:href (util/format "%s/%s/%s" config/website current-project page-name) - :target "_blank"} - svg/external-link]]] - ] - [:td [:span.text-gray-500.text-sm - [:a {:on-click - (fn [e] - (util/stop e) - (-> (p/let [_ (page-handler/delete-page-from-logseq current-project permalink)] - (page-handler/update-state-and-notify page-name)) - (p/catch - (fn [error] - (let [status (.-status error) - not-found-on-server 404] - (if (= not-found-on-server status) - (page-handler/update-state-and-notify page-name) - (let [message (util/format "Failed to remove the page \"%s\" from Logseq" - page-name)] - (notification/show! message :failed))))))))} - (t :publishing/delete)]]]]))]]])]))) + (when current-repo + (p/let [_ (page-handler/get-page-list-by-project-name current-project)] + (let [publishing-pages (state/sub [:me :published-pages]) + pages (get publishing-pages current-repo)] + (rum/with-context [[t] i18n/*tongue-context*] + [:div.flex-1 + [:h1.title (t :my-publishing)] + [:div#cp__publishing-pj-ct + [:span (t :publishing/current-project)] + (project current-project pages)] + [:div#cp__publishing-pg-ct + [:div (t :publishing/pages)] + [:table.table-auto + [:thead + [:tr + [:th (t :publishing/page-name)] + [:th (t :publishing/delete-from-logseq)]]] + [:tbody + (for [{:keys [title permalink]} pages] + [:tr {:key permalink} + [:td [:div.flex {} + [:span [:a {:on-click (fn [e] (util/stop e)) + :href (rfe/href :page {:name title})} + title]] + [:span [:a {:href (util/format "%s/%s/%s" config/website current-project title) + :target "_blank"} + svg/external-link]]] + ] + [:td [:span.text-gray-500.text-sm + [:a {:on-click + (fn [e] + (util/stop e) + (-> (p/let [_ (page-handler/delete-page-from-logseq current-project permalink)] + (page-handler/update-state-and-notify title)) + (p/catch + (fn [error] + (let [status (.-status error) + not-found-on-server 404] + (if (= not-found-on-server status) + (page-handler/update-state-and-notify title) + (let [message (util/format "Failed to remove the page \"%s\" from Logseq" + title)] + (notification/show! message :failed))))))))} + (t :publishing/delete)]]]])]]]])))))) diff --git a/src/main/frontend/db.cljs b/src/main/frontend/db.cljs index c746cae92e..142aec3b98 100644 --- a/src/main/frontend/db.cljs +++ b/src/main/frontend/db.cljs @@ -786,19 +786,6 @@ (map :page/name) distinct)))) -(defn get-published-pages - [] - (when-let [repo (state/get-current-repo)] - (when (get-conn repo) - (->> (q repo [:page/published] {:use-cache? false} - '[:find (pull ?page [*]) - :in $ - :where - [?page :page/properties ?properties] - [(get ?properties :published) ?publish] - [(= "true" ?publish)]]) - react)))) - (defn get-files [repo] (when-let [conn (get-conn repo)] diff --git a/src/main/frontend/handler/page.cljs b/src/main/frontend/handler/page.cljs index 868e05a4e0..82a238f995 100644 --- a/src/main/frontend/handler/page.cljs +++ b/src/main/frontend/handler/page.cljs @@ -436,6 +436,27 @@ (log/error :page/http-delete-failed error) (reject error))))))) +(defn get-page-list-by-project-name + [project] + (js/Promise. + (fn [resolve _] + (if-not (string? project) + (resolve :project-name-is-invalid) + (let [url (util/format "%sprojects/%s/pages" config/api project)] + (util/fetch url + (fn [result] + (log/debug :page/get-page-list result) + (let [data (:result result)] + (if (sequential? data) + (do + (when-let [repo (state/get-current-repo)] + (state/set-state! [:me :published-pages repo] data)) + (resolve data)) + (log/error :page/http-get-list-result-malformed result)))) + (fn [error] + (log/error :page/http-get-list-failed error) + (resolve error)))))))) + (defn update-state-and-notify [page-name] (page-add-properties! page-name {:published false})