diff --git a/changelog/_unreleased/2024-10-10-using-product-stream-preview-api-to-load-products-when-assigning-dynamic-product-group-to-category.md b/changelog/_unreleased/2024-10-10-using-product-stream-preview-api-to-load-products-when-assigning-dynamic-product-group-to-category.md new file mode 100644 index 00000000000..db1a126fcf0 --- /dev/null +++ b/changelog/_unreleased/2024-10-10-using-product-stream-preview-api-to-load-products-when-assigning-dynamic-product-group-to-category.md @@ -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. diff --git a/src/Administration/Resources/app/administration/src/app/component/entity/sw-product-stream-grid-preview/index.js b/src/Administration/Resources/app/administration/src/app/component/entity/sw-product-stream-grid-preview/index.js index 8dd71e7fa5c..d00f95a7bf6 100644 --- a/src/Administration/Resources/app/administration/src/app/component/entity/sw-product-stream-grid-preview/index.js +++ b/src/Administration/Resources/app/administration/src/app/component/entity/sw-product-stream-grid-preview/index.js @@ -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; /** @@ -12,7 +12,10 @@ Component.register('sw-product-stream-grid-preview', { compatConfig: Shopware.compatConfig, - inject: ['repositoryFactory'], + inject: [ + 'repositoryFactory', + 'productStreamPreviewService', + ], emits: ['selection-change'], @@ -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 [ { @@ -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; }); }, diff --git a/src/Administration/Resources/app/administration/src/app/component/entity/sw-product-stream-grid-preview/sw-product-stream-grid-preview.spec.js b/src/Administration/Resources/app/administration/src/app/component/entity/sw-product-stream-grid-preview/sw-product-stream-grid-preview.spec.js index 9f20222d835..2a67a79dd49 100644 --- a/src/Administration/Resources/app/administration/src/app/component/entity/sw-product-stream-grid-preview/sw-product-stream-grid-preview.spec.js +++ b/src/Administration/Resources/app/administration/src/app/component/entity/sw-product-stream-grid-preview/sw-product-stream-grid-preview.spec.js @@ -30,12 +30,6 @@ const mockProducts = [ }, ]; -mockProducts.total = 4; -mockProducts.criteria = { - page: 1, - limit: 25, -}; - const mockCurrency = { id: 'uuid1337', name: 'Euro', @@ -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, }, @@ -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, @@ -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([ diff --git a/src/Administration/Resources/app/administration/src/module/sw-cms/elements/product-listing/config/index.js b/src/Administration/Resources/app/administration/src/module/sw-cms/elements/product-listing/config/index.js index 10de18b36eb..bf1740b9d91 100644 --- a/src/Administration/Resources/app/administration/src/module/sw-cms/elements/product-listing/config/index.js +++ b/src/Administration/Resources/app/administration/src/module/sw-cms/elements/product-listing/config/index.js @@ -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,