Jelajahi Sumber

fix(core): Fix reordering of collections

Fixes #75
Michael Bromley 6 tahun lalu
induk
melakukan
75f8858c3a

+ 16 - 3
packages/core/e2e/collection.e2e-spec.ts

@@ -237,12 +237,12 @@ 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,
                 },
             });
 
@@ -250,6 +250,19 @@ describe('Collection resolver', () => {
             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: {

+ 10 - 4
packages/core/src/service/services/collection.service.ts

@@ -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;
+                }
             });
         }