Skip to content

Commit

Permalink
Merge pull request #21 from fleetbase/hotfix-filters-clear-fn
Browse files Browse the repository at this point in the history
hotfix the `clear()` method for the `FiltersService`
  • Loading branch information
roncodes authored Nov 9, 2023
2 parents f17cab2 + 64d6cca commit 85a8145
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
42 changes: 31 additions & 11 deletions addon/services/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { isBlank } from '@ember/utils';
import { computed, action, set, get } from '@ember/object';
import { getOwner } from '@ember/application';
import { format } from 'date-fns';
import getWithDefault from '../utils/get-with-default';

export default class FiltersService extends Service {
@service router;
Expand Down Expand Up @@ -74,7 +75,7 @@ export default class FiltersService extends Service {
}

@action apply(controller) {
const currentQueryParams = this.getQueryParams();
const currentQueryParams = this.getQueryParams(controller);
const updatableQueryParams = { ...currentQueryParams, ...this.pendingQueryParams };

for (let queryParam in updatableQueryParams) {
Expand All @@ -88,8 +89,10 @@ export default class FiltersService extends Service {
}

@action reset(controller) {
this.clear((queryParam) => {
set(controller, queryParam, undefined);
const queryParams = this.getQueryParams(controller);

Object.keys(queryParams).forEach((queryParam) => {
this.removeFromController(controller, queryParam, undefined);
});
}

Expand All @@ -103,20 +106,18 @@ export default class FiltersService extends Service {
return this.clear(queryParam, callback);
}

if (isBlank(queryParam)) {
if (isBlank(queryParam) && Object.keys(currentQueryParams).length > 0) {
return Object.keys(currentQueryParams).forEach((qp) => this.clear(callback, qp));
}

if (isArray(queryParam)) {
if (isArray(queryParam) && !isBlank(queryParam)) {
return queryParam.forEach((qp) => this.clear(callback, qp));
}

if (typeof queryParam !== 'string') {
return;
if (typeof queryParam === 'string') {
set(this.pendingQueryParams, queryParam, undefined);
}

set(this.pendingQueryParams, queryParam, undefined);

if (typeof callback == 'function') {
callback(queryParam);
}
Expand Down Expand Up @@ -153,10 +154,29 @@ export default class FiltersService extends Service {
return currentRoute.queryParams;
}

@action getQueryParams() {
@action getQueryParams(controller) {
const queryParams = {};

if (controller) {
const controllerQueryParams = getWithDefault(controller, 'queryParams', []);

if (isArray(controllerQueryParams)) {
for (let i = 0; i < controllerQueryParams.length; i++) {
const qp = controllerQueryParams.objectAt(i);

if (this.managedQueryParams.includes(qp)) {
continue;
}

queryParams[qp] = get(controller, qp);
}

return queryParams;
}
}

const currentRoute = this.lookupCurrentRoute();
const currentRouteQueryParams = Object.keys(currentRoute.queryParams);
const queryParams = {};

for (let i = 0; i < currentRouteQueryParams.length; i++) {
const queryParam = currentRouteQueryParams.objectAt(i);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fleetbase/ember-core",
"version": "0.1.6",
"version": "0.1.7",
"description": "Provides all the core services, decorators and utilities for building a Fleetbase extension for the Console.",
"keywords": [
"fleetbase-core",
Expand Down

0 comments on commit 85a8145

Please sign in to comment.