I am building an application, where I have N columns (N lists of items) and by using a drag & drop from angular material I am able to move items between them. I am also using NGRX and EntityAdapter in my app.
I would like to ask, how can I properly update the order of items when I am moving a object?
Example: Moving first item from the "To do" column. that item should be put between brush teeth and pick up groceries.
Right now I know how to update column of every item of my list. The thing that I do not know is how to put it in the correct order (sorting is by default, so probably by id).
Item interface:
export interface Item {
id:string;
title:string;
columnId:string;
}
Dispatching action for columnId
update :
drop(event: CdkDragDrop<string[]>) {
const droppedItem: SavedJob = event.item.data;
const previousColumn = event.previousContainer.id;
const currentColumn = event.container.id;
this.store.dispatch(
updateColumn({
updates: {
id: droppedItem.id,
changes: {
columnId: currentColumn
}
}
})
);
Reducer:
on(SavedJobsActions.updateColumn,(state,action)=> {
return savedJobsAdapter.updateOne(action.updates, {
...state,
entities: {
...state.entities,
[action.updates.id]: {
...state.entities[action.updates.id]
}
}
})
})
How should I properly update both columnId
and the order of each item ? I know I should add order key for each Item, but then updating the order value for each column might be hard.