import React, { useEffect } from "react"; import { Searchbar } from 'react-native-paper'; import { ScrollView, ActivityIndicator, StyleSheet, SafeAreaView, FlatList } from 'react-native'; import { Title } from 'react-native-paper'; import API from "../API"; import CourseCard from "../components/CourseCard"; const getCourses = async (profileObj) => { let courses; let popular; await API.getCourses().then((data) => { courses = data.groups; popular = [...data.groups].sort((a, b) => { return Object.keys(b.subscribed).length - Object.keys(a.subscribed).length; }); }); let watching = {}; let watchingProms = []; Object.keys(profileObj.data).forEach((videoId) => { if (profileObj.data[videoId].profileId) { let profileId = profileObj.data[videoId].profileId; watchingProms.push(API.getUserProfile(profileId).then(profile => { if (!profile.isCourse) return 0; if (!watching[profileId]) watching[profileId] = { profile, progress: [], mostRecent: 0 }; if (watching[profileId].mostRecent < profileObj.data[videoId].ts) watching[profileId].mostRecent = profileObj.data[videoId].ts; watching[profileId].progress.push(profileObj.data[videoId]); })); } }); let watchingArray = []; await Promise.all(watchingProms).then(() => { for (const courseId in watching) { watchingArray.push(watching[courseId]); } watchingArray = watchingArray.sort((a, b) => { return b.mostRecent - a.mostRecent; }); }); return { courses, popular, watching: watchingArray, } } const Courses = () => { const [Me, setMeProfile] = React.useState({}); const [searchQuery, setSearchQuery] = React.useState(''); const [groups, setGroups] = React.useState([]); const [popular, setPopular] = React.useState([]); const [watching, setWatching] = React.useState([]); const [queryTimer, setQueryTimer] = React.useState(0); useEffect(async () => { let Me = await API.getMe(); setMeProfile(Me); //API.getCourses('').then((data) => { // setGroups(data.groups || []); //}); let r = await getCourses(Me); setGroups(r.courses || []); setPopular(r.popular || []); setWatching(r.watching || []); }, []) const onChangeSearch = query => { setSearchQuery(query); if (queryTimer) clearTimeout(queryTimer); let timerId = setTimeout(() => { if (!query) { return API.getCourses('').then((data) => { setGroups(data.groups || []); }); } API.searchCourses(query).then((data) => { setGroups(data.groups || []); }) }, 300); setQueryTimer(timerId); }; const renderProfile = (({ item }) => { return (); }); const watchingCourse = (({ item }) => { return (); }); return ( {groups.length ? <> : } Continue Watching: item.profile._id} /> Recently Added: item._id} /> Popular: item._id} /> ) } export default Courses; const styles = StyleSheet.create({ container: { }, });