| @@ -26,7 +26,7 @@ const RequestExchangeCard = (props) => { | |||
| const requester = useSelector(selectRequester); | |||
| const exchange = useSelector(selectExchange); | |||
| const amIBuyer = useMemo( | |||
| () => exchange?.buyer?.userId === userId, | |||
| () => exchange?.buyer?.user?._id === userId, | |||
| [exchange, userId] | |||
| ); | |||
| const haveIAccepted = useMemo( | |||
| @@ -34,7 +34,7 @@ const RequestExchangeCard = (props) => { | |||
| [amIBuyer, exchange] | |||
| ); | |||
| const interlocutorUserId = useMemo( | |||
| () => (amIBuyer ? exchange?.seller?.userId : exchange?.buyer?.userId), | |||
| () => (amIBuyer ? exchange?.seller?.user?._id : exchange?.buyer?.user?._id), | |||
| [exchange, amIBuyer] | |||
| ); | |||
| const message = useMemo(() => { | |||
| @@ -27,17 +27,14 @@ const RequestExchangeMessage = (props) => { | |||
| const exchange = useSelector(selectExchange); | |||
| const requester = useSelector(selectRequester); | |||
| const handleAcceptExchange = () => { | |||
| const handleAcceptExchangeSuccess = () => { | |||
| console.log("accept salje i prima 3 POZVANA RESPONSE FUNKCIJA"); | |||
| acceptExchangeSocket( | |||
| props.chatId, | |||
| props.userId, | |||
| props.interlocutorUserId, | |||
| () => { | |||
| dispatch( | |||
| acceptExchange({ | |||
| exchangeId: exchange._id, | |||
| }) | |||
| ); | |||
| console.log("accept salje i prima 4 SOCKET FUNKCIJA"); | |||
| dispatch( | |||
| addNewMessage({ | |||
| _id: props.chatId, | |||
| @@ -57,6 +54,15 @@ const RequestExchangeMessage = (props) => { | |||
| } | |||
| ); | |||
| }; | |||
| const handleAcceptExchange = () => { | |||
| console.log("accept salje i prima 1 POZVANA FUNKCIJA"); | |||
| dispatch( | |||
| acceptExchange({ | |||
| exchangeId: exchange._id, | |||
| handleApiResponseSuccess: handleAcceptExchangeSuccess, | |||
| }) | |||
| ); | |||
| }; | |||
| return ( | |||
| <RequestExchangeMessageContainer> | |||
| <RequestExchangeMessageText> | |||
| @@ -61,7 +61,6 @@ export const ChatColumn = (props) => { | |||
| return; | |||
| } | |||
| addMessageListener(({ succeed, data }) => { | |||
| console.log(data); | |||
| if (succeed) { | |||
| dispatch( | |||
| @@ -35,6 +35,7 @@ import { | |||
| } from "../../store/selectors/exchangeSelector"; | |||
| import { | |||
| acceptExchange, | |||
| fetchExchange, | |||
| setRequester, | |||
| } from "../../store/actions/exchange/exchangeActions"; | |||
| import { convertLocalDateToUTCDate } from "../../util/helpers/dateHelpers"; | |||
| @@ -72,7 +73,7 @@ const DirectChat = () => { | |||
| }, [chat, location.state]); | |||
| const amIBuyer = useMemo( | |||
| () => exchange.buyer?.userId === userId, | |||
| () => exchange.buyer?.user?._id === userId, | |||
| [exchange, userId] | |||
| ); | |||
| @@ -130,7 +131,6 @@ const DirectChat = () => { | |||
| // Listener to socket.IO chat | |||
| useEffect(() => { | |||
| addMessageListener(({ succeed, data }) => { | |||
| console.log(data); | |||
| if (succeed) { | |||
| if ( | |||
| [...allChats].find((item) => { | |||
| @@ -143,11 +143,11 @@ const DirectChat = () => { | |||
| message: data.message, | |||
| }) | |||
| ); | |||
| if ( | |||
| data.message?.isAcceptRequest && | |||
| requester === requesterStatus.NOONE | |||
| ) { | |||
| dispatch(setRequester(requesterStatus.interlocutor)); | |||
| if (data.message?.isAcceptRequest) { | |||
| dispatch(fetchExchange(exchange?._id)); | |||
| if (requester === requesterStatus.NOONE) { | |||
| dispatch(setRequester(requesterStatus.interlocutor)); | |||
| } | |||
| } | |||
| } else { | |||
| dispatch(fetchChats()); | |||
| @@ -159,7 +159,7 @@ const DirectChat = () => { | |||
| } | |||
| }); | |||
| return () => removeMessageListener(); | |||
| }, [allChats, routeMatch]); | |||
| }, [allChats, routeMatch, requester]); | |||
| const refreshChat = () => { | |||
| if (routeMatch.params?.chatId === "newMessage") { | |||
| @@ -169,24 +169,21 @@ const DirectChat = () => { | |||
| dispatch(fetchOneChat(routeMatch.params?.chatId)); | |||
| } | |||
| }; | |||
| const handleAcceptExchange = () => { | |||
| const handleAcceptExchangeSuccess = () => { | |||
| let interlocutor = userId === chat?.participants[0]._id ? 1 : 0; | |||
| console.log("accept salje i prima 3 POZVANA RESPONSE FUNKCIJA") | |||
| acceptExchangeSocket( | |||
| chat?._id, | |||
| userId, | |||
| chat?.participants[interlocutor]._id, | |||
| () => { | |||
| dispatch( | |||
| acceptExchange({ | |||
| exchangeId: exchange._id, | |||
| }) | |||
| ); | |||
| console.log("accept salje i prima 4 SOCKET FUNKCIJA") | |||
| dispatch( | |||
| addNewMessage({ | |||
| _id: chat?._id, | |||
| message: { | |||
| user: { | |||
| _id: userId | |||
| _id: userId, | |||
| }, | |||
| isAcceptRequest: true, | |||
| text: "", | |||
| @@ -200,6 +197,15 @@ const DirectChat = () => { | |||
| } | |||
| ); | |||
| }; | |||
| const handleAcceptExchange = () => { | |||
| console.log("accept salje i prima 1 POZVANA FUNKCIJA") | |||
| dispatch( | |||
| acceptExchange({ | |||
| exchangeId: exchange._id, | |||
| handleApiResponseSuccess: handleAcceptExchangeSuccess, | |||
| }) | |||
| ); | |||
| }; | |||
| return ( | |||
| <DirectChatContainer> | |||
| {isLoadingDirectChat || isLoadingDirectChat === undefined ? ( | |||
| @@ -5,10 +5,14 @@ import OfferCard from "../../Cards/OfferCard/OfferCard"; | |||
| import { useSelector } from "react-redux"; | |||
| import { selectExchange } from "../../../store/selectors/exchangeSelector"; | |||
| import { useDispatch } from "react-redux"; | |||
| import { fetchExchange } from "../../../store/actions/exchange/exchangeActions"; | |||
| import { | |||
| fetchExchange, | |||
| setExchange, | |||
| } from "../../../store/actions/exchange/exchangeActions"; | |||
| import { selectSelectedChat } from "../../../store/selectors/chatSelectors"; | |||
| import { selectUserId } from "../../../store/selectors/loginSelectors"; | |||
| import { toggleCreateReviewModal } from "../../../store/actions/modal/modalActions"; | |||
| import { setOneChat } from "../../../store/actions/chat/chatActions"; | |||
| const DirectChatHeader = (props) => { | |||
| const exchange = useSelector(selectExchange); | |||
| @@ -16,6 +20,13 @@ const DirectChatHeader = (props) => { | |||
| const dispatch = useDispatch(); | |||
| const chat = useSelector(selectSelectedChat); | |||
| useEffect(() => { | |||
| return () => { | |||
| dispatch(setExchange({})); | |||
| dispatch(setOneChat({})); | |||
| }; | |||
| }, []); | |||
| useEffect(() => { | |||
| if (chat?.exchange?._id) refetchExchange(); | |||
| }, [chat?.exchange]); | |||
| @@ -1,6 +1,6 @@ | |||
| import React from "react"; | |||
| import PropTypes from "prop-types"; | |||
| import { useSelector } from "react-redux"; | |||
| import { useDispatch, useSelector } from "react-redux"; | |||
| import { selectTotalOffers } from "../../../../store/selectors/offersSelectors"; | |||
| import { OffersContainer } from "./OffersList.styled"; | |||
| import BigProfileCard from "../../../Cards/ProfileCard/BigProfileCard/BigProfileCard"; | |||
| @@ -9,13 +9,17 @@ import Paging from "../../../Paging/Paging"; | |||
| import { startChat } from "../../../../util/helpers/chatHelper"; | |||
| import { selectLatestChats } from "../../../../store/selectors/chatSelectors"; | |||
| import { selectUserId } from "../../../../store/selectors/loginSelectors"; | |||
| import { setRequester } from "../../../../store/actions/exchange/exchangeActions"; | |||
| import requesterStatus from "../../../../constants/requesterStatus"; | |||
| const OffersList = (props) => { | |||
| const totalOffers = useSelector(selectTotalOffers); | |||
| const chats = useSelector(selectLatestChats); | |||
| const userId = useSelector(selectUserId); | |||
| const dispatch = useDispatch(); | |||
| const offers = props?.offers; | |||
| const messageOneUser = (offer) => { | |||
| dispatch(setRequester(requesterStatus.NOONE)); | |||
| startChat(chats, offer, userId); | |||
| }; | |||
| return ( | |||
| @@ -6,8 +6,8 @@ const request = axios.create({ | |||
| // baseURL: "http://192.168.88.175:3005/", | |||
| // baseURL: "http://192.168.88.143:3001/", // DULE | |||
| // baseURL: "https://trampa-api.dilig.net/", | |||
| baseURL: "https://trampa-api-test.dilig.net/", | |||
| // baseURL: "http://localhost:3001/", | |||
| // baseURL: "https://trampa-api-test.dilig.net/", | |||
| baseURL: "http://localhost:3001/", | |||
| // baseURL: process.env.REACT_APP_BASE_API_URL, | |||
| headers: { | |||
| "Content-Type": "application/json", | |||
| @@ -16,9 +16,9 @@ import { logoutUser, refreshUserToken } from "../actions/login/loginActions"; | |||
| // const baseURL = "http://192.168.88.143:3001/"; // DULE | |||
| // const baseURL = "http://192.168.88.175:3005/"; | |||
| // const baseURL = "https://trampa-api.dilig.net/"; | |||
| const baseURL = "https://trampa-api-test.dilig.net/"; | |||
| // const baseURL = "https://trampa-api-test.dilig.net/"; | |||
| // const baseURL = "http://192.168.88.150:3001/"; // DJOLE | |||
| // const baseURL = "http://localhost:3001/"; | |||
| const baseURL = "http://localhost:3001/"; | |||
| // const baseURL = process.env.REACT_APP_BASE_API_URL | |||
| //Interceptor unique name | |||
| @@ -38,8 +38,11 @@ export default ({ dispatch }) => | |||
| } | |||
| // If refresh token is expired, log out user | |||
| console.log(new Date(refreshTokenDecoded?.exp * 1000)) | |||
| console.log(new Date()) | |||
| if (new Date() > new Date(refreshTokenDecoded?.exp * 1000)) { | |||
| dispatch(logoutUser()); | |||
| return Promise.resolve(response) | |||
| } | |||
| // If access token is expired, refresh access token | |||
| if (new Date() > new Date(jwtTokenDecoded.exp * 1000)) { | |||
| @@ -47,25 +47,18 @@ function clearChats() { | |||
| } | |||
| function addNewMessage(state, { payload }) { | |||
| let allChats = [...state.latestChats]; | |||
| console.log("state", state); | |||
| let chat = allChats.find((item) => item._id === payload._id); | |||
| console.log("chat: ", chat); | |||
| if (chat) { | |||
| chat = { | |||
| ...chat, | |||
| messages: [...chat.messages, payload.message], | |||
| }; | |||
| console.log("newchat: ", chat); | |||
| allChats = allChats.filter((item) => item._id !== chat._id); | |||
| console.log("allchats1: ", allChats); | |||
| allChats = [chat, ...allChats]; | |||
| console.log("allchats2: ", allChats); | |||
| } | |||
| let newSelectedChat = {}; | |||
| if (state.selectedChat.chat) newSelectedChat = { ...state.selectedChat }; | |||
| console.log("newSelectedChat1: ", newSelectedChat); | |||
| newSelectedChat = { ...chat }; | |||
| console.log("newSelectedChat2: ", newSelectedChat); | |||
| return { | |||
| ...state, | |||
| latestChats: [...allChats], | |||
| @@ -128,7 +128,6 @@ function* sendMessage(payload) { | |||
| } | |||
| function* startNewChat(payload) { | |||
| try { | |||
| console.log(payload) | |||
| const userId = yield select(selectUserId); | |||
| const newChatData = yield call(attemptCreateNewChat, { | |||
| offerId: payload.payload.offerId, | |||
| @@ -44,10 +44,13 @@ function* validateExchange(payload) { | |||
| } | |||
| function* acceptExchange({ payload }) { | |||
| try { | |||
| console.log("accept salje i prima 2 POZVANA ACCEPT SAGA") | |||
| const userId = yield select(selectUserId); | |||
| yield call(attemptAcceptExchange, userId, payload.exchangeId); | |||
| const data = yield call(attemptFetchExchange, payload.exchangeId); | |||
| yield put(setExchange(data.data)); | |||
| if (payload.handleApiResponseSuccess) | |||
| yield call(payload.handleApiResponseSuccess); | |||
| yield put(acceptExchangeSuccess()); | |||
| } catch (e) { | |||
| yield put(acceptExchangeError()); | |||