Skip to content

Commit

Permalink
Added contact messages delete feature for admin
Browse files Browse the repository at this point in the history
  • Loading branch information
MeetDOD committed Aug 8, 2024
1 parent 02b35f9 commit accf63f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
24 changes: 23 additions & 1 deletion admin/src/pages/ContactMessages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import '../styles/Model.css'
import { ColorRing } from 'react-loader-spinner';
import { MdMessage } from "react-icons/md";
import { TbReportAnalytics } from "react-icons/tb";
import toast from "react-hot-toast";

const ContactMessages = () => {
const [contactMessages, setContactMessages] = useState<IContactMessage[]>([]);
Expand Down Expand Up @@ -59,6 +60,21 @@ const ContactMessages = () => {
}
};

const handleDeleteMessage = async (id: string) => {
try {
await axios.delete(`/api/v1/admin/deletecontactmessage/${id}`, {
headers: {
Authorization: `Bearer ${token}`,
},
});
setContactMessages(contactMessages.filter((msg) => msg.id !== id));
toast.success("Contact Message deleted successfully !")
} catch (error) {
console.error("Error deleting message:", error);
toast.error("Error in deleting message")
}
};

const handleOpenModal = (message: IContactMessage) => {
setSelectedMessage(message);
setOpen(true);
Expand Down Expand Up @@ -114,7 +130,7 @@ const ContactMessages = () => {
<td className="px-8 py-4 font-semibold">{new Date(contactMessage.createdAt).toLocaleDateString()}</td>
<td className="px-8 py-4 font-semibold">{contactMessage.subject.slice(0, 10)}</td>
<td className="px-12 py-4 font-semibold">{contactMessage.message.slice(0, 10)}</td>
<td className="px-2 py-4 grid grid-cols-1 gap-3 justify-center md:grid-cols-2">
<td className="px-2 py-4 grid grid-cols-1 gap-3 justify-center md:grid-cols-3">
<button
onClick={() => handleOpenModal(contactMessage)}
className="font-semibold rounded-md p-2 bg-sky-500 text-white border-2 hover:bg-sky-600"
Expand All @@ -129,6 +145,12 @@ const ContactMessages = () => {
>
Reply
</a>
<button
onClick={() => handleDeleteMessage(contactMessage.id)}
className="font-semibold rounded-md p-2 bg-red-500 text-white border-2 hover:bg-red-600"
>
Delete
</button>
</td>
</tr>
))}
Expand Down
19 changes: 19 additions & 0 deletions backend/src/routes/admin/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1088,4 +1088,23 @@ export const downloadFavoritesReportController = async (req: Request, res: Respo
error: "An unexpected exception occurred!",
});
}
};

export const deleteContactMessage = async (req: Request, res: Response) => {
const { id } = req.params;

try {
const deletedMessage = await prisma.contactMessage.delete({
where: { id },
});

res.status(200).json({
message: "Contact message deleted successfully!",
deletedMessage,
});
} catch (error) {
res.status(500).json({
error: "An error occurred while deleting the contact message!",
});
}
};
4 changes: 3 additions & 1 deletion backend/src/routes/admin/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Router} from 'express';
import { getPostReactionsController,getFavoritesController,adminLoginController, adminProfileController, allUserForAdmin, blockUserController, unblockUserController, getAdminPostsController, getAdminTrendingPostsController, getAdminStatsController, getGraphsStatsController, updatePostController, deletePostController, getPostByIdController, getAllContactMessages, deleteCommentController, downloadReportController, getFeedbacks, toggleFeedbackVisibility, downloadCommentsReportController, downloadFavoritesReportController, downloadReactionsReportController, downloadUsersReportController, downloadContactMessagesReportController, downloadPostsReportController } from './controller';
import { getPostReactionsController,getFavoritesController,adminLoginController, adminProfileController, allUserForAdmin, blockUserController, unblockUserController, getAdminPostsController, getAdminTrendingPostsController, getAdminStatsController, getGraphsStatsController, updatePostController, deletePostController, getPostByIdController, getAllContactMessages, deleteCommentController, downloadReportController, getFeedbacks, toggleFeedbackVisibility, downloadCommentsReportController, downloadFavoritesReportController, downloadReactionsReportController, downloadUsersReportController, downloadContactMessagesReportController, downloadPostsReportController, deleteContactMessage } from './controller';
import { isAdmin } from '../../middleware/adminAuth';

const adminRouter = Router();
Expand Down Expand Up @@ -54,4 +54,6 @@ adminRouter.get("/downloadusersfavoritesreport", downloadFavoritesReportControll

adminRouter.get("/downloadusersreactionreport", downloadReactionsReportController);

adminRouter.delete("/deletecontactmessage/:id", isAdmin, deleteContactMessage);

export default adminRouter;

0 comments on commit accf63f

Please sign in to comment.