Skip to content

Commit

Permalink
feat: add button delete post
Browse files Browse the repository at this point in the history
  • Loading branch information
Barresi committed Apr 30, 2024
1 parent 0f5f11d commit e73c011
Show file tree
Hide file tree
Showing 8 changed files with 461 additions and 7 deletions.
208 changes: 208 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"dependencies": {
"@radix-ui/react-avatar": "^1.0.3",
"@radix-ui/react-dialog": "^1.0.5",
"@radix-ui/react-dropdown-menu": "^2.0.6",
"@radix-ui/react-popover": "^1.0.7",
"@radix-ui/react-scroll-area": "^1.0.4",
"@radix-ui/react-select": "^1.2.2",
Expand Down
14 changes: 8 additions & 6 deletions src/entities/post-news/ui/post-news.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { parseDateToMonth, parseDateToTime } from '@shared/lib/parse-date'
import { type IPost } from '@shared/lib/types/api'
import { Button } from '@shared/ui/button'
import { UserAvatar } from '@shared/ui/user-avatar'
import { type FC, type ReactNode } from 'react'

import eye from '@assets/ui/Eye.svg'
import { LinkName } from '@shared/ui/link-name'

interface IPostNewsProps {
post: IPost | undefined
buttonLike: ReactNode
buttonDelete: ReactNode
}
const PostNews: FC<IPostNewsProps> = ({ post, buttonLike }) => {
const PostNews: FC<IPostNewsProps> = ({ post, buttonLike, buttonDelete }) => {
const createDate = post?.createdAt
? `${parseDateToMonth(post?.createdAt)} в ${parseDateToTime(post?.createdAt)}`
: 'Дата неизвестна'
Expand All @@ -24,13 +25,14 @@ const PostNews: FC<IPostNewsProps> = ({ post, buttonLike }) => {
link={post?.createdBy?.id}
/>
<div>
<h4 className='text-[#D22828] text-[18px] font-bold'>{`${post?.createdBy?.firstName} ${post?.createdBy?.lastName}`}</h4>
<LinkName
link={post?.createdBy.id}
className='text-[#D22828] text-[18px] font-bold'
>{`${post?.createdBy?.firstName} ${post?.createdBy?.lastName}`}</LinkName>
<p className='text-[#ADB5BD]'>{createDate}</p>
</div>
</div>
<div>
<Button variant='text' icon='more' className='p-0' />
</div>
<div>{buttonDelete}</div>
</div>
<p>
{post?.text}
Expand Down
1 change: 1 addition & 0 deletions src/features/button-delete-post/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { ButtonDeletePost } from './ui/button-delete-post'
44 changes: 44 additions & 0 deletions src/features/button-delete-post/ui/button-delete-post.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { getAllPostsThunk } from '@app/store/reducers/news/async-thunks'
import { selectUser } from '@app/store/reducers/profileInfo/selectors'
import { deletePost } from '@shared/api/news/news'
import { useAppDispatch, useAppSelector } from '@shared/lib/hooks/store-hooks'
import { cn } from '@shared/lib/merge-classes'
import { type IPost } from '@shared/lib/types/api'
import { Button } from '@shared/ui/button'
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuTrigger
} from '@shared/ui/dropdown-menu'
import { type FC } from 'react'

interface IButtonDeletePostProps {
post: IPost
className?: string
}
const ButtonDeletePost: FC<IButtonDeletePostProps> = ({ className, post }) => {
const dispatch = useAppDispatch()
const user = useAppSelector(selectUser)
const handleDeletePost = async (): Promise<void> => {
await deletePost(post.id)
dispatch(getAllPostsThunk())
}
return (
<DropdownMenu>
<DropdownMenuTrigger>
<Button variant='text' icon='more' className={cn(className, 'p-0')} />
</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuLabel>Действия</DropdownMenuLabel>
{post.createdById === user?.id && (
<DropdownMenuItem onClick={handleDeletePost} className='cursor-pointer'>
Удалить пост
</DropdownMenuItem>
)}
</DropdownMenuContent>
</DropdownMenu>
)
}
export { ButtonDeletePost }
8 changes: 7 additions & 1 deletion src/pages/page-news/ui/page-news.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { selectFriends } from '@app/store/reducers/friends/selectors'
import { selectAllPosts } from '@app/store/reducers/news/selectors'
import { selectUser } from '@app/store/reducers/profileInfo/selectors'
import { PostNews } from '@entities/post-news'
import { ButtonDeletePost } from '@features/button-delete-post'
import { ButtonLikePost } from '@features/button-like-post'
import { InputCreatePost } from '@features/input-create-post'
import { useAppSelector } from '@shared/lib/hooks/store-hooks'
Expand Down Expand Up @@ -36,7 +37,12 @@ const PageNews: FC = () => {
<div className='list w-full xxl:w-2/3 rounded-[10px] flex flex-col gap-[30px]'>
<InputCreatePost />
{tabs[type].map((post, ind) => (
<PostNews post={post} key={ind} buttonLike={<ButtonLikePost post={post} />} />
<PostNews
post={post}
key={ind}
buttonLike={<ButtonLikePost post={post} />}
buttonDelete={<ButtonDeletePost post={post} />}
/>
))}
</div>
</div>
Expand Down
2 changes: 2 additions & 0 deletions src/pages/page-profile/ui/page-profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { selectAllUsers } from '@app/store/reducers/friends/selectors'
import { selectAllPosts } from '@app/store/reducers/news/selectors'
import { selectUser } from '@app/store/reducers/profileInfo/selectors'
import { PostNews } from '@entities/post-news'
import { ButtonDeletePost } from '@features/button-delete-post'
import { ButtonLikePost } from '@features/button-like-post'
import { InputCreatePost } from '@features/input-create-post'
import { useAppSelector } from '@shared/lib/hooks/store-hooks'
Expand Down Expand Up @@ -50,6 +51,7 @@ const PageProfile: FC = () => {
post={post}
key={ind}
buttonLike={<ButtonLikePost post={post} />}
buttonDelete={<ButtonDeletePost post={post} />}
/>
))}
</div>
Expand Down
Loading

0 comments on commit e73c011

Please sign in to comment.