From 159d2978d09dc411f6f6572d76842af3598fd024 Mon Sep 17 00:00:00 2001 From: Abhishek Thory <76877003+abhithory@users.noreply.github.com> Date: Sat, 21 Oct 2023 04:31:32 -0500 Subject: [PATCH] Favorite: computeNewPosition fixed (#2134) --- front/src/modules/favorites/hooks/useFavorites.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/front/src/modules/favorites/hooks/useFavorites.ts b/front/src/modules/favorites/hooks/useFavorites.ts index 3dc348260..42fa2369b 100644 --- a/front/src/modules/favorites/hooks/useFavorites.ts +++ b/front/src/modules/favorites/hooks/useFavorites.ts @@ -104,7 +104,7 @@ export const useFavorites = () => { }); }; - const computeNewPosition = (destIndex: number) => { + const computeNewPosition = (destIndex: number, sourceIndex: number) => { if (destIndex === 0) { return favorites[destIndex].position / 2; } @@ -112,6 +112,13 @@ export const useFavorites = () => { if (destIndex === favorites.length - 1) { return favorites[destIndex].position + 1; } + + if (sourceIndex < destIndex) { + return ( + (favorites[destIndex + 1].position + favorites[destIndex].position) / 2 + ); + } + return ( (favorites[destIndex - 1].position + favorites[destIndex].position) / 2 ); @@ -121,7 +128,10 @@ export const useFavorites = () => { if (!result.destination || !favorites) { return; } - const newPosition = computeNewPosition(result.destination.index); + const newPosition = computeNewPosition( + result.destination.index, + result.source.index, + ); const reorderFavorites = Array.from(favorites); const [removed] = reorderFavorites.splice(result.source.index, 1);