Skip to content

Commit 2386e4c

Browse files
committed
use entity detail workspace base
1 parent 54bcb35 commit 2386e4c

File tree

1 file changed

+18
-80
lines changed

1 file changed

+18
-80
lines changed

src/packages/language/workspace/language/language-workspace.context.ts

Lines changed: 18 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,40 @@
1-
import { UmbLanguageDetailRepository } from '../../repository/index.js';
1+
import { UMB_LANGUAGE_DETAIL_REPOSITORY_ALIAS, UmbLanguageDetailRepository } from '../../repository/index.js';
22
import type { UmbLanguageDetailModel } from '../../types.js';
3+
import { UMB_LANGUAGE_ENTITY_TYPE, UMB_LANGUAGE_ROOT_ENTITY_TYPE } from '../../entity.js';
34
import { UmbLanguageWorkspaceEditorElement } from './language-workspace-editor.element.js';
45
import { UMB_LANGUAGE_WORKSPACE_ALIAS } from './constants.js';
56
import {
67
type UmbSubmittableWorkspaceContext,
7-
UmbSubmittableWorkspaceContextBase,
88
UmbWorkspaceIsNewRedirectController,
99
type UmbRoutableWorkspaceContext,
10+
UmbEntityDetailWorkspaceContextBase,
1011
} from '@umbraco-cms/backoffice/workspace';
11-
import { UmbObjectState } from '@umbraco-cms/backoffice/observable-api';
1212
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
1313

1414
export class UmbLanguageWorkspaceContext
15-
extends UmbSubmittableWorkspaceContextBase<UmbLanguageDetailModel>
15+
extends UmbEntityDetailWorkspaceContextBase<UmbLanguageDetailModel, UmbLanguageDetailRepository>
1616
implements UmbSubmittableWorkspaceContext, UmbRoutableWorkspaceContext
1717
{
1818
public readonly repository: UmbLanguageDetailRepository = new UmbLanguageDetailRepository(this);
1919

20-
#data = new UmbObjectState<UmbLanguageDetailModel | undefined>(undefined);
21-
readonly data = this.#data.asObservable();
20+
readonly data = this._data.current;
2221

23-
readonly unique = this.#data.asObservablePart((data) => data?.unique);
24-
readonly name = this.#data.asObservablePart((data) => data?.name);
25-
26-
// TODO: this is a temp solution to bubble validation errors to the UI
27-
#validationErrors = new UmbObjectState<any | undefined>(undefined);
28-
readonly validationErrors = this.#validationErrors.asObservable();
22+
readonly unique = this._data.createObservablePart((data) => data?.unique);
23+
readonly name = this._data.createObservablePart((data) => data?.name);
2924

3025
constructor(host: UmbControllerHost) {
31-
super(host, UMB_LANGUAGE_WORKSPACE_ALIAS);
26+
super(host, {
27+
workspaceAlias: UMB_LANGUAGE_WORKSPACE_ALIAS,
28+
entityType: UMB_LANGUAGE_ENTITY_TYPE,
29+
detailRepositoryAlias: UMB_LANGUAGE_DETAIL_REPOSITORY_ALIAS,
30+
});
3231

3332
this.routes.setRoutes([
3433
{
3534
path: 'create',
3635
component: UmbLanguageWorkspaceEditorElement,
3736
setup: async () => {
38-
this.create();
37+
this.create({ parent: { entityType: UMB_LANGUAGE_ROOT_ENTITY_TYPE, unique: null } });
3938

4039
new UmbWorkspaceIsNewRedirectController(
4140
this,
@@ -55,85 +54,24 @@ export class UmbLanguageWorkspaceContext
5554
]);
5655
}
5756

58-
protected override resetState(): void {
59-
super.resetState();
60-
this.#data.setValue(undefined);
61-
}
62-
63-
async load(unique: string) {
64-
this.resetState();
65-
const { data } = await this.repository.requestByUnique(unique);
66-
if (data) {
67-
this.setIsNew(false);
68-
this.#data.update(data);
69-
}
70-
}
71-
72-
async create() {
73-
this.resetState();
74-
const { data } = await this.repository.createScaffold();
75-
if (!data) return;
76-
this.setIsNew(true);
77-
this.#data.update(data);
78-
return { data };
79-
}
80-
81-
getData() {
82-
return this.#data.getValue();
83-
}
84-
85-
getEntityType() {
86-
return 'language';
87-
}
88-
89-
// TODO: Convert to uniques:
90-
getUnique() {
91-
return this.#data.getValue()?.unique;
92-
}
93-
9457
setName(name: string) {
95-
this.#data.update({ name });
58+
this._data.updateCurrentData({ name });
9659
}
9760

9861
setCulture(unique: string) {
99-
this.#data.update({ unique });
62+
this._data.updateCurrentData({ unique });
10063
}
10164

10265
setMandatory(isMandatory: boolean) {
103-
this.#data.update({ isMandatory });
66+
this._data.updateCurrentData({ isMandatory });
10467
}
10568

10669
setDefault(isDefault: boolean) {
107-
this.#data.update({ isDefault });
70+
this._data.updateCurrentData({ isDefault });
10871
}
10972

11073
setFallbackCulture(unique: string) {
111-
this.#data.update({ fallbackIsoCode: unique });
112-
}
113-
114-
async submit() {
115-
const newData = this.getData();
116-
if (!newData) {
117-
throw new Error('No data to submit');
118-
}
119-
120-
if (this.getIsNew()) {
121-
const { error } = await this.repository.create(newData);
122-
if (error) {
123-
throw new Error(error.message);
124-
}
125-
this.setIsNew(false);
126-
} else {
127-
const { error } = await this.repository.save(newData);
128-
if (error) {
129-
throw new Error(error.message);
130-
}
131-
}
132-
}
133-
134-
override destroy(): void {
135-
this.#data.destroy();
136-
super.destroy();
74+
this._data.updateCurrentData({ fallbackIsoCode: unique });
13775
}
13876
}
13977

0 commit comments

Comments
 (0)