Skip to content

Commit 7c10177

Browse files
committed
save grid sorting
1 parent 108b646 commit 7c10177

File tree

15 files changed

+99
-23
lines changed

15 files changed

+99
-23
lines changed

apps/sensenet/src/components/MainRouter.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { LoadSettingsContextProvider } from '@sensenet/hooks-react'
22
import React, { lazy, Suspense } from 'react'
33
import { Route, Switch } from 'react-router-dom'
4+
import { GridKeyEnum } from '../../src/components/grid/enums/GridKey.enum'
45
import { PATHS } from '../application-paths'
56
import { InvalidPathErrorBoundary } from './content/InvalidPathErrorBoundary'
67
import { ErrorBoundary } from './error-boundary'
@@ -42,6 +43,7 @@ export const MainRouter = () => {
4243
showPageTitle
4344
rootPath={PATHS.contentTemplates.snPath}
4445
colDef={contentTemplatesColumnDefs}
46+
gridKey={GridKeyEnum.CONTENTTEMPLATES}
4547
/>
4648
</InvalidPathErrorBoundary>
4749
</Route>

apps/sensenet/src/components/content-list/contenttype-list.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Switch } from '@sensenet/controls-react'
22
import { useRepository } from '@sensenet/hooks-react'
33
import React, { lazy, useState } from 'react'
4+
import { GridKeyEnum } from '../../../src/components/grid/enums/GridKey.enum'
45
import { PATHS } from '../../application-paths'
56
import { contentTypesColumnDefs } from '../grid/Cols/ColumnDefs.'
67

