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

Conversation

niklasva82
Copy link
Member

@niklasva82 niklasva82 commented Sep 18, 2023

Description

This PR adds persistence to column ordering by PATCHing the column_order of the view/list.

Screenshots

Screencast.from.2023-09-18.22-26-31.webm

Changes

  • Adds a PATCH to people/views/{view_id}/column_order every time a column is moved.

Notes to reviewer

Originally, the column order was changed in the state as well, but this resulted in problems with rendering (see mui/mui-x#5704), so now the redux state is not updated. This also means the bug where sorting resets the order persists. Not sure how to fix this issue.

Additionally, debouncing could be a good thing to add, this is because it seems there is no event for when the column dragging is finished, instead it's fired on each step, so if you're dragging the column multiple columns over, this will update the column once for every step. This seems unnecessary and could cause a race condition if the events are fired in fast succession and something causes the first request to be slower than the second.

Related issues

Resolves #1039

@niklasva82
Copy link
Member Author

niklasva82 commented Sep 19, 2023

Added debouncing to keep requests to a reasonable level. But there was still a problem with the rendering when reordering the columns in the redux state. So instead I chose to keep track of the new column order in the component. Of course when the page is reloaded, the columns from the redux state (provided as a prop to the ViewDataTable) are again in the correct order according to the new order.

Screencast.from.2023-09-19.22-32-14.webm

@ziggabyte ziggabyte self-assigned this Sep 21, 2023
Copy link
Contributor

@ziggabyte ziggabyte left a comment

Choose a reason for hiding this comment

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

This is a tricky one to crack!

I have began testing it to see how it works, and I am having some unexpected behaviour: the first time I move a column it works fine, but when dropping the second column the first moves back to it's original place. Not sure what causes it, but I think it should be fixed before further review.

reorder.columns.mp4

@niklasva82
Copy link
Member Author

niklasva82 commented Sep 21, 2023

I believe I know what's wrong. The "origin" that the new order is calculated from is the order of the columns in the redux state, and I don't think it checks for the order in the local state when it creates the new order array, so it's reset when a second column reordering is made. This should be a simple fix, I will get to it tomorrow.

@niklasva82
Copy link
Member Author

The bug above has been fixed and the typing error that the CI caught.

Copy link
Member

@richardolsson richardolsson left a comment

Choose a reason for hiding this comment

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

I get this error when I try to move a column (in a view/list that has many different kinds of columns).

image

Also, it seems like these changes have introduced another bug, which is that when I add a column, it content of the view does not reload to reflect the new column. I have to refresh the page to see the newly added column.

@niklasva82
Copy link
Member Author

Reimplemented using redux store. Everything seems to work now!
Screencast from 2023-09-30 04-09-21.webm

Copy link
Member

@richardolsson richardolsson left a comment

Choose a reason for hiding this comment

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

Nice work on the new redux store logic! I only question two minor parts of it. If you can respond to this asap that would be great, so that we can get this in the release this weekend.

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.

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?

Comment on lines +195 to +219
// 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;
}
}
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!

@niklasva82
Copy link
Member Author

niklasva82 commented Sep 30, 2023 via email

@richardolsson
Copy link
Member

The colList isStale is because I discovered that if you have the view in two browser tabs, add a tab in one tab and then add a tab in the other, it will fech more columns for the data than the number of columns in the header. Very specific I know, but I figured there was no harm in refreshing the columns when you were already refreshing the data. Good idea about the isStale on error, I will fix that.

I accept the motivation behind colList.isStale even if it feels to me like there should be some better solution available (e.g. marking as isStale only after a failure or unexpected response) but this is good enough for the time we have.

I will push a fix to the error handling, and then proceed and merge.

Copy link
Member

@richardolsson richardolsson left a comment

Choose a reason for hiding this comment

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

I feel like this is ready to merge now! I enjoyed the teamwork!

@richardolsson richardolsson dismissed ziggabyte’s stale review October 1, 2023 06:25

As far as I can tell the issues have been fixed.

@richardolsson richardolsson merged commit 13ca881 into main Oct 1, 2023
4 checks passed
@richardolsson richardolsson deleted the issue-1039/view-column-order-peristence branch October 1, 2023 06:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

View column order changes not persistent
3 participants