-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
TypeError : changeChat is not a function #14
Comments
@TanjimH013 Hi are you facing any issue regarding the login feature? |
Hello, Yes am facing such like that issue.... |
are you able to resolve the issue regarding the login page. it seems that the issue is from the firebase. |
Nope... I couldn't. |
import { useEffect, useState } from "react";
import "./chatList.css";
import AddUser from "./addUser/addUser";
import { useUserStore } from "../../../lib/userStore";
import { doc, getDoc, onSnapshot, updateDoc } from "firebase/firestore";
import { db } from "../../../lib/firebase";
import { useChatStore } from "../../../lib/chatStore";
const ChatList = () => {
const [chats, setChats] = useState([]);
const [addMode, setAddMode] = useState(false);
const [input, setInput] = useState("");
const { currentUser } = useUserStore();
const { chatId, changeChat } = useChatStore();
useEffect(() => {
const unSub = onSnapshot(
doc(db, "userchats", currentUser.id),
async (res) => {
const items = res.data().chats;
const promises = items.map(async (item) => {
const userDocRef = doc(db, "users", item.receiverId);
const userDocSnap = await getDoc(userDocRef);
const user = userDocSnap.data();
return { ...item, user };
});
const chatData = await Promise.all(promises);
setChats(chatData.sort((a, b) => b.updatedAt - a.updatedAt));
}
);
return () => {
unSub();
};
}, [currentUser.id]);
const handleSelect = async (chat) => {
const userChats = chats.map((item) => {
const { user, ...rest } = item;
return rest;
});
const chatIndex = userChats.findIndex(
(item) => item.chatId === chat.chatId
);
userChats[chatIndex].isSeen = true;
const userChatsRef = doc(db, "userchats", currentUser.id);
try {
await updateDoc(userChatsRef, {
chats: userChats,
});
changeChat(chat.chatId, chat.user);
} catch (err) {
console.log(err);
}
};
const filteredChats = chats.filter((c) =>
c.user.username.toLowerCase().includes(input.toLowerCase())
);
return (
<div className="chatList">
<div className="search">
<div className="searchBar">
<img src="./search.png" alt="" />
<input
type="text"
placeholder="Search"
onChange={(e) => setInput(e.target.value)}
/>
</div>
<img
src={addMode ? "./minus.png" : "./plus.png"}
alt=""
className="add"
onClick={() => setAddMode((prev) => !prev)}
/>
</div>
{filteredChats.map((chat) => (
<div
className="item"
key={chat.chatId}
onClick={() => handleSelect(chat)}
style={{
backgroundColor: chat?.isSeen ? "transparent" : "#5183fe",
}}
>
<img
src={
chat.user.blocked.includes(currentUser.id)
? "./avatar.png"
: chat.user.avatar || "./avatar.png"
}
alt=""
/>
<div className="texts">
<span>
{chat.user.blocked.includes(currentUser.id)
? "User"
: chat.user.username}
</span>
<p>{chat.lastMessage}</p>
</div>
</div>
))}
{addMode && <AddUser />}
</div>
);
};
export default ChatList; |
When I'm trying to click over the user Rayhan Mahmud it doesn't show me user chat. In the console I've facing ( TypeError : changeChat is not a function ) issue.
The text was updated successfully, but these errors were encountered: