| @@ -47,6 +47,7 @@ import { | |||
| import { closeModal } from "../../../store/actions/modal/modalActions"; | |||
| import { selectQueryString } from "../../../store/selectors/queryStringSelectors"; | |||
| import { routeMatches } from "../../../util/helpers/routeHelpers"; | |||
| import { fetchProfile } from "../../../store/actions/profile/profileActions"; | |||
| const CreateOffer = ({ editOffer, offer, isAdmin, customUserId }) => { | |||
| const dispatch = useDispatch(); | |||
| @@ -71,9 +72,12 @@ const CreateOffer = ({ editOffer, offer, isAdmin, customUserId }) => { | |||
| dynamicRouteMatches(PROFILE_PAGE) || | |||
| dynamicRouteMatches(ADMIN_SINGLE_USER_PAGE) | |||
| ) { | |||
| if (isAdmin) | |||
| if (isAdmin) { | |||
| dispatch(fetchProfileOffers({ idProfile: customUserId, isAdmin })); | |||
| else dispatch(fetchProfileOffers(userId)); | |||
| } else { | |||
| dispatch(fetchProfileOffers(userId)); | |||
| dispatch(fetchProfile(userId)); | |||
| } | |||
| console.log("tu je gde treba"); | |||
| } else if ( | |||
| dynamicRouteMatches(ITEM_DETAILS_PAGE) || | |||
| @@ -39,7 +39,7 @@ import { | |||
| toggleDeleteOfferModal, | |||
| toggleEditOfferModal, | |||
| } from "../../../store/actions/modal/modalActions"; | |||
| import { fetchChats } from "../../../store/actions/chat/chatActions"; | |||
| import { getExistChat } from "../../../store/actions/chat/chatActions"; | |||
| const ItemDetailsCard = (props) => { | |||
| // const offer = props.offer; | |||
| @@ -75,9 +75,22 @@ const ItemDetailsCard = (props) => { | |||
| const date = formatDateLocale(new Date(offer?._created)); | |||
| const handleApiResponseSuccess = (value) => { | |||
| if (value === false) { | |||
| startChat(chats, offer, userId); | |||
| } else { | |||
| startChat(chats, offer, value._id); | |||
| } | |||
| }; | |||
| const startExchange = () => { | |||
| dispatch(fetchChats({ userId: userId, offerId: props.offer._id })); | |||
| startChat(chats, offer, userId); | |||
| dispatch( | |||
| getExistChat({ | |||
| userId: userId, | |||
| offerId: props.offer._id, | |||
| handleApiResponseSuccess, | |||
| }) | |||
| ); | |||
| }; | |||
| const showDeleteOfferModalHandler = () => { | |||
| @@ -43,6 +43,7 @@ import { | |||
| PROFILE_PAGE, | |||
| } from "../../../../constants/pages"; | |||
| import { OfferDescriptionContainer } from "./DeleteOfferLabeledCard/DeleteOfferLabeledCard.styled"; | |||
| import { fetchProfile } from "../../../../store/actions/profile/profileActions"; | |||
| const DeleteOffer = (props) => { | |||
| const dispatch = useDispatch(); | |||
| @@ -60,8 +61,10 @@ const DeleteOffer = (props) => { | |||
| if ( | |||
| dynamicRouteMatches(PROFILE_PAGE) || | |||
| dynamicRouteMatches(ADMIN_SINGLE_USER_PAGE) | |||
| ) | |||
| ) { | |||
| dispatch(fetchProfileOffers({ idProfile: userId, isAdmin: true })); | |||
| dispatch(fetchProfile(userId)); | |||
| } | |||
| if (routeMatches(HOME_PAGE) || routeMatches(BASE_PAGE)) | |||
| dispatch(fetchOffers({ queryString })); | |||
| if (dynamicRouteMatches(ITEM_DETAILS_PAGE)) { | |||
| @@ -13,6 +13,7 @@ const MarketPlace = (props) => { | |||
| const userId = useSelector(selectUserId); | |||
| const dispatch = useDispatch(); | |||
| useEffect(() => { | |||
| console.log("ABG", userId); | |||
| if (userId) | |||
| dispatch( | |||
| fetchChats({ | |||
| @@ -43,6 +43,7 @@ export const MyMessages = (props) => { | |||
| }; | |||
| useEffect(() => { | |||
| if (history.location.pathname.includes("messages")) return; | |||
| if (userId?.length > 1) { | |||
| dispatch(fetchHeaderChats(userId)); | |||
| } | |||
| @@ -55,7 +56,7 @@ export const MyMessages = (props) => { | |||
| const goToMessages = () => { | |||
| if (lastChats.length !== 0) { | |||
| console.log(chats); | |||
| goToMessage(chats[0]?._id) | |||
| goToMessage(chats[0]?._id); | |||
| } else { | |||
| makeErrorToastMessage(t("messages.noMessagesToast")); | |||
| props.closePopover(); | |||
| @@ -8,11 +8,13 @@ import { selectIsLoadingByActionType } from "../../store/selectors/loadingSelect | |||
| import { useSelector } from "react-redux"; | |||
| import { OFFERS_SCOPE } from "../../store/actions/offers/offersActionConstants"; | |||
| import useOffers from "../../hooks/useOffers/useOffers"; | |||
| import useIsLoggedIn from "../../hooks/useIsLoggedIn"; | |||
| // import useIsLoggedIn from "../../hooks/useIsLoggedIn"; | |||
| import AboutPageContent from "../../components/About/AboutPageContent"; | |||
| import { selectUserId } from "../../store/selectors/loginSelectors"; | |||
| const HomePage = (props) => { | |||
| const { isLoggedIn } = useIsLoggedIn(); | |||
| // const { isLoggedIn } = useIsLoggedIn(); | |||
| const userId = useSelector(selectUserId); | |||
| const isLoadingOffers = useSelector( | |||
| selectIsLoadingByActionType(OFFERS_SCOPE) | |||
| ); | |||
| @@ -21,7 +23,8 @@ const HomePage = (props) => { | |||
| const toggleFilters = () => { | |||
| setFiltersOpened((prevFiltersOpened) => !prevFiltersOpened); | |||
| }; | |||
| if (isLoggedIn || props?.isMarketplacePage) { | |||
| console.log("ABBGAG"); | |||
| if (userId || props?.isMarketplacePage) { | |||
| return ( | |||
| <HomePageContainer> | |||
| <MainLayout | |||
| @@ -187,6 +187,7 @@ export default { | |||
| getOneChat: "users/{userId}/chats/{chatId}", | |||
| createChat: "users/{userId}/offers/{offerId}/chat", | |||
| sendMessage: "chats", | |||
| getExistChat: "users/{userId}/offers/{offerId}/chat", | |||
| }, | |||
| exchange: { | |||
| getExchange: "exchanges", | |||
| @@ -33,3 +33,12 @@ export const attemptCreateNewChat = (payload) => { | |||
| }) | |||
| ); | |||
| }; | |||
| export const attemptGetExistChat = (payload) => { | |||
| return getRequest( | |||
| replaceInUrl(apiEndpoints.chat.getExistChat, { | |||
| userId: payload.userId, | |||
| offerId: payload.offerId, | |||
| }) | |||
| ); | |||
| }; | |||
| @@ -17,9 +17,8 @@ const request = axios.create({ | |||
| // queryString.stringify(params, { arrayFormat: "comma" }), | |||
| }); | |||
| export const getRequest = (url, params = null, options = null) => { | |||
| return request.get(url, { params, ...options }); | |||
| }; | |||
| export const getRequest = (url, params = null, options = null) => | |||
| request.get(url, { params, ...options }); | |||
| export const postRequest = (url, data, params = null, options = null) => | |||
| request.post(url, data, { params, ...options }); | |||
| @@ -10,6 +10,7 @@ export const CHAT_HEADER_SCOPE = "CHAT_HEADER_SCOPE"; | |||
| const CHAT_ONE_SCOPE = "CHAT_ONE_SCOPE"; | |||
| const CHAT_SEND_SCOPE = "CHAT_SEND_SCOPE"; | |||
| const CHAT_NEW_SCOPE = "CHAT_NEW_SCOPE"; | |||
| const EXIST_CHAT_SCOPE = "EXIST_CHAT_SCOPE"; | |||
| export const CHAT_FETCH = createFetchType(CHAT_SCOPE); | |||
| export const CHAT_FETCH_SUCCESS = createSuccessType(CHAT_SCOPE); | |||
| @@ -38,3 +39,7 @@ export const CHAT_CLEAR = createSetType("CHAT_CLEAR"); | |||
| export const CHAT_ADD_MESSAGE = createSetType("CHAT_ADD_MESSAGE"); | |||
| export const CHAT_REMOVE_MESSAGE = createSetType("CHAT_REMOVE_MESSAGE"); | |||
| // export const ADD_ONE_CHAT = "CHAT_ONE_ADD"; | |||
| export const GET_EXIST_CHAT = createFetchType(EXIST_CHAT_SCOPE); | |||
| export const GET_EXIST_CHAT_SUCCESS = createSetType(EXIST_CHAT_SCOPE); | |||
| export const GET_EXIST_CHAT_ERROR = createSetType(EXIST_CHAT_SCOPE); | |||
| @@ -20,6 +20,9 @@ import { | |||
| CHAT_SEND_SUCCESS, | |||
| CHAT_SET, | |||
| CHAT_TOTAL_SET, | |||
| GET_EXIST_CHAT, | |||
| GET_EXIST_CHAT_ERROR, | |||
| GET_EXIST_CHAT_SUCCESS, | |||
| } from "./chatActionConstants"; | |||
| export const fetchChats = (payload) => ({ | |||
| @@ -94,3 +97,13 @@ export const addNewMessage = (payload) => ({ | |||
| export const removeLastMessage = () => ({ | |||
| type: CHAT_REMOVE_MESSAGE, | |||
| }); | |||
| export const getExistChat = (payload) => ({ | |||
| type: GET_EXIST_CHAT, | |||
| payload, | |||
| }); | |||
| export const getExistChatSuccess = () => ({ | |||
| type: GET_EXIST_CHAT_SUCCESS, | |||
| }); | |||
| export const getExistChatError = () => ({ | |||
| type: GET_EXIST_CHAT_ERROR, | |||
| }); | |||
| @@ -4,20 +4,23 @@ import { logoutUser } from "../actions/login/loginActions"; | |||
| import { makeErrorToastMessage } from "../utils/makeToastMessage"; | |||
| //Interceptor unique name | |||
| export const authenticationMiddlewareInterceptorName = "AUTHENTICATION_MIDDLEWARE"; | |||
| export const authenticationMiddlewareInterceptorName = | |||
| "AUTHENTICATION_MIDDLEWARE"; | |||
| export default ({ dispatch }) => | |||
| (next) => | |||
| (action) => { | |||
| attachPostRequestListener((error) => { | |||
| if (!error.response) { | |||
| return makeErrorToastMessage(i18next.t("apiErrors.somethingWentWrong")); | |||
| makeErrorToastMessage(i18next.t("apiErrors.somethingWentWrong")); | |||
| return Promise.reject(error); | |||
| } | |||
| if (error.response.status === 401) { | |||
| dispatch(logoutUser()); | |||
| return makeErrorToastMessage(i18next.t("apiErrors.somethingWentWrong")); | |||
| return Promise.reject(error); | |||
| } | |||
| return Promise.reject(error); | |||
| return Promise.resolve(); | |||
| // return Promise.resolve(error); | |||
| }, authenticationMiddlewareInterceptorName); | |||
| next(action); | |||
| @@ -4,6 +4,7 @@ import { | |||
| attemptFetchChats, | |||
| attemptFetchHeaderChats, | |||
| attemptFetchOneChat, | |||
| attemptGetExistChat, | |||
| attemptSendMessage, | |||
| } from "../../request/chatRequest"; | |||
| import { | |||
| @@ -12,6 +13,7 @@ import { | |||
| CHAT_NEW_FETCH, | |||
| CHAT_ONE_FETCH, | |||
| CHAT_SEND_FETCH, | |||
| GET_EXIST_CHAT, | |||
| } from "../actions/chat/chatActionConstants"; | |||
| import { | |||
| addNewMessage, | |||
| @@ -21,6 +23,8 @@ import { | |||
| fetchHeaderChatsSuccess, | |||
| fetchOneChatError, | |||
| fetchOneChatSuccess, | |||
| getExistChatError, | |||
| getExistChatSuccess, | |||
| sendMessageError, | |||
| sendMessageSuccess, | |||
| setChats, | |||
| @@ -80,6 +84,7 @@ function* fetchHeaderChats() { | |||
| console.dir(e); | |||
| } | |||
| } | |||
| function* fetchOneChat(payload) { | |||
| try { | |||
| const userId = yield select(selectUserId); | |||
| @@ -169,6 +174,24 @@ function* startNewChat(payload) { | |||
| } | |||
| } | |||
| function* getExistChat(payload) { | |||
| try { | |||
| const data = yield call(attemptGetExistChat, { | |||
| userId: payload.payload.userId, | |||
| offerId: payload.payload.offerId, | |||
| }); | |||
| console.log(data); | |||
| yield put(getExistChatSuccess()); | |||
| if (payload.payload.handleApiResponseSuccess) { | |||
| yield call(payload.payload.handleApiResponseSuccess, data.data); | |||
| } | |||
| } catch (error) { | |||
| yield put(getExistChatError()); | |||
| console.log(error); | |||
| } | |||
| } | |||
| export default function* chatSaga() { | |||
| yield all([ | |||
| takeLatest(CHAT_FETCH, fetchChats), | |||
| @@ -176,5 +199,6 @@ export default function* chatSaga() { | |||
| takeLatest(CHAT_HEADER_FETCH, fetchHeaderChats), | |||
| takeLatest(CHAT_SEND_FETCH, sendMessage), | |||
| takeLatest(CHAT_NEW_FETCH, startNewChat), | |||
| takeLatest(GET_EXIST_CHAT, getExistChat), | |||
| ]); | |||
| } | |||
| @@ -46,8 +46,9 @@ function* fetchLogin({ payload }) { | |||
| try { | |||
| const isAdmin = payload.isAdmin; | |||
| // delete payload.isAdmin | |||
| const { data } = yield call(attemptLogin, payload); | |||
| if (data.token) { | |||
| const dataFromBe = yield call(attemptLogin, payload); | |||
| const data = dataFromBe?.data; | |||
| if (data?.token) { | |||
| const token = data.token; | |||
| const refresh = data.refresh; | |||
| const tokenDecoded = jwt.decode(token); | |||
| @@ -82,6 +83,7 @@ function* fetchLogin({ payload }) { | |||
| } | |||
| } | |||
| } catch (e) { | |||
| console.dir(e); | |||
| if (e.message) { | |||
| yield put(fetchUserError(e.message)); | |||
| } | |||