From b6f6ab490fb9fc63bafe69a19263a1679c26a3da Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Tue, 21 Jan 2020 23:09:28 +0100 Subject: [PATCH] :fire: Remove old repo code. And make closure-defnes parametrizable. --- frontend/src/uxbox/config.cljs | 3 +- frontend/src/uxbox/main/data/auth.cljs | 2 +- frontend/src/uxbox/main/data/colors.cljs | 2 +- frontend/src/uxbox/main/data/history.cljs | 6 +- frontend/src/uxbox/main/data/icons.cljs | 2 +- frontend/src/uxbox/main/data/images.cljs | 2 +- frontend/src/uxbox/main/data/projects.cljs | 2 +- frontend/src/uxbox/main/data/users.cljs | 8 +- frontend/src/uxbox/main/data/workspace.cljs | 6 +- frontend/src/uxbox/main/repo.cljs | 146 +++++++++++++++--- frontend/src/uxbox/main/repo/auth.cljs | 63 -------- frontend/src/uxbox/main/repo/colors.cljs | 60 -------- frontend/src/uxbox/main/repo/core.cljs | 156 -------------------- frontend/src/uxbox/main/repo/icons.cljs | 71 --------- frontend/src/uxbox/main/repo/images.cljs | 87 ----------- frontend/src/uxbox/main/repo/impl.cljs | 61 -------- frontend/src/uxbox/main/repo/kvstore.cljs | 29 ---- frontend/src/uxbox/main/repo/pages.cljs | 60 -------- frontend/src/uxbox/main/repo/projects.cljs | 42 ------ frontend/src/uxbox/main/repo/users.cljs | 41 ----- frontend/src/uxbox/main/websockets.cljs | 10 ++ frontend/src/uxbox/view/data/viewer.cljs | 4 +- frontend/tools.clj | 20 ++- manage.sh | 5 +- 24 files changed, 173 insertions(+), 715 deletions(-) delete mode 100644 frontend/src/uxbox/main/repo/auth.cljs delete mode 100644 frontend/src/uxbox/main/repo/colors.cljs delete mode 100644 frontend/src/uxbox/main/repo/core.cljs delete mode 100644 frontend/src/uxbox/main/repo/icons.cljs delete mode 100644 frontend/src/uxbox/main/repo/images.cljs delete mode 100644 frontend/src/uxbox/main/repo/impl.cljs delete mode 100644 frontend/src/uxbox/main/repo/kvstore.cljs delete mode 100644 frontend/src/uxbox/main/repo/pages.cljs delete mode 100644 frontend/src/uxbox/main/repo/projects.cljs delete mode 100644 frontend/src/uxbox/main/repo/users.cljs diff --git a/frontend/src/uxbox/config.cljs b/frontend/src/uxbox/config.cljs index a7d3239de..c17e341c9 100644 --- a/frontend/src/uxbox/config.cljs +++ b/frontend/src/uxbox/config.cljs @@ -6,7 +6,6 @@ (ns uxbox.config) -(goog-define url "http://127.0.0.1:6060/api") -(goog-define viewurl "/view/index.html") +(goog-define url "http://127.0.0.1:6060") (goog-define demo-warning false) (goog-define default-language "en") diff --git a/frontend/src/uxbox/main/data/auth.cljs b/frontend/src/uxbox/main/data/auth.cljs index b7b667ae9..a6db3d33a 100644 --- a/frontend/src/uxbox/main/data/auth.cljs +++ b/frontend/src/uxbox/main/data/auth.cljs @@ -13,7 +13,7 @@ [beicon.core :as rx] [potok.core :as ptk] [uxbox.common.spec :as us] - [uxbox.main.repo.core :as rp] + [uxbox.main.repo :as rp] [uxbox.main.store :refer [initial-state]] [uxbox.main.data.users :as du] [uxbox.util.messages :as um] diff --git a/frontend/src/uxbox/main/data/colors.cljs b/frontend/src/uxbox/main/data/colors.cljs index 1ebaa3ce9..69a011577 100644 --- a/frontend/src/uxbox/main/data/colors.cljs +++ b/frontend/src/uxbox/main/data/colors.cljs @@ -9,7 +9,7 @@ [beicon.core :as rx] [clojure.set :as set] [potok.core :as ptk] - [uxbox.main.repo.core :as rp] + [uxbox.main.repo :as rp] [uxbox.main.store :as st] [uxbox.util.color :as color] [uxbox.util.i18n :refer [tr]] diff --git a/frontend/src/uxbox/main/data/history.cljs b/frontend/src/uxbox/main/data/history.cljs index 891e046b8..4c82f6738 100644 --- a/frontend/src/uxbox/main/data/history.cljs +++ b/frontend/src/uxbox/main/data/history.cljs @@ -109,7 +109,7 @@ ptk/WatchEvent (watch [_ state s] (let [params {:page id :pinned true}] - (->> (rp/req :fetch/page-history params) + #_(->> (rp/req :fetch/page-history params) (rx/map :payload) (rx/map pinned-history-fetched)))))) @@ -148,7 +148,7 @@ :max (or max 20)} (when since {:since since}))] - (->> (rp/req :fetch/page-history params) + #_(->> (rp/req :fetch/page-history params) (rx/map :payload) (rx/map history-fetched))))))) @@ -262,7 +262,7 @@ ptk/WatchEvent (watch [_ state stream] (rx/concat - (->> (rp/req :update/page-history item) + #_(->> (rp/req :update/page-history item) (rx/map :payload) (rx/map history-updated)) (->> (rx/filter history-updated? stream) diff --git a/frontend/src/uxbox/main/data/icons.cljs b/frontend/src/uxbox/main/data/icons.cljs index 19010548c..bba1c27ae 100644 --- a/frontend/src/uxbox/main/data/icons.cljs +++ b/frontend/src/uxbox/main/data/icons.cljs @@ -10,7 +10,7 @@ [beicon.core :as rx] [cuerdas.core :as str] [potok.core :as ptk] - [uxbox.main.repo.core :as rp] + [uxbox.main.repo :as rp] [uxbox.main.store :as st] [uxbox.util.data :refer (jscoll->vec)] [uxbox.util.dom :as dom] diff --git a/frontend/src/uxbox/main/data/images.cljs b/frontend/src/uxbox/main/data/images.cljs index 4ce8013f5..3b3245822 100644 --- a/frontend/src/uxbox/main/data/images.cljs +++ b/frontend/src/uxbox/main/data/images.cljs @@ -12,7 +12,7 @@ [potok.core :as ptk] [uxbox.common.spec :as us] [uxbox.main.store :as st] - [uxbox.main.repo.core :as rp] + [uxbox.main.repo :as rp] [uxbox.util.i18n :refer [tr]] [uxbox.util.router :as rt] [uxbox.util.data :refer (jscoll->vec)] diff --git a/frontend/src/uxbox/main/data/projects.cljs b/frontend/src/uxbox/main/data/projects.cljs index 8f99bade0..25b44fbfb 100644 --- a/frontend/src/uxbox/main/data/projects.cljs +++ b/frontend/src/uxbox/main/data/projects.cljs @@ -12,7 +12,7 @@ [potok.core :as ptk] [uxbox.common.pages :as cp] [uxbox.common.spec :as us] - [uxbox.main.repo.core :as rp] + [uxbox.main.repo :as rp] [uxbox.util.router :as rt] [uxbox.util.time :as dt] [uxbox.util.timers :as ts] diff --git a/frontend/src/uxbox/main/data/users.cljs b/frontend/src/uxbox/main/data/users.cljs index 4aa2fcc0d..6ff1db957 100644 --- a/frontend/src/uxbox/main/data/users.cljs +++ b/frontend/src/uxbox/main/data/users.cljs @@ -11,7 +11,7 @@ [cuerdas.core :as str] [potok.core :as ptk] [uxbox.common.spec :as us] - [uxbox.main.repo.core :as rp] + [uxbox.main.repo :as rp] [uxbox.util.i18n :as i18n :refer [tr]] [uxbox.util.messages :as uum] [uxbox.util.storage :refer [storage]])) @@ -87,7 +87,7 @@ (assoc :email (:email data)) (assoc :username (:username data)) (assoc-in [:metadata :language] (:language data)))] - (->> (rp/req :update/profile data) + #_(->> (rp/req :update/profile data) (rx/map :payload) (rx/do on-success) (rx/map profile-fetched) @@ -110,7 +110,7 @@ (watch [_ state s] (let [params {:old-password (:password-old data) :password (:password-1 data)}] - (->> (rp/req :update/profile-password params) + #_(->> (rp/req :update/profile-password params) (rx/catch rp/client-error? (fn [e] (on-error (:payload e)) (rx/empty))) @@ -123,7 +123,7 @@ (deftype UpdatePhoto [file done] ptk/WatchEvent (watch [_ state stream] - (->> (rp/req :update/profile-photo {:file file}) + #_(->> (rp/req :update/profile-photo {:file file}) (rx/do done) (rx/map (constantly fetch-profile))))) diff --git a/frontend/src/uxbox/main/data/workspace.cljs b/frontend/src/uxbox/main/data/workspace.cljs index ab1eca2d4..37bcd0556 100644 --- a/frontend/src/uxbox/main/data/workspace.cljs +++ b/frontend/src/uxbox/main/data/workspace.cljs @@ -18,7 +18,7 @@ [uxbox.main.data.projects :as dp] [uxbox.main.geom :as geom] [uxbox.main.refs :as refs] - [uxbox.main.repo.core :as rp] + [uxbox.main.repo :as rp] [uxbox.main.store :as st] [uxbox.main.streams :as ms] [uxbox.main.websockets :as ws] @@ -77,8 +77,8 @@ (ptk/reify ::initialize ptk/UpdateEvent (update [_ state] - (let [uri (str "ws://localhost:6060/sub/" file-id)] - (assoc-in state [:ws file-id] (ws/open uri)))) + (let [url (ws/url (str "/sub/" file-id))] + (assoc-in state [:ws file-id] (ws/open url)))) ptk/WatchEvent (watch [_ state stream] diff --git a/frontend/src/uxbox/main/repo.cljs b/frontend/src/uxbox/main/repo.cljs index 062291859..c3a54069e 100644 --- a/frontend/src/uxbox/main/repo.cljs +++ b/frontend/src/uxbox/main/repo.cljs @@ -2,27 +2,137 @@ ;; License, v. 2.0. If a copy of the MPL was not distributed with this ;; file, You can obtain one at http://mozilla.org/MPL/2.0/. ;; -;; Copyright (c) 2016 Andrey Antukh +;; Copyright (c) 2019 Andrey Antukh (ns uxbox.main.repo - "A main interface for access to remote resources." - (:require [uxbox.util.http :as http] - [uxbox.main.repo.auth] - [uxbox.main.repo.users] - [uxbox.main.repo.projects] - [uxbox.main.repo.pages] - [uxbox.main.repo.images] - [uxbox.main.repo.icons] - [uxbox.main.repo.kvstore] - [uxbox.main.repo.impl :as impl])) + (:require + [beicon.core :as rx] + [cuerdas.core :as str] + [uxbox.config :refer [url]] + [uxbox.util.http :as http] + [uxbox.util.storage :refer [storage]] + [uxbox.util.transit :as t]) + (:import [goog.Uri QueryData])) -(defn req - "Perform a side effectfull action accesing - remote resources." - ([type] - (impl/request type nil)) - ([type data] - (impl/request type data))) +;; --- Low Level API + +(defn- conditional-decode + [{:keys [body headers] :as response}] + (let [contentype (get headers "content-type")] + (if (str/starts-with? contentype "application/transit+json") + (assoc response :body (t/decode body)) + response))) + +(defn- handle-http-status + [{:keys [body status] :as response}] + (if (http/success? response) + (rx/of {:status status :payload body}) + (rx/throw {:status status :payload body}))) + +(def ^:private +headers+ + {"content-type" "application/transit+json"}) + +(defn- encode-query + [params] + (let [data (QueryData.)] + (.extend data (clj->js params)) + (.toString data))) + +(defn impl-send + [{:keys [body headers auth method query url response-type] + :or {auth true response-type :text}}] + (let [headers (merge {"Accept" "application/transit+json,*/*"} + (when (map? body) +headers+) + headers) + request {:method method + :url url + :headers headers + :query-string (when query (encode-query query)) + :body (if (map? body) (t/encode body) body)} + options {:response-type response-type + :credentials? true}] + (http/send! request options))) + +(defn send! + [request] + (->> (impl-send request) + (rx/map conditional-decode) + (rx/mapcat handle-http-status))) + +;; --- High Level API + +(defn- handle-response + [response] + ;; (prn "handle-response1" response) + (cond + (http/success? response) + (rx/of (:body response)) + + (http/client-error? response) + (rx/throw (:body response)) + + :else + (rx/throw {:type :unexpected + :code (:error response)}))) + +(defn send-query! + [id params] + (let [url (str url "/api/w/query/" (name id))] + (->> (impl-send {:method :get :url url :query params}) + (rx/map conditional-decode) + (rx/mapcat handle-response)))) + +(defn send-mutation! + [id params] + (let [url (str url "/api/w/mutation/" (name id))] + (->> (impl-send {:method :post :url url :body params}) + (rx/map conditional-decode) + (rx/mapcat handle-response)))) + +(defn- dispatch + [& args] + (first args)) + +(defmulti query dispatch) +(defmulti mutation dispatch) + +(defmethod query :default + [id params] + (send-query! id params)) + +(defmethod mutation :default + [id params] + (send-mutation! id params)) + +(defn query! + ([id] (query id {})) + ([id params] (query id params))) + +(defn mutation! + ([id] (mutation id {})) + ([id params] (mutation id params))) + +(defmethod mutation :create-image + [id params] + (let [form (js/FormData.)] + (run! (fn [[key val]] + (.append form (name key) val)) + (seq params)) + (send-mutation! id form))) + +(defmethod mutation :login + [id params] + (let [url (str url "/login")] + (->> (impl-send {:method :post :url url :body params}) + (rx/map conditional-decode) + (rx/mapcat handle-response)))) + +(defmethod mutation :logout + [id params] + (let [url (str url "/logout")] + (->> (impl-send {:method :post :url url :body params :auth false}) + (rx/map conditional-decode) + (rx/mapcat handle-response)))) (def client-error? http/client-error?) (def server-error? http/server-error?) diff --git a/frontend/src/uxbox/main/repo/auth.cljs b/frontend/src/uxbox/main/repo/auth.cljs deleted file mode 100644 index 586e3f30a..000000000 --- a/frontend/src/uxbox/main/repo/auth.cljs +++ /dev/null @@ -1,63 +0,0 @@ -;; This Source Code Form is subject to the terms of the Mozilla Public -;; License, v. 2.0. If a copy of the MPL was not distributed with this -;; file, You can obtain one at http://mozilla.org/MPL/2.0/. -;; -;; Copyright (c) 2016 Andrey Antukh - -(ns uxbox.main.repo.auth - "A main interface for access to remote resources." - (:require [beicon.core :as rx] - [uxbox.config :refer (url)] - [uxbox.main.repo.impl :refer (request send!)])) - -(defmethod request :fetch/profile - [type _] - (let [url (str url "/w/query/profile")] - (send! {:method :get :url url}))) - -(defmethod request :auth/login - [type data] - (let [url (str url "/login")] - (send! {:url url - :method :post - :auth false - :body data}))) - -(defmethod request :auth/logout - [type data] - (let [url (str url "/logout")] - (send! {:url url :method :post :auth false}))) - -(defmethod request :update/profile - [type data] - (let [params {:url (str url "/profile/me") - :method :put - :body data}] - (send! params))) - -(defmethod request :auth/register - [_ data] - (let [params {:url (str url "/auth/register") - :method :post - :body data}] - (send! params))) - -(defmethod request :auth/recovery-request - [_ data] - (let [params {:url (str url "/auth/recovery") - :method :post - :body data}] - (send! params))) - -(defmethod request :auth/validate-recovery-token - [_ token] - (let [params {:url (str url "/auth/recovery/" token) - :method :get}] - (send! params))) - -(defmethod request :auth/recovery - [_ data] - (let [params {:url (str url "/auth/recovery") - :method :put - :body data}] - (send! params))) diff --git a/frontend/src/uxbox/main/repo/colors.cljs b/frontend/src/uxbox/main/repo/colors.cljs deleted file mode 100644 index afb512b8a..000000000 --- a/frontend/src/uxbox/main/repo/colors.cljs +++ /dev/null @@ -1,60 +0,0 @@ -;; This Source Code Form is subject to the terms of the Mozilla Public -;; License, v. 2.0. If a copy of the MPL was not distributed with this -;; file, You can obtain one at http://mozilla.org/MPL/2.0/. -;; -;; Copyright (c) 2016 Andrey Antukh - -(ns uxbox.main.repo.colors - "A main interface for access to remote resources." - (:require [beicon.core :as rx] - [uxbox.config :refer (url)] - [uxbox.main.repo.impl :refer (request send!)] - [uxbox.util.transit :as t])) - -(defn- decode-color-collection - [{:keys [data] :as coll}] - (merge coll - (when data {:data (t/decode data)}))) - -(defn- decode-payload - [{:keys [payload] :as rsp}] - (if (sequential? payload) - (assoc rsp :payload (mapv decode-color-collection payload)) - (assoc rsp :payload (decode-color-collection payload)))) - -(defmethod request :fetch/color-collection - [_ id] - (let [params {:url (str url "/library/color-collections/" id) - :method :get}] - (->> (send! params) - (rx/map decode-payload)))) - -(defmethod request :fetch/color-collections - [_] - (let [params {:url (str url "/library/color-collections") - :method :get}] - (->> (send! params) - (rx/map decode-payload)))) - -(defmethod request :delete/color-collection - [_ id] - (let [url (str url "/library/color-collections/" id)] - (send! {:url url :method :delete}))) - -(defmethod request :create/color-collection - [_ {:keys [data] :as body}] - (let [body (assoc body :data (t/encode data)) - params {:url (str url "/library/color-collections") - :method :post - :body body}] - (->> (send! params) - (rx/map decode-payload)))) - -(defmethod request :update/color-collection - [_ {:keys [id data] :as body}] - (let [body (assoc body :data (t/encode data)) - params {:url (str url "/library/color-collections/" id) - :method :put - :body body}] - (->> (send! params) - (rx/map decode-payload)))) diff --git a/frontend/src/uxbox/main/repo/core.cljs b/frontend/src/uxbox/main/repo/core.cljs deleted file mode 100644 index 0f4ad716b..000000000 --- a/frontend/src/uxbox/main/repo/core.cljs +++ /dev/null @@ -1,156 +0,0 @@ -;; This Source Code Form is subject to the terms of the Mozilla Public -;; License, v. 2.0. If a copy of the MPL was not distributed with this -;; file, You can obtain one at http://mozilla.org/MPL/2.0/. -;; -;; Copyright (c) 2019 Andrey Antukh - -(ns uxbox.main.repo.core - (:require - [beicon.core :as rx] - [cuerdas.core :as str] - [uxbox.main.repo.impl :as impl] - [uxbox.config :refer [url]] - [uxbox.util.http :as http] - [uxbox.util.storage :refer [storage]] - [uxbox.util.transit :as t]) - (:import [goog.Uri QueryData])) - -;; --- Low Level API - -(defn- conditional-decode - [{:keys [body headers] :as response}] - (let [contentype (get headers "content-type")] - (if (str/starts-with? contentype "application/transit+json") - (assoc response :body (t/decode body)) - response))) - -(defn- handle-http-status - [{:keys [body status] :as response}] - (if (http/success? response) - (rx/of {:status status :payload body}) - (rx/throw {:status status :payload body}))) - -(def ^:private +headers+ - {"content-type" "application/transit+json"}) - -(defn- encode-query - [params] - (let [data (QueryData.)] - (.extend data (clj->js params)) - (.toString data))) - -(defn impl-send - [{:keys [body headers auth method query url response-type] - :or {auth true response-type :text}}] - (let [headers (merge {"Accept" "application/transit+json,*/*"} - (when (map? body) +headers+) - headers) - request {:method method - :url url - :headers headers - :query-string (when query (encode-query query)) - :body (if (map? body) (t/encode body) body)} - options {:response-type response-type - :credentials? true}] - (http/send! request options))) - -(defn send! - [request] - (->> (impl-send request) - (rx/map conditional-decode) - (rx/mapcat handle-http-status))) - -;; --- High Level API - -(defn- handle-response - [response] - ;; (prn "handle-response1" response) - (cond - (http/success? response) - (rx/of (:body response)) - - (http/client-error? response) - (rx/throw (:body response)) - - :else - (rx/throw {:type :unexpected - :code (:error response)}))) - -(defn send-query! - [id params] - (let [url (str url "/w/query/" (name id))] - (->> (impl-send {:method :get :url url :query params}) - (rx/map conditional-decode) - (rx/mapcat handle-response)))) - -(defn send-mutation! - [id params] - (let [url (str url "/w/mutation/" (name id))] - (->> (impl-send {:method :post :url url :body params}) - (rx/map conditional-decode) - (rx/mapcat handle-response)))) - -(defn- dispatch - [& args] - (first args)) - -(defmulti query dispatch) -(defmulti mutation dispatch) - -(defmethod query :default - [id params] - (send-query! id params)) - -(defmethod mutation :default - [id params] - (send-mutation! id params)) - -(defn query! - ([id] (query id {})) - ([id params] (query id params))) - -(defn mutation! - ([id] (mutation id {})) - ([id params] (mutation id params))) - -;; --- Legacy Api - -(defn req - "Perform a side effectfull action accesing - remote resources." - ([type] - (impl/request type nil)) - ([type data] - (impl/request type data))) - -(def client-error? http/client-error?) -(def server-error? http/server-error?) - -(defmethod mutation :create-image - [id params] - (let [form (js/FormData.)] - (run! (fn [[key val]] - (.append form (name key) val)) - (seq params)) - (send-mutation! id form))) - -(defmethod mutation :login - [id params] - (let [url (str url "/login")] - (->> (impl-send {:method :post :url url :body params}) - (rx/map conditional-decode) - (rx/mapcat handle-response)))) - -(defmethod mutation :logout - [id params] - (let [url (str url "/logout")] - (->> (impl-send {:method :post :url url :body params :auth false}) - (rx/map conditional-decode) - (rx/mapcat handle-response)))) - -;; (defmethod mutation :register-profile -;; [id params] -;; (let [url (str url "/register")] -;; (->> (impl-send {:method :post :url url :body params :auth false}) -;; (rx/map conditional-decode) -;; (rx/mapcat handle-response)))) diff --git a/frontend/src/uxbox/main/repo/icons.cljs b/frontend/src/uxbox/main/repo/icons.cljs deleted file mode 100644 index 4e1612c49..000000000 --- a/frontend/src/uxbox/main/repo/icons.cljs +++ /dev/null @@ -1,71 +0,0 @@ -;; This Source Code Form is subject to the terms of the Mozilla Public -;; License, v. 2.0. If a copy of the MPL was not distributed with this -;; file, You can obtain one at http://mozilla.org/MPL/2.0/. -;; -;; Copyright (c) 2016 Andrey Antukh - -(ns uxbox.main.repo.icons - "A main interface for access to remote resources." - (:require [beicon.core :as rx] - [uxbox.config :refer (url)] - [uxbox.main.repo.impl :refer (request send!)] - [uxbox.util.transit :as t])) - -(defmethod request :fetch/icon-collections - [_] - (let [params {:url (str url "/library/icon-collections") - :method :get}] - (send! params))) - -(defmethod request :delete/icon-collection - [_ id] - (let [url (str url "/library/icon-collections/" id)] - (send! {:url url :method :delete}))) - -(defmethod request :create/icon-collection - [_ {:keys [data] :as body}] - (let [body (assoc body :data (t/encode data)) - params {:url (str url "/library/icon-collections") - :method :post - :body body}] - (send! params))) - -(defmethod request :update/icon-collection - [_ {:keys [id data] :as body}] - (let [body (assoc body :data (t/encode data)) - params {:url (str url "/library/icon-collections/" id) - :method :put - :body body}] - (send! params))) - -(defmethod request :fetch/icons - [_ {:keys [coll]}] - (let [url (str url "/library/icons") - qp (if coll {:collection coll} nil) - params {:url url :method :get :query qp}] - (send! params))) - -(defmethod request :fetch/icon - [_ {:keys [id]}] - (let [params {:url (str url "/library/icons/" id) - :method :get}] - (send! params))) - -(defmethod request :create/icon - [_ body] - (let [params {:url (str url "/library/icons") - :method :post - :body body}] - (send! params))) - -(defmethod request :delete/icon - [_ id] - (let [url (str url "/library/icons/" id)] - (send! {:url url :method :delete}))) - -(defmethod request :update/icon - [_ {:keys [id collection] :as body}] - (let [params {:url (str url "/library/icons/" id) - :method :put - :body body}] - (send! params))) diff --git a/frontend/src/uxbox/main/repo/images.cljs b/frontend/src/uxbox/main/repo/images.cljs deleted file mode 100644 index baa41abc1..000000000 --- a/frontend/src/uxbox/main/repo/images.cljs +++ /dev/null @@ -1,87 +0,0 @@ -;; This Source Code Form is subject to the terms of the Mozilla Public -;; License, v. 2.0. If a copy of the MPL was not distributed with this -;; file, You can obtain one at http://mozilla.org/MPL/2.0/. -;; -;; Copyright (c) 2016 Andrey Antukh - -(ns uxbox.main.repo.images - "A main interface for access to remote resources." - (:require [beicon.core :as rx] - [uxbox.config :refer (url)] - [uxbox.main.repo.impl :refer (request send!)] - [uxbox.util.transit :as t])) - -(defmethod request :fetch/image-collections - [_] - (let [params {:url (str url "/library/image-collections") - :method :get}] - (send! params))) - -(defmethod request :delete/image-collection - [_ id] - (let [url (str url "/library/image-collections/" id)] - (send! {:url url :method :delete}))) - -(defmethod request :create/image-collection - [_ {:keys [data] :as body}] - (let [body (assoc body :data (t/encode data)) - params {:url (str url "/library/image-collections") - :method :post - :body body}] - (send! params))) - -(defmethod request :update/image-collection - [_ {:keys [id data] :as body}] - (let [body (assoc body :data (t/encode data)) - params {:url (str url "/library/image-collections/" id) - :method :put - :body body}] - (send! params))) - -(defmethod request :fetch/images - [_ {:keys [coll]}] - (let [url (str url "/library/images") - qp (when coll {:collection coll}) - params {:url url :method :get :query qp}] - (send! params))) - -(defmethod request :fetch/image - [_ {:keys [id]}] - (let [params {:url (str url "/library/images/" id) - :method :get}] - (send! params))) - -(defmethod request :create/image - [_ {:keys [collection id file width height mimetype] :as body}] - (let [body (doto (js/FormData.) - (.append "mimetype" mimetype) - (.append "file" file) - (.append "width" width) - (.append "height" height) - (.append "id" id)) - _ (when collection - (.append body "collection" (str collection))) - - params {:url (str url "/library/images") - :method :post - :body body}] - (send! params))) - -(defmethod request :delete/image - [_ id] - (let [url (str url "/library/images/" id)] - (send! {:url url :method :delete}))) - -(defmethod request :update/image - [_ {:keys [id collection] :as body}] - (let [params {:url (str url "/library/images/" id) - :method :put - :body body}] - (send! params))) - -(defmethod request :copy/image - [_ {:keys [id collection] :as body}] - (let [params {:url (str url "/library/images/" id "/copy") - :method :put - :body body}] - (send! params))) diff --git a/frontend/src/uxbox/main/repo/impl.cljs b/frontend/src/uxbox/main/repo/impl.cljs deleted file mode 100644 index 3d7a9a94f..000000000 --- a/frontend/src/uxbox/main/repo/impl.cljs +++ /dev/null @@ -1,61 +0,0 @@ -;; This Source Code Form is subject to the terms of the Mozilla Public -;; License, v. 2.0. If a copy of the MPL was not distributed with this -;; file, You can obtain one at http://mozilla.org/MPL/2.0/. -;; -;; Copyright (c) 2016 Andrey Antukh - -(ns uxbox.main.repo.impl - (:require [clojure.walk :as walk] - [beicon.core :as rx] - [cuerdas.core :as str] - [uxbox.config :refer (url)] - [uxbox.util.http :as http] - [uxbox.util.storage :refer (storage)] - [uxbox.util.transit :as t]) - (:import [goog.Uri QueryData])) - -(defn- conditional-decode - [{:keys [body headers] :as response}] - (let [contentype (get headers "content-type")] - (if (str/starts-with? contentype "application/transit+json") - (assoc response :body (t/decode body)) - response))) - -(defn- handle-http-status - [{:keys [body status] :as response}] - (if (http/success? response) - (rx/of {:status status :payload body}) - (rx/throw {:status status :payload body}))) - -(def ^:private +headers+ - {"content-type" "application/transit+json"}) - -(defn- encode-query - [params] - (let [data (QueryData.)] - (.extend data (clj->js params)) - (.toString data))) - -(defn send! - [{:keys [body headers auth method query url response-type] - :or {auth true response-type :text}}] - (let [headers (merge {"Accept" "application/transit+json,*/*"} - (when (map? body) +headers+) - headers) - request {:method method - :url url - :headers headers - :query-string (when query (encode-query query)) - :body (if (map? body) (t/encode body) body)} - options {:response-type response-type - :credentials? true}] - (->> (http/send! request options) - (rx/map conditional-decode) - (rx/mapcat handle-http-status)))) - -(defmulti request - (fn [type data] type)) - -(defmethod request :default - [type data] - (throw (ex-info (str "No implementation found for " type) {:data data}))) diff --git a/frontend/src/uxbox/main/repo/kvstore.cljs b/frontend/src/uxbox/main/repo/kvstore.cljs deleted file mode 100644 index cd853df61..000000000 --- a/frontend/src/uxbox/main/repo/kvstore.cljs +++ /dev/null @@ -1,29 +0,0 @@ -;; This Source Code Form is subject to the terms of the Mozilla Public -;; License, v. 2.0. If a copy of the MPL was not distributed with this -;; file, You can obtain one at http://mozilla.org/MPL/2.0/. -;; -;; Copyright (c) 2016 Andrey Antukh - -(ns uxbox.main.repo.kvstore - "A main interface for access to remote resources." - (:require [beicon.core :as rx] - [cuerdas.core :as str] - [uxbox.config :refer (url)] - [uxbox.main.repo.impl :refer (request send!)] - [uxbox.util.transit :as t])) - -(defmethod request :fetch/kvstore - [_ id] - (let [url (str url "/kvstore/" id) - params {:url url :method :get}] - (->> (send! params) - (rx/map (fn [{:keys [payload] :as response}] - (if (or (nil? payload) (str/empty? payload)) - (assoc response :payload {:key id :value nil :version nil}) - response)))))) - -(defmethod request :update/kvstore - [_ {:keys [id] :as data}] - (let [url (str url "/kvstore/" id) - params {:url url :method :put :body data}] - (send! params))) diff --git a/frontend/src/uxbox/main/repo/pages.cljs b/frontend/src/uxbox/main/repo/pages.cljs deleted file mode 100644 index e545f5e20..000000000 --- a/frontend/src/uxbox/main/repo/pages.cljs +++ /dev/null @@ -1,60 +0,0 @@ -;; This Source Code Form is subject to the terms of the Mozilla Public -;; License, v. 2.0. If a copy of the MPL was not distributed with this -;; file, You can obtain one at http://mozilla.org/MPL/2.0/. -;; -;; Copyright (c) 2016-2019 Andrey Antukh - -(ns uxbox.main.repo.pages - "A main interface for access to remote resources." - (:require - [uxbox.config :refer [url]] - [uxbox.main.repo.impl :as rp :refer [request send!]])) - -(defmethod request :fetch/pages-by-project - [type {:keys [project] :as params}] - (let [url (str url "/w/query/pages-by-project") - params {:project-id project}] - (send! {:method :get :url url :query params}))) - -(defmethod request :fetch/page-history - [type {:keys [page] :as params}] - (let [url (str url "/w/query/page-history") - query (-> (select-keys params [:max :since :pinned]) - (assoc :id page)) - params {:method :get :url url :query query}] - (send! params))) - -(defmethod request :delete/page - [_ id] - (let [url (str url "/pages/" id)] - (send! {:url url - :method :delete}))) - -(defmethod request :create/page - [type body] - (let [params {:url (str url "/pages") - :method :post - :body body}] - (send! params))) - -(defmethod request :update/page - [type {:keys [id] :as body}] - (let [params {:url (str url "/w/mutation/update-page") - :method :post - :body body}] - (send! params))) - -(defmethod request :update/page-metadata - [type {:keys [id] :as body}] - (let [params {:url (str url "/w/mutation/update-page-metadata") - :method :post - :body body}] - (send! params))) - - -(defmethod request :update/page-history - [type {:keys [id page] :as data}] - (let [params {:url (str url "/pages/" page "/history/" id) - :method :put - :body data}] - (send! params))) diff --git a/frontend/src/uxbox/main/repo/projects.cljs b/frontend/src/uxbox/main/repo/projects.cljs deleted file mode 100644 index 742e76cce..000000000 --- a/frontend/src/uxbox/main/repo/projects.cljs +++ /dev/null @@ -1,42 +0,0 @@ -;; This Source Code Form is subject to the terms of the Mozilla Public -;; License, v. 2.0. If a copy of the MPL was not distributed with this -;; file, You can obtain one at http://mozilla.org/MPL/2.0/. -;; -;; Copyright (c) 2016 Andrey Antukh - -(ns uxbox.main.repo.projects - "A main interface for access to remote resources." - (:require [beicon.core :as rx] - [uxbox.config :refer (url)] - [uxbox.main.repo.pages :as pages] - [uxbox.main.repo.impl :refer (request send!)] - [uxbox.util.transit :as t])) - -(defmethod request :fetch/projects - [type data] - ;; Obtain the list of projects and decode the embedded - ;; page data in order to have it usable. - (send! {:url (str url "/w/query/projects") - :method :get})) - -(defmethod request :fetch/project-by-token - [_ token] - (send! {:url (str url "/projects/by-token/" token) - :method :get})) - -(defmethod request :create/project - [_ data] - (send! {:url (str url "/projects") - :method :post - :body data})) - -(defmethod request :update/project - [_ {:keys [id] :as data}] - (send! {:url (str url "/projects/" id) - :method :put - :body data})) - -(defmethod request :delete/project - [_ id] - (send! {:url (str url "/projects/" id) - :method :delete})) diff --git a/frontend/src/uxbox/main/repo/users.cljs b/frontend/src/uxbox/main/repo/users.cljs deleted file mode 100644 index c4b2e2feb..000000000 --- a/frontend/src/uxbox/main/repo/users.cljs +++ /dev/null @@ -1,41 +0,0 @@ -;; This Source Code Form is subject to the terms of the Mozilla Public -;; License, v. 2.0. If a copy of the MPL was not distributed with this -;; file, You can obtain one at http://mozilla.org/MPL/2.0/. -;; -;; Copyright (c) 2016 Andrey Antukh - -(ns uxbox.main.repo.users - "A main interface for access to remote resources." - (:require [beicon.core :as rx] - [uxbox.config :refer (url)] - [uxbox.main.repo.impl :refer (request send!)] - [uxbox.util.transit :as t])) - -(defmethod request :fetch/profile - [type _] - (let [url (str url "/profile/me") - params {:method :get :url url}] - (send! params))) - -(defmethod request :update/profile - [type body] - (let [params {:url (str url "/profile/me") - :method :put - :body body}] - (send! params))) - -(defmethod request :update/profile-password - [type data] - (let [params {:url (str url "/profile/me/password") - :method :put - :body data}] - (send! params))) - -(defmethod request :update/profile-photo - [_ {:keys [file] :as body}] - (let [body (doto (js/FormData.) - (.append "file" file)) - params {:url (str url "/profile/me/photo") - :method :post - :body body}] - (send! params))) diff --git a/frontend/src/uxbox/main/websockets.cljs b/frontend/src/uxbox/main/websockets.cljs index 5086a66e4..96f203d76 100644 --- a/frontend/src/uxbox/main/websockets.cljs +++ b/frontend/src/uxbox/main/websockets.cljs @@ -8,9 +8,11 @@ "A interface to webworkers exposed functionality." (:require [goog.events :as ev] + [uxbox.config :as cfg] [beicon.core :as rx] [potok.core :as ptk]) (:import + goog.Uri goog.net.WebSocket goog.net.WebSocket.EventType)) @@ -19,6 +21,14 @@ (-send [_ message] "send a message") (-close [_] "close websocket")) +(defn url + [path] + (let [url (.parse Uri cfg/url)] + (.setPath url path) + (if (= (.getScheme url) "http") + (.setScheme url "ws") + (.setScheme url "wss")) + (.toString url))) (defn open [uri] diff --git a/frontend/src/uxbox/view/data/viewer.cljs b/frontend/src/uxbox/view/data/viewer.cljs index 079e2ca77..4d564c9b7 100644 --- a/frontend/src/uxbox/view/data/viewer.cljs +++ b/frontend/src/uxbox/view/data/viewer.cljs @@ -61,7 +61,7 @@ (defrecord LoadData [token] ptk/WatchEvent (watch [_ state stream] - (->> (rp/req :fetch/project-by-token token) + #_(->> (rp/req :fetch/project-by-token token) (rx/map :payload) (rx/map data-loaded)))) @@ -119,7 +119,7 @@ (let [existing (get-in state [:images id])] (if existing (rx/empty) - (->> (rp/req :fetch/image {:id id}) + #_(->> (rp/req :fetch/image {:id id}) (rx/map :payload) (rx/map image-fetched)))))) diff --git a/frontend/tools.clj b/frontend/tools.clj index c9c59ffec..e3bc28f78 100644 --- a/frontend/tools.clj +++ b/frontend/tools.clj @@ -2,6 +2,7 @@ '[clojure.java.shell :as shell] '[clojure.java.io :as io] '[clojure.edn :as edn] + '[clojure.string :as str] '[figwheel.main.api :as figwheel] '[environ.core :refer [env]]) (require '[cljs.build.api :as api] @@ -27,12 +28,21 @@ ;; --- Generic Build Options -(def demo? (edn/read-string (:uxbox-demo-warning env "true"))) - (def closure-defines - {'uxbox.config.url (:uxbox-api-url env "http://localhost:6060/api") - 'uxbox.config.viewurl (:uxbox-view-url env "/view/index.html") - 'uxbox.config.demo-warning demo?}) + (let [url (-> (:uxbox-api-url env "") + (str/trim)) + demo-warn (-> (:uxbox-demo-warning env "") + (str/trim))] + {'uxbox.config.url + (cond + (empty? url) "http://localhost:6060" + (str/starts-with? url "http") url + (str/starts-with? url "\"") (edn/read-string url)) + 'uxbox.config.demo-warning + (cond + (empty? demo-warn) false + (= "true" demo-warn) true + :else false)})) (def default-build-options {:cache-analysis true diff --git a/manage.sh b/manage.sh index a0887ffb8..15036e2e1 100755 --- a/manage.sh +++ b/manage.sh @@ -112,9 +112,8 @@ function build-frontend { --mount source=`pwd`,type=bind,target=/root/uxbox \ --mount source=${BUILDENV_IMGNAME}-m2,target=/root/.m2 \ -w /root/uxbox/frontend \ - -e UXBOX_API_URL="/api" \ - -e UXBOX_VIEW_URL="/view" \ - -e UXBOX_DEMO_WARNING=true \ + -e UXBOX_API_URL=${UXBOX_API_URL} \ + -e UXBOX_DEMO_WARNING=${UXBOX_DEMO_WARNING} \ $IMAGE ./scripts/build-app.sh }