Skip to content

Commit

Permalink
Match the changes in collectibles api in status-go (#18033)
Browse files Browse the repository at this point in the history
  • Loading branch information
vkjr authored and yevh-berdnyk committed Dec 8, 2023
1 parent 4636a18 commit 8d09b02
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 44 deletions.
12 changes: 7 additions & 5 deletions src/status_im2/contexts/wallet/collectible/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[utils.re-frame :as rf]))

(defn header
[{:keys [name description collection-image-url]}]
[{:keys [name description] :as _collectible-details} collection-image-url]
[rn/view {:style style/header}
[quo/text
{:weight :semi-bold
Expand Down Expand Up @@ -112,9 +112,11 @@

(defn view
[]
(let [collectible-details (rf/sub [:wallet/last-collectible-details])
{:keys [name description preview-url traits id]} collectible-details
chain-id (get-in id [:contract-id :chain-id])]
(let [collectible (rf/sub [:wallet/last-collectible-details])
{:keys [collectible-data preview-url
collection-data]} collectible
{:keys [traits description]} collectible-data
chain-id (rf/sub [:wallet/last-collectible-chain-id])]
[scroll-page/scroll-page
{:navigate-back? true
:height 148
Expand All @@ -129,7 +131,7 @@
[rn/image
{:source preview-url
:style style/preview}]]
[header collectible-details]
[header collectible-data (:image-url collection-data)]
[cta-buttons]
[tabs]
[info chain-id]
Expand Down
68 changes: 45 additions & 23 deletions src/status_im2/contexts/wallet/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,10 @@
(def collectibles-request-batch-size 1000)

(defn displayable-collectible?
[{:keys [image-url animation-url]}]
(or (not (string/blank? animation-url))
(not (string/blank? image-url))))
[collectible]
(let [{:keys [image-url animation-url]} (:collectible-data collectible)]
(or (not (string/blank? animation-url))
(not (string/blank? image-url)))))

(defn store-collectibles
[{:keys [db]} [collectibles]]
Expand All @@ -243,28 +244,47 @@

(rf/reg-event-fx :wallet/store-last-collectible-details store-last-collectible-details)

(def collectible-data-types
{:unique-id 0
:header 1
:details 2
:community-header 3})

(def fetch-type
{:never-fetch 0
:always-fetch 1
:fetch-if-not-cached 2
:fetch-if-cache-old 3})

(def max-cache-age-seconds 3600)

(rf/reg-event-fx
:wallet/request-collectibles
(fn [{:keys [db]} [{:keys [start-at-index new-request?]}]]
(let [request-id 0
collectibles-filter nil
data-type (collectible-data-types :header)
fetch-criteria {:fetch-type (fetch-type :fetch-if-not-cached)
:max-cache-age-seconds max-cache-age-seconds}
request-params [request-id
[(chain/chain-id db)]
(map :address (:profile/wallet-accounts db))
collectibles-filter
start-at-index
collectibles-request-batch-size]]
{:json-rpc/call [{:method "wallet_filterOwnedCollectiblesAsync"
:params request-params
:on-success #()
:on-error (fn [error]
(log/error "failed to request collectibles"
{:event :wallet/request-collectibles
:error error
:params request-params}))}]
:fx (if new-request?
[[:dispatch [:wallet/clear-stored-collectibles]]]
[])})))
collectibles-request-batch-size
data-type
fetch-criteria]]
{:fx [[:json-rpc/call
[{:method "wallet_getOwnedCollectiblesAsync"
:params request-params
:on-success #()
:on-error (fn [error]
(log/error "failed to request collectibles"
{:event :wallet/request-collectibles
:error error
:params request-params}))}]]
(when new-request?
[:dispatch [:wallet/clear-stored-collectibles]])]})))

(rf/reg-event-fx :wallet/owned-collectibles-filtering-done
(fn [_ [{:keys [message]}]]
Expand All @@ -283,14 +303,16 @@
(fn [_ [collectible-id]]
(let [request-id 0
collectible-id-converted (cske/transform-keys csk/->PascalCaseKeyword collectible-id)
request-params [request-id [collectible-id-converted]]]
{:json-rpc/call [{:method "wallet_getCollectiblesDetailsAsync"
:params request-params
:on-error (fn [error]
(log/error "failed to request collectible"
{:event :wallet/get-collectible-details
:error error
:params request-params}))}]})))
data-type (collectible-data-types :details)
request-params [request-id [collectible-id-converted] data-type]]
{:fx [[:json-rpc/call
[{:method "wallet_getCollectiblesByUniqueIDAsync"
:params request-params
:on-error (fn [error]
(log/error "failed to request collectible"
{:event :wallet/get-collectible-details
:error error
:params request-params}))}]]]})))

