@fleetbase/ember-core 0.1.6 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/addon/services/crud.js +15 -7
- package/addon/services/filters.js +31 -11
- package/package.json +1 -1
package/addon/services/crud.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import Service from '@ember/service';
|
|
2
2
|
import { inject as service } from '@ember/service';
|
|
3
|
-
import { action } from '@ember/object';
|
|
3
|
+
import { action, get } from '@ember/object';
|
|
4
4
|
import { isArray } from '@ember/array';
|
|
5
5
|
import { dasherize } from '@ember/string';
|
|
6
6
|
import { later } from '@ember/runloop';
|
|
7
7
|
import { pluralize } from 'ember-inflector';
|
|
8
8
|
import { format as formatDate } from 'date-fns';
|
|
9
9
|
import getModelName from '../utils/get-model-name';
|
|
10
|
+
import getWithDefault from '../utils/get-with-default';
|
|
10
11
|
import humanize from '../utils/humanize';
|
|
11
12
|
import first from '../utils/first';
|
|
12
13
|
|
|
@@ -47,7 +48,7 @@ export default class CrudService extends Service {
|
|
|
47
48
|
* @void
|
|
48
49
|
*/
|
|
49
50
|
@action delete(model, options = {}) {
|
|
50
|
-
const modelName = getModelName(model, options
|
|
51
|
+
const modelName = getModelName(model, get(options, 'modelName'), { humanize: true, capitalizeWords: true });
|
|
51
52
|
|
|
52
53
|
this.modalsManager.confirm({
|
|
53
54
|
title: `Are you sure to delete this ${modelName}?`,
|
|
@@ -98,7 +99,7 @@ export default class CrudService extends Service {
|
|
|
98
99
|
}
|
|
99
100
|
|
|
100
101
|
const firstModel = first(selected);
|
|
101
|
-
const modelName = getModelName(firstModel, options
|
|
102
|
+
const modelName = getModelName(firstModel, get(options, 'modelName'), { humanize: true, capitalizeWords: true });
|
|
102
103
|
|
|
103
104
|
// make sure all are the same type
|
|
104
105
|
selected = selected.filter((m) => getModelName(m) === getModelName(firstModel));
|
|
@@ -126,9 +127,11 @@ export default class CrudService extends Service {
|
|
|
126
127
|
}
|
|
127
128
|
|
|
128
129
|
const firstModel = first(selected);
|
|
129
|
-
const modelName = getModelName(firstModel, options
|
|
130
|
+
const modelName = getModelName(firstModel, get(options, 'modelName'), { humanize: true, capitalizeWords: true });
|
|
130
131
|
const count = selected.length;
|
|
131
132
|
const actionMethod = (typeof options.actionMethod === 'string' ? options.actionMethod : `POST`).toLowerCase();
|
|
133
|
+
const fetchParams = getWithDefault(options, 'fetchParams', {});
|
|
134
|
+
const fetchOptions = getWithDefault(options, 'fetchOptions', {});
|
|
132
135
|
|
|
133
136
|
this.modalsManager.show('modals/bulk-action-model', {
|
|
134
137
|
title: `Bulk ${verb} ${pluralize(modelName)}`,
|
|
@@ -152,9 +155,14 @@ export default class CrudService extends Service {
|
|
|
152
155
|
|
|
153
156
|
modal.startLoading();
|
|
154
157
|
|
|
155
|
-
return this.fetch[actionMethod](
|
|
156
|
-
|
|
157
|
-
|
|
158
|
+
return this.fetch[actionMethod](
|
|
159
|
+
options.actionPath,
|
|
160
|
+
{
|
|
161
|
+
ids: selected.map((model) => model.id),
|
|
162
|
+
...fetchParams,
|
|
163
|
+
},
|
|
164
|
+
fetchOptions
|
|
165
|
+
)
|
|
158
166
|
.then((response) => {
|
|
159
167
|
this.notifications.success(response.message ?? options.successNotification ?? `${count} ${pluralize(modelName, count)} were updated successfully.`);
|
|
160
168
|
|
|
@@ -6,6 +6,7 @@ import { isBlank } from '@ember/utils';
|
|
|
6
6
|
import { computed, action, set, get } from '@ember/object';
|
|
7
7
|
import { getOwner } from '@ember/application';
|
|
8
8
|
import { format } from 'date-fns';
|
|
9
|
+
import getWithDefault from '../utils/get-with-default';
|
|
9
10
|
|
|
10
11
|
export default class FiltersService extends Service {
|
|
11
12
|
@service router;
|
|
@@ -74,7 +75,7 @@ export default class FiltersService extends Service {
|
|
|
74
75
|
}
|
|
75
76
|
|
|
76
77
|
@action apply(controller) {
|
|
77
|
-
const currentQueryParams = this.getQueryParams();
|
|
78
|
+
const currentQueryParams = this.getQueryParams(controller);
|
|
78
79
|
const updatableQueryParams = { ...currentQueryParams, ...this.pendingQueryParams };
|
|
79
80
|
|
|
80
81
|
for (let queryParam in updatableQueryParams) {
|
|
@@ -88,8 +89,10 @@ export default class FiltersService extends Service {
|
|
|
88
89
|
}
|
|
89
90
|
|
|
90
91
|
@action reset(controller) {
|
|
91
|
-
this.
|
|
92
|
-
|
|
92
|
+
const queryParams = this.getQueryParams(controller);
|
|
93
|
+
|
|
94
|
+
Object.keys(queryParams).forEach((queryParam) => {
|
|
95
|
+
this.removeFromController(controller, queryParam, undefined);
|
|
93
96
|
});
|
|
94
97
|
}
|
|
95
98
|
|
|
@@ -103,20 +106,18 @@ export default class FiltersService extends Service {
|
|
|
103
106
|
return this.clear(queryParam, callback);
|
|
104
107
|
}
|
|
105
108
|
|
|
106
|
-
if (isBlank(queryParam)) {
|
|
109
|
+
if (isBlank(queryParam) && Object.keys(currentQueryParams).length > 0) {
|
|
107
110
|
return Object.keys(currentQueryParams).forEach((qp) => this.clear(callback, qp));
|
|
108
111
|
}
|
|
109
112
|
|
|
110
|
-
if (isArray(queryParam)) {
|
|
113
|
+
if (isArray(queryParam) && !isBlank(queryParam)) {
|
|
111
114
|
return queryParam.forEach((qp) => this.clear(callback, qp));
|
|
112
115
|
}
|
|
113
116
|
|
|
114
|
-
if (typeof queryParam
|
|
115
|
-
|
|
117
|
+
if (typeof queryParam === 'string') {
|
|
118
|
+
set(this.pendingQueryParams, queryParam, undefined);
|
|
116
119
|
}
|
|
117
120
|
|
|
118
|
-
set(this.pendingQueryParams, queryParam, undefined);
|
|
119
|
-
|
|
120
121
|
if (typeof callback == 'function') {
|
|
121
122
|
callback(queryParam);
|
|
122
123
|
}
|
|
@@ -153,10 +154,29 @@ export default class FiltersService extends Service {
|
|
|
153
154
|
return currentRoute.queryParams;
|
|
154
155
|
}
|
|
155
156
|
|
|
156
|
-
@action getQueryParams() {
|
|
157
|
+
@action getQueryParams(controller) {
|
|
158
|
+
const queryParams = {};
|
|
159
|
+
|
|
160
|
+
if (controller) {
|
|
161
|
+
const controllerQueryParams = getWithDefault(controller, 'queryParams', []);
|
|
162
|
+
|
|
163
|
+
if (isArray(controllerQueryParams)) {
|
|
164
|
+
for (let i = 0; i < controllerQueryParams.length; i++) {
|
|
165
|
+
const qp = controllerQueryParams.objectAt(i);
|
|
166
|
+
|
|
167
|
+
if (this.managedQueryParams.includes(qp)) {
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
queryParams[qp] = get(controller, qp);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return queryParams;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
157
178
|
const currentRoute = this.lookupCurrentRoute();
|
|
158
179
|
const currentRouteQueryParams = Object.keys(currentRoute.queryParams);
|
|
159
|
-
const queryParams = {};
|
|
160
180
|
|
|
161
181
|
for (let i = 0; i < currentRouteQueryParams.length; i++) {
|
|
162
182
|
const queryParam = currentRouteQueryParams.objectAt(i);
|
package/package.json
CHANGED