diff --git a/src/Umbraco.Web.UI.Client/libs/repository/repository-detail-data-source.interface.ts b/src/Umbraco.Web.UI.Client/libs/repository/repository-detail-data-source.interface.ts index e24fc4956a..04f2894b39 100644 --- a/src/Umbraco.Web.UI.Client/libs/repository/repository-detail-data-source.interface.ts +++ b/src/Umbraco.Web.UI.Client/libs/repository/repository-detail-data-source.interface.ts @@ -3,7 +3,7 @@ import type { DataSourceResponse } from '@umbraco-cms/models'; export interface RepositoryDetailDataSource { createScaffold(parentKey: string | null): Promise>; get(key: string): Promise>; - insert(data: DetailType): Promise; - update(data: DetailType): Promise; - trash(key: string): Promise; + insert(data: DetailType): Promise>; + update(data: DetailType): Promise>; + trash(key: string): Promise>; } diff --git a/src/Umbraco.Web.UI.Client/libs/resources/resource.controller.ts b/src/Umbraco.Web.UI.Client/libs/resources/resource.controller.ts index 1352dc1f0f..0c157ff41b 100644 --- a/src/Umbraco.Web.UI.Client/libs/resources/resource.controller.ts +++ b/src/Umbraco.Web.UI.Client/libs/resources/resource.controller.ts @@ -8,6 +8,7 @@ import { import { ApiError, CancelablePromise, ProblemDetails } from '@umbraco-cms/backend-api'; import { UmbController, UmbControllerHostInterface } from '@umbraco-cms/controller'; import { UmbContextConsumerController } from '@umbraco-cms/context-api'; +import type { DataSourceResponse } from '@umbraco-cms/models'; export class UmbResourceController extends UmbController { #promise: Promise; @@ -54,7 +55,7 @@ export class UmbResourceController extends UmbController { /** * Base execute function with a try/catch block and return a tuple with the result and the error. */ - static async tryExecute(promise: Promise): Promise<{ data?: T; error?: ProblemDetails }> { + static async tryExecute(promise: Promise): Promise> { try { return { data: await promise }; } catch (e) { @@ -66,7 +67,7 @@ export class UmbResourceController extends UmbController { * Wrap the {execute} function in a try/catch block and return the result. * If the executor function throws an error, then show the details in a notification. */ - async tryExecuteAndNotify(options?: UmbNotificationOptions): Promise<{ data?: T; error?: ProblemDetails }> { + async tryExecuteAndNotify(options?: UmbNotificationOptions): Promise> { const { data, error } = await UmbResourceController.tryExecute(this.#promise); if (error) { diff --git a/src/Umbraco.Web.UI.Client/src/backoffice/documents/documents/repository/sources/document.detail.server.data.ts b/src/Umbraco.Web.UI.Client/src/backoffice/documents/documents/repository/sources/document.detail.server.data.ts index f9d4bf75e7..404fb5ac61 100644 --- a/src/Umbraco.Web.UI.Client/src/backoffice/documents/documents/repository/sources/document.detail.server.data.ts +++ b/src/Umbraco.Web.UI.Client/src/backoffice/documents/documents/repository/sources/document.detail.server.data.ts @@ -2,7 +2,7 @@ import { RepositoryDetailDataSource } from '@umbraco-cms/repository'; import { ProblemDetails } from '@umbraco-cms/backend-api'; import { UmbControllerHostInterface } from '@umbraco-cms/controller'; import { tryExecuteAndNotify } from '@umbraco-cms/resources'; -import type { DocumentDetails } from '@umbraco-cms/models'; +import type { DataSourceResponse, DocumentDetails } from '@umbraco-cms/models'; /** * A data source for the Template detail that fetches data from the server @@ -94,10 +94,10 @@ export class UmbDocumentDetailServerDataSource implements RepositoryDetailDataSo * @return {*} * @memberof UmbDocumentDetailServerDataSource */ - insert(document: DocumentDetails) { + async insert(document: DocumentDetails) { if (!document.key) { - const error: ProblemDetails = { title: 'Document key is missing' }; - return { error }; + //const error: ProblemDetails = { title: 'Document key is missing' }; + return Promise.reject(); } //const payload = { key: document.key, requestBody: document }; @@ -110,7 +110,7 @@ export class UmbDocumentDetailServerDataSource implements RepositoryDetailDataSo return Promise.reject(); } //return tryExecuteAndNotify(this.#host, DocumentResource.postDocument(payload)); - return tryExecuteAndNotify(this.#host, + return tryExecuteAndNotify(this.#host, fetch('/umbraco/management/api/v1/document/save', { method: 'POST', body: body, @@ -118,7 +118,7 @@ export class UmbDocumentDetailServerDataSource implements RepositoryDetailDataSo 'Content-Type': 'application/json', }, } - ) + ) as any ); } @@ -145,7 +145,7 @@ export class UmbDocumentDetailServerDataSource implements RepositoryDetailDataSo return { error: myError }; } - return tryExecuteAndNotify(this.#host, + return tryExecuteAndNotify(this.#host, fetch('/umbraco/management/api/v1/document/save', { method: 'POST', body: body, @@ -153,9 +153,31 @@ export class UmbDocumentDetailServerDataSource implements RepositoryDetailDataSo 'Content-Type': 'application/json', }, } - ) + ) as any ); + } + /** + * Trash a Document on the server + * @param {Document} Document + * @return {*} + * @memberof UmbDocumentDetailServerDataSource + */ + async trash(key: string) { + if (!key) { + const error: ProblemDetails = { title: 'Key is missing' }; + return { error }; + } + + return tryExecuteAndNotify(this.#host, + fetch('/umbraco/management/api/v1/document/trash', { + method: 'POST', + body: JSON.stringify([key]), + headers: { + 'Content-Type': 'application/json', + }, + }) as any + ); } /**