Skip to content

Commit 4a49b89

Browse files
committed
Implemented deleting post
1 parent f5c52f8 commit 4a49b89

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

src/components/screens/Home.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,51 @@ const Home = () => {
132132
});
133133
};
134134

135+
const deletePost = (postId) => {
136+
fetch(`/posts/comment/${postId}`, {
137+
method: "DELETE",
138+
headers: {
139+
Authorization: `Bearer ${localStorage.getItem("authorization")}`,
140+
"IMS-Request-Id": getUUIDV4().toString(),
141+
"IMS-Request-Date": new Date().toISOString(),
142+
},
143+
})
144+
.then((res) => {
145+
return res.json();
146+
})
147+
.then((result) => {
148+
console.log("DELETE POST RESULT:", result);
149+
const newData = data.filter((item) => {
150+
console.log("item._id:", item._id);
151+
console.log("result.post._id:", result.post._id);
152+
return item._id !== result.post._id;
153+
});
154+
return setData(newData);
155+
})
156+
.catch((err) => {
157+
console.log("ERROR CATCH DELETE POST:", err);
158+
});
159+
};
160+
135161
return (
136162
<div className="home">
137163
{data.map((item) => {
138164
return (
139165
<div key={item._id} className="card home-card">
140-
<h5>@{newState.username}</h5>
166+
<h5>
167+
@{newState.username}
168+
{item.postedBy._id === newState._id && (
169+
<i
170+
className="material-icons"
171+
onClick={() => {
172+
deletePost(item._id);
173+
}}
174+
style={{ float: "right" }}
175+
>
176+
delete
177+
</i>
178+
)}
179+
</h5>
141180
<div className="card-image">
142181
<img src={item.photo} alt="card-pic" />
143182
</div>

0 commit comments

Comments
 (0)