import AsyncStorage from '@react-native-community/async-storage'; import React, {useCallback, useEffect, useState} from 'react'; import {LayoutChangeEvent, StyleSheet, View} from 'react-native'; import Animated from 'react-native-reanimated'; import {AuthContext, ProfileContext} from '../../routes/'; import {MomentType} from 'src/types'; import {defaultMoments} from '../../constants'; import {SCREEN_HEIGHT} from '../../utils'; import TaggsBar from '../taggs/TaggsBar'; import {Moment} from '../moments'; import ProfileBody from './ProfileBody'; import ProfileCutout from './ProfileCutout'; import ProfileHeader from './ProfileHeader'; import {followOrUnfollowUser} from '../../services'; interface ContentProps { y: Animated.Value; isProfileView: boolean; } const Content: React.FC = ({y, isProfileView}) => { const [profileBodyHeight, setProfileBodyHeight] = useState(0); const {user, moments, followers, following, updateFollowers} = isProfileView ? React.useContext(ProfileContext) : React.useContext(AuthContext); const {logout} = React.useContext(AuthContext); const { user: loggedInUser, updateFollowers: updateLoggedInUserFollowers, } = React.useContext(AuthContext); /** * States */ const [imagesMap, setImagesMap] = useState>( new Map(), ); const [followed, setFollowed] = React.useState(false); /** * If own profile is being viewed then do not show the follow button. */ const isOwnProfile = loggedInUser.username === user.username; const onLayout = (e: LayoutChangeEvent) => { const {height} = e.nativeEvent.layout; setProfileBodyHeight(height); }; const {userId} = user; const createImagesMap = useCallback(() => { var map = new Map(); moments.forEach(function (imageObject) { var moment_category = imageObject.moment_category; if (map.has(moment_category)) { map.get(moment_category).push(imageObject); } else { map.set(moment_category, [imageObject]); } }); setImagesMap(map); }, [moments]); useEffect(() => { if (!userId) { return; } createImagesMap(); }, [createImagesMap]); /** * This hook is called load of profile and when you push update the followers list. */ useEffect(() => { if (!userId) { return; } const isActuallyFollowed = followers.some( (follower) => follower.username === loggedInUser.username, ); if (followed != isActuallyFollowed) { setFollowed(isActuallyFollowed); } }, [followers]); /** * Handles a click on the follow / unfollow button. */ const handleFollowUnfollow = async () => { const token = await AsyncStorage.getItem('token'); if (!token) { logout(); return; } const isUpdatedSuccessful = await followOrUnfollowUser( loggedInUser.userId, userId, token, followed, ); if (isUpdatedSuccessful) { setFollowed(!followed); updateFollowers(true); updateLoggedInUserFollowers(true); } }; return ( y.setValue(e.nativeEvent.contentOffset.y)} showsVerticalScrollIndicator={false} scrollEventThrottle={1}> {defaultMoments.map((title, index) => ( ))} ); }; const styles = StyleSheet.create({ container: { flex: 1, }, momentsContainer: { backgroundColor: '#f2f2f2', paddingBottom: SCREEN_HEIGHT / 10, }, }); export default Content;