Skip to content

Commit fc9ff57

Browse files
committed
feat: add option to delete posts from web UI
1 parent 1a0e94d commit fc9ff57

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

components/post.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Content from './content'
77
import Video from './video'
88
import Image from 'next/image'
99
import Reaction from './reaction'
10+
import { useState } from 'react'
1011
import EmojiPicker from 'emoji-picker-react'
1112

1213
const imageFileTypes = ['jpg', 'jpeg', 'png', 'gif', 'webp']
@@ -28,6 +29,7 @@ const Post = ({
2829
profile = false,
2930
user = {
3031
username: 'abc',
32+
id: 'abc',
3133
avatar: '',
3234
displayStreak: false,
3335
streakCount: 0
@@ -44,6 +46,30 @@ const Post = ({
4446
swrKey,
4547
authSession
4648
}) => {
49+
const [isVisible, setIsVisible] = useState(true);
50+
51+
const deletePost = async (id) => {
52+
try {
53+
const response = await fetch('/api/web/post/delete', {
54+
method: 'DELETE',
55+
headers: {
56+
'Content-Type': 'application/json',
57+
},
58+
body: JSON.stringify({ id })
59+
});
60+
const responseText = await response.text()
61+
if (responseText.includes("Post Deleted")){
62+
setIsVisible(false);
63+
}
64+
} catch (error) {
65+
console.error('Error:', error);
66+
}
67+
};
68+
69+
if (!isVisible) {
70+
return null;
71+
}
72+
4773
return (
4874
<>
4975
<section
@@ -156,6 +182,7 @@ const Post = ({
156182
</div>
157183
)}
158184
<footer className="post-reactions" aria-label="Emoji reactions">
185+
<div style={{ display: 'flex', flexWrap: 'wrap', flexGrow: 1 }}>
159186
{reactions.map(reaction => (
160187
<Reaction
161188
key={id + reaction.name}
@@ -171,6 +198,8 @@ const Post = ({
171198
+
172199
</div>
173200
)}
201+
</div>
202+
<Icon glyph="delete" size={32} className="delete-button post-reaction" onClick={() => deletePost(id)} />
174203
</footer>
175204
</section>
176205
</>

components/posts.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ const Posts = ({ posts = [], swrKey = null }) => {
7070
{posts.map(post => (
7171
<Post
7272
key={post.id}
73+
userID={post.user.id}
7374
openEmojiPicker={openEmojiPicker}
7475
authStatus={status}
7576
authSession={session}

pages/api/web/post/delete.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { getServerSession } from 'next-auth/next';
2+
import { authOptions } from '../../auth/[...nextauth]';
3+
import prisma from '../../../../lib/prisma';
4+
5+
export default async (req, res) => {
6+
const session = await getServerSession(req, res, authOptions);
7+
8+
if (!session?.user) {
9+
console.log('Unauthorized access attempt');
10+
return res.status(401).send({message: "Unauthorized"});
11+
}
12+
13+
try {
14+
const update = await prisma.updates.delete({
15+
where: { id: req.body.id },
16+
});
17+
18+
console.log('API Response:', update);
19+
20+
return res.status(200).send({message: "Post Deleted"});
21+
} catch (e) {
22+
console.error('Error deleting post:', e);
23+
return res.status(500).json({ error: true, message: 'Internal Server Error' });
24+
}
25+
};

public/posts.css

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
text-decoration: none;
2525
display: flex;
2626
align-items: center;
27+
justify-content: space-between;
2728
margin-bottom: 8px;
2829
line-height: 1;
2930
}
@@ -40,6 +41,7 @@
4041

4142
.post-header-container {
4243
padding-left: 8px;
44+
flex-grow: 1;
4345
}
4446

4547
.post-header-name {
@@ -122,6 +124,7 @@ a.post-text-mention {
122124
align-items: center;
123125
margin-top: 16px;
124126
}
127+
125128
.post-attachment {
126129
border-radius: 6px;
127130
overflow: hidden;
@@ -160,6 +163,7 @@ a.post-attachment:first-child:last-child {
160163
flex-wrap: wrap;
161164
margin-top: 16px;
162165
margin-bottom: -12px;
166+
justify-content: space-between;
163167
}
164168

165169
.post-reaction {
@@ -198,3 +202,24 @@ a.post-attachment:first-child:last-child {
198202
width: 24px;
199203
height: 24px;
200204
}
205+
206+
.delete-button {
207+
color: var(--colors-background);
208+
background-color: #ec3750;
209+
border-color: black;
210+
}
211+
212+
.delete-button:hover {
213+
transform: scale(1.2);
214+
color: var(--colors-background);
215+
animation: wiggle 0.5s ease-in-out infinite;
216+
}
217+
218+
@keyframes wiggle {
219+
0%, 100% {
220+
transform: rotate(-5deg) scale(1.2);
221+
}
222+
50% {
223+
transform: rotate(5deg) scale(1.2);
224+
}
225+
}

0 commit comments

Comments
 (0)