Skip to content

Commit

Permalink
#25901 Unable to reuse content inside the variants (#26450)
Browse files Browse the repository at this point in the history
* refresh the content palette when the languge change

* make a couple of less state updates

* clean up
  • Loading branch information
hmoreras authored Oct 20, 2023
1 parent 4b7b189 commit 423db1f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ const storeMock = jasmine.createSpyObj(
'filterContentlets',
'filterContentTypes',
'loadContentlets',
'switchView'
'switchView',
'switchLanguage'
],
{
vm$: of({
Expand Down Expand Up @@ -234,4 +235,13 @@ describe('DotPaletteComponent', () => {

expect(store.setAllowedContent).toHaveBeenCalledWith(allowedContent);
});

it('should switch language', async () => {
comp.languageId = '2';

fixture.detectChanges();
await fixture.whenStable();

expect(store.switchLanguage).toHaveBeenCalledWith('2');
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Observable } from 'rxjs';

import { animate, AnimationEvent, state, style, transition, trigger } from '@angular/animations';
import { Component, Input, OnInit, ViewChild } from '@angular/core';
import { Component, Input, ViewChild } from '@angular/core';

import { LazyLoadEvent } from 'primeng/api';

Expand Down Expand Up @@ -32,23 +32,20 @@ import { DotPaletteState, DotPaletteStore } from './store/dot-palette.store';
])
]
})
export class DotPaletteComponent implements OnInit {
export class DotPaletteComponent {
@Input() set allowedContent(items: string[]) {
this.store.setAllowedContent(items);
}
@Input() languageId: string;
@Input() set languageId(languageId: string) {
this.store.switchLanguage(languageId);
}
vm$: Observable<DotPaletteState> = this.store.vm$;

@ViewChild('contentlets') contentlets: DotPaletteContentletsComponent;
@ViewChild('contentTypes') contentTypes: DotPaletteContentTypeComponent;

constructor(private store: DotPaletteStore) {}

ngOnInit(): void {
this.store.setLanguageId(this.languageId);
this.store.getContenttypesData();
}

/**
* Sets value on store to show/hide components on the UI
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,11 @@ describe('DotPaletteStore', () => {
});

it('should update languageId', () => {
dotPaletteStore.setLanguageId('1');
dotPaletteStore.setLanguage('4');
dotPaletteStore.state$.subscribe((data) => {
expect(data.languageId).toEqual('1');
expect(data.languageId).toEqual('4');
expect(data.filter).toEqual('');
expect(data.viewContentlet).toEqual('contentlet:out');
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ export interface DotPaletteState {
loading: boolean;
}

const CONTENTLET_VIEW_IN = 'contentlet:in';
const CONTENTLET_VIEW_OUT = 'contentlet:out';

@Injectable()
export class DotPaletteStore extends ComponentStore<DotPaletteState> {
readonly vm$ = this.state$;
Expand All @@ -40,8 +43,8 @@ export class DotPaletteStore extends ComponentStore<DotPaletteState> {
return { ...state, filter: data };
});

readonly setLanguageId = this.updater((state: DotPaletteState, data: string) => {
return { ...state, languageId: data };
readonly setLanguage = this.updater((state: DotPaletteState, languageId: string) => {
return { ...state, languageId, viewContentlet: CONTENTLET_VIEW_OUT, filter: '' };
});

readonly setViewContentlet = this.updater((state: DotPaletteState, data: string) => {
Expand All @@ -61,6 +64,7 @@ export class DotPaletteStore extends ComponentStore<DotPaletteState> {
loading: !(ComponentStatus.LOADED === ComponentStatus.LOADED)
};
});

readonly setAllowedContent = this.updater((state: DotPaletteState, data: string[]) => {
return { ...state, allowedContent: data };
});
Expand Down Expand Up @@ -168,11 +172,22 @@ export class DotPaletteStore extends ComponentStore<DotPaletteState> {
filter: '',
languageId: '1',
totalRecords: 0,
viewContentlet: 'contentlet:out',
viewContentlet: CONTENTLET_VIEW_OUT,
loading: false
});
}

/**
* Switch language and request Content Types data.
* @param languageId
*
* @memberof DotPaletteStore
*/
switchLanguage(languageId: string): void {
this.setLanguage(languageId);
this.getContenttypesData();
}

/**
* Request contentlets data with filter and pagination params.
*
Expand Down Expand Up @@ -289,7 +304,7 @@ export class DotPaletteStore extends ComponentStore<DotPaletteState> {
* @memberof DotPaletteContentletsComponent
*/
switchView(variableName?: string): void {
const viewContentlet = variableName ? 'contentlet:in' : 'contentlet:out';
const viewContentlet = variableName ? CONTENTLET_VIEW_IN : CONTENTLET_VIEW_OUT;
this.setViewContentlet(viewContentlet);
this.setFilter('');
this.loadContentlets(variableName);
Expand Down

0 comments on commit 423db1f

Please sign in to comment.