delete and create actions

This commit is contained in:
Lone Iversen
2023-11-09 14:27:00 +01:00
parent 8c40e9d217
commit d2085f3a77
5 changed files with 27 additions and 46 deletions

View File

@@ -9,7 +9,7 @@ import {
import { UmbDetailRepository, UmbItemRepository } from '@umbraco-cms/backoffice/repository';
export class UmbDeleteEntityAction<
T extends UmbDetailRepository & UmbItemRepository<any>
T extends UmbDetailRepository & UmbItemRepository<any>,
> extends UmbEntityActionBase<T> {
#modalManager?: UmbModalManagerContext;

View File

@@ -1,55 +1,16 @@
import { UmbDictionaryRepository } from '../../repository/dictionary.repository.js';
import { UmbTextStyles } from "@umbraco-cms/backoffice/style";
import { UmbSectionSidebarContext, UMB_SECTION_SIDEBAR_CONTEXT_TOKEN } from '@umbraco-cms/backoffice/section';
import { UmbTextStyles } from '@umbraco-cms/backoffice/style';
import { UmbEntityActionBase } from '@umbraco-cms/backoffice/entity-action';
import { UmbControllerHostElement } from '@umbraco-cms/backoffice/controller-api';
import { UmbContextConsumerController } from '@umbraco-cms/backoffice/context-api';
import {
UmbModalManagerContext,
UMB_MODAL_MANAGER_CONTEXT_TOKEN,
UMB_CREATE_DICTIONARY_MODAL,
} from '@umbraco-cms/backoffice/modal';
export default class UmbCreateDictionaryEntityAction extends UmbEntityActionBase<UmbDictionaryRepository> {
static styles = [UmbTextStyles];
#modalContext?: UmbModalManagerContext;
#sectionSidebarContext!: UmbSectionSidebarContext;
constructor(host: UmbControllerHostElement, repositoryAlias: string, unique: string) {
super(host, repositoryAlias, unique);
new UmbContextConsumerController(this.host, UMB_MODAL_MANAGER_CONTEXT_TOKEN, (instance) => {
this.#modalContext = instance;
});
new UmbContextConsumerController(this.host, UMB_SECTION_SIDEBAR_CONTEXT_TOKEN, (instance) => {
this.#sectionSidebarContext = instance;
});
}
async execute() {
// TODO: what to do if modal service is not available?
if (!this.#modalContext) return;
if (!this.repository) return;
// TODO: how can we get the current entity detail in the modal? Passing the observable
// feels a bit hacky. Works, but hacky.
const modalContext = this.#modalContext?.open(UMB_CREATE_DICTIONARY_MODAL, {
parentId: this.unique,
parentName: this.#sectionSidebarContext.headline,
});
const { name, parentId } = await modalContext.onSubmit();
if (!name || parentId === undefined) return;
const { data: url } = await this.repository.create({ name, parentId });
if (!url) return;
//TODO: Why do we need to extract the id like this?
const id = url.substring(url.lastIndexOf('/') + 1);
history.pushState({}, '', `/section/dictionary/workspace/dictionary-item/edit/${id}`);
history.pushState({}, '', `/section/dictionary/workspace/dictionary-item/create/${this.unique ?? 'null'}`);
}
}

View File

@@ -110,6 +110,19 @@ export class UmbDictionaryRepository
return { data, error, asObservable: () => this.#treeStore!.items(ids) };
}
async requestItems(ids: Array<string>) {
if (!ids) throw new Error('Dictionary Ids are missing');
await this.#init;
const { data, error } = await this.#treeSource.getItems(ids);
if (data) {
this.#treeStore?.appendItems(data);
}
return { data, error, asObservable: () => this.#treeStore!.items(ids) };
}
async rootTreeItems() {
await this.#init;
return this.#treeStore!.rootItems;

View File

@@ -68,9 +68,8 @@ export class UmbDictionaryWorkspaceContext
const { data } = await this.repository.createScaffold(parentId);
if (!data) return;
this.setIsNew(true);
// TODO: This is a hack to get around the fact that the data is not typed correctly.
// Create and response models are different. We need to look into this.
this.#data.next(data as unknown as DictionaryItemResponseModel);
this.#data.next(data as DictionaryItemResponseModel);
}
async save() {

View File

@@ -4,6 +4,7 @@ import { UmbTextStyles } from '@umbraco-cms/backoffice/style';
import { html, customElement, state } from '@umbraco-cms/backoffice/external/lit';
import type { UmbRoute } from '@umbraco-cms/backoffice/router';
import { UmbLitElement } from '@umbraco-cms/internal/lit-element';
import { UmbWorkspaceIsNewRedirectController } from '@umbraco-cms/backoffice/workspace';
@customElement('umb-dictionary-workspace')
export class UmbWorkspaceDictionaryElement extends UmbLitElement {
@@ -23,9 +24,16 @@ export class UmbWorkspaceDictionaryElement extends UmbLitElement {
{
path: 'create/:parentId',
component: () => this.#element,
setup: (_component, info) => {
setup: async (_component, info) => {
const parentId = info.match.params.parentId === 'null' ? null : info.match.params.parentId;
this.#workspaceContext.create(parentId);
await this.#workspaceContext.create(parentId);
new UmbWorkspaceIsNewRedirectController(
this,
this.#workspaceContext,
this.shadowRoot!.querySelector('umb-router-slot')!,
);
},
},
];