diff --git a/components/post.js b/components/post.js index 3b93d4ff..16191734 100644 --- a/components/post.js +++ b/components/post.js @@ -1,5 +1,4 @@ import { convertTimestampToDate } from '../lib/dates' -import { proxy } from '../lib/images' import { filter } from 'lodash' import Icon from '@hackclub/icons' import Link from 'next/link' @@ -7,7 +6,8 @@ import Content from './content' import Video from './video' import Image from 'next/image' import Reaction from './reaction' -import EmojiPicker from 'emoji-picker-react' +import { useState, useEffect } from 'react' +import toast from 'react-hot-toast' const imageFileTypes = ['jpg', 'jpeg', 'png', 'gif', 'webp'] @@ -28,10 +28,12 @@ const Post = ({ profile = false, user = { username: 'abc', + id: 'abc', avatar: '', displayStreak: false, streakCount: 0 }, + sessionID = 'abc', text, attachments = [], mux = [], @@ -39,11 +41,38 @@ const Post = ({ postedAt, slackUrl, muted = false, - openEmojiPicker = () => {}, + openEmojiPicker = () => { }, authStatus, swrKey, authSession }) => { + const [isVisible, setIsVisible] = useState(true); + + const deletePost = async (id) => { + toast.promise( + fetch('/api/web/post/delete', { + method: 'DELETE', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ id }) + }).then(response => response.text()).then(responseText => { + if (responseText.includes("Post Deleted")) { + setIsVisible(false); + } + }), + { + loading: 'Deleting post...', + success: 'Post Deleted Successfully!', + error: 'Error deleting post.', + } + ); + }; + + if (!isVisible) { + return null; + } + return ( <>
@{user.username} {`${user.streakCount <= 7 ? user.streakCount : '7+'}`} @@ -154,25 +182,28 @@ const Post = ({ )}
) } -export default Post +export default Post \ No newline at end of file diff --git a/pages/api/web/post/delete.js b/pages/api/web/post/delete.js new file mode 100644 index 00000000..4d747674 --- /dev/null +++ b/pages/api/web/post/delete.js @@ -0,0 +1,25 @@ +import { getServerSession } from 'next-auth/next'; +import { authOptions } from '../../auth/[...nextauth]'; +import prisma from '../../../../lib/prisma'; + +export default async (req, res) => { + const session = await getServerSession(req, res, authOptions); + + if (!session?.user) { + console.log('Unauthorized access attempt'); + return res.status(401).send({message: "Unauthorized"}); + } + + try { + const update = await prisma.updates.delete({ + where: { id: req.body.id }, + }); + + console.log('API Response:', update); + + return res.status(200).send({message: "Post Deleted"}); + } catch (e) { + console.error('Error deleting post:', e); + return res.status(500).json({ error: true, message: 'Internal Server Error' }); + } +}; diff --git a/public/posts.css b/public/posts.css index dedca7a4..2bf64dc2 100644 --- a/public/posts.css +++ b/public/posts.css @@ -24,6 +24,7 @@ text-decoration: none; display: flex; align-items: center; + justify-content: space-between; margin-bottom: 8px; line-height: 1; } @@ -40,6 +41,7 @@ .post-header-container { padding-left: 8px; + flex-grow: 1; } .post-header-name { @@ -122,6 +124,7 @@ a.post-text-mention { align-items: center; margin-top: 16px; } + .post-attachment { border-radius: 6px; overflow: hidden; @@ -160,6 +163,7 @@ a.post-attachment:first-child:last-child { flex-wrap: wrap; margin-top: 16px; margin-bottom: -12px; + justify-content: space-between; } .post-reaction { @@ -198,3 +202,24 @@ a.post-attachment:first-child:last-child { width: 24px; height: 24px; } + +.delete-button { + color: var(--colors-background); + background-color: #ec3750; + border-color: black; +} + +.delete-button:hover { + transform: scale(1.2); + color: var(--colors-background); + animation: wiggle 0.5s ease-in-out infinite; +} + +@keyframes wiggle { + 0%, 100% { + transform: rotate(-5deg) scale(1.2); + } + 50% { + transform: rotate(5deg) scale(1.2); + } +}