align partial view folder creation with rest of file system logic

This commit is contained in:
Mads Rasmussen
2024-01-08 18:25:03 +01:00
parent 6bf8318bba
commit 442f095d7b
7 changed files with 193 additions and 60 deletions

View File

@@ -8,7 +8,6 @@ import { UMB_PARTIAL_VIEW_REPOSITORY_ALIAS } from '../repository/index.js';
import { UmbCreateFromSnippetPartialViewAction } from './create/create-from-snippet.action.js';
import { UmbCreateEmptyPartialViewAction } from './create/create-empty.action.js';
import { UmbDeleteEntityAction } from '@umbraco-cms/backoffice/entity-action';
import { UmbCreateFolderEntityAction, UmbDeleteFolderEntityAction } from '@umbraco-cms/backoffice/tree';
import { ManifestEntityAction } from '@umbraco-cms/backoffice/extension-registry';
//Actions for partial view files
@@ -27,8 +26,6 @@ const partialViewActions: Array<ManifestEntityAction> = [
},
];
//TODO: add create folder action when the generic folder action is implemented
//Actions for directories
const partialViewFolderActions: Array<ManifestEntityAction> = [
{
type: 'entityAction',
@@ -54,30 +51,6 @@ const partialViewFolderActions: Array<ManifestEntityAction> = [
entityTypes: [UMB_PARTIAL_VIEW_FOLDER_ENTITY_TYPE, UMB_PARTIAL_VIEW_ROOT_ENTITY_TYPE],
},
},
{
type: 'entityAction',
alias: 'Umb.EntityAction.PartialViewFolder.DeleteFolder',
name: 'Delete Partial View Folder',
api: UmbDeleteFolderEntityAction,
meta: {
icon: 'icon-trash',
label: 'Delete folder...',
repositoryAlias: UMB_PARTIAL_VIEW_REPOSITORY_ALIAS,
entityTypes: [UMB_PARTIAL_VIEW_FOLDER_ENTITY_TYPE],
},
},
{
type: 'entityAction',
alias: 'Umb.EntityAction.PartialViewFolder.CreateFolder',
name: 'Create Partial View folder',
api: UmbCreateFolderEntityAction,
meta: {
icon: 'icon-add',
label: 'Create folder...',
repositoryAlias: UMB_PARTIAL_VIEW_REPOSITORY_ALIAS,
entityTypes: [UMB_PARTIAL_VIEW_FOLDER_ENTITY_TYPE, UMB_PARTIAL_VIEW_ROOT_ENTITY_TYPE],
},
},
];
const createFromSnippetActionModal = {

View File

@@ -1,32 +0,0 @@
import { CreateFolderRequestModel, PartialViewResource } from '@umbraco-cms/backoffice/backend-api';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import { UmbFolderDataSource } from '@umbraco-cms/backoffice/tree';
import { tryExecuteAndNotify } from '@umbraco-cms/backoffice/resources';
export class UmbPartialViewFolderServerDataSource implements UmbFolderDataSource {
#host: UmbControllerHost;
constructor(host: UmbControllerHost) {
this.#host = host;
}
createScaffold(): any {
throw new Error('Method not implemented.');
}
read(unique: string) {
return tryExecuteAndNotify(this.#host, PartialViewResource.getPartialViewFolder({ path: unique }));
}
create(requestBody: CreateFolderRequestModel) {
return tryExecuteAndNotify(this.#host, PartialViewResource.postPartialViewFolder({ requestBody }));
}
update(): any {
throw new Error('Method not implemented.');
}
delete(path: string) {
return tryExecuteAndNotify(this.#host, PartialViewResource.deletePartialViewFolder({ path }));
}
}

View File

@@ -0,0 +1,6 @@
export { UmbPartialViewFolderRepository } from './partial-view-folder.repository.js';
export {
UMB_PARTIAL_VIEW_FOLDER_REPOSITORY_ALIAS,
UMB_DELETE_PARTIAL_VIEW_FOLDER_ENTITY_ACTION_ALIAS,
UMB_CREATE_PARTIAL_VIEW_FOLDER_ENTITY_ACTION_ALIAS,
} from './manifests.js';

View File

@@ -0,0 +1,45 @@
import { UMB_PARTIAL_VIEW_FOLDER_ENTITY_TYPE, UMB_PARTIAL_VIEW_ROOT_ENTITY_TYPE } from '../../entity.js';
import { UmbPartialViewFolderRepository } from './partial-view-folder.repository.js';
import { UmbCreateFolderEntityAction, UmbDeleteFolderEntityAction } from '@umbraco-cms/backoffice/tree';
import type { ManifestRepository } from '@umbraco-cms/backoffice/extension-registry';
export const UMB_PARTIAL_VIEW_FOLDER_REPOSITORY_ALIAS = 'Umb.Repository.PartialView.Folder';
const folderRepository: ManifestRepository = {
type: 'repository',
alias: UMB_PARTIAL_VIEW_FOLDER_REPOSITORY_ALIAS,
name: 'Partial View Folder Repository',
api: UmbPartialViewFolderRepository,
};
export const UMB_DELETE_PARTIAL_VIEW_FOLDER_ENTITY_ACTION_ALIAS = 'Umb.EntityAction.PartialView.Folder.Delete';
export const UMB_CREATE_PARTIAL_VIEW_FOLDER_ENTITY_ACTION_ALIAS = 'Umb.EntityAction.PartialView.Folder.Create';
const entityActions = [
{
type: 'entityAction',
alias: UMB_CREATE_PARTIAL_VIEW_FOLDER_ENTITY_ACTION_ALIAS,
name: 'Create Partial View folder Entity Action',
api: UmbCreateFolderEntityAction,
meta: {
icon: 'icon-add',
label: 'Create folder...',
repositoryAlias: UMB_PARTIAL_VIEW_FOLDER_REPOSITORY_ALIAS,
entityTypes: [UMB_PARTIAL_VIEW_ROOT_ENTITY_TYPE, UMB_PARTIAL_VIEW_FOLDER_ENTITY_TYPE],
},
},
{
type: 'entityAction',
alias: UMB_DELETE_PARTIAL_VIEW_FOLDER_ENTITY_ACTION_ALIAS,
name: 'Delete Partial View folder Entity Action',
api: UmbDeleteFolderEntityAction,
meta: {
icon: 'icon-trash',
label: 'Delete folder...',
repositoryAlias: UMB_PARTIAL_VIEW_FOLDER_REPOSITORY_ALIAS,
entityTypes: [UMB_PARTIAL_VIEW_FOLDER_ENTITY_TYPE],
},
},
];
export const manifests = [folderRepository, ...entityActions];

View File

@@ -0,0 +1,29 @@
import { UMB_PARTIAL_VIEW_FOLDER_ENTITY_TYPE } from '../../entity.js';
import { UmbPartialViewTreeItemModel } from '../types.js';
import { UMB_PARTIAL_VIEW_TREE_STORE_CONTEXT } from '../partial-view-tree.store.js';
import { UmbPartialViewFolderServerDataSource } from './partial-view-folder.server.data-source.js';
import { UmbFolderModel, UmbFolderRepositoryBase } from '@umbraco-cms/backoffice/tree';
import { type UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
export class UmbPartialViewFolderRepository extends UmbFolderRepositoryBase<UmbPartialViewTreeItemModel> {
constructor(host: UmbControllerHost) {
super(
host,
UmbPartialViewFolderServerDataSource,
UMB_PARTIAL_VIEW_TREE_STORE_CONTEXT,
folderToPartialViewTreeItemFolder,
);
}
}
const folderToPartialViewTreeItemFolder = (folder: UmbFolderModel): UmbPartialViewTreeItemModel => {
return {
unique: folder.unique,
parentUnique: folder.parentUnique,
name: folder.name,
entityType: UMB_PARTIAL_VIEW_FOLDER_ENTITY_TYPE,
isFolder: true,
isContainer: false,
hasChildren: false,
};
};

View File

@@ -0,0 +1,111 @@
import { UmbServerPathUniqueSerializer } from '../../../utils/index.js';
import { UmbCreateFolderModel, UmbFolderDataSource, UmbUpdateFolderModel } from '@umbraco-cms/backoffice/tree';
import { CreatePartialViewFolderRequestModel, PartialViewResource } from '@umbraco-cms/backoffice/backend-api';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import { tryExecuteAndNotify } from '@umbraco-cms/backoffice/resources';
/**
* A data source for Partial View folders that fetches data from the server
* @export
* @class UmbPartialViewFolderServerDataSource
* @implements {RepositoryDetailDataSource}
*/
export class UmbPartialViewFolderServerDataSource implements UmbFolderDataSource {
#host: UmbControllerHost;
#serverPathUniqueSerializer = new UmbServerPathUniqueSerializer();
/**
* Creates an instance of UmbPartialViewFolderServerDataSource.
* @param {UmbControllerHost} host
* @memberof UmbPartialViewFolderServerDataSource
*/
constructor(host: UmbControllerHost) {
this.#host = host;
}
/**
* Fetches a Partial View folder from the server
* @param {string} unique
* @return {UmbDataSourceResponse<UmbFolderModel>}
* @memberof UmbPartialViewFolderServerDataSource
*/
async read(unique: string) {
if (!unique) throw new Error('Unique is missing');
const path = this.#serverPathUniqueSerializer.toServerPath(unique);
const { data, error } = await tryExecuteAndNotify(
this.#host,
PartialViewResource.getPartialViewFolderByPath({
path,
}),
);
if (data) {
const mappedData = {
unique: this.#serverPathUniqueSerializer.toUnique(data.path),
parentUnique: data.parent ? this.#serverPathUniqueSerializer.toUnique(data.parent.path) : null,
name: data.name,
};
return { data: mappedData };
}
return { error };
}
/**
* Creates a Partial View folder on the server
* @param {UmbCreateFolderModel} args
* @return {UmbDataSourceResponse<UmbFolderModel>}
* @memberof UmbPartialViewFolderServerDataSource
*/
async create(args: UmbCreateFolderModel) {
if (args.parentUnique === undefined) throw new Error('Parent unique is missing');
if (!args.name) throw new Error('Name is missing');
const parentPath = new UmbServerPathUniqueSerializer().toServerPath(args.parentUnique);
const requestBody: CreatePartialViewFolderRequestModel = {
parentPath,
name: args.name,
};
const { data, error } = await tryExecuteAndNotify(
this.#host,
PartialViewResource.postPartialViewFolder({
requestBody,
}),
);
if (data) {
const newPath = this.#serverPathUniqueSerializer.toUnique(data);
return this.read(newPath);
}
return { error };
}
/**
* Deletes a Partial View folder on the server
* @param {string} unique
* @return {UmbDataSourceErrorResponse}
* @memberof UmbPartialViewServerDataSource
*/
async delete(unique: string) {
if (!unique) throw new Error('Unique is missing');
const path = this.#serverPathUniqueSerializer.toServerPath(unique);
return tryExecuteAndNotify(
this.#host,
PartialViewResource.deletePartialViewFolderByPath({
path,
}),
);
}
async update(args: UmbUpdateFolderModel): Promise<any> {
throw new Error('Updating is not supported');
}
}

View File

@@ -1,6 +1,7 @@
import { UMB_PARTIAL_VIEW_ENTITY_TYPE, UMB_PARTIAL_VIEW_ROOT_ENTITY_TYPE } from '../entity.js';
import { UmbPartialViewTreeRepository } from './partial-view-tree.repository.js';
import { UmbPartialViewTreeStore } from './partial-view-tree.store.js';
import { manifests as folderManifests } from './folder/manifests.js';
import type {
ManifestRepository,
ManifestTree,
@@ -45,4 +46,4 @@ const treeItem: ManifestTreeItem = {
},
};
export const manifests = [treeRepository, treeStore, tree, treeItem];
export const manifests = [treeRepository, treeStore, tree, treeItem, ...folderManifests];