penpot/src/uxbox/repo/colors.cljs
Jesús Espino 3f4ed6faa5 Colors management (#24)
* Colors management

* Minor indentation fixes.

* Remove redundant naming.

* Add missing block comment annotations.

* Use consistently defrecord instead of reify.

* Remove useless mapcat usage and simplify the related code.

* Start using more optimistic updates on collection operations.

* Remove println.

* Remove ^:const metadata.

* Remove neested let.

* Replace when with if on sablono templates.
2016-05-14 21:50:06 +03:00

61 lines
1.9 KiB
Clojure

;; 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 <niwi@niwi.nz>
(ns uxbox.repo.colors
"A main interface for access to remote resources."
(:require [beicon.core :as rx]
[uxbox.repo.core :refer (request url send!)]
[uxbox.state :as ust]
[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))))