File size: 4,548 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { Box, Heading, Text, useColorModeValue } from '@chakra-ui/react';
import React, { useRef } from 'react';
import { useEffect } from 'react';
import { useSelector } from 'react-redux';
import { useAuth } from '../../context/auth';
import {
   calcTotalDiscussion,
   calculateReaction,
} from '../../helper/calculateTotal';
import { getUserProfileData } from '../../helper/getUserProfileData';
import useGetQuerySearchTerm from '../../hooks/useGetQuerySearchTerm';
import ErrorMessage from '../../utils/ErrorMessage';
import PostItem from '../post/PostItem';
import PostItemSkeleton from '../skeletons/PostItemSkeleton';
import SearchInput from './SearchInput';

const Search = () => {
   const user = useAuth();
   const searchInputRef = useRef();

   const querySearchTerm = useGetQuerySearchTerm('spq') || '';

   // scroll top
   useEffect(() => window.scrollTo(0, 0), [querySearchTerm]);

   const {
      transformedData,
      transformedDataLoading: loading,
      transformedDataErr: err,
   } = useSelector((state) => state.transformedData);

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

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

   const searchedPostData = allPostData?.filter(
      (postData) =>
         postData.title.toLowerCase().includes(querySearchTerm.toLowerCase()) ||
         postData.name.toLowerCase().includes(querySearchTerm.toLowerCase())
   );

   const searchTermColor = useColorModeValue(
      'light.headingHover',
      'dark.headingHover'
   );
   const cardBg = useColorModeValue('light.cardBg', 'dark.cardBg');

   if (loading) {
      return (
         <Box flex='1' w='100%' maxW='650px'>
            <PostItemSkeleton />
            <PostItemSkeleton />
            <PostItemSkeleton />
         </Box>
      );
   }

   if (err) {
      return <ErrorMessage offline={true} />;
   }

   return (
      <Box flex={1} maxW={{ base: '100%', md: '650px' }} w='100%'>
         <SearchInput
            ref={searchInputRef}
            querySearchTerm={querySearchTerm}
            display={{ base: 'block', md: 'none' }}
            mb={5}
            route='search'
         />

         {searchedPostData && searchedPostData.length !== 0 ? (
            <>
               {querySearchTerm && (
                  <Heading
                     fontSize={{ base: '1.3rem', md: '1.5rem' }}
                     mb={4}
                     display={{ base: 'none', md: 'block' }}
                  >
                     Search results for '{' '}
                     <Text as='span' color={searchTermColor}>
                        {querySearchTerm}
                     </Text>{' '}
                     '
                  </Heading>
               )}

               {searchedPostData.map((postData) => (
                  <PostItem
                     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
               mt={5}
               p='5rem 1rem'
               textAlign='center'
               borderRadius='5px'
               bg={cardBg}
               className='shadow'
            >
               No results match that query 🤔
            </Box>
         )}
      </Box>
   );
};

export default Search;