Fetch own listings

This commit is contained in:
Vesa Luusua 2017-08-28 14:23:00 +03:00
parent d5c6df154f
commit 8cf6557eb2
2 changed files with 158 additions and 1 deletions

View file

@ -0,0 +1,147 @@
import { updatedEntities, denormalisedEntities } from '../../util/data';
// ================ Action types ================ //
export const FETCH_LISTINGS_REQUEST = 'app/ManageListingsPage/FETCH_LISTINGS_REQUEST';
export const FETCH_LISTINGS_SUCCESS = 'app/ManageListingsPage/FETCH_LISTINGS_SUCCESS';
export const FETCH_LISTINGS_ERROR = 'app/ManageListingsPage/FETCH_LISTINGS_ERROR';
export const ADD_OWN_ENTITIES = 'app/ManageListingsPage/ADD_OWN_ENTITIES';
// ================ Reducer ================ //
const initialState = {
pagination: null,
queryParams: null,
queryInProgress: false,
queryListingsError: null,
currentPageResultIds: [],
ownEntities: {},
};
const resultIds = data => data.data.map(l => l.id);
const merge = (state, apiResponse) => {
return {
...state,
ownEntities: updatedEntities(state.ownEntities, apiResponse.data),
};
};
const manageListingsPageReducer = (state = initialState, action = {}) => {
const { type, payload } = action;
switch (type) {
case FETCH_LISTINGS_REQUEST:
return {
...state,
queryParams: payload.queryParams,
queryInProgress: true,
queryListingsError: null,
currentPageResultIds: [],
};
case FETCH_LISTINGS_SUCCESS:
return {
...state,
currentPageResultIds: resultIds(payload.data),
pagination: payload.data.meta,
queryInProgress: false,
};
case FETCH_LISTINGS_ERROR:
// eslint-disable-next-line no-console
console.error(payload);
return { ...state, queryInProgress: false, queryListingsError: payload };
case ADD_OWN_ENTITIES:
return merge(state, payload);
default:
return state;
}
};
export default manageListingsPageReducer;
// ================ Selectors ================ //
/**
* Get the denormalised own listing entities with the given IDs
*
* @param {Object} state the full Redux store
* @param {Array<UUID>} listingIds listing IDs to select from the store
*/
export const getListingsById = (state, listingIds) => {
const { ownEntities } = state.ManageListingsPage;
try {
return denormalisedEntities(ownEntities, 'listing', listingIds);
} catch (e) {
return [];
}
};
/**
* Get the denormalised own entities from the given entity references.
*
* @param {Object} state the full Redux store
*
* @param {Array<{ id, type }} entityRefs References to entities that
* we want to query from the data. Currently we expect that all the
* entities have the same type.
*
* @return {Array<Object>} denormalised entities
*/
export const getOwnEntities = (state, entityRefs) => {
const { ownEntities } = state.ManageListingsPage;
const type = entityRefs.length > 0 ? entityRefs[0].type : null;
const ids = entityRefs.map(ref => ref.id);
try {
return denormalisedEntities(ownEntities, type, ids);
} catch (e) {
return [];
}
};
// ================ Action creators ================ //
// This works the same way as addMarketplaceEntities,
// but we don't want to mix own listings with searched listings
// (own listings data contains different info - e.g. exact location etc.)
export const addOwnEntities = apiResponse => ({
type: ADD_OWN_ENTITIES,
payload: apiResponse,
});
export const queryListingsRequest = queryParams => ({
type: FETCH_LISTINGS_REQUEST,
payload: { queryParams },
});
export const queryListingsSuccess = response => ({
type: FETCH_LISTINGS_SUCCESS,
payload: { data: response.data },
});
export const queryListingsError = e => ({
type: FETCH_LISTINGS_ERROR,
error: true,
payload: e,
});
export const queryOwnListings = queryParams =>
(dispatch, getState, sdk) => {
dispatch(queryListingsRequest(queryParams));
const { include = [], page, perPage } = queryParams;
// TODO: API can't handle camelCase request parameter yet.
return sdk.listings
.queryOwn({ include, page, per_page: perPage })
.then(response => {
dispatch(addOwnEntities(response));
dispatch(queryListingsSuccess(response));
return response;
})
.catch(e => {
dispatch(queryListingsError(e));
throw e;
});
};

View file

@ -10,5 +10,15 @@ import ListingPage from './ListingPage/ListingPage.duck';
import OrderPage from './OrderPage/OrderPage.duck';
import SalePage from './SalePage/SalePage.duck';
import SearchPage from './SearchPage/SearchPage.duck';
import ManageListingsPage from './ManageListingsPage/ManageListingsPage.duck';
export { CheckoutPage, EditListingPage, InboxPage, ListingPage, OrderPage, SalePage, SearchPage };
export {
CheckoutPage,
EditListingPage,
InboxPage,
ListingPage,
ManageListingsPage,
OrderPage,
SalePage,
SearchPage,
};