| @@ -13,8 +13,8 @@ import SecondPartCreateOffer from "./SecondPart/SecondPartCreateOffer"; | |||
| import ThirdPartCreateOffer from "./ThirdPart/ThirdPartCreateOffer"; | |||
| import { | |||
| addOffer, | |||
| // fetchOffers, | |||
| // fetchOneOffer, | |||
| fetchOffers, | |||
| fetchOneOffer, | |||
| fetchProfileOffers, | |||
| } from "../../../store/actions/offers/offersActions"; | |||
| import { selectUserId } from "../../../store/selectors/loginSelectors"; | |||
| @@ -27,20 +27,29 @@ import selectedTheme from "../../../themes"; | |||
| import { useMemo } from "react"; | |||
| // import { useLocation } 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 { | |||
| OFFER_ADD_SCOPE, | |||
| OFFER_EDIT_SCOPE, | |||
| } from "../../../store/actions/offers/offersActionConstants"; | |||
| 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 location = useLocation(); | |||
| const history = useHistory(); | |||
| const queryString = useSelector(selectQueryString); | |||
| const [informations, setInformations] = useState({}); | |||
| const [currentStep, setCurrentStep] = useState(1); | |||
| const { t } = useTranslation(); | |||
| @@ -52,18 +61,24 @@ const CreateOffer = ({ editOffer, offer }) => { | |||
| const closeCreateOfferModal = () => dispatch(closeModal()); | |||
| 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(); | |||
| }; | |||
| @@ -106,6 +121,7 @@ const CreateOffer = ({ editOffer, offer }) => { | |||
| editOneOffer({ | |||
| offerId: offer._id, | |||
| offerData, | |||
| isAdmin, | |||
| handleApiResponseSuccess, | |||
| }) | |||
| ); | |||
| @@ -195,5 +211,7 @@ CreateOffer.propTypes = { | |||
| closeCreateOfferModal: PropTypes.func, | |||
| editOffer: PropTypes.bool, | |||
| offer: PropTypes.object, | |||
| isAdmin: PropTypes.bool, | |||
| customUserId: PropTypes.string, | |||
| }; | |||
| export default CreateOffer; | |||
| @@ -81,12 +81,20 @@ const ItemDetailsCard = (props) => { | |||
| dispatch( | |||
| toggleDeleteOfferModal({ | |||
| offer: offer.offer, | |||
| isAdmin: props.isAdmin, | |||
| }) | |||
| ); | |||
| }; | |||
| 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 = () => { | |||
| console.log(offer); | |||
| @@ -189,11 +197,13 @@ ItemDetailsCard.propTypes = { | |||
| isMyOffer: PropTypes.bool, | |||
| previewCard: PropTypes.bool, | |||
| createOffer: PropTypes.bool, | |||
| isAdmin: PropTypes.bool, | |||
| }; | |||
| ItemDetailsCard.defaultProps = { | |||
| halfwidth: false, | |||
| sponsored: false, | |||
| showExchangeButton: true, | |||
| isAdmin: false, | |||
| }; | |||
| export default ItemDetailsCard; | |||
| @@ -16,6 +16,7 @@ import BackdropComponent from "../../../MUI/BackdropComponent"; | |||
| import { useDispatch, useSelector } from "react-redux"; | |||
| import { | |||
| fetchOffers, | |||
| fetchOneOffer, | |||
| fetchProfileOffers, | |||
| pinOffer, | |||
| removeOffer, | |||
| @@ -29,6 +30,18 @@ import OfferDescription from "./OfferDescription/OfferDescription"; | |||
| import CancelButton from "./CancelButton/CancelButton"; | |||
| import SaveButton from "./SaveButton/SaveButton"; | |||
| 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 dispatch = useDispatch(); | |||
| @@ -43,8 +56,20 @@ const DeleteOffer = (props) => { | |||
| }; | |||
| 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 = () => { | |||
| @@ -53,7 +78,13 @@ const DeleteOffer = (props) => { | |||
| pinOffer({ offerId: props.offer?._id, handleApiResponseSuccess }) | |||
| ); | |||
| } else { | |||
| dispatch(removeOffer({ offerId, handleApiResponseSuccess })); | |||
| dispatch( | |||
| removeOffer({ | |||
| offerId, | |||
| isAdmin: props.isAdmin, | |||
| handleApiResponseSuccess, | |||
| }) | |||
| ); | |||
| } | |||
| closeDeleteModalHandler(); | |||
| if (history.location.pathname.includes("proizvodi")) { | |||
| @@ -122,6 +153,10 @@ DeleteOffer.propTypes = { | |||
| pin: PropTypes.bool, | |||
| deleteOffer: PropTypes.bool, | |||
| pinnedOffer: PropTypes.bool, | |||
| isAdmin: PropTypes.bool, | |||
| }; | |||
| DeleteOffer.defaultProps = { | |||
| isAdmin: false, | |||
| }; | |||
| export default DeleteOffer; | |||
| @@ -109,6 +109,8 @@ const OfferCard = (props) => { | |||
| toggleEditOfferModal({ | |||
| editOffer: true, | |||
| offer: props.offer, | |||
| isAdmin: props.isAdmin, | |||
| customUserId: props?.offer?.userId, | |||
| }) | |||
| ); | |||
| }; | |||
| @@ -117,6 +119,7 @@ const OfferCard = (props) => { | |||
| dispatch( | |||
| toggleDeleteOfferModal({ | |||
| offer: props.offer, | |||
| isAdmin: props.isAdmin, | |||
| }) | |||
| ); | |||
| }; | |||
| @@ -171,10 +171,12 @@ export default { | |||
| getFeaturedOffers: "offers/featured", | |||
| addOffer: "/users/{userId}/offers", | |||
| editOffer: "/users/{userId}/offers/{offerId}", | |||
| editOfferAsAdmin: "/admin/offers/{offerId}", | |||
| categories: "categories", | |||
| locations: "locations", | |||
| mineOffers: "users", | |||
| removeOffer: "/users/{userId}/offers/{offerId}", | |||
| removeOfferAsAdmin: "/admin/offers/{offerId}", | |||
| pinOffer: "admin/offers/{id}/pin", | |||
| }, | |||
| chat: { | |||
| @@ -46,6 +46,13 @@ export const attemptRemoveOffer = (payload, offerId) => { | |||
| }) | |||
| ); | |||
| }; | |||
| export const attemptRemoveOfferAsAdmin = (offerId) => { | |||
| return deleteRequest( | |||
| replaceInUrl(apiEndpoints.offers.removeOfferAsAdmin, { | |||
| offerId: offerId, | |||
| }) | |||
| ); | |||
| }; | |||
| export const attemptEditOffer = (payload, offerId, editedData) => { | |||
| return putRequest( | |||
| replaceInUrl(apiEndpoints.offers.editOffer, { | |||
| @@ -55,6 +62,14 @@ export const attemptEditOffer = (payload, offerId, editedData) => { | |||
| editedData | |||
| ); | |||
| }; | |||
| export const attemptEditOfferAsAdmin = (offerId, editedData) => { | |||
| return putRequest( | |||
| replaceInUrl(apiEndpoints.offers.editOfferAsAdmin, { | |||
| offerId: offerId, | |||
| }), | |||
| editedData | |||
| ); | |||
| }; | |||
| export const attemptPinOffer = (payload) => { | |||
| return patchRequest( | |||
| replaceInUrl(apiEndpoints.offers.pinOffer, { id: payload }) | |||
| @@ -1,11 +1,13 @@ | |||
| import { | |||
| attemptAddOffer, | |||
| attemptEditOffer, | |||
| attemptEditOfferAsAdmin, | |||
| attemptFetchFeaturedOffers, | |||
| attemptFetchOffers, | |||
| attemptFetchOneOffer, | |||
| attemptPinOffer, | |||
| attemptRemoveOffer, | |||
| attemptRemoveOfferAsAdmin, | |||
| } from "../../request/offersRequest"; | |||
| import { | |||
| // OFFERS_ALL_FETCH, | |||
| @@ -185,7 +187,7 @@ function* createOffer(payload) { | |||
| function* fetchOneOffer(payload) { | |||
| try { | |||
| const data = yield call(attemptFetchOneOffer, payload.payload); | |||
| if (!data?.data) throw new Error() | |||
| if (!data?.data) throw new Error(); | |||
| yield put(fetchOneOfferSuccess(data?.data)); | |||
| } catch (e) { | |||
| console.log(e?.response?.status); | |||
| @@ -227,7 +229,7 @@ function* fetchProfileOffers(payload) { | |||
| if (!userId || userId?.length === 0) | |||
| throw new Error("User id is not defined!"); | |||
| const data = yield call(attemptFetchProfileOffers, userId); | |||
| yield put(setProfileOffers(data.data)); | |||
| yield put(setProfileOffers(data.data.offers)); | |||
| yield put(fetchProfileOffersSuccess()); | |||
| } catch (e) { | |||
| console.dir(e); | |||
| @@ -242,7 +244,8 @@ function* removeOffer(payload) { | |||
| try { | |||
| const userId = yield select(selectUserId); | |||
| 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()); | |||
| if (payload.payload.handleApiResponseSuccess) { | |||
| yield call(payload.payload.handleApiResponseSuccess); | |||
| @@ -295,7 +298,11 @@ function* editOffer(payload) { | |||
| formData.append("location[city]", offerData.location.city); | |||
| formData.append("name", offerData.name); | |||
| 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()); | |||
| if (payload.payload.handleApiResponseSuccess) { | |||
| yield call(payload.payload.handleApiResponseSuccess); | |||