From 9eb8dbb789c0858cd4afc5ab2f0cb0b812bcde3d Mon Sep 17 00:00:00 2001 From: "Ronald A. Richardson" Date: Wed, 15 Nov 2023 17:51:16 +0800 Subject: [PATCH 1/2] added ability to provide fetch service params and options view the crud bulkAction method --- addon/services/crud.js | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/addon/services/crud.js b/addon/services/crud.js index 3a4c2b4..c99b70f 100644 --- a/addon/services/crud.js +++ b/addon/services/crud.js @@ -7,6 +7,7 @@ import { later } from '@ember/runloop'; import { pluralize } from 'ember-inflector'; import { format as formatDate } from 'date-fns'; import getModelName from '../utils/get-model-name'; +import getWithDefault from '../utils/get-with-default'; import humanize from '../utils/humanize'; import first from '../utils/first'; @@ -47,7 +48,7 @@ export default class CrudService extends Service { * @void */ @action delete(model, options = {}) { - const modelName = getModelName(model, options?.modelName, { humanize: true, capitalizeWords: true }); + const modelName = getModelName(model, getWithDefault(options, 'modelName'), { humanize: true, capitalizeWords: true }); this.modalsManager.confirm({ title: `Are you sure to delete this ${modelName}?`, @@ -98,7 +99,7 @@ export default class CrudService extends Service { } const firstModel = first(selected); - const modelName = getModelName(firstModel, options?.modelName, { humanize: true, capitalizeWords: true }); + const modelName = getModelName(firstModel, getWithDefault(options, 'modelName'), { humanize: true, capitalizeWords: true }); // make sure all are the same type selected = selected.filter((m) => getModelName(m) === getModelName(firstModel)); @@ -126,9 +127,11 @@ export default class CrudService extends Service { } const firstModel = first(selected); - const modelName = getModelName(firstModel, options?.modelName, { humanize: true, capitalizeWords: true }); + const modelName = getModelName(firstModel, getWithDefault(options, 'modelName'), { humanize: true, capitalizeWords: true }); const count = selected.length; const actionMethod = (typeof options.actionMethod === 'string' ? options.actionMethod : `POST`).toLowerCase(); + const fetchParams = getWithDefault(options, 'fetchParams', {}); + const fetchOptions = getWithDefault(options, 'fetchOptions', {}); this.modalsManager.show('modals/bulk-action-model', { title: `Bulk ${verb} ${pluralize(modelName)}`, @@ -152,9 +155,14 @@ export default class CrudService extends Service { modal.startLoading(); - return this.fetch[actionMethod](options.actionPath, { - ids: selected.map((model) => model.id), - }) + return this.fetch[actionMethod]( + options.actionPath, + { + ids: selected.map((model) => model.id), + ...fetchParams, + }, + fetchOptions + ) .then((response) => { this.notifications.success(response.message ?? options.successNotification ?? `${count} ${pluralize(modelName, count)} were updated successfully.`); From 0684ca5ce0d41a8c515dfd5c5c1af22419310972 Mon Sep 17 00:00:00 2001 From: "Ronald A. Richardson" Date: Wed, 15 Nov 2023 17:53:54 +0800 Subject: [PATCH 2/2] use `get()` for getting modelName from options --- addon/services/crud.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/addon/services/crud.js b/addon/services/crud.js index c99b70f..9f08f91 100644 --- a/addon/services/crud.js +++ b/addon/services/crud.js @@ -1,6 +1,6 @@ import Service from '@ember/service'; import { inject as service } from '@ember/service'; -import { action } from '@ember/object'; +import { action, get } from '@ember/object'; import { isArray } from '@ember/array'; import { dasherize } from '@ember/string'; import { later } from '@ember/runloop'; @@ -48,7 +48,7 @@ export default class CrudService extends Service { * @void */ @action delete(model, options = {}) { - const modelName = getModelName(model, getWithDefault(options, 'modelName'), { humanize: true, capitalizeWords: true }); + const modelName = getModelName(model, get(options, 'modelName'), { humanize: true, capitalizeWords: true }); this.modalsManager.confirm({ title: `Are you sure to delete this ${modelName}?`, @@ -99,7 +99,7 @@ export default class CrudService extends Service { } const firstModel = first(selected); - const modelName = getModelName(firstModel, getWithDefault(options, 'modelName'), { humanize: true, capitalizeWords: true }); + const modelName = getModelName(firstModel, get(options, 'modelName'), { humanize: true, capitalizeWords: true }); // make sure all are the same type selected = selected.filter((m) => getModelName(m) === getModelName(firstModel)); @@ -127,7 +127,7 @@ export default class CrudService extends Service { } const firstModel = first(selected); - const modelName = getModelName(firstModel, getWithDefault(options, 'modelName'), { humanize: true, capitalizeWords: true }); + const modelName = getModelName(firstModel, get(options, 'modelName'), { humanize: true, capitalizeWords: true }); const count = selected.length; const actionMethod = (typeof options.actionMethod === 'string' ? options.actionMethod : `POST`).toLowerCase(); const fetchParams = getWithDefault(options, 'fetchParams', {});