implement UmbEntityDetailWorkspaceContextBase for member group
This commit is contained in:
@@ -1,38 +1,40 @@
|
||||
import { UmbMemberGroupDetailRepository } from '../../repository/index.js';
|
||||
import { UMB_MEMBER_GROUP_DETAIL_REPOSITORY_ALIAS, UmbMemberGroupDetailRepository } from '../../repository/index.js';
|
||||
import type { UmbMemberGroupDetailModel } from '../../types.js';
|
||||
import { UMB_MEMBER_GROUP_WORKSPACE_ALIAS } from './manifests.js';
|
||||
import { UmbMemberGroupWorkspaceEditorElement } from './member-group-workspace-editor.element.js';
|
||||
import {
|
||||
type UmbSubmittableWorkspaceContext,
|
||||
UmbSubmittableWorkspaceContextBase,
|
||||
UmbWorkspaceIsNewRedirectController,
|
||||
type UmbRoutableWorkspaceContext,
|
||||
UmbEntityDetailWorkspaceContextBase,
|
||||
} from '@umbraco-cms/backoffice/workspace';
|
||||
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
|
||||
import { UmbObjectState } from '@umbraco-cms/backoffice/observable-api';
|
||||
import { UMB_MEMBER_GROUP_ENTITY_TYPE } from '../../entity.js';
|
||||
import { UMB_USER_GROUP_ROOT_ENTITY_TYPE } from '@umbraco-cms/backoffice/user-group';
|
||||
|
||||
export class UmbMemberGroupWorkspaceContext
|
||||
extends UmbSubmittableWorkspaceContextBase<UmbMemberGroupDetailModel>
|
||||
extends UmbEntityDetailWorkspaceContextBase<UmbMemberGroupDetailModel>
|
||||
implements UmbSubmittableWorkspaceContext, UmbRoutableWorkspaceContext
|
||||
{
|
||||
public readonly repository = new UmbMemberGroupDetailRepository(this);
|
||||
#getDataPromise?: Promise<any>;
|
||||
readonly data = this._data.current;
|
||||
|
||||
#data = new UmbObjectState<UmbMemberGroupDetailModel | undefined>(undefined);
|
||||
readonly data = this.#data.asObservable();
|
||||
|
||||
readonly unique = this.#data.asObservablePart((data) => data?.unique);
|
||||
readonly name = this.#data.asObservablePart((data) => data?.name);
|
||||
readonly unique = this._data.createObservablePartOfCurrent((data) => data?.unique);
|
||||
readonly name = this._data.createObservablePartOfCurrent((data) => data?.name);
|
||||
|
||||
constructor(host: UmbControllerHost) {
|
||||
super(host, UMB_MEMBER_GROUP_WORKSPACE_ALIAS);
|
||||
super(host, {
|
||||
workspaceAlias: UMB_MEMBER_GROUP_WORKSPACE_ALIAS,
|
||||
entityType: UMB_MEMBER_GROUP_ENTITY_TYPE,
|
||||
detailRepositoryAlias: UMB_MEMBER_GROUP_DETAIL_REPOSITORY_ALIAS,
|
||||
});
|
||||
|
||||
this.routes.setRoutes([
|
||||
{
|
||||
path: 'create',
|
||||
component: UmbMemberGroupWorkspaceEditorElement,
|
||||
setup: () => {
|
||||
this.create();
|
||||
this.create({ parent: { entityType: UMB_USER_GROUP_ROOT_ENTITY_TYPE, unique: null } });
|
||||
|
||||
new UmbWorkspaceIsNewRedirectController(
|
||||
this,
|
||||
@@ -52,31 +54,16 @@ export class UmbMemberGroupWorkspaceContext
|
||||
]);
|
||||
}
|
||||
|
||||
public isLoaded() {
|
||||
return this.#getDataPromise;
|
||||
}
|
||||
|
||||
protected override resetState(): void {
|
||||
super.resetState();
|
||||
this.#data.setValue(undefined);
|
||||
}
|
||||
|
||||
async load(unique: string) {
|
||||
this.resetState();
|
||||
this.#getDataPromise = this.repository.requestByUnique(unique);
|
||||
type GetDataType = Awaited<ReturnType<UmbMemberGroupDetailRepository['requestByUnique']>>;
|
||||
const { data, asObservable } = (await this.#getDataPromise) as GetDataType;
|
||||
|
||||
if (data) {
|
||||
this.setIsNew(false);
|
||||
this.#data.update(data);
|
||||
}
|
||||
override async load(unique: string) {
|
||||
const response = await super.load(unique);
|
||||
|
||||
this.observe(
|
||||
asObservable(),
|
||||
response.asObservable?.(),
|
||||
(memberGroup) => this.#onMemberGroupStoreChange(memberGroup),
|
||||
'umbMemberGroupStoreObserver',
|
||||
);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
#onMemberGroupStoreChange(memberGroup: UmbMemberGroupDetailModel | undefined) {
|
||||
@@ -85,60 +72,12 @@ export class UmbMemberGroupWorkspaceContext
|
||||
}
|
||||
}
|
||||
|
||||
async create() {
|
||||
this.resetState();
|
||||
this.#getDataPromise = this.repository.createScaffold();
|
||||
const { data } = await this.#getDataPromise;
|
||||
|
||||
if (data) {
|
||||
this.setIsNew(true);
|
||||
this.#data.setValue(data);
|
||||
}
|
||||
|
||||
return { data };
|
||||
}
|
||||
|
||||
async submit() {
|
||||
const data = this.getData();
|
||||
if (!data) throw new Error('No data to save');
|
||||
|
||||
if (this.getIsNew()) {
|
||||
const { error } = await this.repository.create(data);
|
||||
if (error) {
|
||||
throw new Error(error.message);
|
||||
}
|
||||
this.setIsNew(false);
|
||||
} else {
|
||||
const { error } = await this.repository.save(data);
|
||||
if (error) {
|
||||
throw new Error(error.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getData() {
|
||||
return this.#data.getValue();
|
||||
}
|
||||
|
||||
getUnique() {
|
||||
return this.getData()?.unique;
|
||||
}
|
||||
|
||||
getEntityType() {
|
||||
return 'member-group';
|
||||
}
|
||||
|
||||
getName() {
|
||||
return this.#data.getValue()?.name;
|
||||
return this._data.getCurrent()?.name;
|
||||
}
|
||||
|
||||
setName(name: string | undefined) {
|
||||
this.#data.update({ name });
|
||||
}
|
||||
|
||||
public override destroy(): void {
|
||||
this.#data.destroy();
|
||||
super.destroy();
|
||||
this._data.updateCurrent({ name });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user