case 'FILTER':
return { ...state, products: action.payload, isFetching: false, error: '' }
export const filterProducts = colors => (dispatch, getState) => {
dispatch({
type: 'FILTER',
payload: getState().products.products.filter(product => colors.includes(product.color)),
})
}
export function products(state = initialState, action) {
switch (action.type) {
case GET_PRODUCTS_REQUEST:
return { ...state, products: [], isFetching: true, error: '' }
case GET_PRODUCTS_SUCCESS:
return { ...state, products: action.payload, isFetching: false, error: '' }
case GET_PRODUCTS_FAIL:
return { ...state, error: action.payload.message, isFetching: false }
default:
return state
}
}
const mapStateToProps = store => ({ products: store.products })
const mapDispatchToProps = dispatch => ({
getProducts: () => dispatch(getProducts()),
})
export default connect(
mapStateToProps,
mapDispatchToProps
)(ProductsContainer)