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

fix: items order on update bug #2

Merged
merged 1 commit into from
Feb 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions src/pages/Menu/Items/Items.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ export const ItemsPreview = () => {
label: editingNode.label,
};
}
// child is not editing node
if (child.order === editingNode.order) {
// child is in same position as editing node
console.log('child is in same position as editing node', child);
Expand All @@ -266,20 +267,36 @@ export const ItemsPreview = () => {
if (child.order > editingNode.order) {
// child is after editing node
console.log('child is after editing node', child);
// set child order and action based on DELETE or CREATE
const action =
editingNode.action === EnumAction.UPDATE ? undefined : EnumAction.UPDATE;
// set child as UPDATE
let action = EnumAction.UPDATE;
let { order } = child;
if (editingNode.action === EnumAction.DELETE) {
order = child.order - 1;
if (editingNode.action === EnumAction.UPDATE) {
if (child.order < editingNode.original.order) {
// child order is less than editing node original order
// increase child order
order += 1;
} else {
// child order is greater than editing node original order
// keep child order as is and set action as undefined
action = undefined;
}
} else if (editingNode.action === EnumAction.DELETE) {
order -= 1;
} else if (editingNode.action === EnumAction.CREATE) {
order = child.order + 1;
order += 1;
}
// set child as UPDATE
return { ...child, order, action };
}
// child is before editing node
console.log('child is before editing node', child);
if (
editingNode.action === EnumAction.UPDATE &&
child.order > editingNode.original.order
) {
// child order is greater than editing node original order
// decrease child order
return { ...child, action: EnumAction.UPDATE, order: child.order - 1 };
}
return child;
});
if (editingNode.action === EnumAction.CREATE) {
Expand Down