Skip to content

Commit

Permalink
fix(core): Fix reordering of collections
Browse files Browse the repository at this point in the history
Fixes #75
  • Loading branch information
michaelbromley committed Apr 23, 2019
1 parent f65790d commit 75f8858
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
19 changes: 16 additions & 3 deletions packages/core/e2e/collection.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,19 +237,32 @@ describe('Collection resolver', () => {
]);
});

it('alters the position in the current parent', async () => {
it('alters the position in the current parent 1', async () => {
await client.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
input: {
collectionId: pearCollection.id,
collectionId: computersCollection.id,
parentId: electronicsCollection.id,
index: 1,
index: 0,
},
});

const afterResult = await getChildrenOf(electronicsCollection.id);
expect(afterResult.map((i: any) => i.id)).toEqual([computersCollection.id, pearCollection.id]);
});

it('alters the position in the current parent 2', async () => {
await client.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
input: {
collectionId: pearCollection.id,
parentId: electronicsCollection.id,
index: 0,
},
});

const afterResult = await getChildrenOf(electronicsCollection.id);
expect(afterResult.map((i: any) => i.id)).toEqual([pearCollection.id, computersCollection.id]);
});

it('corrects an out-of-bounds negative index value', async () => {
await client.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
input: {
Expand Down
14 changes: 10 additions & 4 deletions packages/core/src/service/services/collection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,15 +275,21 @@ export class CollectionService implements OnModuleInit {
const currentIndex = siblings.findIndex(cat => idsAreEqual(cat.id, input.collectionId));
if (currentIndex !== normalizedIndex) {
siblings.splice(normalizedIndex, 0, siblings.splice(currentIndex, 1)[0]);
siblings.forEach((cat, index) => {
cat.position = index;
siblings.forEach((collection, index) => {
collection.position = index;
if (target.id === collection.id) {
target.position = index;
}
});
}
} else {
target.parent = new Collection({ id: input.parentId });
siblings.splice(normalizedIndex, 0, target);
siblings.forEach((cat, index) => {
cat.position = index;
siblings.forEach((collection, index) => {
collection.position = index;
if (target.id === collection.id) {
target.position = index;
}
});
}

Expand Down

0 comments on commit 75f8858

Please sign in to comment.