Skip to content
This repository was archived by the owner on Jul 28, 2025. It is now read-only.

Commit 2774f65

Browse files
committed
datadispatch
1 parent 9c49882 commit 2774f65

File tree

5 files changed

+31
-8
lines changed

5 files changed

+31
-8
lines changed

src/cdm/DatabaseModel.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,8 @@ export type CalendarProps = {
4747
column: TableColumn;
4848
cellProperties: Cell;
4949
};
50+
51+
export type SortedType = {
52+
id: string;
53+
desc: boolean;
54+
}

src/components/HeaderMenu.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ const HeaderMenu = (headerMenuProps: HeaderMenuProps) => {
8989
{
9090
onClick: (e: any) => {
9191
const sortArray = generateSortedColumns(initialState, column, false);
92+
// Update state
93+
dispatch({
94+
type: ActionTypes.SET_SORT_BY,
95+
sortArray: sortArray,
96+
});
9297
setSortBy(sortArray);
9398
setExpanded(false);
9499
},

src/components/behavior/SortingColumns.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
1+
import { SortedType } from "cdm/DatabaseModel";
12
import { TableColumn, TableDataType } from "cdm/FolderModel";
23
import { LOGGER } from "services/Logger";
34

4-
type SortedType = {
5-
id: string;
6-
desc: boolean;
7-
}
85
export function generateSortedColumns(tableDataType: TableDataType, currentCol: TableColumn, isSortedDesc: boolean): SortedType[] {
96
LOGGER.debug(`=>generateSortedColumns currentCol ${currentCol.id} isSortedDesc ${isSortedDesc}`);
107
const sortArray: SortedType[] = [];
118
tableDataType.columns
129
// Filter if col is already sorted or is current col
1310
.filter(col => col.id === currentCol.id || col.isSorted)
14-
.map((col) => {
11+
.forEach((col) => {
1512
if (currentCol.id === col.id) {
1613
// When is current col
1714
if (currentCol.isSorted && currentCol.isSortedDesc === isSortedDesc) {
1815
// If sort direction is the same, remove sort
19-
col.isSorted = false;
2016
tableDataType.view.diskConfig.updateColumnProperties(
2117
currentCol.id,
2218
{
@@ -25,7 +21,6 @@ export function generateSortedColumns(tableDataType: TableDataType, currentCol:
2521
);
2622
} else {
2723
// If sort direction is different or not sorted, set sort
28-
col.isSorted = true;
2924
sortArray.push({ id: currentCol.id, desc: isSortedDesc });
3025
tableDataType.view.diskConfig.updateColumnProperties(
3126
currentCol.id,
@@ -40,7 +35,6 @@ export function generateSortedColumns(tableDataType: TableDataType, currentCol:
4035
// When is not current col
4136
sortArray.push({ id: col.id, desc: col.isSortedDesc });
4237
}
43-
return col;
4438
});
4539
LOGGER.debug(`<=generateSortedColumns`, `sortArray ${JSON.stringify(sortArray)}`);
4640
return sortArray;

src/components/reducers/DatabaseDispatch.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
DatabaseColumn,
1818
OptionSelect,
1919
RowDatabaseFields,
20+
SortedType,
2021
} from "cdm/DatabaseModel";
2122
import NoteInfo from "services/NoteInfo";
2223
import { DataviewService } from "services/DataviewService";
@@ -427,6 +428,23 @@ export function databaseReducer(state: TableDataType, action: ActionType) {
427428
case ActionTypes.ENABLE_RESET:
428429
return update(state, { skipReset: { $set: false } });
429430

431+
case ActionTypes.SET_SORT_BY:
432+
const sortArray: SortedType[] = action.sortArray;
433+
const modifiedColumns: TableColumn[] = state.columns.map((column) => {
434+
const sortedColumn = sortArray.find((sort) => sort.id === column.id);
435+
if (sortedColumn) {
436+
column.isSorted = true;
437+
column.isSortedDesc = sortedColumn.desc;
438+
}
439+
return column;
440+
});
441+
442+
return update(state, {
443+
columns: {
444+
$set: modifiedColumns,
445+
},
446+
});
447+
430448
// case ActionTypes.MODIFY_COLUMN_SORTING:
431449
case ActionTypes.MODIFY_COLUMN_CONFIG:
432450
// Altern between inline & frontmatter mode

src/helpers/Constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export const ActionTypes = Object.freeze({
1414
ENABLE_RESET: 'enable_reset',
1515
SETTINGS_COLUMN: 'settings_column',
1616
MODIFY_COLUMN_CONFIG: 'modify_column_config',
17+
SET_SORT_BY: 'set_sort_by',
1718
});
1819

1920
/** Flavours of data types */

0 commit comments

Comments
 (0)