File size: 4,502 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 |
import React from 'react';
import {
Box,
Flex,
HStack,
Text,
useColorModeValue,
WrapItem,
} from '@chakra-ui/react';
import CustomAvatar from '../../utils/CustomAvatar';
import { useNavigate } from 'react-router-dom';
import { dateFormat } from '../../helper/calcTimestamp';
import { nanoid } from '@reduxjs/toolkit';
import LangTag from '../../utils/LangTag';
import { SecondaryBtn } from '../../utils/Buttons';
import { saveArchive } from '../../lib/api';
import { useAuth } from '../../context/auth';
import { useState } from 'react';
import { titleRoute } from '../../helper/titleRoute';
import useClickTag from '../../hooks/useClickTag';
import { useDispatch } from 'react-redux';
import { setClickComment } from '../../store/scrollDiscussion';
const SavedPostItem = ({ postData, isArchive }) => {
const navigate = useNavigate();
const user = useAuth();
const { userId } = user;
const dispatch = useDispatch();
const [loading, setLoading] = useState(false);
const handleClickArchive = () => {
setLoading(true);
const archivedPosts = postData.archived || [];
const transformedArchivedPosts = archivedPosts.includes(userId)
? archivedPosts.filter((id) => id !== userId)
: [...archivedPosts, userId];
saveArchive({ archived: transformedArchivedPosts }, postData.id)
.then((_) => {
setLoading(false);
// console.log('archived');
})
.catch((err) => {
setLoading(false);
console.log(err);
});
};
const handleNavigate = () => {
navigate(
`/${titleRoute(postData.username, postData.title, postData.id)}`
);
dispatch(setClickComment(false));
};
const handleClickTag = useClickTag();
const ghostColor = useColorModeValue('light.ghostColor', 'dark.ghostColor');
const headingHover = useColorModeValue(
'light.headingHover',
'dark.headingHover'
);
const colorTertiary = useColorModeValue(
'light.colorTertiary',
'dark.colorTertiary'
);
return (
<HStack as='article' justify='space-between'>
<Flex mb='1.5rem' align='flex-start' flex='1'>
<CustomAvatar
profile={postData.profile}
size='40px'
onClick={() => navigate(`/${postData.username}`)}
/>
<Box ms='.5rem' flex='1'>
<Text
fontWeight={600}
_hover={{ color: headingHover }}
cursor='pointer'
onClick={handleNavigate}
>
{postData.title}
</Text>
<Flex
justify='flex-start'
align='center'
fontSize='13px'
gap='.2rem'
wrap='wrap'
>
<Text
color={ghostColor}
_hover={{ color: headingHover }}
cursor='pointer'
onClick={() => navigate(`/${postData.username}`)}
>
{postData.name}
</Text>
<Text color={colorTertiary}>
{' '}
• {dateFormat(postData.createdAt)}
</Text>
<Text color={colorTertiary}>
{' '}
• {postData.readTime} min read
</Text>
{postData.tags.length !== 0 && (
<>
<Text ps='2px'>•</Text>
{postData.tags?.map((tag) => (
<WrapItem
key={nanoid()}
onClick={(e) => handleClickTag(e, tag.tagName)}
>
<LangTag tag={tag} />
</WrapItem>
))}
</>
)}
</Flex>
</Box>
</Flex>
<SecondaryBtn
onClick={handleClickArchive}
disabled={loading}
p='0 .7rem'
>
{isArchive
? loading
? 'Unarchiving'
: 'Unarchive'
: loading
? 'Archiving'
: 'Archive'}
</SecondaryBtn>
</HStack>
);
};
export default SavedPostItem;
|