feat: create a user config repository that holds the configuration object

This commit is contained in:
Jacob Overgaard
2024-07-08 17:05:05 +02:00
parent 7c3a9e63a5
commit bb8eb65cb8
9 changed files with 118 additions and 1 deletions

View File

@@ -0,0 +1,2 @@
export const UMB_USER_CONFIG_REPOSITORY_ALIAS = 'Umb.Repository.User.Config';
export const UMB_USER_CONFIG_STORE_ALIAS = 'Umb.Store.User.Config';

View File

@@ -0,0 +1,2 @@
export * from './constants.js';
export * from './user-config.repository.js';

View File

@@ -0,0 +1,18 @@
import { UMB_USER_CONFIG_REPOSITORY_ALIAS, UMB_USER_CONFIG_STORE_ALIAS } from './constants.js';
import type { ManifestRepository, ManifestStore, ManifestTypes } from '@umbraco-cms/backoffice/extension-registry';
const store: ManifestStore = {
type: 'store',
alias: UMB_USER_CONFIG_STORE_ALIAS,
name: 'User Config Store',
api: () => import('./user-config.store.js'),
};
const repository: ManifestRepository = {
type: 'repository',
alias: UMB_USER_CONFIG_REPOSITORY_ALIAS,
name: 'User Config Repository',
api: () => import('./user-config.repository.js'),
};
export const manifests: Array<ManifestTypes> = [repository, store];

View File

@@ -0,0 +1,52 @@
import type { UmbUserConfigurationModel } from '../../types.js';
import { UmbUserConfigServerDataSource } from './user-config.server.data-source.js';
import { UMB_USER_CONFIG_STORE_CONTEXT } from './user-config.store.token.js';
import { UmbRepositoryBase } from '@umbraco-cms/backoffice/repository';
import type { UmbApi } from '@umbraco-cms/backoffice/extension-api';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import type { Observable } from '@umbraco-cms/backoffice/observable-api';
export class UmbUserConfigRepository extends UmbRepositoryBase implements UmbApi {
#dataStore?: typeof UMB_USER_CONFIG_STORE_CONTEXT.TYPE;
#dataSource = new UmbUserConfigServerDataSource(this);
constructor(host: UmbControllerHost) {
super(host);
this.consumeContext(UMB_USER_CONFIG_STORE_CONTEXT, (store) => {
this.#dataStore = store;
this.#init();
});
}
async #init() {
const { data } = await this.#dataSource.getUserConfig();
if (data) {
this.#dataStore?.update(data);
}
}
/**
* Subscribe to the entire user configuration.
*/
all() {
if (!this.#dataStore) {
throw new Error('Data store not initialized');
}
return this.#dataStore.all();
}
/**
* Subscribe to a part of the user configuration.
*/
part<Part extends keyof UmbUserConfigurationModel>(part: Part): Observable<UmbUserConfigurationModel[Part]> {
if (!this.#dataStore) {
throw new Error('Data store not initialized');
}
return this.#dataStore.part(part);
}
}
export default UmbUserConfigRepository;

View File

@@ -0,0 +1,19 @@
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import { UserService } from '@umbraco-cms/backoffice/external/backend-api';
import { tryExecuteAndNotify } from '@umbraco-cms/backoffice/resources';
export class UmbUserConfigServerDataSource {
#host;
constructor(host: UmbControllerHost) {
this.#host = host;
}
/**
* Get the user configuration.
* @memberof UmbUserConfigServerDataSource
*/
getUserConfig() {
return tryExecuteAndNotify(this.#host, UserService.getUserConfiguration());
}
}

View File

@@ -0,0 +1,4 @@
import type { UmbUserConfigStore } from './user-config.store.js';
import { UmbContextToken } from '@umbraco-cms/backoffice/context-api';
export const UMB_USER_CONFIG_STORE_CONTEXT = new UmbContextToken<UmbUserConfigStore>('UmbUserConfigStore');

View File

@@ -0,0 +1,12 @@
import type { UmbUserConfigurationModel } from '../../types.js';
import { UMB_USER_CONFIG_STORE_CONTEXT } from './user-config.store.token.js';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import { UmbStoreObjectBase } from '@umbraco-cms/backoffice/store';
export class UmbUserConfigStore extends UmbStoreObjectBase<UmbUserConfigurationModel> {
constructor(host: UmbControllerHost) {
super(host, UMB_USER_CONFIG_STORE_CONTEXT.toString());
}
}
export default UmbUserConfigStore;

View File

@@ -1,5 +1,6 @@
import { manifests as avatarManifests } from './avatar/manifests.js';
import { manifests as changePasswordManifests } from './change-password/manifests.js';
import { manifests as configManifests } from './config/manifests.js';
import { manifests as detailManifests } from './detail/manifests.js';
import { manifests as disableManifests } from './disable/manifests.js';
import { manifests as enableManifests } from './enable/manifests.js';
@@ -12,6 +13,7 @@ export const manifests: Array<ManifestTypes> = [
...itemManifests,
...avatarManifests,
...changePasswordManifests,
...configManifests,
...disableManifests,
...enableManifests,
...unlockManifests,

View File

@@ -1,6 +1,10 @@
import type { UmbUserEntityType } from './entity.js';
import type { UmbReferenceByUnique } from '@umbraco-cms/backoffice/models';
import { UserStateModel, type UserTwoFactorProviderModel } from '@umbraco-cms/backoffice/external/backend-api';
import {
type UserConfigurationResponseModel,
UserStateModel,
type UserTwoFactorProviderModel,
} from '@umbraco-cms/backoffice/external/backend-api';
export type UmbUserStateEnum = UserStateModel;
export const UmbUserStateEnum = UserStateModel;
@@ -32,3 +36,5 @@ export interface UmbUserStartNodesModel {
}
export type UmbUserMfaProviderModel = UserTwoFactorProviderModel;
export type UmbUserConfigurationModel = UserConfigurationResponseModel;