Skip to content

Commit

Permalink
refresh pages when focused
Browse files Browse the repository at this point in the history
  • Loading branch information
in-mai-space committed Dec 3, 2024
1 parent d7a298c commit 3c9d4f0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
17 changes: 13 additions & 4 deletions frontend/app/(app)/(tabs)/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ import {
import { useAuthStore } from '../../../auth/authStore';
import HomeMenu from '../../../components/home/menu-bar';
import { Category, Filter } from '../../../consts/home-menu';
import { useEffect, useState } from 'react';
import { useCallback, useEffect, useState } from 'react';
import { useUserFollowingPosts } from '../../../hooks/user';
import BigDiveLog from '../../../components/divelog/divelog';
import NearbyDiveLog from '../../../components/home/nearby-divelog';
import { MasonryFlashList } from '@shopify/flash-list';
import DiveLogSkeleton from '../divelog/components/skeleton';
import * as Location from 'expo-location';
import { DEFAULT_SHERM_LOCATION } from '../../../consts/location';
Expand All @@ -23,7 +22,8 @@ import FilterMenu from '../../../components/home/filter';
import usePulsingAnimation from '../../../utils/skeleton';
import { useInfoPopup } from '../../../contexts/info-popup-context';
import InfoPopup from '../../../components/info-popup';
import { InfiniteData } from '@tanstack/react-query';
import { useQueryClient } from '@tanstack/react-query';
import { useFocusEffect } from 'expo-router';

const Home = () => {
const { setClose } = useInfoPopup();
Expand Down Expand Up @@ -95,6 +95,16 @@ const Home = () => {
}
}, [selectedFilters, selectedCategory, nearbyPosts]);

useFocusEffect(
useCallback(() => {
if (selectedCategory === Category.NEARBY) {
refetchNearByPosts();
} else {
refetchFollowingPosts();
}
}, []),
);

// render a following post
const renderFollowingPost = ({ item }: { item: any }) => (
<View className="w-full">
Expand Down Expand Up @@ -245,7 +255,6 @@ const Home = () => {
}}
*/
>

<View className="flex flex-row">
<View className="w-[48%] mr-[2%]">
{split(nearbyPosts?.pages.flatMap((page) => page) || [])[0].map(
Expand Down
8 changes: 7 additions & 1 deletion frontend/app/(app)/(tabs)/notification.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useFocusEffect } from 'expo-router';
import React, { useEffect, useState } from 'react';
import React, { useCallback, useEffect, useState } from 'react';
import { FlatList, SafeAreaView, SectionList, Text, View } from 'react-native';
import { useAuthStore } from '../../../auth/authStore';
import NotificationEntry from '../../../components/notification/notification';
Expand All @@ -23,6 +23,12 @@ const Notification = () => {
isFetchingNextPage,
} = useUserNotification(mongoDBId || '');

useFocusEffect(
useCallback(() => {
refetch();
}, [refetch]),
);

const groupNotificationsAndSetSections = (pages: any) => {
if (!pages) return;

Expand Down
19 changes: 17 additions & 2 deletions frontend/app/(app)/user/components/menu.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useCallback, useState } from 'react';
import {
Dimensions,
FlatList,
Expand All @@ -22,6 +22,7 @@ import Species from './species';
import BigDiveLog from '../../../../components/divelog/divelog';
import PopulatedInfoPopupButton from '../../../../components/populated-info-popup';
import DiveLogSkeleton from '../../divelog/components/skeleton';
import { useFocusEffect } from 'expo-router';

const Menu = ({ id }: { id: string }) => {
const { width } = Dimensions.get('window');
Expand Down Expand Up @@ -51,6 +52,8 @@ const Menu = ({ id }: { id: string }) => {
fetchNextPage: diveLogFetchNextPage,
hasNextPage: diveLogHasNextPage,
isFetchingNextPage: diveLogIsFetchingNextPage,
refetch: refetchDivelog,
isRefetching: isRefetchingDivelog,
} = useUserDiveLogs(id);

const {
Expand All @@ -60,8 +63,20 @@ const Menu = ({ id }: { id: string }) => {
fetchNextPage: speciesFetchNextPage,
hasNextPage: speciesHasNextPage,
isFetchingNextPage: speciesIsFetchingNextPage,
refetch: refetchSpecies,
} = useUserSpecies(id);

useFocusEffect(
useCallback(() => {
console.log(`Refetching ${category} data`);
if (category === 'Dives') {
refetchDivelog();
} else if (category === 'Species') {
refetchSpecies();
}
}, [refetchDivelog, refetchSpecies, category]),
);

const diveLogData = diveLogPages?.pages.flatMap((page) => page) ?? [];
const speciesData = speciesPages?.pages.flatMap((page) => page) ?? [];

Expand Down Expand Up @@ -165,7 +180,7 @@ const Menu = ({ id }: { id: string }) => {
</View>

{category === 'Dives' &&
(diveLogIsLoading ? (
(diveLogIsLoading || isRefetchingDivelog ? (
<FlatList
data={[1, 2]}
renderItem={() => (
Expand Down
9 changes: 9 additions & 0 deletions frontend/app/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,21 @@ import {
configureReanimatedLogger,
ReanimatedLogLevel,
} from 'react-native-reanimated';
import * as Notifications from 'expo-notifications';

configureReanimatedLogger({
level: ReanimatedLogLevel.warn,
strict: false,
});

Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: true,
shouldSetBadge: true,
}),
});

const queryClient = new QueryClient();

const InitialLayout = () => {
Expand Down

0 comments on commit 3c9d4f0

Please sign in to comment.