From f4f450c981fd129689c77f19d98f3676769de77f Mon Sep 17 00:00:00 2001 From: Mads Rasmussen Date: Fri, 12 Jan 2024 14:02:49 +0100 Subject: [PATCH] add rename repository base --- .../common/rename/rename-repository-base.ts | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/Umbraco.Web.UI.Client/src/packages/core/entity-action/common/rename/rename-repository-base.ts diff --git a/src/Umbraco.Web.UI.Client/src/packages/core/entity-action/common/rename/rename-repository-base.ts b/src/Umbraco.Web.UI.Client/src/packages/core/entity-action/common/rename/rename-repository-base.ts new file mode 100644 index 0000000000..aa242fbc3a --- /dev/null +++ b/src/Umbraco.Web.UI.Client/src/packages/core/entity-action/common/rename/rename-repository-base.ts @@ -0,0 +1,74 @@ +import { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api'; +import { UMB_NOTIFICATION_CONTEXT_TOKEN, UmbNotificationContext } from '@umbraco-cms/backoffice/notification'; +import { UMB_ACTION_EVENT_CONTEXT, UmbActionEvent, UmbActionEventContext } from '@umbraco-cms/backoffice/action'; +import { UmbRepositoryBase } from '@umbraco-cms/backoffice/repository'; +import { UmbRenameDataSource, UmbRenameDataSourceConstructor } from '@umbraco-cms/backoffice/entity-action'; +import { UmbContextToken } from '@umbraco-cms/backoffice/context-api'; +import { UmbDetailStore } from '@umbraco-cms/backoffice/store'; + +export abstract class UmbRenameRepositoryBase extends UmbRepositoryBase { + #detailStore?: UmbDetailStore; + #renameSource: UmbRenameDataSource; + #notificationContext?: UmbNotificationContext; + #actionEventContext?: UmbActionEventContext; + #init: Promise; + + constructor( + host: UmbControllerHost, + detailSource: UmbRenameDataSourceConstructor, + detailStoreContextAlias: string | UmbContextToken, + ) { + super(host); + this.#renameSource = new detailSource(host); + + this.#init = Promise.all([ + this.consumeContext(detailStoreContextAlias, (instance) => { + this.#detailStore = instance; + }).asPromise(), + + this.consumeContext(UMB_NOTIFICATION_CONTEXT_TOKEN, (instance) => { + this.#notificationContext = instance; + }).asPromise(), + + this.consumeContext(UMB_ACTION_EVENT_CONTEXT, (instance) => { + this.#actionEventContext = instance; + }).asPromise(), + ]); + } + + /** + * Rename + * @param {string} unique + * @param {string} name + * @return {*} + * @memberof UmbRenameRepositoryBase + */ + async rename(unique: string, name: string) { + if (!unique) throw new Error('Unique is missing'); + if (!name) throw new Error('Name is missing'); + await this.#init; + + const requestEventData = { unique, parentUnique: null }; + this.#actionEventContext?.dispatchEvent(new UmbActionEvent('detail-create-request', requestEventData)); + + const { data, error } = await this.#renameSource.rename(unique, name); + + if (data) { + this.#detailStore?.removeItem(unique); + this.#detailStore?.append(data); + + // TODO: how do we handle generic notifications? Is this the correct place to do it? + const notification = { data: { message: `Renamed` } }; + this.#notificationContext!.peek('positive', notification); + + const successEventData = { unique: data.unique, parentUnique: data.parentUnique }; + this.#actionEventContext?.dispatchEvent(new UmbActionEvent('detail-create-success', successEventData)); + } + + if (error) { + this.#actionEventContext?.dispatchEvent(new UmbActionEvent('detail-create-error', requestEventData)); + } + + return { data, error }; + } +}