Fixed query params not resetting when deleting a custom view

no issue

- added a `transitionTo` after deleting a custom view that transitions to the `posts` route with default query params
- refactored `reset-posts-query` helper to a more generic `reset-query-params` helper
  - moved default query params definitions to this helper and expose them so we have a single source of truth
  - exposed `resetQueryParams()` function from the helper for use outside of templates
  - adjust the function and helper behaviour to accept the route name as the first param so that `router.currentRouteName` can be used as a generic reset
This commit is contained in:
Kevin Ansfield 2020-02-03 16:27:42 +00:00
parent 2c137f0b38
commit a3c71b8ba8
9 changed files with 35 additions and 31 deletions

View File

@ -1,9 +1,11 @@
import ModalComponent from 'ghost-admin/components/modal-base';
import {resetQueryParams} from 'ghost-admin/helpers/reset-query-params';
import {inject as service} from '@ember/service';
import {task, timeout} from 'ember-concurrency';
export default ModalComponent.extend({
customViews: service(),
router: service(),
delayedModelColor: '',
@ -35,7 +37,9 @@ export default ModalComponent.extend({
deleteTask: task(function* () {
let view = yield this.customViews.deleteViewTask.perform(this.model);
let routeName = this.router.currentRouteName;
this.send('closeModal');
this.router.transitionTo(routeName, {queryParams: resetQueryParams(routeName)});
return view;
}),

View File

@ -1,4 +1,5 @@
import Controller from '@ember/controller';
import {DEFAULT_QUERY_PARAMS} from 'ghost-admin/helpers/reset-query-params';
import {alias} from '@ember/object/computed';
import {computed} from '@ember/object';
import {get} from '@ember/object';
@ -36,13 +37,9 @@ export default Controller.extend({
store: service(),
// default values for these are set in `init` and defined in `helpers/reset-query-params`
queryParams: ['type', 'author', 'tag', 'order'],
type: null,
author: null,
tag: null,
order: null,
_hasLoadedTags: false,
_hasLoadedAuthors: false,
@ -53,6 +50,7 @@ export default Controller.extend({
this._super(...arguments);
this.availableTypes = TYPES;
this.availableOrders = ORDERS;
this.setProperties(DEFAULT_QUERY_PARAMS.posts);
},
postsInfinityModel: alias('model'),

View File

@ -1,20 +0,0 @@
import {helper} from '@ember/component/helper';
// in order to reset query params to their defaults when using <LinkTo> it's
// necessary to explicitly set each param. This helper makes it easier to
// provide a "resetting" link, especially when used with custom views
export default helper(function resetPostsQuery(params/*, hash*/) {
let resetQuery = {
type: null,
author: null,
tag: null,
order: null
};
if (params[0]) {
Object.assign(resetQuery, params[0]);
}
return resetQuery;
});

View File

@ -0,0 +1,22 @@
import {helper} from '@ember/component/helper';
export const DEFAULT_QUERY_PARAMS = {
posts: {
type: null,
author: null,
tag: null,
order: null
}
};
// in order to reset query params to their defaults when using <LinkTo> or
// `transitionTo` it's necessary to explicitly set each param. This helper makes
// it easier to provide a "resetting" link, especially when used with custom views
export function resetQueryParams(routeName, newParams) {
return Object.assign({}, DEFAULT_QUERY_PARAMS[routeName], newParams);
}
export default helper(function (params/*, hash*/) {
return resetQueryParams(...params);
});

View File

@ -31,7 +31,7 @@
<ul class="gh-nav-list gh-nav-manage">
<li class="gh-nav-list-h">Manage</li>
<li class="gh-nav-list-new relative">
<GhLinkToCustomViewsIndex @route="posts" @query={{reset-posts-query}} data-test-nav="posts">{{svg-jar "posts"}}Posts</GhLinkToCustomViewsIndex>
<GhLinkToCustomViewsIndex @route="posts" @query={{reset-query-params "posts"}} data-test-nav="posts">{{svg-jar "posts"}}Posts</GhLinkToCustomViewsIndex>
<LinkTo @route="editor.new" @model="post" @classNames="gh-secondary-action gh-nav-new-post" @alt="New post" @title="New post" data-test-nav="new-story"><span>{{svg-jar "add-stroke"}}</span></LinkTo>
{{#if this.customViews.forPosts}}
<button type="button" class="absolute left-3 top-2 z-9999 flex items-align pl2 h4 gh-nav-button-expand {{if this.navigation.settings.expanded.posts "expanded"}}" {{on "click" (fn this.navigation.toggleExpansion "posts")}}>
@ -41,7 +41,7 @@
<ul class="gh-nav-view-list">
{{#each this.customViews.forPosts as |view|}}
<li>
<LinkTo @route="posts" @query={{reset-posts-query view.filter}} data-test-nav-custom="{{view.route}}-{{view.name}}" title="{{view.name}}">
<LinkTo @route="posts" @query={{reset-query-params "posts" view.filter}} data-test-nav-custom="{{view.route}}-{{view.name}}" title="{{view.name}}">
{{!-- <div class="dib w4 h4 mr2 bg-{{view.color}} br-100"></div> --}}
<span class="flex items-center svg-{{view.color}}">
{{#if view.icon}}

View File

@ -1,6 +1,6 @@
<section class="gh-canvas" {{did-insert (action "setMainClass" "gh-main-primarybg" target=this.ui)}}>
<header class="gh-canvas-header post-header">
<GhCustomViewTitle @title="Pages" @query={{reset-posts-query}} />
<GhCustomViewTitle @title="Pages" @query={{reset-query-params "posts"}} />
<section class="view-actions">
<GhContentfilter
@selectedType={{this.selectedType}}

View File

@ -1,6 +1,6 @@
<section class="gh-canvas">
<GhCanvasHeader class="gh-canvas-header post-header">
<GhCustomViewTitle @title="Pages" @query={{reset-posts-query}} />
<GhCustomViewTitle @title="Pages" @query={{reset-query-params "posts"}} />
<section class="view-actions">
<GhContentfilter

View File

@ -1,6 +1,6 @@
<section class="gh-canvas" {{did-insert (action "setMainClass" "gh-main-primarybg" target=this.ui)}}>
<header class="gh-canvas-header post-header">
<GhCustomViewTitle @title="Posts" @query={{reset-posts-query}} />
<GhCustomViewTitle @title="Posts" @query={{reset-query-params "posts"}} />
<section class="view-actions">
<GhContentfilter
@selectedType={{this.selectedType}}

View File

@ -1,6 +1,6 @@
<section class="gh-canvas">
<GhCanvasHeader class="gh-canvas-header post-header">
<GhCustomViewTitle @title="Posts" @query={{reset-posts-query}} />
<GhCustomViewTitle @title="Posts" @query={{reset-query-params "posts"}} />
<section class="view-actions">
<GhContentfilter