User generic addEntities action to add entities to the global store

This commit is contained in:
Kimmo Puputti 2017-05-02 11:35:10 +03:00
parent e2955bfa13
commit 6354600d34
5 changed files with 8 additions and 17 deletions

View file

@ -1,4 +1,4 @@
import { showListingsSuccess as globalShowListingsSuccess } from '../../ducks/marketplaceData.duck';
import { addEntities } from '../../ducks/marketplaceData.duck';
import { createStripeAccount } from '../../ducks/user.duck';
const requestAction = actionType => params => ({ type: actionType, payload: { params } });
@ -132,7 +132,7 @@ export function requestShowListing(actionPayload) {
.show(actionPayload)
.then(response => {
// EditListingPage fetches new listing data, which also needs to be added to global data
dispatch(globalShowListingsSuccess(response));
dispatch(addEntities(response));
// In case of success, we'll clear state.EditListingPage (user will be redirected away)
dispatch(showListingsSuccess(response));
return response;

View file

@ -1,4 +1,4 @@
import { showListingsSuccess } from '../../ducks/marketplaceData.duck';
import { addEntities } from '../../ducks/marketplaceData.duck';
import { fetchCurrentUser } from '../../ducks/user.duck';
// ================ Action types ================ //
@ -47,7 +47,7 @@ export const showListing = listingId =>
return sdk.listings
.show({ id: listingId, include: ['author', 'images'] })
.then(data => {
dispatch(showListingsSuccess(data));
dispatch(addEntities(data));
return data;
})
.catch(e => {

View file

@ -3,7 +3,7 @@ import { types } from '../../util/sdkLoader';
import { createUser, createCurrentUser, createListing, fakeIntl } from '../../util/test-data';
import { renderShallow } from '../../util/test-helpers';
import { ListingPageComponent } from './ListingPage';
import { showListingsSuccess } from '../../ducks/marketplaceData.duck';
import { addEntities } from '../../ducks/marketplaceData.duck';
import { showListingRequest, showListingError, showListing } from './ListingPage.duck';
const { UUID } = types;
@ -46,7 +46,7 @@ describe('ListingPage', () => {
expect(dispatch.mock.calls).toEqual([
[showListingRequest(id)],
[expect.anything()], // fetchCurrentUser() call
[showListingsSuccess(data)],
[addEntities(data)],
]);
});
});

View file

@ -1,4 +1,4 @@
import { showListingsSuccess } from '../../ducks/marketplaceData.duck';
import { addEntities } from '../../ducks/marketplaceData.duck';
// ================ Action types ================ //
@ -78,7 +78,7 @@ export const searchListings = searchParams =>
return searchOrQuery
.then(response => {
dispatch(showListingsSuccess(response));
dispatch(addEntities(response));
dispatch(searchListingsSuccess(response));
return response;
})

View file

@ -1,10 +1,7 @@
import { updatedEntities, denormalisedEntities } from '../util/data';
const successAction = actionType => data => ({ type: actionType, payload: data });
// ================ Action types ================ //
const SHOW_LISTINGS_SUCCESS = 'app/sdk/SHOW_LISTINGS_SUCCESS';
const ADD_ENTITIES = 'app/sdk/ADD_ENTITIES';
// ================ Reducer ================ //
@ -24,9 +21,6 @@ const merge = (state, apiResponse) => {
export default function marketplaceDataReducer(state = initialState, action = {}) {
const { type, payload } = action;
switch (type) {
case SHOW_LISTINGS_SUCCESS:
return merge(state, payload);
case ADD_ENTITIES:
return merge(state, payload);
@ -75,9 +69,6 @@ export const getEntities = (marketplaceData, entityRefs) => {
// ================ Action creators ================ //
// SDK method: listings.show
export const showListingsSuccess = successAction(SHOW_LISTINGS_SUCCESS);
export const addEntities = apiResponse => ({
type: ADD_ENTITIES,
payload: apiResponse,