File size: 3,617 Bytes
f5071ca |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
import { Box, Flex } from '@chakra-ui/react';
import React, { useEffect, useState } from 'react';
import { useLocation, useParams } from 'react-router-dom';
import { useSelector } from 'react-redux';
import TopLayer from '../components/profile/TopLayer';
import ProfileLeftPart from '../components/profile/ProfileLeftPart';
import ErrorMessage from '../utils/ErrorMessage';
import ProfileSkeleton from '../components/skeletons/ProfileSkeleton';
import { claculateWrittenComments } from '../helper/calculateTotal';
import ProfileRightPart from '../components/profile/ProfileRightPart';
import Error from './Error';
const Profile = () => {
// scroll top
const location = useLocation();
useEffect(() => window.scrollTo(0, 0), [location]);
const { username } = useParams();
const [moreInfo, setMoreInfo] = useState(false);
const { profileData, profileDataLoading, profileDataErr } = useSelector(
(state) => state.profileData
);
const { transformedData } = useSelector((state) => state.transformedData);
let currentUserProfile = null;
let userId;
if (profileData) {
currentUserProfile = profileData.find(
(data) => data.username === username
);
userId = currentUserProfile?.id;
}
if (profileDataErr) {
return <ErrorMessage offline={true} />;
}
if (!currentUserProfile && !profileDataLoading) {
return <Error />;
}
if (profileDataLoading) {
return <ProfileSkeleton />;
}
let pinnedPosts = null;
let otherPosts = null;
let totalCommentWritten = 0;
if (transformedData) {
otherPosts = transformedData
.filter(
(postData) =>
postData.userId === userId && !postData.draft && !postData.pinned
)
.sort((a, b) => b.createdAt - a.createdAt);
pinnedPosts = transformedData
.filter((postData) => postData.userId === userId && postData.pinned)
.sort((a, b) => b.createdAt - a.createdAt);
totalCommentWritten = transformedData.reduce(
(count, postItem) =>
count + claculateWrittenComments(postItem.comments, userId),
0
);
}
const totalPublishPosts =
(pinnedPosts?.length || 0) + (otherPosts?.length || 0);
return (
<Box mt='-.5rem !important' w='100%' flex='1'>
<Box
h={['7rem', '7rem', '9rem']}
background={currentUserProfile?.background || '#000000'}
/>
<Box mx={{ base: 'none', md: '.5rem' }}>
<Box maxW='1000px' mx='auto'>
<TopLayer
profileData={currentUserProfile}
moreInfo={moreInfo}
setMoreInfo={setMoreInfo}
/>
{/* bottom layer */}
<Flex
mt='1rem'
align='flex-start'
flexDir={{ base: 'column', md: 'row' }}
>
{/* left */}
<ProfileLeftPart
publishedPosts={totalPublishPosts}
profileData={currentUserProfile}
totalCommentWritten={totalCommentWritten}
display={{
base: !moreInfo && 'none',
md: 'block',
}}
/>
{/* right */}
<ProfileRightPart
pinnedPosts={pinnedPosts}
otherPosts={otherPosts}
/>
</Flex>
</Box>
</Box>
</Box>
);
};
export default Profile;
|