import { StatusBar } from 'expo-status-bar';
import React, { useState, useEffect } from 'react';
import { View, ActivityIndicator, StyleSheet, SafeAreaView, FlatList } from 'react-native';
import API from './../API.js';
import Post from './../components/Post.js';
import NewPost from "./../components/NewPost.js";
import ProfileHeader from '../components/ProfileHeader.js';
import AsyncStorage from '@react-native-async-storage/async-storage';
const storeProfilePosts = async (profileid, value) => {
try {
const jsonValue = JSON.stringify(value)
await AsyncStorage.setItem('profile_' + profileid, jsonValue)
} catch (e) {
}
}
const getProfilePosts = async (profileid) => {
try {
const value = await AsyncStorage.getItem('profile_' + profileid)
if (value !== null) {
console.log(JSON.parse(value))
return JSON.parse(value);
}
return [];
} catch (e) {
console.log('fail getProfilePosts', e)
return [];
}
}
let Profile = ({ navigation, route }) => {
let [Posts, setPosts] = useState([]);
let [profile, setProfile] = useState({});
useEffect(() => {
let subscribed = true;
const getData = async () => {
setPosts([]);
if (route.params && route.params.profileid) {
console.log('Loading Cache Profile:' + route.params.profileid);
await API.getUserProfile(route.params.profileid).then((profileObj) => {
if(!subscribed) return 0;
let profile = profileObj.profile
setProfile(profileObj);
navigation.setOptions({ title: profile.firstName + " " + profile.lastName });
});
await getProfilePosts(route.params.profileid).then(setPosts);
console.log('Loaded Cache Profile:' + route.params.profileid);
API.getPosts(route.params.profileid).then((data) => {
if(!subscribed) return 0;
setPosts(data);
storeProfilePosts(route.params.profileid, data);
console.log('Store Cache Profile:' + route.params.profileid);
});
} else {
navigation.navigate('Feed')
}
}
getData();
return ()=>{
subscribed = false;
}
}, [route.params]);
const renderPost = (({ item }) => {
if (item.nonOrganicType)
return (<>>);
return ();
});
const header = (
setPosts([newPost, ...Posts])} />
)
return (
{(Posts.length !== 0 || profile._id) &&
item._id || item.createdAt}
ListHeaderComponent={header}
refreshing={Posts.length === 0}
initialNumToRender={3}
maxToRenderPerBatch={3}
removeClippedSubviews={true}
onRefresh={() => {
API.getPosts(route.params.profileid).then(setPosts);
}}
/>
}
);
}
export default Profile;
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#edf2f7",
},
});