Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

telegram like reactions #1235

Merged
merged 2 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 33 additions & 39 deletions src/components/CommunityChat/Reaction/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { db } from "../../../lib/firebase";
import { useSnackbar } from "notistack";
import { useState } from "react";

const Reaction = ({ message, userUid }) => {
const Reaction = ({ message, userUid, currentUid }) => {
const [reactionOpen, setReactionOpen] = useState(false);
const { id, reaction } = message;
const { enqueueSnackbar } = useSnackbar();
Expand All @@ -26,53 +26,47 @@ const Reaction = ({ message, userUid }) => {
}

async function addReaction(type) {
let updatedData = reaction;
let updatedData = { ...reaction }; // Create a copy of the existing reactions
const msgDocRef = db.collection("messages").doc(id);

if (!updatedData) {
updatedData = {
[type]: [userUid],
};
} else {
const rxnUser = checkOccurenceOfReaction();
if (rxnUser) {
const rxnIdx = reaction[rxnUser].indexOf(userUid);
updatedData[rxnUser].splice(rxnIdx, 1);
if (rxnUser !== type) {
if (updatedData[type]) {
updatedData[type].push(userUid);
} else {
updatedData = {
...updatedData,
[rxnUser]: [userUid],
};
}
}

if (updatedData[type]) {
// Check if the user has already reacted with this emoji
const userIndex = updatedData[type].indexOf(currentUid);

if (userIndex !== -1) {
// If the user's reaction already exists, remove it
updatedData[type].splice(userIndex, 1);
} else {
if (updatedData[type]) {
updatedData[type].push(userUid);
} else {
updatedData = {
...updatedData,
[type]: [userUid],
};
// If the user's reaction doesn't exist, toggle off any existing reaction
for (const existingType in updatedData) {
const existingUserIndex = updatedData[existingType].indexOf(currentUid);
if (existingUserIndex !== -1) {
updatedData[existingType].splice(existingUserIndex, 1);
break; // Exit the loop after toggling off the existing reaction
}
}

updatedData[type].push(currentUid); // Add the new reaction
}
} else {
// If the reaction type doesn't exist, create a new array with the user's UID
updatedData[type] = [currentUid];
}

msgDocRef
.update({

// Update the reaction data in the message document
try {
await msgDocRef.update({
reaction: updatedData,
})
.catch((e) => {
enqueueSnackbar("Error while reacting", {
variant: "error",
});
});

setReactionOpen(false);
setReactionOpen(false);
} catch (error) {
enqueueSnackbar("Error while reacting", {
variant: "error",
});
}
}


return (
<ClickAwayListener onClickAway={() => setReactionOpen(false)}>
<div className="flex-center">
Expand Down
4 changes: 3 additions & 1 deletion src/components/CommunityChat/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@
position: absolute;
padding: 0;
bottom: -13px;
right: 0;
left: 12px;
display: flex;
gap: 8px;
}

.rxn-container {
Expand Down
2 changes: 1 addition & 1 deletion src/components/CommunityChat/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ const ChatBox = () => {
<h6 className="message-time">
{getTime(message?.createdAt?.seconds)}
</h6>
<Reaction message={message} userUid={message.uid} />
<Reaction message={message} userUid={message.uid} currentUid={user.uid} />
{user.uid === message.uid && (
<span className="flex-center message-options">
<OptionIcon
Expand Down
Loading