Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wallet: Activity Items - Sections #19906

Merged
merged 20 commits into from
May 17, 2024
3 changes: 1 addition & 2 deletions src/status_im/contexts/wallet/account/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@
:size 32
:default-active @selected-tab
:data (tabs-data watch-only?)
:on-change (rn/use-callback (fn [tab]
(reset! selected-tab tab)))
:on-change (rn/use-callback (fn [tab] (reset! selected-tab tab)))
:scrollable? true
:scroll-on-press? true}]
[tabs/view {:selected-tab @selected-tab}]
Expand Down
12 changes: 7 additions & 5 deletions src/status_im/contexts/wallet/common/activity_tab/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@
{:title (i18n/label :t/no-activity)
:description (i18n/label :t/empty-tab-description)
:image (resources/get-themed-image :no-activity theme)}]
[rn/flat-list
{:data activity-list
:style {:flex 1}
:content-container-style {:padding-bottom jump-to.constants/floating-shell-button-height}
:render-fn activity-item}])))
[rn/section-list
{:sections activity-list
:sticky-section-headers-enabled false
:style {:flex 1}
:content-container-style {:padding-bottom jump-to.constants/floating-shell-button-height}
:render-fn activity-item
:render-section-header-fn (fn [{:keys [title]}] [quo/divider-date title])}])))
23 changes: 16 additions & 7 deletions src/status_im/subs/wallet/activities.cljs
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
(ns status-im.subs.wallet.activities
(:require [re-frame.core :as rf]))
(:require
[re-frame.core :as rf]
[status-im.contexts.wallet.common.activity-tab.constants :as constants]
[utils.datetime :as datetime]))

(rf/reg-sub
:wallet/all-activities
:<- [:wallet]
:-> :activities)

(rf/reg-sub
:wallet/activities-for-current-viewing-account
(rf/reg-sub :wallet/activities-for-current-viewing-account
:<- [:wallet/all-activities]
:<- [:wallet/current-viewing-account-address]
(fn [[activities current-viewing-account-address]]
(filter (fn [{:keys [sender recipient]}]
(or (= sender current-viewing-account-address)
(= recipient current-viewing-account-address)))
activities)))
(->> activities
(filter (fn [{:keys [sender recipient activity-type]}]
(let [receiving-activity? (= activity-type constants/wallet-activity-type-receive)
relevant-address (if receiving-activity? recipient sender)]
(= relevant-address current-viewing-account-address))))
(distinct)
(group-by (fn [{:keys [timestamp]}]
(datetime/timestamp->relative-short-date (* timestamp 1000))))
(map (fn [[date activities]]
{:title date :data activities :timestamp (:timestamp (first activities))}))
(sort-by (fn [{:keys [timestamp]}] (- timestamp))))))
33 changes: 33 additions & 0 deletions src/status_im/subs/wallet/activities_test.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
(ns status-im.subs.wallet.activities-test
(:require
[cljs.test :refer [is testing]]
[re-frame.db :as rf-db]
status-im.subs.root
status-im.subs.wallet.collectibles
[test-helpers.unit :as h]
[utils.re-frame :as rf]))

(h/deftest-sub :wallet/all-activities
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙌🏼

[sub-name]
(testing "Return the activities list from wallet data"
(swap! rf-db/app-db assoc-in
[:wallet :activities]
[{:id 1 :name "Transaction1"}
{:id 2 :name "Transaction2"}])
(is (= [{:id 1 :name "Transaction1"} {:id 2 :name "Transaction2"}] (rf/sub [sub-name])))))

