From d8ad2e409e7da8c27a0601d576afb8c5bc029216 Mon Sep 17 00:00:00 2001 From: Mads Rasmussen Date: Thu, 18 Apr 2024 13:13:46 +0200 Subject: [PATCH] set default filter trough constructor --- .../default/collection-default.context.ts | 5 +- .../collection/user-collection.context.ts | 104 +++++++++--------- 2 files changed, 57 insertions(+), 52 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/packages/core/collection/default/collection-default.context.ts b/src/Umbraco.Web.UI.Client/src/packages/core/collection/default/collection-default.context.ts index df2ec4e533..11247ffae2 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/core/collection/default/collection-default.context.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/core/collection/default/collection-default.context.ts @@ -51,6 +51,7 @@ export class UmbDefaultCollectionContext< public readonly view = new UmbCollectionViewManager(this); #defaultViewAlias: string; + #defaultFilter: Partial; #initResolver?: () => void; #initialized = false; @@ -59,10 +60,11 @@ export class UmbDefaultCollectionContext< this.#initialized ? resolve() : (this.#initResolver = resolve); }); - constructor(host: UmbControllerHost, defaultViewAlias: string) { + constructor(host: UmbControllerHost, defaultViewAlias: string, defaultFilter: Partial = {}) { super(host, UMB_DEFAULT_COLLECTION_CONTEXT); this.#defaultViewAlias = defaultViewAlias; + this.#defaultFilter = defaultFilter; this.pagination.addEventListener(UmbChangeEvent.TYPE, this.#onPageChange); } @@ -79,6 +81,7 @@ export class UmbDefaultCollectionContext< } this.#filter.setValue({ + ...this.#defaultFilter, ...this.#config, ...this.#filter.getValue(), skip: 0, diff --git a/src/Umbraco.Web.UI.Client/src/packages/user/user/collection/user-collection.context.ts b/src/Umbraco.Web.UI.Client/src/packages/user/user/collection/user-collection.context.ts index 3b454ca29b..7246c8ee7d 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/user/user/collection/user-collection.context.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/user/user/collection/user-collection.context.ts @@ -7,65 +7,69 @@ import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api'; import { UmbDirectionModel } from '@umbraco-cms/backoffice/models'; import { UmbArrayState, UmbStringState } from '@umbraco-cms/backoffice/observable-api'; +const orderByOptions: Array = [ + { + unique: 'nameAscending', + label: '#user_sortNameAscending', + config: { + orderBy: UmbUserOrderByModel.NAME, + orderDirection: UmbDirectionModel.ASCENDING, + }, + }, + { + unique: 'nameDescending', + label: '#user_sortNameDescending', + config: { + orderBy: UmbUserOrderByModel.NAME, + orderDirection: UmbDirectionModel.DESCENDING, + }, + }, + { + unique: 'createDateDescending', + label: '#user_sortCreateDateDescending', + config: { + orderBy: UmbUserOrderByModel.CREATE_DATE, + orderDirection: UmbDirectionModel.DESCENDING, + }, + }, + { + unique: 'createDateAscending', + label: '#user_sortCreateDateAscending', + config: { + orderBy: UmbUserOrderByModel.CREATE_DATE, + orderDirection: UmbDirectionModel.ASCENDING, + }, + }, + { + unique: 'lastLoginDateDescending', + label: '#user_sortLastLoginDateDescending', + config: { + orderBy: UmbUserOrderByModel.LAST_LOGIN_DATE, + orderDirection: UmbDirectionModel.DESCENDING, + }, + }, +]; + export class UmbUserCollectionContext extends UmbDefaultCollectionContext< UmbUserDetailModel, UmbUserCollectionFilterModel > { - #orderByOptions = new UmbArrayState( - [ - { - unique: 'nameAscending', - label: '#user_sortNameAscending', - config: { - orderBy: UmbUserOrderByModel.NAME, - orderDirection: UmbDirectionModel.ASCENDING, - }, - }, - { - unique: 'nameDescending', - label: '#user_sortNameDescending', - config: { - orderBy: UmbUserOrderByModel.NAME, - orderDirection: UmbDirectionModel.DESCENDING, - }, - }, - { - unique: 'createDateDescending', - label: '#user_sortCreateDateDescending', - config: { - orderBy: UmbUserOrderByModel.CREATE_DATE, - orderDirection: UmbDirectionModel.DESCENDING, - }, - }, - { - unique: 'createDateAscending', - label: '#user_sortCreateDateAscending', - config: { - orderBy: UmbUserOrderByModel.CREATE_DATE, - orderDirection: UmbDirectionModel.ASCENDING, - }, - }, - { - unique: 'lastLoginDateDescending', - label: '#user_sortLastLoginDateDescending', - config: { - orderBy: UmbUserOrderByModel.LAST_LOGIN_DATE, - orderDirection: UmbDirectionModel.DESCENDING, - }, - }, - ], - (x) => x.label, - ); + #orderByOptions = new UmbArrayState([], (x) => x.label); orderByOptions = this.#orderByOptions.asObservable(); #activeOrderByOption = new UmbStringState(undefined); activeOrderByOption = this.#activeOrderByOption.asObservable(); constructor(host: UmbControllerHost) { - super(host, UMB_COLLECTION_VIEW_USER_GRID); - // init default orderBy option - const defaultOrderByOption = this.#orderByOptions.getValue()[0]; - this.setActiveOrderByOption(defaultOrderByOption.unique); + const firstOption: UmbUserOrderByOption = orderByOptions[0]; + + super(host, UMB_COLLECTION_VIEW_USER_GRID, { + orderBy: firstOption.config.orderBy, + orderDirection: firstOption.config.orderDirection, + }); + + this.#orderByOptions.setValue(orderByOptions); + this.#activeOrderByOption.setValue(firstOption.unique); } /** @@ -80,8 +84,6 @@ export class UmbUserCollectionContext extends UmbDefaultCollectionContext< this.setFilter({ orderBy: option?.config.orderBy, orderDirection: option?.config.orderDirection }); } - getActiveOrderByOption() {} - /** * Sets the state filter for the collection and refreshes the collection. * @param {Array} selection