eagles
diff --git a/src/components/dashboard/products/ProductsTable.tsx b/src/components/dashboard/products/ProductsTable.tsx
index c5e8492..3941abe 100644
--- a/src/components/dashboard/products/ProductsTable.tsx
+++ b/src/components/dashboard/products/ProductsTable.tsx
@@ -162,10 +162,7 @@ const ProductsTable: React.FC = () => {
) : products.length === 0 ? (
-
+ |
No products found.
|
diff --git a/src/page-sections/UserList.tsx b/src/page-sections/UserList.tsx
index 5a4ce15..61314a5 100644
--- a/src/page-sections/UserList.tsx
+++ b/src/page-sections/UserList.tsx
@@ -8,6 +8,7 @@ import { HiMiniUserGroup } from "react-icons/hi2";
import { useAppDispatch } from "../redux/hooks";
import Spinner from "../components/dashboard/Spinner";
import { fetchChats, fetchUsers } from "../redux/reducers/chatSlice";
+import { socket } from "../config/socket";
interface UserListProps {
onUserSelect: (chat: any | null, user: any) => void;
@@ -38,6 +39,17 @@ const UserList: React.FC = ({
useEffect(() => {
dispatch(fetchChats());
dispatch(fetchUsers());
+
+ const handlePrivateMessage = () => {
+ dispatch(fetchChats());
+ dispatch(fetchUsers());
+ };
+
+ socket.on("private message recieved", handlePrivateMessage);
+
+ return () => {
+ socket.off("private message recieved", handlePrivateMessage);
+ };
}, [dispatch]);
useEffect(() => {
@@ -114,7 +126,7 @@ const UserList: React.FC = ({
@@ -193,7 +205,7 @@ const UserList: React.FC = ({
{chat.messages.length > 0
? chat.messages[
chat.messages.length - 1
- ].message.substring(0, 20)
+ ].message.substring(0, 15)
: "No messages yet"}
...
diff --git a/src/pages/ChatPage.tsx b/src/pages/ChatPage.tsx
index 61d5545..1c94329 100644
--- a/src/pages/ChatPage.tsx
+++ b/src/pages/ChatPage.tsx
@@ -1,7 +1,6 @@
import React, { useState, useEffect, useRef } from "react";
import { useSelector } from "react-redux";
import { Link } from "react-router-dom";
-import { MdSportsGolf } from "react-icons/md";
import UserList from "../page-sections/UserList";
import ChatWindow from "../page-sections/ChatWindow";
@@ -31,8 +30,20 @@ const ChatPage: React.FC = () => {
setRefetch(true);
};
+ const handlePastMessages = (pastMessages: any[]) => {
+ setPublicMessages(
+ pastMessages.map((msg) => ({
+ isOwner: msg.sender === profile?.name,
+ message: msg.message,
+ sender: msg.sender,
+ createdAt: msg.createdAt,
+ })),
+ );
+ };
+
useEffect(() => {
dispatch(fetchUser());
+ socket.on("past messages", handlePastMessages);
}, [dispatch]);
useEffect(() => {
@@ -43,32 +54,31 @@ const ChatPage: React.FC = () => {
}, []);
useEffect(() => {
- const handleConnect = () => {
- socket.emit("private chats");
- };
+ socket.on("past messages", handlePastMessages);
+ }, [isPublicChat]);
+ useEffect(() => {
const handleChatMessage = (msg: any) => {
if (!publicMessageIds.current.has(msg.id)) {
setPublicMessages((prevMessages) => [...prevMessages, msg]);
publicMessageIds.current.add(msg.id);
}
};
+ socket.on("chat message", handleChatMessage);
+ socket.on("past messages", handlePastMessages);
- const handlePastMessages = (pastMessages: any[]) => {
- setPublicMessages(
- pastMessages.map((msg) => ({
- isOwner: msg.sender === profile?.name,
- message: msg.message,
- sender: msg.sender,
- createdAt: msg.createdAt,
- })),
- );
+ return () => {
+ socket.off("chat message", handleChatMessage);
+ socket.off("past messages", handlePastMessages);
};
+ }, [publicMessageIds, selectedChat]);
+ useEffect(() => {
+ const handleConnect = () => {
+ socket.emit("private chats");
+ };
socket.on("connect", handleConnect);
- socket.on("chat message", handleChatMessage);
- socket.on("past messages", handlePastMessages);
- }, [publicMessages]);
+ }, [selectedChat]);
useEffect(() => {
const handlePrivateMessage = (message: any) => {
@@ -191,6 +201,12 @@ const ChatPage: React.FC = () => {
}));
try {
+ socket.emit("private chat message", {
+ sender: profile?.username,
+ userId: profile?.id,
+ receiverId: newChatUser.id,
+ message: text,
+ });
await dispatch(
sendMessage({ message: text, id: newChatUser.id }),
).unwrap();
diff --git a/src/redux/reducers/listAddSlice.ts b/src/redux/reducers/listAddSlice.ts
new file mode 100644
index 0000000..71ddc56
--- /dev/null
+++ b/src/redux/reducers/listAddSlice.ts
@@ -0,0 +1,39 @@
+import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
+
+import axios from "../api/api";
+
+export const createAds = createAsyncThunk("listAdds", async () => {
+ const response = await axios.get("/products/ads", {
+ headers: {
+ "Content-Type": "application/json",
+ },
+ });
+ return response.data;
+});
+
+const createAdsSlice = createSlice({
+ name: "ads",
+ initialState: {
+ isLoading: false,
+ data: [],
+ error: false,
+ },
+ reducers: {},
+ extraReducers: (builder) => {
+ builder
+ .addCase(createAds.pending, (state) => {
+ state.isLoading = true;
+ state.error = false;
+ })
+ .addCase(createAds.fulfilled, (state, action) => {
+ state.isLoading = false;
+ state.data = action.payload;
+ })
+ .addCase(createAds.rejected, (state, action) => {
+ state.isLoading = false;
+ state.isLoading = true;
+ });
+ },
+});
+
+export default createAdsSlice.reducer;
diff --git a/src/redux/store.ts b/src/redux/store.ts
index c057976..5dccda1 100644
--- a/src/redux/store.ts
+++ b/src/redux/store.ts
@@ -19,6 +19,7 @@ import authReducer from "./reducers/authSlice";
import wishListSlice from "./reducers/wishListSlice";
import ordersReducer from "./reducers/ordersSlice";
import PaymentSlice from "./reducers/payment";
+import adsReducer from "./reducers/listAddSlice";
const store = configureStore({
reducer: {
@@ -41,6 +42,7 @@ const store = configureStore({
wishes: wishListSlice,
order: ordersReducer,
payment: PaymentSlice,
+ ads: adsReducer,
},
});
export type RootState = ReturnType;
diff --git a/src/routes/AppRoutes.tsx b/src/routes/AppRoutes.tsx
index 62fb7a1..4054464 100644
--- a/src/routes/AppRoutes.tsx
+++ b/src/routes/AppRoutes.tsx
@@ -44,14 +44,12 @@ const AppRoutes = () => {
setNavigate(navigate);
// setNavigateFunction(navigate);
}, [navigate]);
-
const AlreadyLogged = ({ children }) => {
const navigate = useNavigate();
const token = localStorage.getItem("accessToken");
const decodedToken = token ? JSON.parse(atob(token!.split(".")[1])) : {};
const tokenIsValid = decodedToken.id && decodedToken.roleId;
const isSeller = decodedToken.roleId === 2;
-
useEffect(() => {
if (tokenIsValid) {
isSeller ? navigate("/dashboard") : navigate("/");
@@ -60,7 +58,6 @@ const AppRoutes = () => {
return tokenIsValid ? null : children;
};
-
return (
@@ -122,5 +119,4 @@ const AppRoutes = () => {
);
};
-
export default AppRoutes;