Skip to content

Commit a891d78

Browse files
authored
SELF-1144 - use real points on home screen and improve points screen (#1361)
* fix whitespace * move effects for fetching points and incoming points to hooks, add items to deps array so that they refresh when we expect points to change. * cleanup
1 parent b93804b commit a891d78

File tree

4 files changed

+72
-39
lines changed

4 files changed

+72
-39
lines changed

app/src/components/NavBar/Points.tsx

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { useFocusEffect, useNavigation } from '@react-navigation/native';
1111
import type { NativeStackNavigationProp } from '@react-navigation/native-stack';
1212

1313
import { PointHistoryList } from '@/components/PointHistoryList';
14+
import { useIncomingPoints, usePoints } from '@/hooks/usePoints';
1415
import BellWhiteIcon from '@/images/icons/bell_white.svg';
1516
import ClockIcon from '@/images/icons/clock.svg';
1617
import LockWhiteIcon from '@/images/icons/lock_white.svg';
@@ -36,24 +37,17 @@ import {
3637
} from '@/utils/notifications/notificationService';
3738
import {
3839
formatTimeUntilDate,
39-
getIncomingPoints,
40-
getPointsAddress,
41-
getTotalPoints,
42-
type IncomingPoints,
4340
recordBackupPointEvent,
4441
recordNotificationPointEvent,
4542
} from '@/utils/points';
4643

4744
const Points: React.FC = () => {
48-
const [selfPoints, setSelfPoints] = useState(0);
4945
const { bottom } = useSafeAreaInsets();
5046
const navigation =
5147
useNavigation<NativeStackNavigationProp<RootStackParamList>>();
5248
const [isNovaSubscribed, setIsNovaSubscribed] = useState(false);
5349
const [isEnabling, setIsEnabling] = useState(false);
54-
const [incomingPoints, setIncomingPoints] = useState<IncomingPoints | null>(
55-
null,
56-
);
50+
const incomingPoints = useIncomingPoints();
5751
const loadEvents = usePointEventStore(state => state.loadEvents);
5852
const { hasCompletedBackupForPoints, setBackupForPointsCompleted } =
5953
useSettingStore();
@@ -85,14 +79,7 @@ const Points: React.FC = () => {
8579
loadEvents();
8680
}, [loadEvents]);
8781

88-
useEffect(() => {
89-
const fetchPoints = async () => {
90-
const address = await getPointsAddress();
91-
const points = await getTotalPoints(address);
92-
setSelfPoints(points);
93-
};
94-
fetchPoints();
95-
}, []);
82+
const points = usePoints();
9683

9784
useEffect(() => {
9885
const checkSubscription = async () => {
@@ -102,14 +89,6 @@ const Points: React.FC = () => {
10289
checkSubscription();
10390
}, []);
10491

105-
useEffect(() => {
106-
const fetchIncomingPoints = async () => {
107-
const incoming = await getIncomingPoints();
108-
setIncomingPoints(incoming);
109-
};
110-
fetchIncomingPoints();
111-
}, []);
112-
11392
const handleContentLayout = () => {
11493
if (!isContentReady) {
11594
setIsContentReady(true);
@@ -211,15 +190,12 @@ const Points: React.FC = () => {
211190

212191
setIsBackingUp(true);
213192
try {
193+
// this will add event to store and the new event will then trigger useIncomingPoints hook to refetch incoming points
214194
const response = await recordBackupPointEvent();
215195

216196
if (response.success) {
217197
setBackupForPointsCompleted();
218198

219-
const address = await getPointsAddress();
220-
const points = await getTotalPoints(address);
221-
setSelfPoints(points);
222-
223199
if (listRefreshRef.current) {
224200
await listRefreshRef.current();
225201
}
@@ -296,22 +272,14 @@ const Points: React.FC = () => {
296272
<XStack gap={4} alignItems="center">
297273
<Text
298274
color={black}
275+
textAlign="center"
299276
fontFamily="DIN OT"
300277
fontWeight="500"
301278
fontSize={32}
302279
lineHeight={32}
303280
letterSpacing={-1}
304281
>
305-
{`${selfPoints} Self `}
306-
</Text>
307-
<Text
308-
fontFamily="DIN OT"
309-
fontWeight="500"
310-
fontSize={32}
311-
lineHeight={32}
312-
letterSpacing={-1}
313-
>
314-
points
282+
{`${points} Self points`}
315283
</Text>
316284
</XStack>
317285
<Text

app/src/components/PointHistoryList.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ const styles = StyleSheet.create({
348348
justifyContent: 'center',
349349
alignItems: 'center',
350350
paddingHorizontal: 20,
351+
paddingTop: 5,
351352
},
352353
});
353354

app/src/hooks/usePoints.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { useEffect, useState } from 'react';
2+
3+
import { usePointEventStore } from '@/stores/pointEventStore';
4+
import {
5+
getIncomingPoints,
6+
getNextSundayNoonUTC,
7+
getPointsAddress,
8+
getTotalPoints,
9+
type IncomingPoints,
10+
} from '@/utils/points';
11+
12+
/*
13+
* Hook to fetch incoming points for the user. It refetches the incoming points when there is a new event in the point events store.
14+
*/
15+
export const useIncomingPoints = (): IncomingPoints | null => {
16+
const [incomingPoints, setIncomingPoints] = useState<null | IncomingPoints>(
17+
null,
18+
);
19+
const pointEvents = usePointEventStore(state => state.events);
20+
const pointEventsCount = pointEvents.length;
21+
22+
useEffect(() => {
23+
const fetchIncomingPoints = async () => {
24+
try {
25+
const points = await getIncomingPoints();
26+
setIncomingPoints(points);
27+
} catch (error) {
28+
console.error('Error fetching incoming points:', error);
29+
}
30+
};
31+
fetchIncomingPoints();
32+
// when we record a new point event, we want to refetch incoming points
33+
}, [pointEventsCount]);
34+
35+
return incomingPoints;
36+
};
37+
38+
/*
39+
* Hook to fetch total points for the user. It refetches the total points when the next points update time is reached (each Sunday noon UTC).
40+
*/
41+
export const usePoints = () => {
42+
const [truePoints, setTruePoints] = useState({
43+
points: 0,
44+
});
45+
const nextPointsUpdate = getNextSundayNoonUTC().getTime();
46+
47+
useEffect(() => {
48+
const fetchPoints = async () => {
49+
try {
50+
const address = await getPointsAddress();
51+
const points = await getTotalPoints(address);
52+
setTruePoints({ points });
53+
} catch (error) {
54+
console.error('Error fetching total points:', error);
55+
}
56+
};
57+
fetchPoints();
58+
// refresh when points update time changes as its the only time points can change
59+
}, [nextPointsUpdate]);
60+
61+
return truePoints.points;
62+
};

app/src/screens/home/HomeScreen.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import IdCardLayout from '@/components/homeScreen/idCard';
2424
import { useAppUpdates } from '@/hooks/useAppUpdates';
2525
import useConnectionModal from '@/hooks/useConnectionModal';
2626
import { useEarnPointsFlow } from '@/hooks/useEarnPointsFlow';
27+
import { usePoints } from '@/hooks/usePoints';
2728
import { useReferralConfirmation } from '@/hooks/useReferralConfirmation';
2829
import { useTestReferralFlow } from '@/hooks/useTestReferralFlow';
2930
import LogoInversed from '@/images/logo_inversed.svg';
@@ -52,7 +53,8 @@ const HomeScreen: React.FC = () => {
5253
Record<string, { data: IDDocument; metadata: DocumentMetadata }>
5354
>({});
5455
const [loading, setLoading] = useState(true);
55-
const [selfPoints] = useState(312);
56+
57+
const selfPoints = usePoints();
5658

5759
// Calculate card dimensions exactly like IdCardLayout does
5860
const { width: screenWidth } = Dimensions.get('window');

0 commit comments

Comments
 (0)