diff --git a/src/Umbraco.Web.UI.Client/src/mocks/data/document-type.data.ts b/src/Umbraco.Web.UI.Client/src/mocks/data/document-type.data.ts index 3e4cbb3580..41458946a2 100644 --- a/src/Umbraco.Web.UI.Client/src/mocks/data/document-type.data.ts +++ b/src/Umbraco.Web.UI.Client/src/mocks/data/document-type.data.ts @@ -1164,6 +1164,11 @@ class UmbDocumentTypeData extends UmbEntityData { const items = this.treeData.filter((item) => allowedTypeKeys.includes(item.id ?? '')); return items.map((item) => item); } + + /** For internal use */ + getAll() { + return this.data; + } } export const umbDocumentTypeData = new UmbDocumentTypeData(); diff --git a/src/Umbraco.Web.UI.Client/src/mocks/data/document.data.ts b/src/Umbraco.Web.UI.Client/src/mocks/data/document.data.ts index 0a3d0fda4c..6822e966f0 100644 --- a/src/Umbraco.Web.UI.Client/src/mocks/data/document.data.ts +++ b/src/Umbraco.Web.UI.Client/src/mocks/data/document.data.ts @@ -1,3 +1,4 @@ +import { umbDocumentTypeData } from './document-type.data.js'; import { UmbEntityData } from './entity.data.js'; import { createDocumentTreeItem } from './utils.js'; import { @@ -5,6 +6,7 @@ import { DocumentResponseModel, DocumentTreeItemResponseModel, PagedDocumentTreeItemResponseModel, + PagedDocumentTypeResponseModel, } from '@umbraco-cms/backoffice/backend-api'; export const data: Array = [ @@ -595,6 +597,26 @@ class UmbDocumentData extends UmbEntityData { const items = this.treeData.filter((item) => ids.includes(item.id ?? '')); return items.map((item) => item); } + + getDocumentByIdAllowedDocumentTypes(id: string): PagedDocumentTypeResponseModel { + const item = this.getById(id); + if (item?.contentTypeId) { + const docType = umbDocumentTypeData.getById(item.contentTypeId); + if (docType) { + const items = docType.allowedContentTypes ?? []; + const total = items?.length; + return { items, total }; + } + } + return { items: [], total: 0 }; + } + + getAllowedDocumentTypesAtRoot(): PagedDocumentTypeResponseModel { + const items = umbDocumentTypeData.getAll(); //.filter((docType) => docType.allowedAsRoot); + + const total = items?.length; + return { items, total }; + } } export const umbDocumentData = new UmbDocumentData(); diff --git a/src/Umbraco.Web.UI.Client/src/mocks/handlers/document.handlers.ts b/src/Umbraco.Web.UI.Client/src/mocks/handlers/document.handlers.ts index b91e5a26e9..65462102b7 100644 --- a/src/Umbraco.Web.UI.Client/src/mocks/handlers/document.handlers.ts +++ b/src/Umbraco.Web.UI.Client/src/mocks/handlers/document.handlers.ts @@ -61,4 +61,19 @@ export const handlers = [ return res(ctx.status(200), ctx.json(document)); }), + + rest.get(umbracoPath('/document/:id/allowed-document-types'), (req, res, ctx) => { + const id = req.params.id as string; + if (!id) return; + + const response = umbDocumentData.getDocumentByIdAllowedDocumentTypes(id); + + return res(ctx.status(200), ctx.json(response)); + }), + + rest.get(umbracoPath('/document/root/allowed-document-types'), (req, res, ctx) => { + const response = umbDocumentData.getAllowedDocumentTypesAtRoot(); + + return res(ctx.status(200), ctx.json(response)); + }), ]; diff --git a/src/Umbraco.Web.UI.Client/src/packages/documents/document-types/modals/allowed-document-types/allowed-document-types-modal.element.ts b/src/Umbraco.Web.UI.Client/src/packages/documents/document-types/modals/allowed-document-types/allowed-document-types-modal.element.ts index 4aad7b6baa..be67f7d571 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/documents/document-types/modals/allowed-document-types/allowed-document-types-modal.element.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/documents/document-types/modals/allowed-document-types/allowed-document-types-modal.element.ts @@ -1,16 +1,16 @@ -import { UmbDocumentTypeRepository } from '../../repository/document-type.repository.js'; import { html, nothing, customElement, state, ifDefined } from '@umbraco-cms/backoffice/external/lit'; import { UUITextStyles } from '@umbraco-cms/backoffice/external/uui'; import { UmbAllowedDocumentTypesModalData, UmbAllowedDocumentTypesModalResult } from '@umbraco-cms/backoffice/modal'; import { UmbModalBaseElement } from '@umbraco-cms/internal/modal'; -import { DocumentTypeResponseModel, DocumentTypeTreeItemResponseModel } from '@umbraco-cms/backoffice/backend-api'; +import { DocumentTypeTreeItemResponseModel } from '@umbraco-cms/backoffice/backend-api'; +import { UmbDocumentRepository } from '@umbraco-cms/backoffice/document'; @customElement('umb-allowed-document-types-modal') export class UmbAllowedDocumentTypesModalElement extends UmbModalBaseElement< UmbAllowedDocumentTypesModalData, UmbAllowedDocumentTypesModalResult > { - #documentTypeRepository = new UmbDocumentTypeRepository(this); + #documentRepository = new UmbDocumentRepository(this); @state() private _allowedDocumentTypes: DocumentTypeTreeItemResponseModel[] = []; @@ -39,34 +39,21 @@ export class UmbAllowedDocumentTypesModalElement extends UmbModalBaseElement< } private async _retrieveAllowedChildrenOf(id: string) { - const { data } = await this.#documentTypeRepository.requestAllowedChildTypesOf(id); + const { data } = await this.#documentRepository.requestAllowedDocumentTypesOf(id); if (data) { - this._allowedDocumentTypes = data; + // TODO: implement pagination, or get 1000? + this._allowedDocumentTypes = data.items; } } private async _retrieveAllowedChildrenOfRoot() { - // TODO: This is a hack until we get the right end points (Which will become a Document end point. meaning this modal should have another name, it should be named so its clear that this is for documents, not document types. ex.: 'create-document-modal') - const { data } = await this.#documentTypeRepository.requestRootTreeItems(); - if (!data) return; + const { data } = await this.#documentRepository.requestAllowedDocumentTypesAtRoot(); - const allFullModels: Array = []; - await Promise.all( - data.items.map((item) => { - if (item.id) { - return this.#documentTypeRepository.requestById(item.id).then((result) => { - if (result.data) { - allFullModels.push({ ...result.data }); - } - }); - } - return Promise.resolve(); - }) - ); - - this._allowedDocumentTypes = allFullModels.filter((item) => item.allowedAsRoot) ?? []; - // End of hack...^^ + if (data) { + // TODO: implement pagination, or get 1000? + this._allowedDocumentTypes = data.items; + } } private _handleCancel() { diff --git a/src/Umbraco.Web.UI.Client/src/packages/documents/documents/repository/document.repository.ts b/src/Umbraco.Web.UI.Client/src/packages/documents/documents/repository/document.repository.ts index 8d1154f2fd..1bf8fa0e65 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/documents/documents/repository/document.repository.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/documents/documents/repository/document.repository.ts @@ -56,6 +56,7 @@ export class UmbDocumentRepository // TODO: Move // TREE: + async requestTreeRoot() { await this.#init; @@ -95,6 +96,17 @@ export class UmbDocumentRepository return { data, error, asObservable: () => this.#treeStore!.childrenOf(parentId) }; } + // Structure permissions; + + async requestAllowedDocumentTypesAtRoot() { + return this.#detailDataSource.getAllowedDocumentTypesAtRoot(); + } + async requestAllowedDocumentTypesOf(id: string) { + if (!id) throw new Error('Id is missing'); + await this.#init; + return this.#detailDataSource.getAllowedDocumentTypesOf(id); + } + async requestItemsLegacy(ids: Array) { await this.#init; diff --git a/src/Umbraco.Web.UI.Client/src/packages/documents/documents/repository/sources/document.server.data.ts b/src/Umbraco.Web.UI.Client/src/packages/documents/documents/repository/sources/document.server.data.ts index 777fff649f..f41d6ac3ab 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/documents/documents/repository/sources/document.server.data.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/documents/documents/repository/sources/document.server.data.ts @@ -168,4 +168,28 @@ export class UmbDocumentServerDataSource }).then((res) => res.json()) ); } + + /** + * Get the allowed document types for a given parent id + * @param {string} id + * @return {*} + * @memberof UmbDocumentTypeServerDataSource + */ + async getAllowedDocumentTypesOf(id: string) { + if (!id) throw new Error('Id is missing'); + + return tryExecuteAndNotify(this.#host, DocumentResource.getDocumentByIdAllowedDocumentTypes({ id })); + } + + /** + * Get the allowed document types for root + * @param {string} id + * @return {*} + * @memberof UmbDocumentTypeServerDataSource + */ + async getAllowedDocumentTypesAtRoot() { + console.log('source requestAllowedDocumentTypesAtRoot'); + // Notice, here we need to implement pagination. + return tryExecuteAndNotify(this.#host, DocumentResource.getDocumentRootAllowedDocumentTypes({ take: 4 })); + } }