From 6915056964dacb1580d725d0fa0cd7743a12e3a9 Mon Sep 17 00:00:00 2001 From: Wojciech Date: Thu, 1 Oct 2020 08:12:50 +0200 Subject: [PATCH] feat: add new shortcut methods to ViewHelpers relates to #294 --- spec/backend/helpers/helper-stub.ts | 12 ++++ .../utils/view-helpers/view-helpers.ts | 68 ++++++++++++++++++- 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/spec/backend/helpers/helper-stub.ts b/spec/backend/helpers/helper-stub.ts index 820ce697b..4d1bd61c0 100644 --- a/spec/backend/helpers/helper-stub.ts +++ b/spec/backend/helpers/helper-stub.ts @@ -12,6 +12,12 @@ const expectedResult = { resourceUrl: 'resourceUrl', dashboardUrl: 'dashboardUrl', pageUrl: 'pageUrl', + editUrl: 'editUrl', + showUrl: 'showUrl', + deleteUrl: 'deleteUrl', + newUrl: 'newUrl', + listUrl: 'listUrl', + bulkDeleteUrl: 'bulkDeleteUrl', } export default (): ViewHelpers => ( @@ -31,6 +37,12 @@ export default (): ViewHelpers => ( resourceUrl: sinon.stub().returns(expectedResult.resourceUrl), dashboardUrl: sinon.stub().returns(expectedResult.dashboardUrl), pageUrl: sinon.stub().returns(expectedResult.pageUrl), + editUrl: sinon.stub().returns(expectedResult.editUrl), + showUrl: sinon.stub().returns(expectedResult.showUrl), + deleteUrl: sinon.stub().returns(expectedResult.deleteUrl), + newUrl: sinon.stub().returns(expectedResult.newUrl), + listUrl: sinon.stub().returns(expectedResult.listUrl), + bulkDeleteUrl: sinon.stub().returns(expectedResult.bulkDeleteUrl), } ) diff --git a/src/backend/utils/view-helpers/view-helpers.ts b/src/backend/utils/view-helpers/view-helpers.ts index db38bd25d..10e0a4cdf 100644 --- a/src/backend/utils/view-helpers/view-helpers.ts +++ b/src/backend/utils/view-helpers/view-helpers.ts @@ -141,12 +141,78 @@ export class ViewHelpers { return this.urlBuilder(['pages', pageName]) } + /** + * Returns url for a `edit` action in given Resource. Uses {@link recordActionUrl} + * + * @param {string} resourceId id to the resource + * @param {string} recordId id to the record + * @param {string} [search] optional query string + */ + editUrl(resourceId: string, recordId: string, search?: string): string { + return this.recordActionUrl({ resourceId, recordId, actionName: 'edit', search }) + } + + /** + * Returns url for a `show` action in given Resource. Uses {@link recordActionUrl} + * + * @param {string} resourceId id to the resource + * @param {string} recordId id to the record + * @param {string} [search] optional query string + */ + showUrl(resourceId: string, recordId: string, search?: string): string { + return this.recordActionUrl({ resourceId, recordId, actionName: 'show', search }) + } + + /** + * Returns url for a `delete` action in given Resource. Uses {@link recordActionUrl} + * + * @param {string} resourceId id to the resource + * @param {string} recordId id to the record + * @param {string} [search] optional query string + */ + deleteUrl(resourceId: string, recordId: string, search?: string): string { + return this.recordActionUrl({ resourceId, recordId, actionName: 'delete', search }) + } + + + /** + * Returns url for a `new` action in given Resource. Uses {@link resourceActionUrl} + * + * @param {string} resourceId id to the resource + * @param {string} [search] optional query string + */ + newUrl(resourceId: string, search?: string): string { + return this.resourceActionUrl({ resourceId, actionName: 'new', search }) + } + + /** + * Returns url for a `new` action in given Resource. Uses {@link resourceActionUrl} + * + * @param {string} resourceId id to the resource + * @param {string} [search] optional query string + */ + listUrl(resourceId: string, search?: string): string { + return this.resourceActionUrl({ resourceId, actionName: 'list', search }) + } + + /** + * Returns url for a `bulkDelete` action in given Resource. Uses {@link bulkActionUrl} + * + * @param {string} resourceId id to the resource + * @param {Array} recordIds separated by comma records + * @param {string} [search] optional query string + */ + bulkDeleteUrl(resourceId: string, recordIds: Array, search?: string): string { + return this.bulkActionUrl({ resourceId, recordIds, actionName: 'bulkDelete', search }) + } + /** * Returns resourceAction url * * @param {ResourceActionParams} options * @param {string} options.resourceId * @param {string} options.actionName + * @param {string} [options.search] optional query string * * @return {string} */ @@ -177,7 +243,7 @@ export class ViewHelpers { * * @param {BulkActionParams} options * @param {string} options.resourceId - * @param {string} [options.recordIds] + * @param {Array} [options.recordIds] * @param {string} options.actionName * * @return {string}