@@ -41,15 +42,8 @@ const ContentTypeList: React.FC = () => {
4142
<ContentComponent
4243
renderBeforeGrid={renderBeforeGrid}
4344
colDef={contentTypesColumnDefs}
45+
gridKey={GridKeyEnum.CONTENTTYPES}
4446
rootPath={PATHS.contentTypes.snPath}
45-
fieldsToDisplay={[
46-
{ field: 'DisplayName' },
47-
{ field: 'Name' },
48-
{ field: 'Description' },
49-
{ field: 'ParentTypeName' as any },
50-
{ field: 'ModificationDate' },
51-
{ field: 'ModifiedBy' },
52-
]}
5347
loadChildrenSettings={{
5448
select: ['DisplayName', 'Name', 'Description', 'ParentTypeName' as any, 'ModificationDate', 'ModifiedBy'],
5549
query: contentTypeQuery,

apps/sensenet/src/components/content/CustomContent.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ConstantContent } from '@sensenet/client-core'
22
import React, { FunctionComponent, useContext, useEffect } from 'react'
33
import { useRouteMatch } from 'react-router-dom'
4+
import { GridKeyEnum } from '../../../src/components/grid/enums/GridKey.enum'
45
import { ResponsivePersonalSettings } from '../../context'
56
import { useTreeNavigation } from '../../hooks/use-tree-navigation'
67
import { contentExplorerColumnDefs } from '../grid/Cols/ColumnDefs.'
@@ -28,6 +29,7 @@ export const CustomContent: FunctionComponent<ContentProps> = ({ rootPath }) =>
2829
<Explore
2930
currentPath={currentPath}
3031
colDef={contentExplorerColumnDefs}
32+
gridKey={GridKeyEnum.CONTENT}
3133
onNavigate={onNavigate}
3234
rootPath={path}
3335
fieldsToDisplay={customDrawer?.settings.columns}

apps/sensenet/src/components/content/Explore.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ import { ColDef } from 'ag-grid-community'
1414
import { clsx } from 'clsx'
1515
import React, { useContext, useRef, useState } from 'react'
1616
import { useHistory } from 'react-router'
17+
import { GridKeyEnum } from '../../../src/components/grid/enums/GridKey.enum'
1718
import { ResponsivePersonalSettings } from '../../context'
1819
import { useGlobalStyles } from '../../globalStyles'
1920
import { useQuery, useSelectionService, useSnRoute } from '../../hooks'
2021
import { getPrimaryActionUrl, navigateToAction } from '../../services'
2122
import { ContentBreadcrumbs } from '../ContentBreadcrumbs'
2223
import { DocumentViewer } from '../document-viewer'
2324
import { EditBinary } from '../edit/edit-binary'
24-
import { contentColumnDefs } from '../grid/Cols/ColumnDefs.'
2525
import { Grid } from '../grid/Grid'
2626
import { SimpleTree } from '../tree/simpletree'
2727
import { BrowseView, EditView, ImageView, NewView, PermissionView, VersionView } from '../view-controls'
@@ -125,7 +125,8 @@ export type ExploreProps = {
125125
alwaysRefreshChildren?: boolean
126126
showPageTitle?: boolean
127127
disableColumnSettings?: boolean
128-
colDef?: ColDef[]
128+
colDef: ColDef[]
129+
gridKey: GridKeyEnum
129130
}
130131

131132
export function Explore({
@@ -141,6 +142,7 @@ export function Explore({
141142
alwaysRefreshChildren,
142143
disableColumnSettings,
143144
colDef,
145+
gridKey,
144146
}: ExploreProps) {
145147
const theme = useTheme()
146148
const selectionService = useSelectionService()
@@ -261,7 +263,8 @@ export function Explore({
261263
onActivateItem={onActivateItemOverride}
262264
onActiveItemChange={(item) => selectionService.activeContent.setValue(item)}
263265
parentIdOrPath={currentPath}
264-
colDef={colDef ?? contentColumnDefs}
266+
colDef={colDef}
267+
gridKey={gridKey}
265268
/>
266269
</>
267270
)

apps/sensenet/src/components/content/index.tsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import { ConstantContent } from '@sensenet/client-core'
22
import { tuple } from '@sensenet/client-utils'
33
import React, { FunctionComponent, useContext, useEffect } from 'react'
44
import { useRouteMatch } from 'react-router-dom'
5+
import { GridKeyEnum } from '../../../src/components/grid/enums/GridKey.enum'
56
import { ResponsivePersonalSettings } from '../../context'
67
import { useLocalization } from '../../hooks'
78
import { useTreeNavigation } from '../../hooks/use-tree-navigation'
9+
import { contentColumnDefs } from '../grid/Cols/ColumnDefs.'
810
import { PageTitle } from '../PageTitle'
911
import { useTreeLoading } from '../tree/Contexts/TreeLoadingProvider'
1012
import { Explore, ExploreProps } from './Explore'
@@ -13,24 +15,31 @@ export const BrowseType = tuple('explorer')
1315

1416
type ContentProps = Partial<ExploreProps>
1517

16-
export const Content: FunctionComponent<ContentProps> = ({ rootPath, showPageTitle, ...props }) => {
18+
export const Content: FunctionComponent<ContentProps> = (props) => {
1719
const routeMatch = useRouteMatch<{ browseType: string; action?: string }>()
1820
const settings = useContext(ResponsivePersonalSettings)
19-
const path = rootPath || settings.content.root || ConstantContent.PORTAL_ROOT.Path
21+
const path = props.rootPath || settings.content.root || ConstantContent.PORTAL_ROOT.Path
2022
const { currentPath, onNavigate } = useTreeNavigation(path)
2123
const localization = useLocalization().pageTitles
2224

2325
const { setEnabledPath } = useTreeLoading()
2426
useEffect(() => {
25-
setEnabledPath(rootPath ?? '')
26-
}, [rootPath, setEnabledPath])
27+
setEnabledPath(props.rootPath ?? '')
28+
}, [props.rootPath, setEnabledPath])
2729

2830
switch (routeMatch.params.browseType) {
2931
default:
3032
return (
3133
<>
32-
{showPageTitle ? <PageTitle title={localization[currentPath as keyof typeof localization]} /> : null}
33-
<Explore currentPath={currentPath} onNavigate={onNavigate} rootPath={path} {...props} />
34+
{props.showPageTitle ? <PageTitle title={localization[currentPath as keyof typeof localization]} /> : null}
35+
<Explore
36+
currentPath={currentPath}
37+
onNavigate={onNavigate}
38+
rootPath={path}
39+
{...props}
40+
colDef={props.colDef ?? contentColumnDefs}
41+
gridKey={props.gridKey ?? GridKeyEnum.OTHER}
42+
/>
3443
</>
3544
)
3645
}

apps/sensenet/src/components/grid/Cols/ColumnDefs..tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,6 @@ export const trashColumnDefs: ColDef[] = [
315315
filter: true,
316316
sortable: true,
317317
resizable: true,
318-
sort: 'desc',
319318
},
320319
{
321320
headerName: 'Actions',

apps/sensenet/src/components/grid/Grid.tsx

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import React, { useCallback, useContext, useEffect, useRef, useState } from 'rea
1515
import { useSelectionService } from '../../hooks'
1616
import { ContentContextMenu } from '../context-menu/content-context-menu'
1717
import { DropFileArea } from '../DropFileArea'
18-
import { contentColumnDefs } from './Cols/ColumnDefs.'
1918
import { GridProps } from './Props/GridProps'
2019
import { useGridLoading } from './Providers/GridLoadingProvider'
2120

@@ -44,7 +43,7 @@ export function Grid<T extends GenericContent = GenericContent>(props: GridProps
4443
const columnApi = useRef<ColumnApi | null>(null)
4544
const gridRef = useRef<HTMLDivElement>(null)
4645
const fixedColumns: string[] = ['0', 'Icon', 'Actions']
47-
const [columnDefs, setColumnDefs] = useState<ColDef[]>(props.colDef ?? contentColumnDefs)
46+
const [columnDefs, setColumnDefs] = useState<ColDef[]>(props.colDef)
4847
const loadingTimeoutRef = useRef<NodeJS.Timeout | null>(null)
4948

5049
const setLoadingWithMinDuration = useCallback(
@@ -136,11 +135,32 @@ export function Grid<T extends GenericContent = GenericContent>(props: GridProps
136135
}
137136
}
138137

138+
const restoreSortModel = () => {
139+
if (columnApi.current) {
140+
const sortModelString = localStorage.getItem(`sortModel-${props.gridKey}`)
141+
if (sortModelString) {
142+
const sortModel = JSON.parse(sortModelString)
143+
console.log('props.gridKey:', props.gridKey)
144+
console.log('sortModel:', sortModel)
145+
sortModel.forEach((s: { colId: string; sort: 'asc' | 'desc' }) => {
146+
const col = columnApi.current!.getColumnState().find((c) => c.colId === s.colId)
147+
if (col) {
148+
columnApi.current!.applyColumnState({
149+
state: [{ colId: s.colId, sort: s.sort }],
150+
applyOrder: false,
151+
})
152+
}
153+
})
154+
}
155+
}
156+
}
157+
139158
const onGridReady = (params: GridReadyEvent) => {
140159
gridApi.current = params.api
141160
columnApi.current = params.columnApi
142161
restoreColumnFlexRatios()
143-
setLoadingWithMinDuration(false) // Reset loading when grid is ready
162+
restoreSortModel()
163+
setLoadingWithMinDuration(false)
144164
}
145165

146166
useEffect(() => {
@@ -149,6 +169,19 @@ export function Grid<T extends GenericContent = GenericContent>(props: GridProps
149169
}
150170
}, [props.colDef])
151171

172+
const onSortChanged = useCallback(() => {
173+
if (columnApi.current) {
174+
const sortModel = columnApi.current
175+
.getColumnState()
176+
.filter((c) => c.sort)
177+
.map((c) => ({
178+
colId: c.colId,
179+
sort: c.sort,
180+
}))
181+
localStorage.setItem(`sortModel-${props.gridKey}`, JSON.stringify(sortModel))
182+
}
183+
}, [props.gridKey])
184+
152185
return (
153186
<DropFileArea parentContent={parentContent} style={{ height: '100%', overflow: 'hidden' }}>
154187
{isGridLoading && (
@@ -175,6 +208,7 @@ export function Grid<T extends GenericContent = GenericContent>(props: GridProps
175208
onSelectionChanged={onSelectionChanged}
176209
onCellContextMenu={(event) => onContextMenu(event)}
177210
onColumnResized={onColumnResized}
211+
onSortChanged={onSortChanged}
178212
suppressNoRowsOverlay={true}
179213
/>
180214
</div>

apps/sensenet/src/components/grid/Props/GridProps.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ export interface GridProps<T extends GenericContent> {
2121
containerProps?: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
2222
disableColumnSettings?: boolean
2323
colDef: ColDef[]
24+
gridKey: string
2425
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export enum GridKeyEnum {
2+
CONTENT = 'Content',
3+
USERS = 'Users',
4+
GROUPS = 'Groups',
5+
SAVEDQUERIES = 'SavedQueries',
6+
TRASH = 'Trash',
7+
CONTENTTYPES = 'ContentTypes',
8+
CONTENTTEMPLATES = 'ContentTemplates',
9+
SETTINGS = 'Settings',
10+
LOCALIZATION = 'Localization',
11+
WEBHOOKS = 'WebHooks',
12+
SEARCHRESULTS = 'SearchResults',
13+
OTHER = 'Other',
14+
}

apps/sensenet/src/components/search/saved-queries.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Query } from '@sensenet/default-content-types'
44
import { LoadSettingsContext, useRepository, useRepositoryEvents } from '@sensenet/hooks-react'
55
import React, { useContext, useEffect, useState } from 'react'
66
import { useHistory, useRouteMatch } from 'react-router-dom'
7+
import { GridKeyEnum } from '../../../src/components/grid/enums/GridKey.enum'
78
import { PATHS } from '../../application-paths'
89
import { useLocalization, useQuery, useSelectionService } from '../../hooks'
910
import { navigateToAction } from '../../services'
@@ -82,7 +83,11 @@ export default function SavedQueries() {
8283
return (
8384
<>
8485
{queries.length > 0 ? (
85-
<Content rootPath={PATHS.savedQueries.snPath} colDef={savedQueriesColumnDefs} />
86+
<Content
87+
rootPath={PATHS.savedQueries.snPath}
88+
colDef={savedQueriesColumnDefs}
89+
gridKey={GridKeyEnum.SAVEDQUERIES}
90+
/>
8691
) : (
8792
<Typography variant="subtitle1" style={{ padding: '0 15px' }}>
8893
{localization.noSavedQuery}

0 commit comments

Comments
 (0)