File size: 5,798 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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
import React from 'react';
import { Box, HStack } from '@chakra-ui/react';
import { useState } from 'react';
import { useEffect } from 'react';
import { useSelector } from 'react-redux';
import { Navigate, useLocation, useNavigate } from 'react-router-dom';
import Header from '../components/savedPosts/Header';
import Left from '../components/savedPosts/Left';
import Right from '../components/savedPosts/Right';
import { useAuth } from '../context/auth';
import ErrorMessage from '../utils/ErrorMessage';
import Error from './Error';
const SavedPosts = () => {
const user = useAuth();
const userId = user?.userId;
const navigate = useNavigate();
const location = useLocation();
// const [savedPosts, setSavedPosts] = useState([]);
// const [archivedPosts, setArchivedPosts] = useState([]);
const [allTags, setAllTags] = useState([]);
const [selectedTagName, setSelectedTagName] = useState('All tags');
const [searchTerm, setSearchTerm] = useState('');
//scroll top
useEffect(() => window.scrollTo(0, 0), [selectedTagName]);
const {
transformedData,
transformedDataLoading: loading,
transformedDataErr: err,
} = useSelector((state) => state.transformedData);
const queryParam = new URLSearchParams(location.search);
const query = queryParam.get('');
/*option 1 */
let savedPosts = [];
let archivedPosts = [];
if (transformedData) {
savedPosts = transformedData.filter(
(postItem) =>
postItem.saved?.includes(userId) &&
!postItem.archived?.includes(userId)
);
archivedPosts = transformedData.filter((postItem) =>
postItem.archived?.includes(userId)
);
}
useEffect(() => {
if (transformedData) {
const savedPosts = transformedData.filter(
(postItem) =>
postItem.saved?.includes(userId) &&
!postItem.archived?.includes(userId)
);
const archivedPosts = transformedData.filter((postItem) =>
postItem.archived?.includes(userId)
);
const currentPosts = query ? archivedPosts : savedPosts;
const allTags = [{ tagName: 'All tags', active: true }];
currentPosts.forEach((postData) => {
if (postData.tags.length !== 0) {
allTags.push(...postData.tags);
}
});
const transform = new Set(allTags.map((item) => item.tagName));
const transformedTags = [...transform].map((tagName) =>
tagName === 'All tags' ? { tagName, active: true } : { tagName }
);
setAllTags(transformedTags);
setSelectedTagName('All tags');
}
}, [transformedData, query, userId]);
/* option 2 (using state)
=> setting data inside useEffect takes a while to get data
=> if i create state and set the state inside useEffect , although loading is false but need to wait stateChange and compnent rerender time to finish
*/
// useEffect(() => {
// if (transformedData) {
// const savedPosts = transformedData.filter(
// (postItem) =>
// postItem.saved?.includes(userId) &&
// !postItem.archived?.includes(userId)
// );
// const archivedPosts = transformedData.filter((postItem) =>
// postItem.archived?.includes(userId)
// );
// const currentPosts = query ? archivedPosts : savedPosts;
// const allTags = [{ tagName: 'All tags', active: true }];
// currentPosts.forEach((postData) => {
// if (postData.tags.length !== 0) {
// allTags.push(...postData.tags);
// }
// });
// const transform = new Set(allTags.map((item) => item.tagName));
// const transformedTags = [...transform].map((tagName) =>
// tagName === 'All tags' ? { tagName, active: true } : { tagName }
// );
// setSavedPosts(savedPosts);
// setArchivedPosts(archivedPosts);
// setAllTags(transformedTags);
// setSelectedTagName('All tags');
// }
// }, [query, transformedData, userId]);
if (!user) {
return <Navigate to='/create-account' replace />;
}
if (err) {
return <ErrorMessage offline={true} />;
}
const handleClickTag = (tagName) => {
const transformedTags = allTags.map((item) =>
item.tagName === tagName
? { ...item, active: true }
: { ...item, active: false }
);
setSelectedTagName(tagName);
setAllTags(transformedTags);
};
const handleSearch = ({ target }) => {
setSearchTerm(target.value);
};
if (query !== 'archive' && query !== null) {
return <Error />;
}
const toggleViewArchive = () => {
if (query) {
navigate('/readinglist');
} else {
navigate('/readinglist?=archive');
}
};
return (
<Box flex='1' maxW='1280px' w='100%' p={{ md: '.5rem', xl: '1rem' }}>
<Header
readingCount={savedPosts.length}
archiveCount={archivedPosts.length}
allTags={allTags}
handleClickTag={handleClickTag}
selectedTagName={selectedTagName}
handleSearch={handleSearch}
toggleViewArchive={toggleViewArchive}
/>
<HStack mt='1rem' align='flex-start'>
<Left allTags={allTags} handleClickTag={handleClickTag} />
<Right
savedPosts={savedPosts}
archivedPosts={archivedPosts}
selectedTagName={selectedTagName}
searchTerm={searchTerm}
loading={loading}
/>
</HStack>
</Box>
);
};
export default SavedPosts;
|