Run courses refresh only when Courses tab is focused

This commit is contained in:
Adolfo Reyna
2026-02-24 00:41:50 -05:00
parent 1f38b43d64
commit 7f7fb8efb7

View File

@@ -1,6 +1,7 @@
import React, { useEffect } from "react"; import React, { useEffect } from "react";
import { Searchbar, Title } from 'react-native-paper'; import { Searchbar, Title } from 'react-native-paper';
import { ScrollView, ActivityIndicator, StyleSheet, SafeAreaView, View } from 'react-native'; import { ScrollView, ActivityIndicator, StyleSheet, SafeAreaView, View } from 'react-native';
import { useIsFocused } from "@react-navigation/native";
import API from "../API"; import API from "../API";
import CourseCard from "../components/CourseCard"; import CourseCard from "../components/CourseCard";
import { useSnapshot } from 'valtio'; import { useSnapshot } from 'valtio';
@@ -147,6 +148,7 @@ const getCoursesCache = async () => {
const Courses = () => { const Courses = () => {
const gState = useSnapshot(GlobalState); const gState = useSnapshot(GlobalState);
const viewer = gState.me; const viewer = gState.me;
const isFocused = useIsFocused();
const [searchQuery, setSearchQuery] = React.useState(''); const [searchQuery, setSearchQuery] = React.useState('');
const [groups, setGroups] = React.useState([]); const [groups, setGroups] = React.useState([]);
const [popular, setPopular] = React.useState([]); const [popular, setPopular] = React.useState([]);
@@ -154,6 +156,7 @@ const Courses = () => {
const [queryTimer, setQueryTimer] = React.useState(0); const [queryTimer, setQueryTimer] = React.useState(0);
useEffect(() => { useEffect(() => {
if (!isFocused) return;
let subscribed = true; let subscribed = true;
const getData = async () => { const getData = async () => {
const cached = await getCoursesCache(); const cached = await getCoursesCache();
@@ -184,7 +187,7 @@ const Courses = () => {
return () => { return () => {
subscribed = false; subscribed = false;
} }
}, [viewer]) }, [isFocused, viewer?._id])
const onChangeSearch = query => { const onChangeSearch = query => {
setSearchQuery(query); setSearchQuery(query);