Skip to content

Commit

Permalink
SDK: Unable to Edit Unpublished Page in Angular Example (#30542)
Browse files Browse the repository at this point in the history
This pull request includes several updates to the `DotCMSPagesComponent`
in the Angular example application. The changes primarily focus on
integrating editor-related functionality and improving code formatting.

### Editor Integration:

* Added `DOTCMS_CLIENT_TOKEN` import and injected it into the
`DotCMSPagesComponent` to enable editor-related functionalities.
[[1]](diffhunk://#diff-db19dde961c6af3dbddd86db7544e4ea4ff42baae9095cee4e3017c1d4dcb395L20-R26)
[[2]](diffhunk://#diff-db19dde961c6af3dbddd86db7544e4ea4ff42baae9095cee4e3017c1d4dcb395R66-R85)
* Implemented a new method `#listenToEditorChanges` to handle editor
changes and update the component state accordingly.

### Code Formatting:

* Reformatted the import statements for better readability and
maintainability.
* Adjusted the method signature formatting for `#setPageContent` to be
consistent with the rest of the codebase.

### Video


https://github.com/user-attachments/assets/7ee487ce-412f-4504-8ff1-f92dac86beee

---------

Co-authored-by: Jalinson Diaz <[email protected]>
  • Loading branch information
2 people authored and dsolistorres committed Nov 5, 2024
1 parent d3b8f06 commit 74e0c68
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
34 changes: 28 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 {
CLIENT_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 @@ -51,6 +56,8 @@ export class DotCMSPagesComponent implements OnInit {
readonly #destroyRef = inject(DestroyRef);
readonly #router = inject(Router);
readonly #pageService = inject(PageService);
readonly #client = inject(DOTCMS_CLIENT_TOKEN);

protected readonly $context = signal<PageRender>({
page: null,
nav: null,
Expand All @@ -63,6 +70,10 @@ export class DotCMSPagesComponent implements OnInit {
protected readonly editorCofig: any = { params: { depth: 2 } };

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

this.#router.events
.pipe(
filter(
Expand Down Expand Up @@ -90,10 +101,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 @@ -122,6 +130,20 @@ export class DotCMSPagesComponent implements OnInit {
* This is a temporary workaround to avoid the editor to be stuck in the loading state.
* This will be removed once the editor is able to detect when the client is ready without use DotcmsLayoutComponent.
*/
postMessageToEditor({ action: CUSTOMER_ACTIONS.CLIENT_READY, payload: {} });
postMessageToEditor({ action: CLIENT_ACTIONS.CLIENT_READY, payload: {} });
}

#listenToEditorChanges() {
this.#client.editor.on('changes', (page) => {
if (!page) {
return;
}
this.$context.update((state) => ({
...state,
page: page as DotCMSPageAsset,
status: 'success',
error: null,
}));
});
}
}
4 changes: 2 additions & 2 deletions examples/angular/src/app/pages/services/page.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ export class PageService {
route: ActivatedRoute,
config: any
): Observable<PageAndNavResponse> {
const queryParams = route.snapshot.queryParamMap;
const params = route.snapshot.queryParams;
const url = route.snapshot.url.map((segment) => segment.path).join('/');
const path = url || '/';

const pageParams = getPageRequestParams({
path,
params: queryParams,
params
});

return from(this.client.page.get({ ...pageParams, ...config.params })).pipe(
Expand Down

0 comments on commit 74e0c68

Please sign in to comment.