Skip to content

Commit

Permalink
feat: Add default sort value (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
rboixaderg authored Mar 11, 2024
1 parent 1c432a8 commit 3f1adde
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
0.29.0
------
- feat: Add default sort value

0.28.4
------
- chore: version
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.28.4",
"version": "0.29.0",
"repository": {
"type": "git",
"url": "[email protected]:guillotinaweb/guillotina_react.git"
Expand Down
13 changes: 12 additions & 1 deletion src/guillo-gmi/components/panel/items.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions src/guillo-gmi/hooks/useRegistry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ export interface IRegistry {
fieldsToFilter: {
[key: string]: string[]
}
defaultSortValue: {
[key: string]: {
direction: 'asc' | 'des'
field: string
}
}
}
const registry: IRegistry = {
paths: {},
Expand Down Expand Up @@ -131,6 +137,7 @@ const registry: IRegistry = {
fieldsToFilter: {
UserManager: ['id', 'email', 'user_name'],
},
defaultSortValue: {},
}

const get = (key, param, fallback = undefined) => {
Expand Down Expand Up @@ -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
}
Expand All @@ -222,6 +233,7 @@ export function useRegistry(data) {
getProperties,
getItemsColumn,
getFieldsToFilter,
getDefaultSortValue,
getSchemas,
}
}
Expand Down

0 comments on commit 3f1adde

Please sign in to comment.