add partial view item repo + store

This commit is contained in:
Mads Rasmussen
2024-03-03 15:24:31 +01:00
parent 5f41645cbc
commit bdb0579fc0
9 changed files with 146 additions and 3 deletions

View File

@@ -1,5 +1,6 @@
import { UMB_PARTIAL_VIEW_ENTITY_TYPE } from '../entity.js';
import { UMB_PARTIAL_VIEW_DETAIL_REPOSITORY_ALIAS } from '../repository/index.js';
import { UMB_PARTIAL_VIEW_ITEM_REPOSITORY_ALIAS } from '../repository/item/index.js';
import { manifests as createManifests } from './create/manifests.js';
import { manifests as renameManifests } from './rename/manifests.js';
import type { ManifestTypes } from '@umbraco-cms/backoffice/extension-registry';
@@ -12,7 +13,7 @@ const partialViewActions: Array<ManifestTypes> = [
kind: 'delete',
meta: {
detailRepositoryAlias: UMB_PARTIAL_VIEW_DETAIL_REPOSITORY_ALIAS,
itemRepositoryAlias: UMB_PARTIAL_VIEW_ITEM_REPOSITORY_ALIAS, // TODO: implement partial view item repo
itemRepositoryAlias: UMB_PARTIAL_VIEW_ITEM_REPOSITORY_ALIAS,
entityTypes: [UMB_PARTIAL_VIEW_ENTITY_TYPE],
},
},

View File

@@ -0,0 +1,3 @@
export { UmbPartialViewItemRepository } from './partial-view-item.repository.js';
export { UMB_PARTIAL_VIEW_ITEM_REPOSITORY_ALIAS, UMB_PARTIAL_VIEW_ITEM_STORE_ALIAS } from './manifests.js';
export { UMB_PARTIAL_VIEW_ITEM_STORE_CONTEXT } from './partial-view-item.store.js';

View File

@@ -0,0 +1,20 @@
import type { ManifestRepository, ManifestItemStore } from '@umbraco-cms/backoffice/extension-registry';
export const UMB_PARTIAL_VIEW_ITEM_REPOSITORY_ALIAS = 'Umb.Repository.PartialView.Item';
export const UMB_PARTIAL_VIEW_ITEM_STORE_ALIAS = 'Umb.ItemStore.PartialView';
const repository: ManifestRepository = {
type: 'repository',
alias: UMB_PARTIAL_VIEW_ITEM_REPOSITORY_ALIAS,
name: 'Partial View Item Repository',
api: () => import('./partial-view-item.repository.js'),
};
const itemStore: ManifestItemStore = {
type: 'itemStore',
alias: 'Umb.ItemStore.PartialView',
name: 'Partial View Item Store',
api: () => import('./partial-view-item.store.js'),
};
export const manifests = [repository, itemStore];

View File

@@ -0,0 +1,13 @@
import type { UmbPartialViewItemModel } from '../../types.js';
import { UmbPartialViewItemServerDataSource } from './partial-view-item.server.data-source.js';
import { UMB_PARTIAL_VIEW_ITEM_STORE_CONTEXT } from './partial-view-item.store.js';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import { UmbItemRepositoryBase } from '@umbraco-cms/backoffice/repository';
export class UmbPartialViewItemRepository extends UmbItemRepositoryBase<UmbPartialViewItemModel> {
constructor(host: UmbControllerHost) {
super(host, UmbPartialViewItemServerDataSource, UMB_PARTIAL_VIEW_ITEM_STORE_CONTEXT);
}
}
export default UmbPartialViewItemRepository;

View File

