Skip to content

Commit

Permalink
Merge branch 'next-36797/can-show-product-preview-when-assign-dynamic…
Browse files Browse the repository at this point in the history
…-product-group-to-category' into 'trunk'

NEXT-36797 - Use product stream preview API to load products when assigning a dynamic product group to a category

See merge request shopware/6/product/platform!14985
  • Loading branch information
thuylt committed Oct 15, 2024
2 parents d321dd9 + 87367dd commit 55b0191
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: Using product stream preview API to load products when assigning a dynamic product group to a category
issue: NEXT-36797
---
# Administration
* Changed `loadProducts` method in `sw-product-stream-grid-preview` component to use the product stream preview API to load products.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import template from './sw-product-stream-grid-preview.html.twig';
import './sw-product-stream-grid-preview.scss';

const { Component, Context } = Shopware;
const { Component, Context, Defaults } = Shopware;
const { Criteria } = Shopware.Data;

/**
Expand All @@ -12,7 +12,10 @@ Component.register('sw-product-stream-grid-preview', {

compatConfig: Shopware.compatConfig,

inject: ['repositoryFactory'],
inject: [
'repositoryFactory',
'productStreamPreviewService',
],

emits: ['selection-change'],

Expand Down Expand Up @@ -66,6 +69,20 @@ Component.register('sw-product-stream-grid-preview', {
return this.repositoryFactory.create('currency');
},

salesChannelRepository() {
return this.repositoryFactory.create('sales_channel');
},

salesChannelCriteria() {
return new Criteria(1, 1)
.addFilter(
Criteria.not('OR', [
Criteria.equals('typeId', Defaults.productComparisonTypeId),
]),
)
.addSorting(Criteria.sort('type.iconName', 'ASC'));
},

defaultColumns() {
return [
{
Expand Down Expand Up @@ -185,15 +202,17 @@ Component.register('sw-product-stream-grid-preview', {
]),
);

return this.productRepository
.search(this.criteria, {
...Context.api,
inheritance: true,
return this.salesChannelRepository
.searchIds(this.salesChannelCriteria)
.then(({ data }) => {
return this.productStreamPreviewService.preview(data.at(0), this.criteria, [], {
'sw-currency-id': Context.app.systemCurrencyId,
'sw-inheritance': true,
});
})
.then((products) => {
this.products = products;
this.total = products.total;

.then((result) => {
this.products = Object.values(result.elements);
this.total = result.total;
this.isLoading = false;
});
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@ const mockProducts = [
},
];

mockProducts.total = 4;
mockProducts.criteria = {
page: 1,
limit: 25,
};

const mockCurrency = {
id: 'uuid1337',
name: 'Euro',
Expand Down Expand Up @@ -90,9 +84,23 @@ const createWrapper = async () => {
create: () => ({
get: () => Promise.resolve(mockCurrency),
search: () => Promise.resolve(mockProducts),
searchIds: () =>
Promise.resolve({
total: 1,
data: ['b3faca3079184aca8113494690736d76'],
}),
}),
},
validationService: {},
productStreamPreviewService: {
preview: () =>
Promise.resolve({
elements: mockProducts,
total: 4,
page: 1,
limit: 10,
}),
},
},
attachTo: document.body,
},
Expand Down Expand Up @@ -120,7 +128,7 @@ describe('components/entity/sw-product-stream-grid-preview.spec', () => {
});

it('should load products with correct criteria when filters are being set', async () => {
const spyLoadProducts = jest.spyOn(wrapper.vm, 'loadProducts');
const spyPreviewProduct = jest.spyOn(wrapper.vm.productStreamPreviewService, 'preview');

await wrapper.setProps({
filters: mockFilter,
Expand All @@ -140,9 +148,7 @@ describe('components/entity/sw-product-stream-grid-preview.spec', () => {

await wrapper.vm.$nextTick();

expect(spyLoadProducts).toHaveBeenCalledTimes(1);
expect(wrapper.vm.products).toStrictEqual(mockProducts);
expect(wrapper.vm.total).toBe(mockProducts.total);
expect(spyPreviewProduct).toHaveBeenCalledTimes(1);
expect(wrapper.vm.systemCurrency).toStrictEqual(mockCurrency);
expect(wrapper.vm.filters).toStrictEqual(mockFilter);
expect(wrapper.vm.criteria.filters).toEqual([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export default {
createdComponent() {
this.initElementConfig('product-listing');

if (Shopware.Utils.types.isEmpty(this.productSortingsConfigValue)) {
if (Object.keys(this.productSortingsConfigValue).length === 0) {
this.productSortings = new EntityCollection(
this.productSortingRepository.route,
this.productSortingRepository.schema.entity,
Expand Down

0 comments on commit 55b0191

Please sign in to comment.