Message count
This commit is contained in:
@@ -3,20 +3,25 @@ import { useState } from 'react';
|
||||
import { toast } from 'react-toastify';
|
||||
import markMessageAsRead from '@/app/actions/markMessageAsRead';
|
||||
import deleteMessage from '@/app/actions/deleteMessage';
|
||||
import { useGlobalContext } from '@/context/GlobalContext';
|
||||
|
||||
const MessageCard = ({ message }) => {
|
||||
const [isRead, setIsRead] = useState(message.read);
|
||||
const [isDeleted, setIsDeleted] = useState(false);
|
||||
|
||||
const { setUnreadCount } = useGlobalContext();
|
||||
|
||||
const handleReadClick = async () => {
|
||||
const read = await markMessageAsRead(message._id);
|
||||
setIsRead(read);
|
||||
setUnreadCount((prevCount) => (read ? prevCount - 1 : prevCount + 1));
|
||||
toast.success(`Marked as ${read ? 'read' : 'new'}`);
|
||||
};
|
||||
|
||||
const handleDeleteClick = async () => {
|
||||
await deleteMessage(message._id);
|
||||
setIsDeleted(true);
|
||||
setUnreadCount((prevCount) => (isRead ? prevCount : prevCount - 1));
|
||||
toast.success('Message Deleted');
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user