From e2813fb346d8341997a670342f7dd50ed34a4297 Mon Sep 17 00:00:00 2001 From: Boyan Tabakov Date: Wed, 17 Jan 2018 14:51:49 +0200 Subject: [PATCH] Update to use current API endpoints for own listings --- .../ManageListingCard/ManageListingCard.js | 6 ++--- .../EditListingPage/EditListingPage.duck.js | 14 ++++++------ .../EditListingPage/EditListingPage.js | 4 ++-- .../ManageListingsPage.duck.js | 16 +++++++------- .../ManageListingsPage/ManageListingsPage.js | 2 +- src/ducks/marketplaceData.duck.js | 15 +++++++++++++ src/util/data.js | 10 +++++++++ src/util/types.js | 22 +++++++++++++++++++ 8 files changed, 68 insertions(+), 21 deletions(-) diff --git a/src/components/ManageListingCard/ManageListingCard.js b/src/components/ManageListingCard/ManageListingCard.js index 034f9e8a..ac5ef2b0 100644 --- a/src/components/ManageListingCard/ManageListingCard.js +++ b/src/components/ManageListingCard/ManageListingCard.js @@ -7,7 +7,7 @@ import classNames from 'classnames'; import routeConfiguration from '../../routeConfiguration'; import { propTypes } from '../../util/types'; import { formatMoney } from '../../util/currency'; -import { ensureListing } from '../../util/data'; +import { ensureOwnListing } from '../../util/data'; import { createSlug } from '../../util/urlHelpers'; import { createResourceLocatorString } from '../../util/routes'; import { @@ -71,7 +71,7 @@ export const ManageListingCardComponent = props => { onToggleMenu, } = props; const classes = classNames(rootClassName || css.root, className); - const currentListing = ensureListing(listing); + const currentListing = ensureOwnListing(listing); const id = currentListing.id.uuid; const { title = '', price, closed } = currentListing.attributes; const slug = createSlug(title); @@ -255,7 +255,7 @@ ManageListingCardComponent.propTypes = { hasClosingError: bool.isRequired, hasOpeningError: bool.isRequired, intl: intlShape.isRequired, - listing: propTypes.listing.isRequired, + listing: propTypes.ownListing.isRequired, isMenuOpen: bool.isRequired, actionsInProgressListingId: shape({ uuid: string.isRequired }), onCloseListing: func.isRequired, diff --git a/src/containers/EditListingPage/EditListingPage.duck.js b/src/containers/EditListingPage/EditListingPage.duck.js index 1ac65bb0..373a179e 100644 --- a/src/containers/EditListingPage/EditListingPage.duck.js +++ b/src/containers/EditListingPage/EditListingPage.duck.js @@ -216,22 +216,22 @@ export const removeListingImage = imageId => ({ // take the params object that the corresponding SDK endpoint method // expects. -// SDK method: listings.create +// SDK method: ownListings.create export const createListing = requestAction(CREATE_LISTING_REQUEST); export const createListingSuccess = successAction(CREATE_LISTING_SUCCESS); export const createListingError = errorAction(CREATE_LISTING_ERROR); -// SDK method: listings.update +// SDK method: ownListings.update export const updateListing = requestAction(UPDATE_LISTING_REQUEST); export const updateListingSuccess = successAction(UPDATE_LISTING_SUCCESS); export const updateListingError = errorAction(UPDATE_LISTING_ERROR); -// SDK method: listings.show +// SDK method: ownListings.show export const showListings = requestAction(SHOW_LISTINGS_REQUEST); export const showListingsSuccess = successAction(SHOW_LISTINGS_SUCCESS); export const showListingsError = errorAction(SHOW_LISTINGS_ERROR); -// SDK method: listings.uploadImage +// SDK method: images.uploadListingImage export const uploadImage = requestAction(UPLOAD_IMAGE_REQUEST); export const uploadImageSuccess = successAction(UPLOAD_IMAGE_SUCCESS); export const uploadImageError = errorAction(UPLOAD_IMAGE_ERROR); @@ -241,7 +241,7 @@ export const uploadImageError = errorAction(UPLOAD_IMAGE_ERROR); export function requestShowListing(actionPayload) { return (dispatch, getState, sdk) => { dispatch(showListings(actionPayload)); - return sdk.listings + return sdk.ownListings .show(actionPayload) .then(response => { // EditListingPage fetches new listing data, which also needs to be added to global data @@ -267,7 +267,7 @@ export function requestCreateListing(data) { dispatch(createListing(cleanedData)); - return sdk.listings + return sdk.ownListings .create(cleanedData) .then(response => { const id = response.data.data.id.uuid; @@ -312,7 +312,7 @@ export function requestUpdateListing(tab, data) { dispatch(updateListing(cleanedData)); const { id } = cleanedData; let updateResponse; - return sdk.listings + return sdk.ownListings .update(cleanedData) .then(response => { updateResponse = response; diff --git a/src/containers/EditListingPage/EditListingPage.js b/src/containers/EditListingPage/EditListingPage.js index f521a9bb..5ee785f4 100644 --- a/src/containers/EditListingPage/EditListingPage.js +++ b/src/containers/EditListingPage/EditListingPage.js @@ -7,7 +7,7 @@ import { connect } from 'react-redux'; import { types as sdkTypes } from '../../util/sdkLoader'; import { createSlug } from '../../util/urlHelpers'; import { propTypes } from '../../util/types'; -import { getListingsById } from '../../ducks/marketplaceData.duck'; +import { getOwnListingsById } from '../../ducks/marketplaceData.duck'; import { manageDisableScrolling, isScrollingDisabled } from '../../ducks/UI.duck'; import { stripeAccountClearError, createStripeAccount } from '../../ducks/user.duck'; import { EditListingWizard, NamedRedirect, Page } from '../../components'; @@ -216,7 +216,7 @@ const mapStateToProps = state => { const fetchInProgress = createStripeAccountInProgress; const getListing = id => { - const listings = getListingsById(state, [id]); + const listings = getOwnListingsById(state, [id]); return listings.length === 1 ? listings[0] : null; }; return { diff --git a/src/containers/ManageListingsPage/ManageListingsPage.duck.js b/src/containers/ManageListingsPage/ManageListingsPage.duck.js index d3a59955..582a72aa 100644 --- a/src/containers/ManageListingsPage/ManageListingsPage.duck.js +++ b/src/containers/ManageListingsPage/ManageListingsPage.duck.js @@ -42,15 +42,15 @@ const merge = (state, apiResponse) => { }; const updateListingAttributes = (state, listingEntity) => { - const oldListing = state.ownEntities.listing[listingEntity.id.uuid]; + const oldListing = state.ownEntities.ownListing[listingEntity.id.uuid]; const updatedListing = { ...oldListing, attributes: listingEntity.attributes }; const ownListingEntities = { - ...state.ownEntities.listing, + ...state.ownEntities.ownListing, [listingEntity.id.uuid]: updatedListing, }; return { ...state, - ownEntities: { ...state.ownEntities, listing: ownListingEntities }, + ownEntities: { ...state.ownEntities, ownListing: ownListingEntities }, }; }; @@ -146,7 +146,7 @@ export default manageListingsPageReducer; export const getListingsById = (state, listingIds) => { const { ownEntities } = state.ManageListingsPage; try { - return denormalisedEntities(ownEntities, 'listing', listingIds); + return denormalisedEntities(ownEntities, 'ownListing', listingIds); } catch (e) { return []; } @@ -238,8 +238,8 @@ export const queryOwnListings = queryParams => (dispatch, getState, sdk) => { const { include = [], page, perPage } = queryParams; - return sdk.listings - .queryOwn({ include, page, per_page: perPage }) + return sdk.ownListings + .query({ include, page, per_page: perPage }) .then(response => { dispatch(addOwnEntities(response)); dispatch(queryListingsSuccess(response)); @@ -254,7 +254,7 @@ export const queryOwnListings = queryParams => (dispatch, getState, sdk) => { export const closeListing = listingId => (dispatch, getState, sdk) => { dispatch(closeListingRequest(listingId)); - return sdk.listings + return sdk.ownListings .close({ id: listingId }, { expand: true }) .then(response => { dispatch(closeListingSuccess(response)); @@ -268,7 +268,7 @@ export const closeListing = listingId => (dispatch, getState, sdk) => { export const openListing = listingId => (dispatch, getState, sdk) => { dispatch(openListingRequest(listingId)); - return sdk.listings + return sdk.ownListings .open({ id: listingId }, { expand: true }) .then(response => { dispatch(openListingSuccess(response)); diff --git a/src/containers/ManageListingsPage/ManageListingsPage.js b/src/containers/ManageListingsPage/ManageListingsPage.js index e7e843db..42d59d13 100644 --- a/src/containers/ManageListingsPage/ManageListingsPage.js +++ b/src/containers/ManageListingsPage/ManageListingsPage.js @@ -171,7 +171,7 @@ ManageListingsPageComponent.propTypes = { listingId: propTypes.uuid.isRequired, error: propTypes.error.isRequired, }), - listings: arrayOf(propTypes.listing), + listings: arrayOf(propTypes.ownListing), onCloseListing: func.isRequired, onOpenListing: func.isRequired, openingListing: shape({ uuid: string.isRequired }), diff --git a/src/ducks/marketplaceData.duck.js b/src/ducks/marketplaceData.duck.js index e15919e0..25b459f5 100644 --- a/src/ducks/marketplaceData.duck.js +++ b/src/ducks/marketplaceData.duck.js @@ -46,6 +46,21 @@ export const getListingsById = (state, listingIds) => { } }; +/** + * Get the denormalised ownListing entities with the given IDs + * + * @param {Object} state the full Redux store + * @param {Array} listingIds listing IDs to select from the store + */ +export const getOwnListingsById = (state, listingIds) => { + const { entities } = state.marketplaceData; + try { + return denormalisedEntities(entities, 'ownListing', listingIds); + } catch (e) { + return []; + } +}; + /** * Get the denormalised entities from the given entity references. * diff --git a/src/util/data.js b/src/util/data.js index aeeb07be..70c5b13c 100644 --- a/src/util/data.js +++ b/src/util/data.js @@ -141,6 +141,16 @@ export const ensureListing = listing => { return { ...empty, ...listing }; }; +/** + * Create shell objects to ensure that attributes etc. exists. + * + * @param {Object} listing entity object, which is to be ensured agains null values + */ +export const ensureOwnListing = listing => { + const empty = { id: null, type: 'ownListing', attributes: { customAttributes: {} }, images: [] }; + return { ...empty, ...listing }; +}; + /** * Create shell objects to ensure that attributes etc. exists. * diff --git a/src/util/types.js b/src/util/types.js index 727f08e6..25d6ec99 100644 --- a/src/util/types.js +++ b/src/util/types.js @@ -134,6 +134,19 @@ const listingAttributes = shape({ deleted: propTypes.value(false).isRequired, price: propTypes.money, customAttributes: object, + publicData: object, +}); + +const ownListingAttributes = shape({ + title: string.isRequired, + description: string.isRequired, + address: string.isRequired, + geolocation: propTypes.latlng.isRequired, + closed: bool.isRequired, + deleted: propTypes.value(false).isRequired, + price: propTypes.money, + customAttributes: object, + publicData: object, }); const deletedListingAttributes = shape({ @@ -150,6 +163,15 @@ propTypes.listing = shape({ images: arrayOf(propTypes.image), }); +// Denormalised ownListing object +propTypes.ownListing = shape({ + id: propTypes.uuid.isRequired, + type: propTypes.value('ownListing').isRequired, + attributes: oneOfType([ownListingAttributes, deletedListingAttributes]).isRequired, + author: propTypes.user, + images: arrayOf(propTypes.image), +}); + // Denormalised booking object propTypes.booking = shape({ id: propTypes.uuid.isRequired,