update dictionary workspace
This commit is contained in:
@@ -1,42 +1,34 @@
|
||||
import type { UmbDictionaryDetailModel } from '../types.js';
|
||||
import { UmbDictionaryDetailRepository } from '../repository/index.js';
|
||||
import { UMB_DICTIONARY_DETAIL_REPOSITORY_ALIAS, type UmbDictionaryDetailRepository } from '../repository/index.js';
|
||||
import { UMB_DICTIONARY_ENTITY_TYPE } from '../entity.js';
|
||||
import { UmbDictionaryWorkspaceEditorElement } from './dictionary-workspace-editor.element.js';
|
||||
import { UMB_DICTIONARY_WORKSPACE_ALIAS } from './manifests.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_ACTION_EVENT_CONTEXT } from '@umbraco-cms/backoffice/action';
|
||||
import {
|
||||
UmbRequestReloadChildrenOfEntityEvent,
|
||||
UmbRequestReloadStructureForEntityEvent,
|
||||
} from '@umbraco-cms/backoffice/entity-action';
|
||||
|
||||
export class UmbDictionaryWorkspaceContext
|
||||
extends UmbSubmittableWorkspaceContextBase<UmbDictionaryDetailModel>
|
||||
extends UmbEntityDetailWorkspaceContextBase<UmbDictionaryDetailModel, UmbDictionaryDetailRepository>
|
||||
implements UmbSubmittableWorkspaceContext, UmbRoutableWorkspaceContext
|
||||
{
|
||||
//
|
||||
public readonly detailRepository = new UmbDictionaryDetailRepository(this);
|
||||
readonly data = this._data.current;
|
||||
|
||||
#parent = new UmbObjectState<{ entityType: string; unique: string | null } | undefined>(undefined);
|
||||
readonly parentUnique = this.#parent.asObservablePart((parent) => (parent ? parent.unique : undefined));
|
||||
readonly parentEntityType = this.#parent.asObservablePart((parent) => (parent ? parent.entityType : undefined));
|
||||
readonly unique = this._data.createObservablePart((data) => data?.unique);
|
||||
readonly entityType = this._data.createObservablePart((data) => data?.entityType);
|
||||
|
||||
#data = new UmbObjectState<UmbDictionaryDetailModel | undefined>(undefined);
|
||||
readonly data = this.#data.asObservable();
|
||||
|
||||
readonly unique = this.#data.asObservablePart((data) => data?.unique);
|
||||
readonly entityType = this.#data.asObservablePart((data) => data?.entityType);
|
||||
|
||||
readonly name = this.#data.asObservablePart((data) => data?.name);
|
||||
readonly dictionary = this.#data.asObservablePart((data) => data);
|
||||
readonly name = this._data.createObservablePart((data) => data?.name);
|
||||
readonly dictionary = this._data.createObservablePart((data) => data);
|
||||
|
||||
constructor(host: UmbControllerHost) {
|
||||
super(host, 'Umb.Workspace.Dictionary');
|
||||
super(host, {
|
||||
workspaceAlias: UMB_DICTIONARY_WORKSPACE_ALIAS,
|
||||
entityType: UMB_DICTIONARY_ENTITY_TYPE,
|
||||
detailRepositoryAlias: UMB_DICTIONARY_DETAIL_REPOSITORY_ALIAS,
|
||||
});
|
||||
|
||||
this.routes.setRoutes([
|
||||
{
|
||||
@@ -45,7 +37,7 @@ export class UmbDictionaryWorkspaceContext
|
||||
setup: async (_component, info) => {
|
||||
const parentEntityType = info.match.params.entityType;
|
||||
const parentUnique = info.match.params.parentUnique === 'null' ? null : info.match.params.parentUnique;
|
||||
this.create({ entityType: parentEntityType, unique: parentUnique });
|
||||
this.create({ parent: { entityType: parentEntityType, unique: parentUnique } });
|
||||
|
||||
new UmbWorkspaceIsNewRedirectController(
|
||||
this,
|
||||
@@ -65,34 +57,18 @@ export class UmbDictionaryWorkspaceContext
|
||||
]);
|
||||
}
|
||||
|
||||
protected override resetState(): void {
|
||||
super.resetState();
|
||||
this.#data.setValue(undefined);
|
||||
}
|
||||
|
||||
getData() {
|
||||
return this.#data.getValue();
|
||||
}
|
||||
|
||||
getUnique() {
|
||||
return this.getData()?.unique;
|
||||
}
|
||||
|
||||
getEntityType() {
|
||||
return 'dictionary';
|
||||
}
|
||||
|
||||
setName(name: string) {
|
||||
this.#data.update({ name });
|
||||
this._data.updateCurrentData({ name });
|
||||
}
|
||||
|
||||
setPropertyValue(isoCode: string, translation: string) {
|
||||
if (!this.#data.value) return;
|
||||
const currentData = this._data.getCurrentData();
|
||||
if (!currentData) return;
|
||||
|
||||
// TODO: This can use some of our own methods, to make it simpler. see appendToFrozenArray()
|
||||
// update if the code already exists
|
||||
const updatedValue =
|
||||
this.#data.value.translations?.map((translationItem) => {
|
||||
currentData.translations?.map((translationItem) => {
|
||||
if (translationItem.isoCode === isoCode) {
|
||||
return { ...translationItem, translation };
|
||||
}
|
||||
@@ -104,70 +80,7 @@ export class UmbDictionaryWorkspaceContext
|
||||
updatedValue?.push({ isoCode, translation });
|
||||
}
|
||||
|
||||
this.#data.setValue({ ...this.#data.value, translations: updatedValue });
|
||||
}
|
||||
|
||||
async load(unique: string) {
|
||||
this.resetState();
|
||||
const { data } = await this.detailRepository.requestByUnique(unique);
|
||||
if (data) {
|
||||
this.setIsNew(false);
|
||||
this.#data.setValue(data);
|
||||
}
|
||||
}
|
||||
|
||||
async create(parent: { entityType: string; unique: string | null }) {
|
||||
this.resetState();
|
||||
this.#parent.setValue(parent);
|
||||
const { data } = await this.detailRepository.createScaffold();
|
||||
if (!data) return;
|
||||
this.setIsNew(true);
|
||||
this.#data.setValue(data);
|
||||
}
|
||||
|
||||
async submit() {
|
||||
if (!this.#data.value) {
|
||||
throw new Error('No data to submit.');
|
||||
}
|
||||
if (!this.#data.value.unique) {
|
||||
throw new Error('No unique value to submit.');
|
||||
}
|
||||
|
||||
if (this.getIsNew()) {
|
||||
const parent = this.#parent.getValue();
|
||||
if (!parent) {
|
||||
throw new Error('Parent is not set');
|
||||
}
|
||||
const { error } = await this.detailRepository.create(this.#data.value, parent.unique);
|
||||
if (error) {
|
||||
throw new Error(error.message);
|
||||
}
|
||||
|
||||
// TODO: this might not be the right place to alert the tree, but it works for now
|
||||
const eventContext = await this.getContext(UMB_ACTION_EVENT_CONTEXT);
|
||||
const event = new UmbRequestReloadChildrenOfEntityEvent({
|
||||
entityType: parent.entityType,
|
||||
unique: parent.unique,
|
||||
});
|
||||
eventContext.dispatchEvent(event);
|
||||
|
||||
this.setIsNew(false);
|
||||
} else {
|
||||
await this.detailRepository.save(this.#data.value);
|
||||
|
||||
const actionEventContext = await this.getContext(UMB_ACTION_EVENT_CONTEXT);
|
||||
const event = new UmbRequestReloadStructureForEntityEvent({
|
||||
unique: this.getUnique()!,
|
||||
entityType: this.getEntityType(),
|
||||
});
|
||||
|
||||
actionEventContext.dispatchEvent(event);
|
||||
}
|
||||
}
|
||||
|
||||
public override destroy(): void {
|
||||
this.#data.destroy();
|
||||
super.destroy();
|
||||
this._data.setCurrentData({ ...currentData, translations: updatedValue });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user