From e97e4cbb5ad5ade55d4766527368f643d4d10405 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Thu, 24 Mar 2016 12:43:14 +0200 Subject: [PATCH] Basic backend integration for page history fetching. --- src/uxbox/data/pages.cljs | 46 ++++++++ src/uxbox/repo/pages.cljs | 5 + src/uxbox/ui/workspace/sidebar.cljs | 4 +- .../{document_history.cljs => history.cljs} | 108 ++++++++---------- src/uxbox/ui/workspace/sidebar/sitemap.cljs | 2 +- 5 files changed, 100 insertions(+), 65 deletions(-) rename src/uxbox/ui/workspace/sidebar/{document_history.cljs => history.cljs} (54%) diff --git a/src/uxbox/data/pages.cljs b/src/uxbox/data/pages.cljs index 2a42d1a75..bb831644b 100644 --- a/src/uxbox/data/pages.cljs +++ b/src/uxbox/data/pages.cljs @@ -164,3 +164,49 @@ (defn delete-page [id] (DeletePage. id)) + +;; --- Page History Fetched + +(defrecord PageHistoryFetched [history] + rs/UpdateEvent + (-apply-update [_ state] + (-> state + (assoc-in [:workspace :history :items] history) + (assoc-in [:workspace :history :selected] nil)))) + +;; --- Fetch Page History + +(defrecord FetchPageHistory [id] + rs/WatchEvent + (-apply-watch [_ state s] + (println "FetchPageHistory" id) + (letfn [(on-success [{history :payload}] + (->PageHistoryFetched history)) + (on-failure [e] + (uum/error (tr "errors.fetch-page-history")) + (rx/empty))] + (->> (rp/do :fetch/page-history {:page id}) + (rx/map on-success) + (rx/catch on-failure)))) + + rs/EffectEvent + (-apply-effect [_ state] + )) + +(defn fetch-page-history + [id] + (FetchPageHistory. id)) + +;; --- Clean Page History + +(defrecord CleanPageHistory [] + rs/UpdateEvent + (-apply-update [_ state] + (println "CleanPageHistory") + (-> state + (assoc-in [:workspace :history :items] nil) + (assoc-in [:workspace :history :selected] nil)))) + +(defn clean-page-history + [] + (CleanPageHistory.)) diff --git a/src/uxbox/repo/pages.cljs b/src/uxbox/repo/pages.cljs index 36f8c3def..90fe468ef 100644 --- a/src/uxbox/repo/pages.cljs +++ b/src/uxbox/repo/pages.cljs @@ -23,6 +23,11 @@ (let [url (str url "/projects/" project "/pages")] (send! {:method :get :url url}))) +(defmethod -do :fetch/page-history + [type {:keys [page] :as params}] + (let [url (str url "/pages/" page "/history")] + (send! {:method :get :url url}))) + (defmethod -do :delete/page [_ id] (let [url (str url "/pages/" id)] diff --git a/src/uxbox/ui/workspace/sidebar.cljs b/src/uxbox/ui/workspace/sidebar.cljs index 4df38713f..0b792faf6 100644 --- a/src/uxbox/ui/workspace/sidebar.cljs +++ b/src/uxbox/ui/workspace/sidebar.cljs @@ -17,7 +17,7 @@ [uxbox.ui.workspace.sidebar.options :refer (options-toolbox)] [uxbox.ui.workspace.sidebar.layers :refer (layers-toolbox)] [uxbox.ui.workspace.sidebar.sitemap :refer (sitemap-toolbox)] - [uxbox.ui.workspace.sidebar.document-history :refer (document-history-toolbox)] + [uxbox.ui.workspace.sidebar.history :refer (history-toolbox)] [uxbox.ui.workspace.sidebar.icons :refer (icons-toolbox)] [uxbox.ui.workspace.sidebar.drawtools :refer (draw-toolbox)])) @@ -34,7 +34,7 @@ (when (contains? flags :sitemap) (sitemap-toolbox)) (when (contains? flags :document-history) - (document-history-toolbox)) + (history-toolbox)) (when (contains? flags :layers) (layers-toolbox))]]))) diff --git a/src/uxbox/ui/workspace/sidebar/document_history.cljs b/src/uxbox/ui/workspace/sidebar/history.cljs similarity index 54% rename from src/uxbox/ui/workspace/sidebar/document_history.cljs rename to src/uxbox/ui/workspace/sidebar/history.cljs index 36b04eeda..38bd100b6 100644 --- a/src/uxbox/ui/workspace/sidebar/document_history.cljs +++ b/src/uxbox/ui/workspace/sidebar/history.cljs @@ -5,7 +5,7 @@ ;; Copyright (c) 2015-2016 Andrey Antukh ;; Copyright (c) 2015-2016 Juan de la Cruz -(ns uxbox.ui.workspace.sidebar.document-history +(ns uxbox.ui.workspace.sidebar.history (:require [sablono.core :as html :refer-macros [html]] [rum.core :as rum] [lentes.core :as l] @@ -15,21 +15,51 @@ [uxbox.state :as st] [uxbox.shapes :as shapes] [uxbox.library :as library] + [uxbox.util.datetime :as dt] [uxbox.util.data :refer (read-string)] [uxbox.data.workspace :as dw] + [uxbox.data.pages :as dpg] [uxbox.ui.workspace.base :as wb] [uxbox.ui.icons :as i] [uxbox.ui.mixins :as mx] [uxbox.util.dom :as dom])) +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Lenses +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(def ^:const history-l + (as-> (l/in [:workspace :history]) $ + (l/focus-atom $ st/state))) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Component ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -(defn document-history-toolbox-render +(defn- history-toolbox-will-mount [own] - (let [workspace (rum/react wb/workspace-l) - local (:rum/local own) + (let [page @wb/page-l] + (rs/emit! (dpg/fetch-page-history (:id page))) + (add-watch wb/page-l ::key (fn [_ _ ov nv] + (when (> (:version nv) (:version ov)) + (rs/emit! (dpg/fetch-page-history (:id nv)))))) + own)) + +(defn- history-toolbox-will-unmount + [own] + (rs/emit! (dpg/clean-page-history)) + (remove-watch wb/page-l ::key) + own) + +(defn- history-toolbox-transfer-state + [oldown own] + own) + +(defn history-toolbox-render + [own] + (let [local (:rum/local own) + page (rum/react wb/page-l) + history (rum/react history-l) section (:section @local :main) close #(rs/emit! (dw/toggle-flag :document-history)) main? (= section :main) @@ -63,65 +93,19 @@ [:ul.history-content [:li.current [:div.pin-icon i/pin] - [:span "Current version"]] - [:li - [:div.pin-icon i/pin] - [:span "Version 02/02/2016 12:33h"]] - [:li - [:div.pin-icon i/pin] - [:span "Version 02/02/2016 12:33h"]] - [:li - [:div.pin-icon i/pin] - [:span "Version 02/02/2016 12:33h"]] - [:li - [:div.pin-icon i/pin] - [:span "Version 02/02/2016 12:33h"]] - [:li - [:div.pin-icon i/pin] - [:span "Version 02/02/2016 12:33h"]] - [:li - [:div.pin-icon i/pin] - [:span "Version 02/02/2016 12:33h"]] - [:li - [:div.pin-icon i/pin] - [:span "Version 02/02/2016 12:33h"]] - [:li - [:div.pin-icon i/pin] - [:span "Version 02/02/2016 12:33h"]] - [:li - [:div.pin-icon i/pin] - [:span "Version 02/02/2016 12:33h"]] - [:li - [:div.pin-icon i/pin] - [:span "Version 02/02/2016 12:33h"]] - [:li - [:div.pin-icon i/pin] - [:span "Version 02/02/2016 12:33h"]] - [:li - [:div.pin-icon i/pin] - [:span "Version 02/02/2016 12:33h"]] - [:li - [:div.pin-icon i/pin] - [:span "Version 02/02/2016 12:33h"]] - [:li - [:div.pin-icon i/pin] - [:span "Version 02/02/2016 12:33h"]] - [:li - [:div.pin-icon i/pin] - [:span "Version 02/02/2016 12:33h"]] - [:li - [:div.pin-icon i/pin] - [:span "Version 02/02/2016 12:33h"]] - [:li - [:div.pin-icon i/pin] - [:span "Version 02/02/2016 12:33h"]] - [:li - [:div.pin-icon i/pin] - [:span "Version 02/02/2016 12:33h"]]])]]))) + [:span (str "Version " (:version page) " (current)")]] + (for [item (:items history)] + [:li {:key (str (:id item))} + [:div.pin-icon i/pin] + [:span (str "Version " (:version item) + " (" (dt/timeago (:created-at item)) ")")]]) + ])]]))) - -(def ^:static document-history-toolbox +(def ^:static history-toolbox (mx/component - {:render document-history-toolbox-render + {:render history-toolbox-render :name "document-history-toolbox" + :will-mount history-toolbox-will-mount + :will-unmount history-toolbox-will-unmount + :transfer-state history-toolbox-transfer-state :mixins [mx/static rum/reactive (mx/local)]})) diff --git a/src/uxbox/ui/workspace/sidebar/sitemap.cljs b/src/uxbox/ui/workspace/sidebar/sitemap.cljs index ae8bf3ef8..90fddf58d 100644 --- a/src/uxbox/ui/workspace/sidebar/sitemap.cljs +++ b/src/uxbox/ui/workspace/sidebar/sitemap.cljs @@ -33,7 +33,7 @@ ;; Lenses ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -(def ^:static pages-l +(def ^:const pages-l (letfn [(getter [state] (let [project (get-in state [:workspace :project])] (stpr/project-pages state project)))]