File size: 3,442 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
import React from 'react';
import { Box } from '@chakra-ui/react';
import PostItemSkeleton from '../skeletons/PostItemSkeleton';
import PostItem from './PostItem';
import { useSelector } from 'react-redux';
import ErrorMessage from '../../utils/ErrorMessage';
import { getUserProfileData } from '../../helper/getUserProfileData';
import { useAuth } from '../../context/auth';
import {
   calcTotalDiscussion,
   calculateReaction,
} from '../../helper/calculateTotal';
import { useLocation, useNavigate } from 'react-router-dom';
import SortNavbar from '../sortNavbar/SortNavbar';
import { sortPosts } from '../../helper/sortPosts';

const AllPost = ({ transformedData, loading, err }) => {
   const user = useAuth();
   const navigate = useNavigate();
   const location = useLocation();

   const profileData = useSelector((state) => state.profileData.profileData);

   let allPostData = [];
   if (transformedData && !loading && !err) {
      allPostData = transformedData.filter((postData) => !postData.draft);
   }

   const handleClickNavItem = (value) => {
      if (value === 'relevant') {
         navigate('/');
         return;
      }

      navigate(`/?sort=${value}`);
   };

   const queryParam = new URLSearchParams(location.search);
   const sort = queryParam.get('sort');
   const followingTags =
      profileData?.find((userData) => userData.id === user?.userId)
         ?.followingTags || [];

   // sorting posts =>  [relevant | latest | top]
   const currentPosts = sortPosts(sort, allPostData, followingTags);

   return (
      <Box flex='2' maxW={{ base: '100%', md: '650px' }}>
         {/* navbar */}
         <SortNavbar handleClickNavItem={handleClickNavItem} />

         {/* posts */}
         <Box>
            {err && <ErrorMessage offline={true} />}

            {loading && !err && (
               <>
                  <PostItemSkeleton firstItem={true} />
                  <PostItemSkeleton />
                  <PostItemSkeleton />
                  <PostItemSkeleton />
                  <PostItemSkeleton />
               </>
            )}

            {currentPosts.map((postData, idx) => (
               <PostItem
                  isFirstItem={idx === 0}
                  key={postData.id}
                  name={postData.name}
                  username={postData.username}
                  profile={postData.profile}
                  coverImg={postData.cvImg}
                  id={postData.id}
                  createdAt={postData.createdAt}
                  title={postData.title}
                  tags={postData.tags}
                  readTime={postData.readTime}
                  isUpdated={postData?.updated}
                  userId={postData.userId}
                  currentUserId={user?.userId} // authenticated userId
                  showHover={true}
                  currentUserProfile={getUserProfileData(
                     profileData,
                     postData.userId
                  )}
                  totalDiscussion={calcTotalDiscussion(postData.comments)}
                  totalReaction={calculateReaction(
                     postData.heart,
                     postData.unicorn,
                     postData.saved
                  )}
                  saved={postData.saved}
                  alreadySaved={postData.saved?.includes(user?.userId)}
               />
            ))}
         </Box>
      </Box>
   );
};

export default AllPost;