| import ThirdPartCreateOffer from "./ThirdPart/ThirdPartCreateOffer"; | import ThirdPartCreateOffer from "./ThirdPart/ThirdPartCreateOffer"; | ||||
| import { | import { | ||||
| addOffer, | addOffer, | ||||
| // fetchOffers, | |||||
| // fetchOneOffer, | |||||
| fetchOffers, | |||||
| fetchOneOffer, | |||||
| fetchProfileOffers, | fetchProfileOffers, | ||||
| } from "../../../store/actions/offers/offersActions"; | } from "../../../store/actions/offers/offersActions"; | ||||
| import { selectUserId } from "../../../store/selectors/loginSelectors"; | import { selectUserId } from "../../../store/selectors/loginSelectors"; | ||||
| import { useMemo } from "react"; | import { useMemo } from "react"; | ||||
| // import { useLocation } from "react-router-dom"; | // import { useLocation } from "react-router-dom"; | ||||
| import { useHistory } from "react-router-dom"; | import { useHistory } from "react-router-dom"; | ||||
| import { PROFILE_PAGE } from "../../../constants/pages"; | |||||
| import { replaceInRoute } from "../../../util/helpers/routeHelpers"; | |||||
| import { | |||||
| ADMIN_ITEM_DETAILS_PAGE, | |||||
| ADMIN_SINGLE_USER_PAGE, | |||||
| BASE_PAGE, | |||||
| HOME_PAGE, | |||||
| ITEM_DETAILS_PAGE, | |||||
| PROFILE_PAGE, | |||||
| } from "../../../constants/pages"; | |||||
| import { dynamicRouteMatches, replaceInRoute } from "../../../util/helpers/routeHelpers"; | |||||
| import { selectIsLoadingByActionType } from "../../../store/selectors/loadingSelectors"; | import { selectIsLoadingByActionType } from "../../../store/selectors/loadingSelectors"; | ||||
| import { | import { | ||||
| OFFER_ADD_SCOPE, | OFFER_ADD_SCOPE, | ||||
| OFFER_EDIT_SCOPE, | OFFER_EDIT_SCOPE, | ||||
| } from "../../../store/actions/offers/offersActionConstants"; | } from "../../../store/actions/offers/offersActionConstants"; | ||||
| import { closeModal } from "../../../store/actions/modal/modalActions"; | import { closeModal } from "../../../store/actions/modal/modalActions"; | ||||
| // import { routeMatches } from "../../../util/helpers/routeHelpers"; | |||||
| import { selectQueryString } from "../../../store/selectors/queryStringSelectors"; | |||||
| import { routeMatches } from "../../../util/helpers/routeHelpers"; | |||||
| const CreateOffer = ({ editOffer, offer }) => { | |||||
| const CreateOffer = ({ editOffer, offer, isAdmin, customUserId }) => { | |||||
| const dispatch = useDispatch(); | const dispatch = useDispatch(); | ||||
| // const location = useLocation(); | // const location = useLocation(); | ||||
| const history = useHistory(); | const history = useHistory(); | ||||
| const queryString = useSelector(selectQueryString); | |||||
| const [informations, setInformations] = useState({}); | const [informations, setInformations] = useState({}); | ||||
| const [currentStep, setCurrentStep] = useState(1); | const [currentStep, setCurrentStep] = useState(1); | ||||
| const { t } = useTranslation(); | const { t } = useTranslation(); | ||||
| const closeCreateOfferModal = () => dispatch(closeModal()); | const closeCreateOfferModal = () => dispatch(closeModal()); | ||||
| const handleApiResponseSuccess = () => { | const handleApiResponseSuccess = () => { | ||||
| // if (routeMatches(BASE_PAGE) || routeMatches(HOME_PAGE)) | |||||
| // dispatch(fetchOffers({ queryString: "" })); | |||||
| // if (location.pathname.includes("profile")) | |||||
| // dispatch(fetchProfileOffers(userId)); | |||||
| // if (location.pathname.includes("proizvodi")) | |||||
| // dispatch(fetchOneOffer(offer._id)); | |||||
| dispatch(fetchProfileOffers(userId)); | |||||
| history.push( | |||||
| replaceInRoute(PROFILE_PAGE, { | |||||
| idProfile: userId, | |||||
| }) | |||||
| ); | |||||
| if (editOffer) { | |||||
| if (routeMatches(BASE_PAGE) || routeMatches(HOME_PAGE)) | |||||
| dispatch(fetchOffers({ queryString })); | |||||
| if (dynamicRouteMatches(PROFILE_PAGE) || dynamicRouteMatches(ADMIN_SINGLE_USER_PAGE)) | |||||
| if (isAdmin) dispatch(fetchProfileOffers(customUserId)); | |||||
| else dispatch(fetchProfileOffers(userId)); | |||||
| if ( | |||||
| dynamicRouteMatches(ITEM_DETAILS_PAGE) || | |||||
| dynamicRouteMatches(ADMIN_ITEM_DETAILS_PAGE) | |||||
| ) | |||||
| dispatch(fetchOneOffer(offer._id)); | |||||
| } else | |||||
| history.push( | |||||
| replaceInRoute(PROFILE_PAGE, { | |||||
| idProfile: userId, | |||||
| }) | |||||
| ); | |||||
| closeCreateOfferModal(); | closeCreateOfferModal(); | ||||
| }; | }; | ||||
| editOneOffer({ | editOneOffer({ | ||||
| offerId: offer._id, | offerId: offer._id, | ||||
| offerData, | offerData, | ||||
| isAdmin, | |||||
| handleApiResponseSuccess, | handleApiResponseSuccess, | ||||
| }) | }) | ||||
| ); | ); | ||||
| closeCreateOfferModal: PropTypes.func, | closeCreateOfferModal: PropTypes.func, | ||||
| editOffer: PropTypes.bool, | editOffer: PropTypes.bool, | ||||
| offer: PropTypes.object, | offer: PropTypes.object, | ||||
| isAdmin: PropTypes.bool, | |||||
| customUserId: PropTypes.string, | |||||
| }; | }; | ||||
| export default CreateOffer; | export default CreateOffer; |
| dispatch( | dispatch( | ||||
| toggleDeleteOfferModal({ | toggleDeleteOfferModal({ | ||||
| offer: offer.offer, | offer: offer.offer, | ||||
| isAdmin: props.isAdmin, | |||||
| }) | }) | ||||
| ); | ); | ||||
| }; | }; | ||||
| const showEditOfferModalHandler = () => { | const showEditOfferModalHandler = () => { | ||||
| dispatch(toggleEditOfferModal({ editOffer: true, offer: offer.offer })); | |||||
| dispatch( | |||||
| toggleEditOfferModal({ | |||||
| editOffer: true, | |||||
| offer: offer?.offer, | |||||
| isAdmin: props.isAdmin, | |||||
| customUserId: offer?.offer?.userId | |||||
| }) | |||||
| ); | |||||
| }; | }; | ||||
| const showPinOfferModalHandler = () => { | const showPinOfferModalHandler = () => { | ||||
| console.log(offer); | console.log(offer); | ||||
| isMyOffer: PropTypes.bool, | isMyOffer: PropTypes.bool, | ||||
| previewCard: PropTypes.bool, | previewCard: PropTypes.bool, | ||||
| createOffer: PropTypes.bool, | createOffer: PropTypes.bool, | ||||
| isAdmin: PropTypes.bool, | |||||
| }; | }; | ||||
| ItemDetailsCard.defaultProps = { | ItemDetailsCard.defaultProps = { | ||||
| halfwidth: false, | halfwidth: false, | ||||
| sponsored: false, | sponsored: false, | ||||
| showExchangeButton: true, | showExchangeButton: true, | ||||
| isAdmin: false, | |||||
| }; | }; | ||||
| export default ItemDetailsCard; | export default ItemDetailsCard; |
| import { useDispatch, useSelector } from "react-redux"; | import { useDispatch, useSelector } from "react-redux"; | ||||
| import { | import { | ||||
| fetchOffers, | fetchOffers, | ||||
| fetchOneOffer, | |||||
| fetchProfileOffers, | fetchProfileOffers, | ||||
| pinOffer, | pinOffer, | ||||
| removeOffer, | removeOffer, | ||||
| import CancelButton from "./CancelButton/CancelButton"; | import CancelButton from "./CancelButton/CancelButton"; | ||||
| import SaveButton from "./SaveButton/SaveButton"; | import SaveButton from "./SaveButton/SaveButton"; | ||||
| import { closeModal } from "../../../../store/actions/modal/modalActions"; | import { closeModal } from "../../../../store/actions/modal/modalActions"; | ||||
| import { | |||||
| dynamicRouteMatches, | |||||
| routeMatches, | |||||
| } from "../../../../util/helpers/routeHelpers"; | |||||
| import { | |||||
| ADMIN_ITEM_DETAILS_PAGE, | |||||
| ADMIN_SINGLE_USER_PAGE, | |||||
| BASE_PAGE, | |||||
| HOME_PAGE, | |||||
| ITEM_DETAILS_PAGE, | |||||
| PROFILE_PAGE, | |||||
| } from "../../../../constants/pages"; | |||||
| const DeleteOffer = (props) => { | const DeleteOffer = (props) => { | ||||
| const dispatch = useDispatch(); | const dispatch = useDispatch(); | ||||
| }; | }; | ||||
| const handleApiResponseSuccess = () => { | const handleApiResponseSuccess = () => { | ||||
| dispatch(fetchProfileOffers(userId)); | |||||
| dispatch(fetchOffers({ queryString })); | |||||
| if ( | |||||
| dynamicRouteMatches(PROFILE_PAGE) || | |||||
| dynamicRouteMatches(ADMIN_SINGLE_USER_PAGE) | |||||
| ) | |||||
| dispatch(fetchProfileOffers(userId)); | |||||
| if (routeMatches(HOME_PAGE) || routeMatches(BASE_PAGE)) | |||||
| dispatch(fetchOffers({ queryString })); | |||||
| if ( | |||||
| dynamicRouteMatches(ITEM_DETAILS_PAGE) || | |||||
| dynamicRouteMatches(ADMIN_ITEM_DETAILS_PAGE) | |||||
| ) { | |||||
| if (props.pin) dispatch(fetchOneOffer(props.offer?._id)); | |||||
| else history.goBack(); | |||||
| } | |||||
| }; | }; | ||||
| const removeOfferHandler = () => { | const removeOfferHandler = () => { | ||||
| pinOffer({ offerId: props.offer?._id, handleApiResponseSuccess }) | pinOffer({ offerId: props.offer?._id, handleApiResponseSuccess }) | ||||
| ); | ); | ||||
| } else { | } else { | ||||
| dispatch(removeOffer({ offerId, handleApiResponseSuccess })); | |||||
| dispatch( | |||||
| removeOffer({ | |||||
| offerId, | |||||
| isAdmin: props.isAdmin, | |||||
| handleApiResponseSuccess, | |||||
| }) | |||||
| ); | |||||
| } | } | ||||
| closeDeleteModalHandler(); | closeDeleteModalHandler(); | ||||
| if (history.location.pathname.includes("proizvodi")) { | if (history.location.pathname.includes("proizvodi")) { | ||||
| pin: PropTypes.bool, | pin: PropTypes.bool, | ||||
| deleteOffer: PropTypes.bool, | deleteOffer: PropTypes.bool, | ||||
| pinnedOffer: PropTypes.bool, | pinnedOffer: PropTypes.bool, | ||||
| isAdmin: PropTypes.bool, | |||||
| }; | |||||
| DeleteOffer.defaultProps = { | |||||
| isAdmin: false, | |||||
| }; | }; | ||||
| export default DeleteOffer; | export default DeleteOffer; |
| toggleEditOfferModal({ | toggleEditOfferModal({ | ||||
| editOffer: true, | editOffer: true, | ||||
| offer: props.offer, | offer: props.offer, | ||||
| isAdmin: props.isAdmin, | |||||
| customUserId: props?.offer?.userId, | |||||
| }) | }) | ||||
| ); | ); | ||||
| }; | }; | ||||
| dispatch( | dispatch( | ||||
| toggleDeleteOfferModal({ | toggleDeleteOfferModal({ | ||||
| offer: props.offer, | offer: props.offer, | ||||
| isAdmin: props.isAdmin, | |||||
| }) | }) | ||||
| ); | ); | ||||
| }; | }; |
| getFeaturedOffers: "offers/featured", | getFeaturedOffers: "offers/featured", | ||||
| addOffer: "/users/{userId}/offers", | addOffer: "/users/{userId}/offers", | ||||
| editOffer: "/users/{userId}/offers/{offerId}", | editOffer: "/users/{userId}/offers/{offerId}", | ||||
| editOfferAsAdmin: "/admin/offers/{offerId}", | |||||
| categories: "categories", | categories: "categories", | ||||
| locations: "locations", | locations: "locations", | ||||
| mineOffers: "users", | mineOffers: "users", | ||||
| removeOffer: "/users/{userId}/offers/{offerId}", | removeOffer: "/users/{userId}/offers/{offerId}", | ||||
| removeOfferAsAdmin: "/admin/offers/{offerId}", | |||||
| pinOffer: "admin/offers/{id}/pin", | pinOffer: "admin/offers/{id}/pin", | ||||
| }, | }, | ||||
| chat: { | chat: { |
| }) | }) | ||||
| ); | ); | ||||
| }; | }; | ||||
| export const attemptRemoveOfferAsAdmin = (offerId) => { | |||||
| return deleteRequest( | |||||
| replaceInUrl(apiEndpoints.offers.removeOfferAsAdmin, { | |||||
| offerId: offerId, | |||||
| }) | |||||
| ); | |||||
| }; | |||||
| export const attemptEditOffer = (payload, offerId, editedData) => { | export const attemptEditOffer = (payload, offerId, editedData) => { | ||||
| return putRequest( | return putRequest( | ||||
| replaceInUrl(apiEndpoints.offers.editOffer, { | replaceInUrl(apiEndpoints.offers.editOffer, { | ||||
| editedData | editedData | ||||
| ); | ); | ||||
| }; | }; | ||||
| export const attemptEditOfferAsAdmin = (offerId, editedData) => { | |||||
| return putRequest( | |||||
| replaceInUrl(apiEndpoints.offers.editOfferAsAdmin, { | |||||
| offerId: offerId, | |||||
| }), | |||||
| editedData | |||||
| ); | |||||
| }; | |||||
| export const attemptPinOffer = (payload) => { | export const attemptPinOffer = (payload) => { | ||||
| return patchRequest( | return patchRequest( | ||||
| replaceInUrl(apiEndpoints.offers.pinOffer, { id: payload }) | replaceInUrl(apiEndpoints.offers.pinOffer, { id: payload }) |
| import { | import { | ||||
| attemptAddOffer, | attemptAddOffer, | ||||
| attemptEditOffer, | attemptEditOffer, | ||||
| attemptEditOfferAsAdmin, | |||||
| attemptFetchFeaturedOffers, | attemptFetchFeaturedOffers, | ||||
| attemptFetchOffers, | attemptFetchOffers, | ||||
| attemptFetchOneOffer, | attemptFetchOneOffer, | ||||
| attemptPinOffer, | attemptPinOffer, | ||||
| attemptRemoveOffer, | attemptRemoveOffer, | ||||
| attemptRemoveOfferAsAdmin, | |||||
| } from "../../request/offersRequest"; | } from "../../request/offersRequest"; | ||||
| import { | import { | ||||
| // OFFERS_ALL_FETCH, | // OFFERS_ALL_FETCH, | ||||
| function* fetchOneOffer(payload) { | function* fetchOneOffer(payload) { | ||||
| try { | try { | ||||
| const data = yield call(attemptFetchOneOffer, payload.payload); | const data = yield call(attemptFetchOneOffer, payload.payload); | ||||
| if (!data?.data) throw new Error() | |||||
| if (!data?.data) throw new Error(); | |||||
| yield put(fetchOneOfferSuccess(data?.data)); | yield put(fetchOneOfferSuccess(data?.data)); | ||||
| } catch (e) { | } catch (e) { | ||||
| console.log(e?.response?.status); | console.log(e?.response?.status); | ||||
| if (!userId || userId?.length === 0) | if (!userId || userId?.length === 0) | ||||
| throw new Error("User id is not defined!"); | throw new Error("User id is not defined!"); | ||||
| const data = yield call(attemptFetchProfileOffers, userId); | const data = yield call(attemptFetchProfileOffers, userId); | ||||
| yield put(setProfileOffers(data.data)); | |||||
| yield put(setProfileOffers(data.data.offers)); | |||||
| yield put(fetchProfileOffersSuccess()); | yield put(fetchProfileOffersSuccess()); | ||||
| } catch (e) { | } catch (e) { | ||||
| console.dir(e); | console.dir(e); | ||||
| try { | try { | ||||
| const userId = yield select(selectUserId); | const userId = yield select(selectUserId); | ||||
| const offerId = payload.payload.offerId; | const offerId = payload.payload.offerId; | ||||
| yield call(attemptRemoveOffer, userId, offerId); | |||||
| if (payload.payload.isAdmin) yield call(attemptRemoveOfferAsAdmin, offerId); | |||||
| else yield call(attemptRemoveOffer, userId, offerId); | |||||
| yield put(removeOfferSuccess()); | yield put(removeOfferSuccess()); | ||||
| if (payload.payload.handleApiResponseSuccess) { | if (payload.payload.handleApiResponseSuccess) { | ||||
| yield call(payload.payload.handleApiResponseSuccess); | yield call(payload.payload.handleApiResponseSuccess); | ||||
| formData.append("location[city]", offerData.location.city); | formData.append("location[city]", offerData.location.city); | ||||
| formData.append("name", offerData.name); | formData.append("name", offerData.name); | ||||
| formData.append("subcategory", offerData.subcategory); | formData.append("subcategory", offerData.subcategory); | ||||
| yield call(attemptEditOffer, userId, offerId, formData); | |||||
| if (payload.payload.isAdmin) { | |||||
| yield call(attemptEditOfferAsAdmin, offerId, formData); | |||||
| } else { | |||||
| yield call(attemptEditOffer, userId, offerId, formData); | |||||
| } | |||||
| yield put(editOfferSuccess()); | yield put(editOfferSuccess()); | ||||
| if (payload.payload.handleApiResponseSuccess) { | if (payload.payload.handleApiResponseSuccess) { | ||||
| yield call(payload.payload.handleApiResponseSuccess); | yield call(payload.payload.handleApiResponseSuccess); |