Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 97 additions & 40 deletions app/src/components/NavBar/Points.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,54 @@ const Points: React.FC = () => {
}, []),
);

// Detect when returning from backup screen and record points if backup was completed
useFocusEffect(
React.useCallback(() => {
const currentBackupEnabled =
useSettingStore.getState().cloudBackupEnabled;
const currentHasCompletedBackup =
useSettingStore.getState().hasCompletedBackupForPoints;

// If backup is enabled but points haven't been recorded yet, record them now
// This happens when user just completed backup and returned to this screen
if (currentBackupEnabled && !currentHasCompletedBackup) {
const recordPoints = async () => {
try {
const response = await recordBackupPointEvent();

if (response.success) {
useSettingStore.getState().setBackupForPointsCompleted();
selfClient.trackEvent(PointEvents.EARN_BACKUP_SUCCESS);

if (listRefreshRef.current) {
await listRefreshRef.current();
}

const callbackId = registerModalCallbacks({
onButtonPress: () => {},
onModalDismiss: () => {},
});
navigation.navigate('Modal', {
titleText: 'Success!',
bodyText:
'Account backed up successfully! You earned 100 points.\n\nPoints will be distributed to your wallet on the next Sunday at noon UTC.',
buttonText: 'OK',
callbackId,
});
} else {
selfClient.trackEvent(PointEvents.EARN_BACKUP_FAILED);
}
} catch (error) {
selfClient.trackEvent(PointEvents.EARN_BACKUP_FAILED);
console.error('Error recording backup points after return:', error);
}
};

recordPoints();
}
}, [navigation]),
);

// Mock function to check if user has backed up their account
const hasUserBackedUpAccount = (): boolean => {
return hasCompletedBackupForPoints;
Expand Down Expand Up @@ -208,58 +256,67 @@ const Points: React.FC = () => {
}
selfClient.trackEvent(PointEvents.EARN_BACKUP);

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

if (response.success) {
setBackupForPointsCompleted();
selfClient.trackEvent(PointEvents.EARN_BACKUP_SUCCESS);
if (listRefreshRef.current) {
await listRefreshRef.current();
}
const cloudBackupEnabled = useSettingStore.getState().cloudBackupEnabled;

const callbackId = registerModalCallbacks({
onButtonPress: () => {},
onModalDismiss: () => {},
});
navigation.navigate('Modal', {
titleText: 'Success!',
bodyText:
'Account backed up successfully! You earned 100 points.\n\nPoints will be distributed to your wallet on the next Sunday at noon UTC.',
buttonText: 'OK',
callbackId,
});
} else {
// If backup is already enabled, just record points
if (cloudBackupEnabled) {
setIsBackingUp(true);
try {
// this will add event to store and the new event will then trigger useIncomingPoints hook to refetch incoming points
const response = await recordBackupPointEvent();

if (response.success) {
setBackupForPointsCompleted();
selfClient.trackEvent(PointEvents.EARN_BACKUP_SUCCESS);

if (listRefreshRef.current) {
await listRefreshRef.current();
}

const callbackId = registerModalCallbacks({
onButtonPress: () => {},
onModalDismiss: () => {},
});
navigation.navigate('Modal', {
titleText: 'Success!',
bodyText:
'Account backed up successfully! You earned 100 points.\n\nPoints will be distributed to your wallet on the next Sunday at noon UTC.',
buttonText: 'OK',
callbackId,
});
} else {
selfClient.trackEvent(PointEvents.EARN_BACKUP_FAILED);
const callbackId = registerModalCallbacks({
onButtonPress: () => {},
onModalDismiss: () => {},
});
navigation.navigate('Modal', {
titleText: 'Verification Failed',
bodyText:
response.error || 'Failed to register points. Please try again.',
buttonText: 'OK',
callbackId,
});
}
} catch (error) {
selfClient.trackEvent(PointEvents.EARN_BACKUP_FAILED);
const callbackId = registerModalCallbacks({
onButtonPress: () => {},
onModalDismiss: () => {},
});
navigation.navigate('Modal', {
titleText: 'Verification Failed',
titleText: 'Error',
bodyText:
response.error || 'Failed to register points. Please try again.',
error instanceof Error ? error.message : 'Failed to backup account',
buttonText: 'OK',
callbackId,
});
} finally {
setIsBackingUp(false);
}
} catch (error) {
selfClient.trackEvent(PointEvents.EARN_BACKUP_FAILED);
const callbackId = registerModalCallbacks({
onButtonPress: () => {},
onModalDismiss: () => {},
});
navigation.navigate('Modal', {
titleText: 'Error',
bodyText:
error instanceof Error ? error.message : 'Failed to backup account',
buttonText: 'OK',
callbackId,
});
} finally {
setIsBackingUp(false);
} else {
// Navigate to backup screen and return to Points after backup completes
navigation.navigate('CloudBackupSettings', { returnToScreen: 'Points' });
}
};

Expand Down
3 changes: 2 additions & 1 deletion app/src/navigation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ export type RootStackParamList = Omit<
| undefined;
CloudBackupSettings:
| {
nextScreen?: string;
nextScreen?: 'SaveRecoveryPhrase';
returnToScreen?: 'Points';
}
| undefined;
AccountVerifiedSuccess: undefined;
Expand Down
8 changes: 7 additions & 1 deletion app/src/screens/account/settings/CloudBackupScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type NextScreen = keyof Pick<RootStackParamList, 'SaveRecoveryPhrase'>;
type CloudBackupScreenProps = StaticScreenProps<
| {
nextScreen?: NextScreen;
returnToScreen?: 'Points';
}
| undefined
>;
Expand All @@ -47,7 +48,8 @@ const CloudBackupScreen: React.FC<CloudBackupScreenProps> = ({
useSettingStore();
const { upload, disableBackup } = useBackupMnemonic();
const [pending, setPending] = useState(false);

const navigation =
useNavigation<NativeStackNavigationProp<RootStackParamList>>();
const { showModal } = useModal(
useMemo(
() => ({
Expand Down Expand Up @@ -98,6 +100,10 @@ const CloudBackupScreen: React.FC<CloudBackupScreenProps> = ({
toggleCloudBackupEnabled();
trackEvent(BackupEvents.CLOUD_BACKUP_ENABLED_DONE);
setPending(false);

if (params?.returnToScreen) {
navigation.navigate(params.returnToScreen);
}
}, [
cloudBackupEnabled,
getOrCreateMnemonic,
Expand Down
Loading