add global context to load in the document configuration

This commit is contained in:
Jacob Overgaard
2024-03-15 11:09:12 +01:00
parent f33d0b120d
commit d083028543
5 changed files with 60 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
import { UmbControllerBase } from '@umbraco-cms/backoffice/class-api';
import { UmbContextToken } from '@umbraco-cms/backoffice/context-api';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import type { UmbApi } from '@umbraco-cms/backoffice/extension-api';
import {
DocumentResource,
type DocumentConfigurationResponseModel,
} from '@umbraco-cms/backoffice/external/backend-api';
import { tryExecuteAndNotify } from '@umbraco-cms/backoffice/resources';
/**
* A context for fetching and caching the document configuration.
*/
export class UmbDocumentConfigurationContext extends UmbControllerBase implements UmbApi {
static DocumentConfiguration: Promise<DocumentConfigurationResponseModel | null>;
constructor(host: UmbControllerHost) {
super(host);
this.provideContext(UMB_DOCUMENT_CONFIGURATION_CONTEXT, this);
}
/**
* Get the document configuration from the server, or return the cached configuration if it has already been fetched.
* @returns A promise that resolves to the document configuration, or null if the configuration could not be fetched.
*/
getDocumentConfiguration(): Promise<DocumentConfigurationResponseModel | null> {
debugger;
return (UmbDocumentConfigurationContext.DocumentConfiguration ??= this.fetchDocumentConfiguration());
}
/**
* Fetch the document configuration from the server.
* @returns A promise that resolves to the document configuration, or null if the configuration could not be fetched.
*/
async fetchDocumentConfiguration() {
const { data } = await tryExecuteAndNotify(this, DocumentResource.getDocumentConfiguration());
return data ?? null;
}
}
export default UmbDocumentConfigurationContext;
export const UMB_DOCUMENT_CONFIGURATION_CONTEXT = new UmbContextToken<UmbDocumentConfigurationContext>(
'UmbDocumentConfigurationContext',
);

View File

@@ -0,0 +1 @@
export * from './document-configuration.context.js';

View File

@@ -0,0 +1,10 @@
import type { ManifestGlobalContext } from '@umbraco-cms/backoffice/extension-registry';
export const manifests: Array<ManifestGlobalContext> = [
{
type: 'globalContext',
alias: 'Umb.GlobalContext.DocumentConfiguration',
name: 'Document Configuration Context',
js: () => import('./document-configuration.context.js'),
},
];

View File

@@ -9,6 +9,7 @@ export * from './components/index.js';
export * from './entity.js';
export * from './entity-actions/index.js';
export * from './modals/index.js';
export * from './global-contexts/index.js';
export * from './tree/index.js';
export { UMB_CONTENT_MENU_ALIAS } from './menu.manifests.js';

View File

@@ -11,6 +11,7 @@ import { manifests as modalManifests } from './modals/manifests.js';
import { manifests as treeManifests } from './tree/manifests.js';
import { manifests as userPermissionManifests } from './user-permissions/manifests.js';
import { manifests as workspaceManifests } from './workspace/manifests.js';
import { manifests as globalContextManifests } from './global-contexts/manifests.js';
export const manifests = [
...breadcrumbManifests,
@@ -26,4 +27,5 @@ export const manifests = [
...treeManifests,
...userPermissionManifests,
...workspaceManifests,
...globalContextManifests,
];