(rf/reg-event-fx :wallet/get-collectible-details-done
(fn [_ [{:keys [message]}]]
Expand Down
31 changes: 17 additions & 14 deletions src/status_im2/contexts/wallet/events_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,27 @@

(deftest store-collectibles
(testing "(displayable-collectible?) helper function"
(let [expected-results [[true {:image-url "https://..." :animation-url "https://..."}]
[true {:image-url "" :animation-url "https://..."}]
[true {:image-url nil :animation-url "https://..."}]
[true {:image-url "https://..." :animation-url ""}]
[true {:image-url "https://..." :animation-url nil}]
[false {:image-url "" :animation-url nil}]
[false {:image-url nil :animation-url nil}]
[false {:image-url nil :animation-url ""}]
[false {:image-url "" :animation-url ""}]]]
(let [expected-results [[true
{:collectible-data {:image-url "https://..." :animation-url "https://..."}}]
[true {:collectible-data {:image-url "" :animation-url "https://..."}}]
[true {:collectible-data {:image-url nil :animation-url "https://..."}}]
[true {:collectible-data {:image-url "https://..." :animation-url ""}}]
[true {:collectible-data {:image-url "https://..." :animation-url nil}}]
[false {:collectible-data {:image-url "" :animation-url nil}}]
[false {:collectible-data {:image-url nil :animation-url nil}}]
[false {:collectible-data {:image-url nil :animation-url ""}}]
[false {:collectible-data {:image-url "" :animation-url ""}}]]]
(doseq [[result collection] expected-results]
(is (= result (events/displayable-collectible? collection))))))
(testing "save-collectibles-request-details"
(let [db {:wallet {}}
collectibles [{:image-url "https://..." :animation-url "https://..."}
{:image-url "" :animation-url "https://..."}
{:image-url "" :animation-url nil}]
expected-db {:wallet {:collectibles [{:image-url "https://..." :animation-url "https://..."}
{:image-url "" :animation-url "https://..."}]}}
collectibles [{:collectible-data {:image-url "https://..." :animation-url "https://..."}}
{:collectible-data {:image-url "" :animation-url "https://..."}}
{:collectible-data {:image-url "" :animation-url nil}}]
expected-db {:wallet {:collectibles [{:collectible-data
{:image-url "https://..." :animation-url "https://..."}}
{:collectible-data
{:image-url "" :animation-url "https://..."}}]}}
effects (events/store-collectibles {:db db} [collectibles])
result-db (:db effects)]
(is (= result-db expected-db)))))
Expand Down
10 changes: 8 additions & 2 deletions src/status_im2/subs/wallet/collectibles.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@
:<- [:wallet]
(fn [wallet]
(map (fn [collectible]
(assoc collectible :preview-url (preview-url collectible)))
(assoc collectible :preview-url (preview-url (:collectible-data collectible))))
(:collectibles wallet))))

(re-frame/reg-sub
:wallet/last-collectible-details
:<- [:wallet]
(fn [wallet]
(let [last-collectible (:last-collectible-details wallet)]
(assoc last-collectible :preview-url (preview-url last-collectible)))))
(assoc last-collectible :preview-url (preview-url (:collectible-data last-collectible))))))

(re-frame/reg-sub
:wallet/last-collectible-chain-id
:<- [:wallet/last-collectible-details]
(fn [collectible]
(get-in collectible [:id :contract-id :chain-id])))

0 comments on commit 8d09b02

Please sign in to comment.