@@ -0,0 +1,68 @@
import { UMB_PARTIAL_VIEW_ENTITY_TYPE, UMB_PARTIAL_VIEW_FOLDER_ENTITY_TYPE } from '../../entity.js';
import type { UmbPartialViewItemModel } from '../../types.js';
import { UmbServerFilePathUniqueSerializer } from '@umbraco-cms/backoffice/server-file-system';
import type { UmbItemDataSource } from '@umbraco-cms/backoffice/repository';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import { tryExecuteAndNotify } from '@umbraco-cms/backoffice/resources';
import { PartialViewResource } from '@umbraco-cms/backoffice/external/backend-api';
/**
* A data source for script items that fetches data from the server
* @export
* @class UmbPartialViewItemServerDataSource
* @implements {UmbItemDataSource}
*/
export class UmbPartialViewItemServerDataSource implements UmbItemDataSource<UmbPartialViewItemModel> {
#host: UmbControllerHost;
#serverFilePathUniqueSerializer = new UmbServerFilePathUniqueSerializer();
/**
* Creates an instance of UmbPartialViewItemServerDataSource.
* @param {UmbControllerHost} host
* @memberof UmbPartialViewItemServerDataSource
*/
constructor(host: UmbControllerHost) {
this.#host = host;
}
/**
* Fetches the items for the given uniques from the server
* @param {Array<string>} uniques
* @return {*}
* @memberof UmbPartialViewItemServerDataSource
*/
async getItems(uniques: Array<string>) {
if (!uniques) throw new Error('Uniques are missing');
const paths = uniques
.map((unique) => {
const serverPath = this.#serverFilePathUniqueSerializer.toServerPath(unique);
return serverPath ? encodeURI(serverPath) : null;
})
.filter((x) => x !== null) as string[];
const { data, error } = await tryExecuteAndNotify(
this.#host,
PartialViewResource.getItemPartialView({
path: paths,
}),
);
if (data) {
const items: Array<UmbPartialViewItemModel> = data.map((item) => {
return {
entityType: item.isFolder ? UMB_PARTIAL_VIEW_FOLDER_ENTITY_TYPE : UMB_PARTIAL_VIEW_ENTITY_TYPE,
unique: this.#serverFilePathUniqueSerializer.toUnique(item.path),
parentUnique: item.parent ? this.#serverFilePathUniqueSerializer.toUnique(item.parent.path) : null,
name: item.name,
isFolder: item.isFolder,
path: item.path,
};
});
return { data: items };
}
return { error };
}
}

View File

@@ -0,0 +1,28 @@
import type { UmbPartialViewItemModel } from '../../types.js';
import { UmbContextToken } from '@umbraco-cms/backoffice/context-api';
import type { UmbControllerHostElement } from '@umbraco-cms/backoffice/controller-api';
import { UmbItemStoreBase } from '@umbraco-cms/backoffice/store';
/**
* @export
* @class UmbPartialViewItemStore
* @extends {UmbItemStoreBase}
* @description - Data Store for PartialView items
*/
export class UmbPartialViewItemStore extends UmbItemStoreBase<UmbPartialViewItemModel> {
/**
* Creates an instance of UmbPartialViewItemStore.
* @param {UmbControllerHostElement} host
* @memberof UmbPartialViewItemStore
*/
constructor(host: UmbControllerHostElement) {
super(host, UMB_PARTIAL_VIEW_ITEM_STORE_CONTEXT.toString());
}
}
export default UmbPartialViewItemStore;
export const UMB_PARTIAL_VIEW_ITEM_STORE_CONTEXT = new UmbContextToken<UmbPartialViewItemStore>(
'UmbPartialViewItemStore',
);

View File

@@ -1,3 +1,4 @@
import { manifests as itemManifests } from './item/manifests.js';
import type { ManifestRepository, ManifestStore } from '@umbraco-cms/backoffice/extension-registry';
export const UMB_PARTIAL_VIEW_DETAIL_REPOSITORY_ALIAS = 'Umb.Repository.PartialView.Detail';
@@ -17,4 +18,4 @@ const store: ManifestStore = {
api: () => import('./partial-view-detail.store.js'),
};
export const manifests = [repository, store];
export const manifests = [repository, store, ...itemManifests];

View File

@@ -1,4 +1,4 @@
import type { UmbPartialViewEntityType } from './entity.js';
import type { UmbPartialViewEntityType, UmbPartialViewFolderEntityType } from './entity.js';
export interface UmbPartialViewDetailModel {
entityType: UmbPartialViewEntityType;
@@ -8,3 +8,11 @@ export interface UmbPartialViewDetailModel {
name: string;
content: string;
}
export interface UmbPartialViewItemModel {
entityType: UmbPartialViewEntityType | UmbPartialViewFolderEntityType;
unique: string;
parentUnique: string | null;
path: string;
name: string;
}

View File

@@ -1,4 +1,5 @@
import { UMB_SCRIPT_ENTITY_TYPE } from '../../entity.js';
import { UMB_SCRIPT_ITEM_REPOSITORY_ALIAS } from '../../repository/item/index.js';
import type { ManifestTypes } from '@umbraco-cms/backoffice/extension-registry';
export const UMB_RENAME_SCRIPT_REPOSITORY_ALIAS = 'Umb.Repository.Script.Rename';