Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
return null;
}
// Fire the onBeforeDispatch callback to inform a caller that fetchProducts will return data.
onBeforeDispatch();
dispatch(requestProducts({
hash,
cached,
cachedTime,
requestParams,
}));
return new PipelineRequest(pipeline)
.setInput(requestParams)
.dispatch()
.then((response) => {
let totalResultCount = response.totalProductCount;
/**
* When the next check was written, getHighlightProducts and getLiveshoppingProducts
* didn't deliver a totalProductCount within their responses - they simply returned all
* available products.
* So we set the products count of the response as totalProductCount to decrease the
* amount of logic, which is necessary to deal with product related pipelines.
*/
if (
typeof totalResultCount === 'undefined' &&
(
pipeline === pipelines.SHOPGATE_CATALOG_GET_HIGHLIGHT_PRODUCTS ||
return new Promise((resolve, reject) => {
if (shouldFetchData(getEntryByType(URL_TYPE_REGISTER, state), 'url')) {
dispatch(requestUrl(URL_TYPE_REGISTER));
new PipelineRequest('shopgate.user.getRegistrationUrl')
.setTrusted()
.dispatch()
.then(({ url, expires }) => {
dispatch(receiveUrl(URL_TYPE_REGISTER, url, expires));
resolve(url);
})
.catch((error) => {
logger.error(error);
dispatch(errorUrl(URL_TYPE_REGISTER));
reject();
});
} else {
// The registrationUrl is still valid
resolve(getRegisterUrl(state));
}
});
const getProductOptions = productId => (dispatch, getState) => {
const state = getState();
const cachedData = state.product.optionsByProductId[productId];
if (!shouldFetchData(cachedData)) {
return;
}
dispatch(requestProductOptions(productId));
new PipelineRequest('shopgate.catalog.getProductOptions')
.setInput({ productId })
.dispatch()
.then(result => dispatch(receiveProductOptions(productId, result.options)))
.catch((error) => {
logger.error(error);
dispatch(errorProductOptions(productId));
});
};
export default ({ login, password }) => (dispatch) => {
dispatch(requestLogin(login, password));
const params = {
strategy: 'basic',
parameters: {
login,
password,
},
};
new PipelineRequest('shopgate.user.loginUser')
.setTrusted()
.setHandledErrors([EINVALIDCALL])
.setInput(params)
.dispatch()
.then(({ success, messages }) => {
if (success) {
dispatch(successLogin());
} else {
dispatch(errorLogin(messages));
}
})
.catch((error) => {
const { code } = error;
if (code === EINVALIDCALL) {
/**
) => (dispatch) => {
const hash = generateResultHash({
pipeline: pipelines.SHOPGATE_CATALOG_GET_PRODUCT_REVIEWS,
productId,
}, false);
dispatch(requestProductReviewsList(hash));
/**
* For testing purposes there's need to keep and return the promise reference, not a result of
* chained functions.
* Otherwise test case won't get the original resolver.
* To get more insights, please take a look at ../spec.js.
* @type {Promise}
*/
const request = new PipelineRequest(pipelines.SHOPGATE_CATALOG_GET_PRODUCT_REVIEWS)
.setInput({
productId,
limit,
offset,
sort,
})
.dispatch();
request
.then((result) => {
dispatch(receiveProductReviewsList(hash, productId, result.reviews, result.totalReviewCount));
})
.catch((error) => {
logger.error(error);
dispatch(errorProductReviewsList(hash));
});
const getProductDescription = productId => (dispatch, getState) => {
const state = getState();
const description = state.product.descriptionsByProductId[productId];
if (!shouldFetchData(description)) {
return;
}
dispatch(requestProductDescription(productId));
new PipelineRequest('shopgate.catalog.getProductDescription')
.setInput({ productId })
.dispatch()
.then(result => dispatch(receiveProductDescription(productId, result.description)))
.catch((error) => {
logger.error(error);
dispatch(errorProductDescription(productId));
});
};
const getFavorites = (ignoreCache = false) => (dispatch, getState) => {
const data = getState().favorites.products;
if (!ignoreCache && !shouldFetchData(data)) {
return new Promise(res => res());
}
const timestamp = Date.now();
dispatch(requestFavorites());
const promise = new PipelineRequest('shopgate.user.getFavorites')
.setHandledErrors([EFAVORITE, EUNKNOWN, EBIGAPI])
.dispatch();
promise
.then((result) => {
dispatch(receiveFavorites(result.products, timestamp));
})
.catch((err) => {
logger.error(err);
dispatch(errorFetchFavorites(err));
});
return promise;
};
if (!forceFetch && !shouldFetchData(product)) {
if (product.productData) {
dispatch(receiveProductCached(product.productData));
}
return;
}
const requestParams = {
...configuration.get(DEFAULT_PRODUCTS_FETCH_PARAMS),
productId,
};
dispatch(requestProduct(productId, forceFetch));
new PipelineRequest(pipelines.SHOPGATE_CATALOG_GET_PRODUCT)
.setInput(requestParams)
.dispatch()
.then((result) => {
dispatch(receiveProduct(productId, result));
})
.catch((error) => {
logger.error(error);
dispatch(errorProduct(productId, error.code));
});
};
const fetchProductOptions = productId => (dispatch, getState) => {
const state = getState();
const cachedData = state.product.optionsByProductId[productId];
if (!shouldFetchData(cachedData)) {
return;
}
dispatch(requestProductOptions(productId));
new PipelineRequest(pipelines.SHOPGATE_CATALOG_GET_PRODUCT_OPTIONS)
.setInput({ productId })
.dispatch()
.then(result => dispatch(receiveProductOptions(productId, result.options)))
.catch((error) => {
logger.error(error);
dispatch(errorProductOptions(productId, error.code));
});
};
const fetchCategoryChildren = categoryId => (dispatch, getState) => {
const state = getState();
const category = state.category.childrenByCategoryId[categoryId];
if (!shouldFetchData(category, 'children')) {
return;
}
dispatch(requestCategoryChildren(categoryId));
new PipelineRequest(pipelines.SHOPGATE_CATALOG_GET_CATEGORY_CHILDREN)
.setInput({ categoryId })
.dispatch()
.then(result => dispatch(receiveCategoryChildren(categoryId, result.categories)))
.catch((error) => {
logger.error(error);
dispatch(errorCategoryChildren(categoryId));
});
};