Last update to useEffect methods

This commit is contained in:
Adolfo Reyna
2022-11-21 17:25:44 -05:00
parent 7586a20447
commit 5cf38ae6d3
6 changed files with 88 additions and 50 deletions
+26 -17
View File
@@ -43,9 +43,9 @@ const getCourses = async (profileObj) => {
});
});
return {
courses: courses.slice(0,10),
popular: popular.slice(0,10),
watching: watchingArray.slice(0,10),
courses: courses.slice(0, 10),
popular: popular.slice(0, 10),
watching: watchingArray.slice(0, 10),
}
}
@@ -57,7 +57,7 @@ const storeCoursesCache = async (value) => {
}
}
const getCoursesCache= async () => {
const getCoursesCache = async () => {
try {
const value = await AsyncStorage.getItem('courses')
if (value !== null) {
@@ -77,19 +77,28 @@ const Courses = () => {
const [watching, setWatching] = React.useState([]);
const [queryTimer, setQueryTimer] = React.useState(0);
useEffect(async () => {
await getCoursesCache().then((r)=>{
console.log("Courses Cache");
setGroups(r.courses || []);
setPopular(r.popular || []);
setWatching(r.watching || []);
});
let r = await getCourses(viewer);
console.log("Courses Live");
setGroups(r.courses || []);
setPopular(r.popular || []);
setWatching(r.watching || []);
storeCoursesCache(r);
useEffect(() => {
let subscribed = true;
const getData = async () => {
await getCoursesCache().then((r) => {
console.log("Courses Cache");
setGroups(r.courses || []);
setPopular(r.popular || []);
setWatching(r.watching || []);
});
let r = await getCourses(viewer);
console.log("Courses Live");
if(subscribed){
setGroups(r.courses || []);
setPopular(r.popular || []);
setWatching(r.watching || []);
}
storeCoursesCache(r);
};
getData();
return () => {
subscribed = false;
}
}, [])
const onChangeSearch = query => {
+9 -6
View File
@@ -1,10 +1,8 @@
import React, { useEffect } from "react";
import { Searchbar } from 'react-native-paper';
import { View, ActivityIndicator, StyleSheet, SafeAreaView, FlatList } from 'react-native';
import { StyleSheet, SafeAreaView, FlatList } from 'react-native';
import API from "../API";
import UserName from "../components/UserName";
import GroupCard from "../components/GroupCard";
import ProfileSmallHeader from '../components/ProfileSmallHeader.js'
const Groups = () => {
const [searchQuery, setSearchQuery] = React.useState('');
@@ -12,9 +10,14 @@ const Groups = () => {
const [queryTimer, setQueryTimer] = React.useState(0);
useEffect(() => {
let subscribed = true;
API.getRecentGroups('').then((data) => {
setGroups(data.groups || []);
if (subscribed)
setGroups(data.groups || []);
});
return () => {
subscribed = false;
}
}, [])
@@ -39,7 +42,7 @@ const Groups = () => {
return (<GroupCard profileObj={item} />);
});
return (
<SafeAreaView style={{flex: 1}}>
<SafeAreaView style={{ flex: 1 }}>
<Searchbar
placeholder="Search Groups"
onChangeText={onChangeSearch}
@@ -48,7 +51,7 @@ const Groups = () => {
<FlatList
contentContainerStyle={styles.container}
numColumns={2}
columnWrapperStyle={{justifyContent: "space-evenly"}}
columnWrapperStyle={{ justifyContent: "space-evenly" }}
data={groups}
renderItem={renderProfile}
keyExtractor={item => item._id}
+8 -3
View File
@@ -12,9 +12,14 @@ const Search = () => {
const [queryTimer, setQueryTimer] = React.useState(0);
useEffect(() => {
let subscribed = true;
API.searchProfiles('').then((data) => {
setProfiles(data.profiles || []);
if (subscribed)
setProfiles(data.profiles || []);
});
return () => {
subscribed = false;
}
}, [])
const onChangeSearch = query => {
@@ -31,7 +36,7 @@ const Search = () => {
return (<ProfileCard profileObj={item} />);
});
return (
<SafeAreaView style={{flex:1}}>
<SafeAreaView style={{ flex: 1 }}>
<Searchbar
placeholder="Search Users"
onChangeText={onChangeSearch}
@@ -41,7 +46,7 @@ const Search = () => {
<FlatList
contentContainerStyle={styles.container}
numColumns={2}
columnWrapperStyle={{justifyContent: "space-evenly"}}
columnWrapperStyle={{ justifyContent: "space-evenly" }}
data={profiles}
renderItem={renderProfile}
keyExtractor={item => item._id}