delete message

This commit is contained in:
Guillaume Dorce 2022-09-28 22:51:09 +02:00
parent 85f8330b7d
commit 9874f07209
6 changed files with 46 additions and 21 deletions

View File

@ -29,21 +29,21 @@ const Likes = ({ likes }: {likes: number}) => {
); );
}; };
const Message = ({ text = '', user, date, image = '', id }: any) => { const Message = ({ message }: any) => {
return ( return (
<> <>
<div className="flex bg-grey-dark rounded-xl w-full max-w-3xl p-5 gap-5 shadow-md shadow-grey-dark" id={"messageId" + id}> <div className="flex bg-grey-dark rounded-xl w-full max-w-3xl p-5 gap-5 shadow-md shadow-grey-dark" id={"messageId" + message.id}>
{user && <Avatar user={user} />} {message.author && <Avatar user={message.author} />}
<div className="flex flex-col gap-2 relative flex-grow"> <div className="flex flex-col gap-2 relative flex-grow">
<div className="flex justify-between"> <div className="flex justify-between">
<div className="text-red-light text-xl username"> <div className="text-red-light text-xl username">
{user.firstName} {user.lastName} {message.author.firstName} {message.author.lastName}
</div> </div>
<PopupMessage id={id} /> <PopupMessage message={message} />
</div> </div>
<Text text={text} /> <Text text={message.content} />
<Image image={image} /> <Image image={message.image} />
<div className="text-grey-light date">{date}</div> <div className="text-grey-light date">{message.createdAt}</div>
<Likes likes={2} /> <Likes likes={2} />
</div> </div>

View File

@ -28,7 +28,7 @@ const MessageWrapper = () => {
return ( return (
<main className="messages-wrapper flex flex-col p-4 gap-4 overflow-scroll w-full max-w-3xl"> <main className="messages-wrapper flex flex-col p-4 gap-4 overflow-scroll w-full max-w-3xl">
{messages.isLoading ? '' : messages.data?.map((message: any) => ( {messages.isLoading ? '' : messages.data?.map((message: any) => (
<Message user={message.author} text={message.content} image={message.image} date={message.createdAt} id={message.id} key={message.id}/> <Message message={message} key={message.id}/>
))} ))}
</main> </main>
); );

View File

@ -1,22 +1,42 @@
import { useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
import { FaEllipsisH } from 'react-icons/fa'; import { FaEllipsisH } from 'react-icons/fa';
import Modal from './Modal'; import Modal from './Modal';
import { deleteMessage } from '@controllers/MessageController';
import { useQueryClient } from '@tanstack/react-query';
const DeleteModal = ({ const DeleteModal = ({
id, authorId,
messageId,
showDelete, showDelete,
setShowDelete, setShowDelete,
}: { }: {
id: string; authorId: string;
messageId: string;
showDelete: boolean; showDelete: boolean;
setShowDelete: (showDelete: boolean) => void; setShowDelete: (showDelete: boolean) => void;
}) => { }) => {
const queryClient = useQueryClient();
const handleDelete = async () => {
try {
const response = await deleteMessage(messageId);
if (response.status === 200) {
console.log(await response.json());
queryClient.invalidateQueries(['messages']);
setShowDelete(false);
}
} catch (error) {
console.error(error);
setShowDelete(false);
}
};
return ( return (
<Modal show={showDelete}> <Modal show={showDelete}>
<div className="text-white mb-2">Voulez vous vraiment supprimer ce message ?</div> <div className="text-white mb-2">Voulez vous vraiment supprimer ce message ?</div>
<button <button
className="popup-item bg-red text-white border-red border-2 rounded-xl p-2 mr-2 transition-all hover:cursor-pointer hover:bg-white hover:text-red" className="popup-item bg-red text-white border-red border-2 rounded-xl p-2 mr-2 transition-all hover:cursor-pointer hover:bg-white hover:text-red"
onClick={() => setShowDelete(!showDelete)} onClick={handleDelete}
> >
Supprimer Supprimer
</button> </button>
@ -31,11 +51,13 @@ const DeleteModal = ({
}; };
const EditModal = ({ const EditModal = ({
id, authorId,
messageId,
showEdit, showEdit,
setShowEdit, setShowEdit,
}: { }: {
id: string; authorId: string;
messageId: string;
showEdit: boolean; showEdit: boolean;
setShowEdit: (showEdit: boolean) => void; setShowEdit: (showEdit: boolean) => void;
}) => { }) => {
@ -46,20 +68,20 @@ const EditModal = ({
); );
}; };
const PopupMessage = ({ id }: { id: string }) => { const PopupMessage = ({ message }: { message: any }) => {
const [show, setShow] = useState(false); const [show, setShow] = useState(false);
const [showEdit, setShowEdit] = useState(false); const [showEdit, setShowEdit] = useState(false);
const [showDelete, setShowDelete] = useState(false); const [showDelete, setShowDelete] = useState(false);
useEffect(() => { useEffect(() => {
const handleClick = (e: any) => { const handleClick = (e: any) => {
if (e.target.closest('#messageId' + id) === null) { if (e.target.closest('#messageId' + message.id) === null) {
setShow(false); setShow(false);
} }
}; };
document.addEventListener('click', handleClick); document.addEventListener('click', handleClick);
}, [id]); }, [message.id]);
return ( return (
<> <>
@ -98,8 +120,8 @@ const PopupMessage = ({ id }: { id: string }) => {
</button> </button>
</div> </div>
</div> </div>
<EditModal id={id} showEdit={showEdit} setShowEdit={setShowEdit} /> <EditModal authorId={message.author.id} messageId={message.id} showEdit={showEdit} setShowEdit={setShowEdit} />
<DeleteModal id={id} showDelete={showDelete} setShowDelete={setShowDelete} /> <DeleteModal authorId={message.author.id} messageId={message.id} showDelete={showDelete} setShowDelete={setShowDelete} />
</> </>
); );
}; };

View File

@ -36,13 +36,12 @@ const newMessage = async (data: FormData) => {
const deleteMessage = async (id: string) => { const deleteMessage = async (id: string) => {
const token = new Cookies().get('token'); const token = new Cookies().get('token');
const response = await fetch(`/api/posts/${id}`, { return fetch(`/api/posts/delete/${id}`, {
method: 'DELETE', method: 'DELETE',
headers: { headers: {
'Authorization': `Bearer ${token}` 'Authorization': `Bearer ${token}`
}, },
}); });
return response.json();
}; };
const editMessage = async (id: string, data: FormData) => { const editMessage = async (id: string, data: FormData) => {

View File

@ -27,6 +27,9 @@
"@components/*": [ "@components/*": [
"src/components/*" "src/components/*"
], ],
"@controllers/*": [
"src/controllers/*"
],
} }
}, },
"include": [ "include": [

View File

@ -24,6 +24,7 @@ export default defineConfig({
'@components': path.resolve(__dirname, './src/components'), '@components': path.resolve(__dirname, './src/components'),
'@styles': path.resolve(__dirname, './src/assets/styles'), '@styles': path.resolve(__dirname, './src/assets/styles'),
'@layouts': path.resolve(__dirname, './src/layouts'), '@layouts': path.resolve(__dirname, './src/layouts'),
'@controllers': path.resolve(__dirname, './src/controllers'),
} }
}, },
}); });