From e090ae1d8c764c468b47e522b0b7506cd3b8a930 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roger=20Boixader=20G=C3=BCell?= Date: Mon, 11 Mar 2024 10:59:44 +0100 Subject: [PATCH] feat: Add default sort value --- CHANGELOG.md | 4 ++++ package.json | 2 +- src/guillo-gmi/components/panel/items.tsx | 13 ++++++++++++- src/guillo-gmi/hooks/useRegistry.tsx | 12 ++++++++++++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 72fcc46..3a0f953 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +0.29.0 +------ +- feat: Add default sort value + 0.28.4 ------ - chore: version diff --git a/package.json b/package.json index b9b864f..8068f0e 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "0.28.4", + "version": "0.29.0", "repository": { "type": "git", "url": "git@github.com:guillotinaweb/guillotina_react.git" diff --git a/src/guillo-gmi/components/panel/items.tsx b/src/guillo-gmi/components/panel/items.tsx index 52bd7a3..9d72096 100644 --- a/src/guillo-gmi/components/panel/items.tsx +++ b/src/guillo-gmi/components/panel/items.tsx @@ -118,7 +118,18 @@ export function PanelItems() { setState({ loading: true, total: Ctx.context.length }) const { get } = Ctx.registry const fnName = get('searchEngineQueryParamsFunction', SearchEngine) - + if (sortParsed === undefined) { + const defaultSortValue = Ctx.registry.getDefaultSortValue( + Ctx.context['@type'], + { + key: 'id', + direction: 'des', + } + ) + sortParsed = parser( + `_sort_${defaultSortValue.direction}=${defaultSortValue.key}}` + ) + } const qsParsed = Ctx.client[fnName]({ path: Ctx.path, start: page * PageSize, diff --git a/src/guillo-gmi/hooks/useRegistry.tsx b/src/guillo-gmi/hooks/useRegistry.tsx index 1a0b305..f3c924b 100644 --- a/src/guillo-gmi/hooks/useRegistry.tsx +++ b/src/guillo-gmi/hooks/useRegistry.tsx @@ -72,6 +72,12 @@ export interface IRegistry { fieldsToFilter: { [key: string]: string[] } + defaultSortValue: { + [key: string]: { + direction: 'asc' | 'des' + field: string + } + } } const registry: IRegistry = { paths: {}, @@ -131,6 +137,7 @@ const registry: IRegistry = { fieldsToFilter: { UserManager: ['id', 'email', 'user_name'], }, + defaultSortValue: {}, } const get = (key, param, fallback = undefined) => { @@ -198,6 +205,10 @@ const getFieldsToFilter = (type: string, fallback) => { return registry.fieldsToFilter[type] || fallback } +const getDefaultSortValue = (type: string, fallback) => { + return registry.defaultSortValue[type] || fallback +} + export const defaultComponent = (context) => { return context.is_folderish ? FolderCtx : ItemCtx } @@ -222,6 +233,7 @@ export function useRegistry(data) { getProperties, getItemsColumn, getFieldsToFilter, + getDefaultSortValue, getSchemas, } }