Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persistence of view column ordering. #1541

Merged
merged 10 commits into from
Oct 1, 2023
26 changes: 26 additions & 0 deletions src/features/views/components/ViewDataTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import {
} from 'utils/types/zetkin';

import messageIds from 'features/views/l10n/messageIds';
import useDebounce from 'utils/hooks/useDebounce';

declare module '@mui/x-data-grid-pro' {
interface ColumnMenuPropsOverrides {
Expand Down Expand Up @@ -292,6 +293,27 @@ const ViewDataTable: FunctionComponent<ViewDataTableProps> = ({
width: 50,
};

const debouncedUpdateColumnOrder = useDebounce((order: number[]) => {
return model.updateColumnOrder(order);
}, 1000);

const moveColumn = (field: string, targetIndex: number) => {
// The column index is offset by 2 compared to the API (avatar and checkbox)
targetIndex -= 2;
const columnId = colIdFromFieldName(field);
const origIndex = columns.findIndex((col) => col.id == columnId);
const columnOrder = columns.map((col) => col.id);

// Remove column and place it in new location
columnOrder.splice(origIndex, 1);
const newColumnOrder = [
...columnOrder.slice(0, targetIndex),
columnId,
...columnOrder.slice(targetIndex),
];
debouncedUpdateColumnOrder(newColumnOrder);
};

const unConfiguredGridColumns = [
avatarColumn,
...columns.map((col) => ({
Expand All @@ -304,6 +326,7 @@ const ViewDataTable: FunctionComponent<ViewDataTableProps> = ({
...columnTypes[col.type].getColDef(col, accessLevel),
})),
];

const { columns: gridColumns, setColumnWidth } =
useConfigurableDataGridColumns('viewInstances', unConfiguredGridColumns);

Expand Down Expand Up @@ -432,6 +455,9 @@ const ViewDataTable: FunctionComponent<ViewDataTableProps> = ({
}
}
}}
onColumnOrderChange={(params) => {
moveColumn(params.column.field, params.targetIndex);
}}
onColumnResize={(params) => {
setColumnWidth(params.colDef.field, params.width);
}}
Expand Down
4 changes: 4 additions & 0 deletions src/features/views/models/ViewDataModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ export default class ViewDataModel extends ModelBase {
return this._repo.updateColumn(this._orgId, this._viewId, columnId, data);
}

updateColumnOrder(columnOrder: number[]): Promise<void> {
return this._repo.updateColumnOrder(this._orgId, this._viewId, columnOrder);
}

updateContentQuery(query: Pick<ZetkinQuery, 'filter_spec'>) {
return this._repo.updateViewContentQuery(this._orgId, this._viewId, query);
}
Expand Down
13 changes: 13 additions & 0 deletions src/features/views/repos/ViewDataRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
cellUpdated,
columnAdded,
columnDeleted,
columnOrderUpdated,
columnsLoad,
columnsLoaded,
columnUpdated,
Expand Down Expand Up @@ -174,6 +175,18 @@ export default class ViewDataRepo {
this._store.dispatch(columnUpdated([viewId, column]));
}

async updateColumnOrder(
orgId: number,
viewId: number,
columnOrder: number[]
) {
await this._apiClient.patch<{ order: number[] }>(
`/api/orgs/${orgId}/people/views/${viewId}/column_order`,
{ order: columnOrder }
);
richardolsson marked this conversation as resolved.
Show resolved Hide resolved
this._store.dispatch(columnOrderUpdated([viewId, columnOrder]));
}

updateView(
orgId: number,
viewId: number,
Expand Down
47 changes: 43 additions & 4 deletions src/features/views/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,10 @@ const viewsSlice = createSlice({
}
},
columnAdded: (state, action: PayloadAction<[number, ZetkinViewColumn]>) => {
const [viewId, column] = action.payload;
const [viewId] = action.payload;
const colList = state.columnsByViewId[viewId];
if (colList) {
colList.items = colList.items.concat([
remoteItem(column.id, { data: column }),
]);
colList.isStale = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary? Adding a column was working before this PR, right? So why not keep the old logic for just concatenating it to the end of the list of columns?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried changing back to the previous code and it still worked fine. So if you can please explain why you found this change to be necessary I'd appreciate that.

const rowList = state.rowsByViewId[viewId];

if (rowList) {
Expand All @@ -180,6 +178,46 @@ const viewsSlice = createSlice({
}
}
},
columnOrderUpdated: (state, action: PayloadAction<[number, number[]]>) => {
const [viewId, columnOrder] = action.payload;
// Re-arrange columns
const colList = state.columnsByViewId[viewId];
if (colList) {
const newColListItems = columnOrder.map((colId) => {
const col = colList.items.find((col) => col.id == colId);
if (col) {
return col;
} else {
throw new Error('Could not find column!');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should never happen, but if it does, it will now crash the app. Instead of throwing an error and crashing, wouldn't it be better to just mark the column list as stale and force it to reload?

}
});

// Re-arrange columns of data-rows
const rowList = state.rowsByViewId[viewId];
if (rowList) {
const newRowListItems = rowList.items.map((row) => {
if (row.data) {
return {
...row,
data: {
content: columnOrder.map((colId) => {
const idx = colList.items.findIndex(
(col) => col.id == colId
)!;
return row.data?.content[idx];
}),
id: row.data.id,
},
};
} else {
return row;
}
});
state.columnsByViewId[viewId].items = newColListItems;
state.rowsByViewId[viewId].items = newRowListItems;
}
}
Comment on lines +198 to +222
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beautiful! This is a big improvement over Gen2 where the data had to be reloaded after reordering columns. Nice work!

},
columnUpdated: (
state,
action: PayloadAction<[number, ZetkinViewColumn]>
Expand Down Expand Up @@ -484,6 +522,7 @@ export const {
cellUpdated,
columnAdded,
columnDeleted,
columnOrderUpdated,
columnUpdated,
columnsLoad,
columnsLoaded,
Expand Down
Loading