Skip to content

Commit

Permalink
Merge pull request #22 from fleetbase/allow-fetch-params-options-on-c…
Browse files Browse the repository at this point in the history
…rud-service-bulk-actions

added ability to provide fetch service params and options view the cr…
  • Loading branch information
roncodes authored Nov 16, 2023
2 parents aeda88f + 0684ca5 commit 4fee207
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions addon/services/crud.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
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';
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';

Expand Down Expand Up @@ -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, get(options, 'modelName'), { humanize: true, capitalizeWords: true });

this.modalsManager.confirm({
title: `Are you sure to delete this ${modelName}?`,
Expand Down Expand Up @@ -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, get(options, 'modelName'), { humanize: true, capitalizeWords: true });

// make sure all are the same type
selected = selected.filter((m) => getModelName(m) === getModelName(firstModel));
Expand Down Expand Up @@ -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, 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', {});
const fetchOptions = getWithDefault(options, 'fetchOptions', {});

this.modalsManager.show('modals/bulk-action-model', {
title: `Bulk ${verb} ${pluralize(modelName)}`,
Expand All @@ -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.`);

Expand Down

0 comments on commit 4fee207

Please sign in to comment.