Skip to content

Commit a369575

Browse files
committed
feat: create a user config repository that holds the configuration object
1 parent a829ba3 commit a369575

File tree

9 files changed

+118
-1
lines changed

9 files changed

+118
-1
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const UMB_USER_CONFIG_REPOSITORY_ALIAS = 'Umb.Repository.User.Config';
2+
export const UMB_USER_CONFIG_STORE_ALIAS = 'Umb.Store.User.Config';
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './constants.js';
2+
export * from './user-config.repository.js';
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { UMB_USER_CONFIG_REPOSITORY_ALIAS, UMB_USER_CONFIG_STORE_ALIAS } from './constants.js';
2+
import type { ManifestRepository, ManifestStore, ManifestTypes } from '@umbraco-cms/backoffice/extension-registry';
3+
4+
const store: ManifestStore = {
5+
type: 'store',
6+
alias: UMB_USER_CONFIG_STORE_ALIAS,
7+
name: 'User Config Store',
8+
api: () => import('./user-config.store.js'),
9+
};
10+
11+
const repository: ManifestRepository = {
12+
type: 'repository',
13+
alias: UMB_USER_CONFIG_REPOSITORY_ALIAS,
14+
name: 'User Config Repository',
15+
api: () => import('./user-config.repository.js'),
16+
};
17+
18+
export const manifests: Array<ManifestTypes> = [repository, store];
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import type { UmbUserConfigurationModel } from '../../types.js';
2+
import { UmbUserConfigServerDataSource } from './user-config.server.data-source.js';
3+
import { UMB_USER_CONFIG_STORE_CONTEXT } from './user-config.store.token.js';
4+
import { UmbRepositoryBase } from '@umbraco-cms/backoffice/repository';
5+
import type { UmbApi } from '@umbraco-cms/backoffice/extension-api';
6+
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
7+
import type { Observable } from '@umbraco-cms/backoffice/observable-api';
8+
9+
export class UmbUserConfigRepository extends UmbRepositoryBase implements UmbApi {
10+
#dataStore?: typeof UMB_USER_CONFIG_STORE_CONTEXT.TYPE;
11+
#dataSource = new UmbUserConfigServerDataSource(this);
12+
13+
constructor(host: UmbControllerHost) {
14+
super(host);
15+
this.consumeContext(UMB_USER_CONFIG_STORE_CONTEXT, (store) => {
16+
this.#dataStore = store;
17+
this.#init();
18+
});
19+
}
20+
21+
async #init() {
22+
const { data } = await this.#dataSource.getUserConfig();
23+
24+
if (data) {
25+
this.#dataStore?.update(data);
26+
}
27+
}
28+
29+
/**
30+
* Subscribe to the entire user configuration.
31+
*/
32+
all() {
33+
if (!this.#dataStore) {
34+
throw new Error('Data store not initialized');
35+
}
36+
37+
return this.#dataStore.all();
38+
}
39+
40+
/**
41+
* Subscribe to a part of the user configuration.
42+
*/
43+
part<Part extends keyof UmbUserConfigurationModel>(part: Part): Observable<UmbUserConfigurationModel[Part]> {
44+
if (!this.#dataStore) {
45+
throw new Error('Data store not initialized');
46+
}
47+
48+
return this.#dataStore.part(part);
49+
}
50+
}
51+
52+
export default UmbUserConfigRepository;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
2+
import { UserService } from '@umbraco-cms/backoffice/external/backend-api';
3+
import { tryExecuteAndNotify } from '@umbraco-cms/backoffice/resources';
4+
5+
export class UmbUserConfigServerDataSource {
6+
#host;
7+
8+
constructor(host: UmbControllerHost) {
9+
this.#host = host;
10+
}
11+
12+
/**
13+
* Get the user configuration.
14+
* @memberof UmbUserConfigServerDataSource
15+
*/
16+
getUserConfig() {
17+
return tryExecuteAndNotify(this.#host, UserService.getUserConfiguration());
18+
}
19+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import type { UmbUserConfigStore } from './user-config.store.js';
2+
import { UmbContextToken } from '@umbraco-cms/backoffice/context-api';
3+
4+
export const UMB_USER_CONFIG_STORE_CONTEXT = new UmbContextToken<UmbUserConfigStore>('UmbUserConfigStore');
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import type { UmbUserConfigurationModel } from '../../types.js';
2+
import { UMB_USER_CONFIG_STORE_CONTEXT } from './user-config.store.token.js';
3+
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
4+
import { UmbStoreObjectBase } from '@umbraco-cms/backoffice/store';
5+
6+
export class UmbUserConfigStore extends UmbStoreObjectBase<UmbUserConfigurationModel> {
7+
constructor(host: UmbControllerHost) {
8+
super(host, UMB_USER_CONFIG_STORE_CONTEXT.toString());
9+
}
10+
}
11+
12+
export default UmbUserConfigStore;

src/packages/user/user/repository/manifests.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { manifests as avatarManifests } from './avatar/manifests.js';
22
import { manifests as changePasswordManifests } from './change-password/manifests.js';
3+
import { manifests as configManifests } from './config/manifests.js';
34
import { manifests as detailManifests } from './detail/manifests.js';
45
import { manifests as disableManifests } from './disable/manifests.js';
56
import { manifests as enableManifests } from './enable/manifests.js';
@@ -12,6 +13,7 @@ export const manifests: Array<ManifestTypes> = [
1213
...itemManifests,
1314
...avatarManifests,
1415
...changePasswordManifests,
16+
...configManifests,
1517
...disableManifests,
1618
...enableManifests,
1719
...unlockManifests,

src/packages/user/user/types.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import type { UmbUserEntityType } from './entity.js';
22
import type { UmbReferenceByUnique } from '@umbraco-cms/backoffice/models';
3-
import { UserStateModel, type UserTwoFactorProviderModel } from '@umbraco-cms/backoffice/external/backend-api';
3+
import {
4+
type UserConfigurationResponseModel,
5+
UserStateModel,
6+
type UserTwoFactorProviderModel,
7+
} from '@umbraco-cms/backoffice/external/backend-api';
48

59
export type UmbUserStateEnum = UserStateModel;
610
export const UmbUserStateEnum = UserStateModel;
@@ -32,3 +36,5 @@ export interface UmbUserStartNodesModel {
3236
}
3337

3438
export type UmbUserMfaProviderModel = UserTwoFactorProviderModel;
39+
40+
export type UmbUserConfigurationModel = UserConfigurationResponseModel;

0 commit comments

Comments
 (0)