|
70 | 70 | (let [current-collectible-idx (get-in db [:wallet :accounts account :current-collectible-idx] 0) |
71 | 71 | collectibles-filter nil |
72 | 72 | data-type (collectible-data-types :header) |
73 | | - fetch-criteria {:fetch-type (fetch-type :fetch-if-not-cached) |
| 73 | + fetch-criteria {:fetch-type (fetch-type :fetch-if-cache-old) |
74 | 74 | :max-cache-age-seconds max-cache-age-seconds} |
75 | 75 | chain-ids (chain/chain-ids db) |
76 | 76 | request-params [request-id |
|
171 | 171 |
|
172 | 172 | (rf/reg-event-fx |
173 | 173 | :wallet/collectible-ownership-update-finished |
174 | | - (fn [{:keys [db]} [{:keys [accounts chainId]}]] |
| 174 | + (fn [{:keys [db]} [{:keys [accounts chainId message]}]] |
175 | 175 | (let [address (first accounts) |
176 | 176 | pending-chain-ids (get-in db [:wallet :ui :collectibles :updating address]) |
177 | 177 | updated-chain-ids (disj pending-chain-ids chainId) |
178 | | - all-chain-updated? (and (some? pending-chain-ids) (empty? updated-chain-ids))] |
| 178 | + all-chain-updated? (and (some? pending-chain-ids) (empty? updated-chain-ids)) |
| 179 | + collectible-id (data-store/rpc->collectible-id message)] |
179 | 180 | {:db (cond-> db |
180 | 181 | (some? pending-chain-ids) |
181 | 182 | (assoc-in [:wallet :ui :collectibles :updating address] updated-chain-ids)) |
182 | 183 | :fx [(when all-chain-updated? |
183 | | - [:dispatch [:wallet/request-collectibles-for-account address]])]}))) |
| 184 | + [:dispatch [:wallet/request-collectibles-for-account address]]) |
| 185 | + (when collectible-id |
| 186 | + (let [collectible-unique-id (collectible-utils/get-collectible-unique-id {:id |
| 187 | + collectible-id}) |
| 188 | + pending-collectible? (-> db |
| 189 | + (get-in [:wallet :ui :collectibles :pending]) |
| 190 | + (contains? collectible-unique-id))] |
| 191 | + (when pending-collectible? |
| 192 | + [:dispatch |
| 193 | + [:wallet/update-pending-collectible-details collectible-id |
| 194 | + collectible-unique-id]])))]}))) |
| 195 | + |
| 196 | +(rf/reg-event-fx |
| 197 | + :wallet/update-pending-collectible-details |
| 198 | + (fn [{:keys [db]} [collectible-id collectible-unique-id]] |
| 199 | + {:db (update-in db [:wallet :ui :collectibles :pending] dissoc collectible-unique-id) |
| 200 | + :fx [[:dispatch [:wallet/get-collectibles-by-unique-id-async collectible-id]]]})) |
184 | 201 |
|
185 | 202 | (defn- update-collectibles-in-account |
186 | 203 | [existing-collectibles updated-collectibles] |
|
282 | 299 | [{{collectible-id :id :as collectible} :collectible |
283 | 300 | aspect-ratio :aspect-ratio |
284 | 301 | gradient-color :gradient-color}]] |
| 302 | + {:db (assoc-in db |
| 303 | + [:wallet :ui :collectible] |
| 304 | + {:details collectible |
| 305 | + :aspect-ratio aspect-ratio |
| 306 | + :gradient-color gradient-color}) |
| 307 | + :fx [[:dispatch [:wallet/get-collectibles-by-unique-id-async collectible-id]] |
| 308 | + ;; We delay the navigation because we need re-frame to update the DB on time. |
| 309 | + ;; By doing it, we skip a blink while visiting the collectible detail page. |
| 310 | + [:dispatch-later |
| 311 | + {:ms 20 |
| 312 | + :dispatch [:open-modal :screen/wallet.collectible]}]]})) |
| 313 | + |
| 314 | +(rf/reg-event-fx |
| 315 | + :wallet/get-collectibles-by-unique-id-async |
| 316 | + (fn [_ [collectible-id]] |
285 | 317 | (let [request-id 0 |
286 | 318 | collectible-id-converted (cske/transform-keys transforms/->PascalCaseKeyword collectible-id) |
287 | 319 | data-type (collectible-data-types :details) |
288 | 320 | request-params [request-id [collectible-id-converted] data-type]] |
289 | | - {:db (assoc-in db |
290 | | - [:wallet :ui :collectible] |
291 | | - {:details collectible |
292 | | - :aspect-ratio aspect-ratio |
293 | | - :gradient-color gradient-color}) |
294 | | - :fx [[:json-rpc/call |
| 321 | + {:fx [[:json-rpc/call |
295 | 322 | [{:method "wallet_getCollectiblesByUniqueIDAsync" |
296 | 323 | :params request-params |
297 | 324 | :on-error (fn [error] |
298 | 325 | (log/error "failed to request collectible" |
299 | 326 | {:event :wallet/navigate-to-collectible-details |
300 | 327 | :error error |
301 | | - :params request-params}))}]] |
302 | | - ;; We delay the navigation because we need re-frame to update the DB on time. |
303 | | - ;; By doing it, we skip a blink while visiting the collectible detail page. |
304 | | - [:dispatch-later |
305 | | - {:ms 17 |
306 | | - :dispatch [:open-modal :screen/wallet.collectible]}]]}))) |
| 328 | + :params request-params}))}]]]}))) |
307 | 329 |
|
308 | 330 | (defn- keep-not-empty-value |
309 | 331 | [old-value new-value] |
|
318 | 340 | (transforms/json->clj message)) |
319 | 341 | {[collectible] :collectibles} response |
320 | 342 | known-collectible-info (get-in db [:wallet :ui :collectible :details]) |
| 343 | + current-account-address (get-in db [:wallet :current-viewing-account-address]) |
| 344 | + total-owned (collectible-utils/collectible-balance collectible |
| 345 | + current-account-address) |
321 | 346 | merged-collectible (as-> known-collectible-info c |
322 | 347 | (merge-with keep-not-empty-value |
323 | 348 | c |
324 | 349 | collectible) |
325 | 350 | (update c |
326 | 351 | :ownership |
327 | | - collectible-utils/remove-duplicates-in-ownership))] |
| 352 | + collectible-utils/remove-duplicates-in-ownership)) |
| 353 | + updated-collectible (assoc merged-collectible |
| 354 | + :total-owned |
| 355 | + total-owned)] |
328 | 356 | (if collectible |
329 | | - {:db (assoc-in db [:wallet :ui :collectible :details] merged-collectible)} |
| 357 | + {:db (assoc-in db [:wallet :ui :collectible :details] updated-collectible) |
| 358 | + :fx [[:dispatch [:wallet/update-collectibles-data updated-collectible]]]} |
330 | 359 | (log/error "failed to get collectible details" |
331 | 360 | {:event :wallet/get-collectible-details-done |
332 | 361 | :response response}))))) |
333 | 362 |
|
| 363 | +(rf/reg-event-fx |
| 364 | + :wallet/update-collectibles-data |
| 365 | + (fn [{:keys [db]} [collectible]] |
| 366 | + (let [collectibles-by-address (collectible-utils/group-collectibles-by-ownership-address collectible)] |
| 367 | + {:db (update-in db |
| 368 | + [:wallet :accounts] |
| 369 | + #(reduce-kv |
| 370 | + (fn [accounts address updated-collectibles] |
| 371 | + (if (contains? accounts address) |
| 372 | + (update-in accounts |
| 373 | + [address :collectibles] |
| 374 | + update-collectibles-in-account |
| 375 | + [updated-collectibles]) |
| 376 | + accounts)) |
| 377 | + % |
| 378 | + collectibles-by-address))}))) |
| 379 | + |
334 | 380 | (rf/reg-event-fx |
335 | 381 | :wallet/clear-collectible-details |
336 | 382 | (fn [{:keys [db]}] |
|
0 commit comments