diff --git a/core-web/libs/portlets/edit-ema/portlet/src/lib/edit-ema-editor/components/edit-ema-palette/edit-ema-palette.component.spec.ts b/core-web/libs/portlets/edit-ema/portlet/src/lib/edit-ema-editor/components/edit-ema-palette/edit-ema-palette.component.spec.ts index 22b127afefa8..9ab30cbe9f50 100644 --- a/core-web/libs/portlets/edit-ema/portlet/src/lib/edit-ema-editor/components/edit-ema-palette/edit-ema-palette.component.spec.ts +++ b/core-web/libs/portlets/edit-ema/portlet/src/lib/edit-ema-editor/components/edit-ema-palette/edit-ema-palette.component.spec.ts @@ -237,6 +237,24 @@ describe('EditEmaPaletteComponent', () => { contenttypeName: 'TestNameContentType' }); }); + + it('should show contentlets from content type when a variant is present', () => { + spectator.setInput('variantId', 'cool-variant'); + spectator.detectChanges(); + + const storeSpy = jest.spyOn(store, 'loadContentlets'); + spectator.triggerEventHandler( + EditEmaPaletteContentTypeComponent, + 'showContentlets', + 'TestNameContentType' + ); + expect(storeSpy).toHaveBeenCalledWith({ + filter: '', + languageId: '1', + contenttypeName: 'TestNameContentType', + variantId: 'cool-variant' + }); + }); }); describe('Contentlets', () => { @@ -295,15 +313,23 @@ describe('EditEmaPaletteComponent', () => { }); }); - it('should show content types when component emits click on back button', () => { - const resetContentLetsSpy = jest.spyOn(store, 'resetContentlets'); - spectator.triggerEventHandler( - EditEmaPaletteContentletsComponent, - 'showContentTypes', - true - ); + it('should load contentlets on paginate when a variant is present', () => { + spectator.setInput('variantId', 'cool-variant'); + spectator.detectChanges(); - expect(resetContentLetsSpy).toHaveBeenCalled(); + const storeSpy = jest.spyOn(store, 'loadContentlets'); + spectator.triggerEventHandler(EditEmaPaletteContentletsComponent, 'paginate', { + contentTypeVarName: 'TestNameContentType', + page: 1 + }); + + expect(storeSpy).toHaveBeenCalledWith({ + filter: '', + languageId: '1', + contenttypeName: 'TestNameContentType', + page: 1, + variantId: 'cool-variant' + }); }); }); }); diff --git a/core-web/libs/portlets/edit-ema/portlet/src/lib/edit-ema-editor/components/edit-ema-palette/edit-ema-palette.component.ts b/core-web/libs/portlets/edit-ema/portlet/src/lib/edit-ema-editor/components/edit-ema-palette/edit-ema-palette.component.ts index 1bc81b0ee329..6b2b0d280a62 100644 --- a/core-web/libs/portlets/edit-ema/portlet/src/lib/edit-ema-editor/components/edit-ema-palette/edit-ema-palette.component.ts +++ b/core-web/libs/portlets/edit-ema/portlet/src/lib/edit-ema-editor/components/edit-ema-palette/edit-ema-palette.component.ts @@ -33,6 +33,7 @@ import { DotPaletteStore, PALETTE_TYPES } from './store/edit-ema-palette.store'; }) export class EditEmaPaletteComponent implements OnInit, OnDestroy { @Input() languageId: number; + @Input() variantId: string; @Input() containers: DotPageContainerStructure; private readonly store = inject(DotPaletteStore); @@ -55,7 +56,8 @@ export class EditEmaPaletteComponent implements OnInit, OnDestroy { this.store.loadContentlets({ filter: '', languageId: this.languageId.toString(), - contenttypeName: contentTypeName + contenttypeName: contentTypeName, + variantId: this.variantId }); } @@ -77,7 +79,8 @@ export class EditEmaPaletteComponent implements OnInit, OnDestroy { filter: '', languageId: this.languageId.toString(), contenttypeName: contentTypeVarName, - page: page + page: page, + variantId: this.variantId }); } @@ -92,7 +95,8 @@ export class EditEmaPaletteComponent implements OnInit, OnDestroy { this.store.loadContentlets({ filter, contenttypeName: currentContentType, - languageId: this.languageId.toString() + languageId: this.languageId.toString(), + variantId: this.variantId }); } diff --git a/core-web/libs/portlets/edit-ema/portlet/src/lib/edit-ema-editor/components/edit-ema-palette/store/edit-ema-palette.store.spec.ts b/core-web/libs/portlets/edit-ema/portlet/src/lib/edit-ema-editor/components/edit-ema-palette/store/edit-ema-palette.store.spec.ts index e8084e0fb088..bfbb9a45e479 100644 --- a/core-web/libs/portlets/edit-ema/portlet/src/lib/edit-ema-editor/components/edit-ema-palette/store/edit-ema-palette.store.spec.ts +++ b/core-web/libs/portlets/edit-ema/portlet/src/lib/edit-ema-editor/components/edit-ema-palette/store/edit-ema-palette.store.spec.ts @@ -1,3 +1,4 @@ +import { describe, it, expect } from '@jest/globals'; import { SpectatorService } from '@ngneat/spectator'; import { createServiceFactory } from '@ngneat/spectator/jest'; import { of } from 'rxjs'; @@ -147,7 +148,52 @@ describe('EditEmaPaletteStore', () => { lang: '1', filter: '', offset: '0', - query: '+contentType: TestContentType1 +deleted: false' + query: '+contentType:TestContentType1 +deleted:false +variant:DEFAULT' + }); + expect(patchStateSpy).toHaveBeenCalled(); + expect(state).toEqual({ + contentlets: { + filter: { + query: '', + contentTypeVarName: 'TestContentType1' + }, + items: [], + totalRecords: 0, + itemsPerPage: PALETTE_PAGINATOR_ITEMS_PER_PAGE + }, + contenttypes: { + filter: '', + items: [] + }, + status: EditEmaPaletteStoreStatus.LOADED, + currentContentType: 'TestContentType1', + currentPaletteType: PALETTE_TYPES.CONTENTLET, + allowedTypes: [] + }); + done(); + }); + }); + + it('should load contentlets with variant', (done) => { + const contentServiceSpy = jest.spyOn(spectator.inject(DotESContentService), 'get'); + const patchStateSpy = jest.spyOn(spectator.service, 'patchState'); + + contentServiceSpy.mockClear(); // clear the spy because it had the register of the last call + + spectator.service.loadContentlets({ + filter: '', + contenttypeName: 'TestContentType1', + languageId: '1', + variantId: 'cool-variant' + }); + + spectator.service.vm$.subscribe((state) => { + expect(contentServiceSpy).toHaveBeenCalledWith({ + itemsPerPage: PALETTE_PAGINATOR_ITEMS_PER_PAGE, + lang: '1', + filter: '', + offset: '0', + query: '+contentType:TestContentType1 +deleted:false +variant:(DEFAULT OR cool-variant)' }); expect(patchStateSpy).toHaveBeenCalled(); expect(state).toEqual({ diff --git a/core-web/libs/portlets/edit-ema/portlet/src/lib/edit-ema-editor/components/edit-ema-palette/store/edit-ema-palette.store.ts b/core-web/libs/portlets/edit-ema/portlet/src/lib/edit-ema-editor/components/edit-ema-palette/store/edit-ema-palette.store.ts index 1a3a6cc935ad..4d6232327cf6 100644 --- a/core-web/libs/portlets/edit-ema/portlet/src/lib/edit-ema-editor/components/edit-ema-palette/store/edit-ema-palette.store.ts +++ b/core-web/libs/portlets/edit-ema/portlet/src/lib/edit-ema-editor/components/edit-ema-palette/store/edit-ema-palette.store.ts @@ -11,6 +11,7 @@ import { DotPropertiesService } from '@dotcms/data-access'; import { + DEFAULT_VARIANT_ID, DotCMSContentlet, DotCMSContentType, DotContainerStructure, @@ -143,20 +144,25 @@ export class DotPaletteStore extends ComponentStore { contenttypeName: string; languageId: string; page?: number; + variantId?: string; }> ) => { return data$.pipe( tap(() => this.setStatus(EditEmaPaletteStoreStatus.LOADING)), - switchMap(({ filter, contenttypeName, languageId, page = 0 }) => { + switchMap(({ filter, contenttypeName, languageId, variantId, page = 0 }) => { this.setCurrentContentType(contenttypeName); + const variantTerm = variantId + ? `+variant:(${DEFAULT_VARIANT_ID} OR ${variantId})` + : `+variant:${DEFAULT_VARIANT_ID}`; + return this.dotESContentService .get({ itemsPerPage: PALETTE_PAGINATOR_ITEMS_PER_PAGE, lang: languageId || '1', filter: filter || '', offset: (page * PALETTE_PAGINATOR_ITEMS_PER_PAGE).toString(), - query: `+contentType: ${contenttypeName} +deleted: false`.trim() + query: `+contentType:${contenttypeName} +deleted:false ${variantTerm}`.trim() }) .pipe( tapResponse( diff --git a/core-web/libs/portlets/edit-ema/portlet/src/lib/edit-ema-editor/edit-ema-editor.component.html b/core-web/libs/portlets/edit-ema/portlet/src/lib/edit-ema-editor/edit-ema-editor.component.html index 42d872c62fbe..9f61a15f203d 100644 --- a/core-web/libs/portlets/edit-ema/portlet/src/lib/edit-ema-editor/edit-ema-editor.component.html +++ b/core-web/libs/portlets/edit-ema/portlet/src/lib/edit-ema-editor/edit-ema-editor.component.html @@ -77,6 +77,7 @@ *ngIf="es.showPalette" [languageId]="es.editor.viewAs.language.id" [containers]="es.editor.containers" + [variantId]="es.editorData.variantId" data-testId="palette" /> @if(es.editorData.canEditVariant && (es.editorData.mode === editorMode.EDIT ||