Skip to content

Commit

Permalink
delete msg function passed, which re-fetches the conversation
Browse files Browse the repository at this point in the history
  • Loading branch information
CosmosMil committed Jun 30, 2023
1 parent 9d54486 commit 0318505
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
23 changes: 12 additions & 11 deletions apps/codac-quasseln/src/components/chat/main-chat/message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ const deleteChatMessage = gql`
}
`;

const Message = ({ message }: { message: any }) => {
console.log("message.id: ", message.id);
const Message = ({ message, deleteMsg }: { message: any, deleteMsg: () => void }) => {
// console.log("message.id: ", message.id);
const [hiddenDiv, setHiddenDiv] = useState(false);
const [editToggle, setEditToggle] = useState(false);
const [newMsg, setNewMsg] = useState(message?.attributes?.body || "");
Expand All @@ -76,21 +76,21 @@ const Message = ({ message }: { message: any }) => {

const [deleteMessageMutation] = useMutation(deleteChatMessage);
// the mentor has permmision to delete as well... condicional... id not working and only deleting first message...
const deleteMessage = async (messageId: any) => {
console.log("object :>> ", message.attributes.author.data?.id);
const deleteMessage = (messageId: any) => {
// console.log("object :>> ", message.attributes.author.data?.id);
if (userId === message.attributes.author.data.id || user?.role?.name === "Mentor") {
deleteMessageMutation({
variables: {
id: message.id,
},
});
await refetch();
deleteMsg()
}
};
const updateMessage = async (e: FormEvent<HTMLFormElement>, message: any) => {
e.preventDefault();
console.log("message.id :>> ", message.id);
console.log("msg attributes:>>", message.attributes);
// console.log("message.id :>> ", message.id);
// console.log("msg attributes:>>", message.attributes);
if (userId === message.attributes.author.data.id) {
if (newMsg) {
updateMessageMutation({
Expand All @@ -112,7 +112,7 @@ const Message = ({ message }: { message: any }) => {
<>
<div className="option-A" style={{ border: "2px solid white", margin: "1rem" }}>
<p>
{message && message.attributes.author.data.attributes.username} -{" "}
{message && message.attributes.author.data?.attributes.username} -{" "}
{message && message.attributes.createdAt}{" "}
<button
onClick={() => {
Expand All @@ -134,14 +134,15 @@ const Message = ({ message }: { message: any }) => {
<p>{message && message.attributes?.body}</p>
{hiddenDiv && (
<>
<span>No</span>
<span
<button>No</button>
<button
onClick={() => {
deleteMessage(message.id);

}}
>
Yes
</span>
</button>
</>
)}
</div>
Expand Down
26 changes: 20 additions & 6 deletions apps/codac-quasseln/src/pages/main-chat/[chatId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,16 @@ const updatePinnedConversation = gql`
const SingleChat = (props: Props) => {
const [testVariable, setTestVariable] = useState(false);
const { user } = useAuth();
console.log("user :>> ", user);
// console.log("user :>> ", user);
const userId = user?.id;
// console.log("userId :>> ", userId);
// console.log("userId :>> ", userId)

const deleteMsg = () => {
refetch()
}




// UPDATE PINNED CONVERSATION FUNCTION
const [updatePinnedMutation] = useMutation(updatePinnedConversation);
Expand Down Expand Up @@ -220,7 +227,8 @@ const SingleChat = (props: Props) => {
setMessageText("");
}
refetch();
setTestVariable(!testVariable);
// setTestVariable(!testVariable);

};

// const [deleteMessageMutation] = useMutation(deleteChatMessage);
Expand Down Expand Up @@ -284,12 +292,13 @@ const SingleChat = (props: Props) => {
// console.log('chatId :>> ', chatId);
// console.log('router :>> ', router);
// console.log('data :>> ', chatRooms);
console.log("messages history :>> ", allMessages?.conversation?.data.attributes.messages.data);
// console.log("messages history :>> ", allMessages?.conversation?.data.attributes.messages.data);

// +++++++++++++++++++++++ MODALS ++++++++++++++++++++++

return (
<div>

<h1
style={{
color: "white",
Expand Down Expand Up @@ -379,7 +388,7 @@ const SingleChat = (props: Props) => {
{conversations &&
conversations.chatroom?.data?.attributes.conversations?.data?.map(
(conversation: any) => {
console.log("conversation :>> ", conversation);
// console.log("conversation :>> ", conversation);
if (conversation.attributes.pinned === false) {
return (
<div
Expand Down Expand Up @@ -443,7 +452,12 @@ const SingleChat = (props: Props) => {
>
{allMessages &&
allMessages?.conversation?.data.attributes?.messages?.data?.map((message: any) => {
return <Message message={message} />;
return <Message
message={message}
deleteMsg={deleteMsg}


/>;
})}
</div>

Expand Down

0 comments on commit 0318505

Please sign in to comment.