Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add option to delete posts from web UI #752

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 55 additions & 24 deletions components/post.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { convertTimestampToDate } from '../lib/dates'
import { proxy } from '../lib/images'
import { filter } from 'lodash'
import Icon from '@hackclub/icons'
import Link from 'next/link'
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']

Expand All @@ -28,22 +28,51 @@ const Post = ({
profile = false,
user = {
username: 'abc',
id: 'abc',
avatar: '',
displayStreak: false,
streakCount: 0
},
sessionID = 'abc',
text,
attachments = [],
mux = [],
reactions = [],
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 (
<>
<section
Expand Down Expand Up @@ -80,11 +109,10 @@ const Post = ({
<span className="post-header-name">
<strong>@{user.username}</strong>
<span
className={`badge post-header-streak ${
!user.displayStreak || user.streakCount === 0
? 'header-streak-zero'
: ''
}`}
className={`badge post-header-streak ${!user.displayStreak || user.streakCount === 0
? 'header-streak-zero'
: ''
}`}
title={`${user.streakCount}-day streak`}
>
{`${user.streakCount <= 7 ? user.streakCount : '7+'}`}
Expand Down Expand Up @@ -154,25 +182,28 @@ const Post = ({
</div>
)}
<footer className="post-reactions" aria-label="Emoji reactions">
{reactions.map(reaction => (
<Reaction
key={id + reaction.name}
{...reaction}
postID={id}
authStatus={authStatus}
authSession={authSession}
swrKey={swrKey}
/>
))}
{authStatus == 'authenticated' && (
<div className="post-reaction" onClick={() => openEmojiPicker(id)}>
+
</div>
)}
<div style={{ display: 'flex', flexWrap: 'wrap', flexGrow: 1 }}>
cskartikey marked this conversation as resolved.
Show resolved Hide resolved
{reactions.map(reaction => (
<Reaction
key={id + reaction.name}
{...reaction}
postID={id}
authStatus={authStatus}
authSession={authSession}
swrKey={swrKey}
/>
))}
{authStatus == 'authenticated' && (
<div className="post-reaction" onClick={() => openEmojiPicker(id)}>
+
</div>
)}
</div>
{( authStatus == 'authenticated' && authSession.user.id === user.id) && <Icon glyph="delete" size={32} className="delete-button post-reaction" onClick={() => deletePost(id)} />}
</footer>
</section>
</>
)
}

export default Post
export default Post
25 changes: 25 additions & 0 deletions pages/api/web/post/delete.js
Original file line number Diff line number Diff line change
@@ -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' });
}
};
25 changes: 25 additions & 0 deletions public/posts.css
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
text-decoration: none;
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 8px;
line-height: 1;
}
Expand All @@ -40,6 +41,7 @@

.post-header-container {
padding-left: 8px;
flex-grow: 1;
}

.post-header-name {
Expand Down Expand Up @@ -122,6 +124,7 @@ a.post-text-mention {
align-items: center;
margin-top: 16px;
}

.post-attachment {
border-radius: 6px;
overflow: hidden;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
}