Update rename-server-file-repository-base.ts

This commit is contained in:
Mads Rasmussen
2024-03-30 19:40:54 +01:00
parent bb0cc7ca92
commit a465ee37d4

View File

@@ -1,16 +1,24 @@
import type { UmbRenameServerFileDataSource, UmbRenameServerFileDataSourceConstructor } from './types.js';
import type { UmbContextToken } from '@umbraco-cms/backoffice/context-api';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import { UMB_NOTIFICATION_CONTEXT } from '@umbraco-cms/backoffice/notification';
import type { UmbDetailStore } from '@umbraco-cms/backoffice/store';
import { UmbRepositoryBase } from '@umbraco-cms/backoffice/repository';
export abstract class UmbRenameServerFileRepositoryBase<
DetailModelType extends { unique: string },
> extends UmbRepositoryBase {
#renameSource: UmbRenameServerFileDataSource<DetailModelType>;
#detailStoreContextAlias: string | UmbContextToken<UmbDetailStore<DetailModelType>>;
constructor(host: UmbControllerHost, detailSource: UmbRenameServerFileDataSourceConstructor<DetailModelType>) {
constructor(
host: UmbControllerHost,
detailSource: UmbRenameServerFileDataSourceConstructor<DetailModelType>,
detailStoreContextAlias: string | UmbContextToken<UmbDetailStore<DetailModelType>>,
) {
super(host);
this.#renameSource = new detailSource(host);
this.#detailStoreContextAlias = detailStoreContextAlias;
}
/**
@@ -27,6 +35,13 @@ export abstract class UmbRenameServerFileRepositoryBase<
const { data, error } = await this.#renameSource.rename(unique, name);
if (data) {
const detailStore = await this.getContext(this.#detailStoreContextAlias);
/* When renaming a file the unique changed because it is based on the path/name
We need to remove the old item and append the new item */
detailStore.removeItem(unique);
detailStore.append(data);
const notificationContext = await this.getContext(UMB_NOTIFICATION_CONTEXT);
const notification = { data: { message: `Renamed` } };
notificationContext.peek('positive', notification);