Skip to content

Commit

Permalink
feat: show header and footer in Angular exampple with read-only token…
Browse files Browse the repository at this point in the history
… in edit mode
  • Loading branch information
rjvelazco committed Oct 31, 2024
1 parent c0a01e6 commit 9583467
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions examples/angular/src/app/pages/pages.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ import { HeaderComponent } from './components/header/header.component';
import { NavigationComponent } from './components/navigation/navigation.component';
import { FooterComponent } from './components/footer/footer.component';
import { PageService } from './services/page.service';
import { CUSTOMER_ACTIONS, postMessageToEditor } from '@dotcms/client';
import {
CUSTOMER_ACTIONS,
isInsideEditor,
postMessageToEditor,
} from '@dotcms/client';
import { DYNAMIC_COMPONENTS } from './components';
import { DOTCMS_CLIENT_TOKEN } from '../app.config';

export type PageError = {
message: string;
Expand Down Expand Up @@ -58,19 +63,26 @@ export class DotCMSPagesComponent implements OnInit {
status: 'idle',
});
protected readonly components = signal<any>(DYNAMIC_COMPONENTS);
protected readonly client = inject(DOTCMS_CLIENT_TOKEN);

// This should be PageApiOptions from @dotcms/client
protected readonly editorCofig: any = { params: { depth: 2 } };

ngOnInit() {
if (isInsideEditor()) {
this.#listenToEditorChanges();
}

this.#router.events
.pipe(
filter(
(event): event is NavigationEnd => event instanceof NavigationEnd
),
startWith(null), // Trigger initial load
tap(() => this.#setLoading()),
switchMap(() => this.#pageService.getPageAndNavigation(this.#route, this.editorCofig)),
switchMap(() =>
this.#pageService.getPageAndNavigation(this.#route, this.editorCofig)
),
takeUntilDestroyed(this.#destroyRef)
)
.subscribe(({ page = {}, nav, error }) => {
Expand All @@ -90,10 +102,7 @@ export class DotCMSPagesComponent implements OnInit {
});
}

#setPageContent(
page: DotCMSPageAsset,
nav: DotcmsNavigationItem | null
) {
#setPageContent(page: DotCMSPageAsset, nav: DotcmsNavigationItem | null) {
this.$context.set({
status: 'success',
page,
Expand Down Expand Up @@ -124,4 +133,18 @@ export class DotCMSPagesComponent implements OnInit {
*/
postMessageToEditor({ action: CUSTOMER_ACTIONS.CLIENT_READY, payload: {} });
}

#listenToEditorChanges() {
this.client.editor.on('changes', (page) => {
if (!page) {
return;
}

this.$context.update((state) => ({
...state,
status: 'loading',
error: null,
}));
});
}
}

0 comments on commit 9583467

Please sign in to comment.