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

Fixes undo button on snackbar #4149

Merged
merged 1 commit into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ describe('ContentNode methods', () => {

describe('tableMove method', () => {
let node,
oldParent,
oldObj,
parent,
payload,
change,
Expand All @@ -553,9 +553,9 @@ describe('ContentNode methods', () => {
put: jest.fn(() => Promise.resolve()),
};
updated = true;
oldParent = { id: uuid4(), title: 'Parent' };
oldObj = { id: uuid4(), title: 'Parent' };
parent = { id: uuid4(), root_id: uuid4(), title: 'Parent' };
node = { id: uuid4(), parent: oldParent.id, title: 'Source node' };
node = { id: uuid4(), parent: oldObj.id, title: 'Source node' };
payload = { id: uuid4(), parent: parent.id, changed: true, lft: 1, title: 'Payload' };
change = {
key: payload.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,30 +182,22 @@ describe('Change Types', () => {
});

it('should persist only the specified fields in the MovedChange', async () => {
const oldObj = { parent: '4' };
const change = new MovedChange({
key: '1',
table: TABLE_NAMES.CONTENTNODE,
target: '2',
position: RELATIVE_TREE_POSITIONS.LAST_CHILD,
parent: '3',
oldParent: '4',
source: CLIENTID,
oldObj,
});
const rev = await change.saveChange();
const persistedChange = await db[CHANGES_TABLE].get(rev);
expect(persistedChange).toEqual({
rev,
channel_id,
...pick(change, [
'type',
'key',
'table',
'target',
'position',
'parent',
'source',
'oldParent',
]),
...pick(change, ['type', 'key', 'table', 'target', 'position', 'parent', 'source', 'oldObj']),
});
});

Expand Down Expand Up @@ -427,18 +419,21 @@ describe('Change Types Unhappy Paths', () => {

// MovedChange
it('should throw error when MovedChange is instantiated without target', () => {
const oldObj = { a: 1, b: 2 };
expect(
() =>
new MovedChange({
key: '1',
table: TABLE_NAMES.CONTENTNODE,
position: RELATIVE_TREE_POSITIONS.LAST_CHILD,
source: CLIENTID,
oldObj,
})
).toThrow(new TypeError('target is required for a MovedChange but it was undefined'));
});

it('should throw error when MovedChange is instantiated with incorrect position type', () => {
const oldObj = { a: 1, b: { c: 1 } };
expect(
() =>
new MovedChange({
Expand All @@ -447,11 +442,13 @@ describe('Change Types Unhappy Paths', () => {
target: '2',
position: 'invalid',
source: CLIENTID,
oldObj,
})
).toThrow(new ReferenceError('invalid is not a valid position value'));
});

it('should throw error when MovedChange is instantiated without parent', () => {
const oldObj = { a: 1, b: 2, parent: 3 };
expect(
() =>
new MovedChange({
Expand All @@ -460,6 +457,7 @@ describe('Change Types Unhappy Paths', () => {
target: '2',
position: RELATIVE_TREE_POSITIONS.LAST_CHILD,
source: CLIENTID,
oldObj,
})
).toThrow(new ReferenceError('parent is required for a MovedChange but it was undefined'));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,13 @@ class ResourceCounts extends ChangeDispatcher {
*/
async applyMove(change) {
// Only if the node is being moved to a new parent do we need to update the ancestor counts
if (change.oldParent === change.parent) {
if (change.oldObj.parent === change.parent) {
return;
}

const node = await this.table.get(change.key);
await this.resource.updateAncestors(
{ id: change.oldParent, includeSelf: true, ignoreChanges: true },
{ id: change.oldObj.parent, includeSelf: true, ignoreChanges: true },
this._applyDiff.bind(this, node, -1)
);
await this.resource.updateAncestors(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,18 +383,18 @@ export class DeletedChange extends Change {
}

export class MovedChange extends Change {
constructor({ target, position, parent, oldParent, ...fields }) {
constructor({ oldObj, target, position, parent, ...fields }) {
fields.type = CHANGE_TYPES.MOVED;
super(fields);
if (this.table !== TABLE_NAMES.CONTENTNODE) {
throw TypeError(
`${this.changeType} is only supported by ${TABLE_NAMES.CONTENTNODE} table but ${this.table} was passed instead`
);
}
this.setAndValidateObj(oldObj, 'oldObj', omitIgnoredSubFields);
this.setAndValidateIsDefined(target, 'target');
this.setAndValidateLookup(position, 'position', RELATIVE_TREE_POSITIONS_LOOKUP);
this.setAndValidateIsDefined(parent, 'parent');
this.setAndValidateIsDefined(oldParent, 'oldParent');
this.setChannelAndUserId();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1395,7 +1395,6 @@ export const ContentNode = new TreeResource({
// so that we can avoid doing fetches while such changes
// are pending.
parent: parent.id,
oldParent: isCreate ? null : node.parent,
oldObj: isCreate ? null : node,
table: this.tableName,
source: CLIENTID,
Expand Down