(h/deftest-sub :wallet/activities-for-current-viewing-account
[sub-name]
(testing "Return activities filtered and grouped by account and dates"
(swap! rf-db/app-db
(fn [db]
(-> db
(assoc-in [:wallet :activities]
[{:sender "acc1" :recipient "acc2" :timestamp 1588291200}
{:sender "acc2" :recipient "acc1" :timestamp 1588377600}
{:sender "acc3" :recipient "acc4" :timestamp 1588464000}])
(assoc-in [:wallet :current-viewing-account-address] "acc1"))))
(is (= [{:title "May 1, 2020"
:data [{:sender "acc1" :recipient "acc2" :timestamp 1588291200}]
:timestamp 1588291200}]
(rf/sub [sub-name])))))
7 changes: 4 additions & 3 deletions src/status_im/subs/wallet/send.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@
:wallet/recent-recipients
:<- [:wallet/activities-for-current-viewing-account]
:<- [:wallet/current-viewing-account-address]
(fn [[activities current-viewing-account-address]]
(let [users-sent-transactions (filter (fn [{:keys [sender]}]
(fn [[sections current-viewing-account-address]]
(let [all-transactions (mapcat :data sections)
users-sent-transactions (filter (fn [{:keys [sender]}]
(= sender current-viewing-account-address))
activities)]
all-transactions)]
(set (map :recipient users-sent-transactions)))))

(rf/reg-sub
Expand Down
14 changes: 14 additions & 0 deletions src/utils/datetime.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,20 @@
(previous-years? datetime)
(.format ^js (date-fmt) datetime))))

(defn full-date->short-date
OmarBasem marked this conversation as resolved.
Show resolved Hide resolved
[s]
(let [words (string/split s #"\s+")
last-word (last words)]
(if (or (= "AM" last-word) (= "PM" last-word))
(string/join " " (take (- (count words) 2) words))
s)))

(defn timestamp->relative-short-date
[ms]
(->> ms
timestamp->relative
full-date->short-date))

(defn timestamp->mini-date
[ms]
(.format ^js (short-date-fmt)
Expand Down
29 changes: 29 additions & 0 deletions src/utils/datetime_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,35 @@
(is (= "Today 12:00 PM" (datetime/timestamp->relative 163684800000)))
(is (= "Today 12:00 AM" (datetime/timestamp->relative 163641600000))))))

(deftest timestamp->relative-short-date-test
(with-redefs [t/*ms-fn* (constantly 163696545000)
datetime/time-zone-offset (t/period :hours 0)
datetime/is-24-hour (constantly false)]
(testing "short date format for previous years"
(is (= "Dec 31, 1974" (datetime/timestamp->relative-short-date 157766399000)))
(is (= "Jan 1, 1973" (datetime/timestamp->relative-short-date 94694400000))))

(testing "short date format for dates 7 days ago or older, but in the current year"
(is (= "03 Mar" (datetime/timestamp->relative-short-date 163091745000)))
(is (= "02 Mar" (datetime/timestamp->relative-short-date 163004400000)))
(is (= "01 Jan" (datetime/timestamp->relative-short-date 157820400000))))

(testing "short date format for dates within the last 6 days"
(is (= "Sat" (datetime/timestamp->relative-short-date 163523745000)))
(is (= "Fri" (datetime/timestamp->relative-short-date 163437345000)))
(is (= "Thu" (datetime/timestamp->relative-short-date 163350945000)))
(is (= "Wed" (datetime/timestamp->relative-short-date 163264545000)))
(is (= "Tue" (datetime/timestamp->relative-short-date 163178145000))))

(testing "short date format for yesterday"
(is (= "Yesterday" (datetime/timestamp->relative-short-date 163610145000)))
(is (= "Yesterday" (datetime/timestamp->relative-short-date 163641599000))))

(testing "short date format for today, at various timestamps"
(is (= "Today" (datetime/timestamp->relative-short-date 163696545000)))
(is (= "Today" (datetime/timestamp->relative-short-date 163684800000)))
(is (= "Today" (datetime/timestamp->relative-short-date 163641600000))))))

OmarBasem marked this conversation as resolved.
Show resolved Hide resolved
#_((deftest day-relative-before-yesterday-force-24H-test
(with-redefs [t/*ms-fn* (constantly epoch-plus-3d)
datetime/is-24-hour (constantly true)
Expand Down