From a5c9560b56ff9c338034fdcf47ee8f5df8a9845d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrian=20Or=C5=82=C3=B3w?= <adrian.orlow@fishbrain.com>
Date: Fri, 22 Mar 2024 13:35:36 +0100
Subject: [PATCH] feat: partial fix dependency cycle

---
 .../bioEntity/thunks/getMultiBioEntity.ts     | 43 +++---------------
 .../fetchReactionsAndGetBioEntitiesIds.ts     | 45 +++++++++++++++++++
 2 files changed, 52 insertions(+), 36 deletions(-)
 create mode 100644 src/redux/bioEntity/thunks/utils/fetchReactionsAndGetBioEntitiesIds.ts

diff --git a/src/redux/bioEntity/thunks/getMultiBioEntity.ts b/src/redux/bioEntity/thunks/getMultiBioEntity.ts
index ec8a92eb..eb1a3740 100644
--- a/src/redux/bioEntity/thunks/getMultiBioEntity.ts
+++ b/src/redux/bioEntity/thunks/getMultiBioEntity.ts
@@ -1,7 +1,3 @@
-import { getBioEntitiesIdsFromReaction } from '@/components/Map/MapViewer/utils/listeners/mapSingleClick/getBioEntitiesIdsFromReaction';
-import { SIZE_OF_EMPTY_ARRAY, ZERO } from '@/constants/common';
-import { selectTab } from '@/redux/drawer/drawer.slice';
-import { getReactionsByIds } from '@/redux/reactions/reactions.thunks';
 import { BioEntityContent } from '@/types/models';
 import { PerfectMultiSearchParams } from '@/types/search';
 import { ThunkConfig } from '@/types/store';
@@ -9,7 +5,8 @@ import { getErrorMessage } from '@/utils/getErrorMessage';
 import { PayloadAction, createAsyncThunk } from '@reduxjs/toolkit';
 import { addNumbersToEntityNumberData } from '../../entityNumber/entityNumber.slice';
 import { MULTI_BIO_ENTITY_FETCHING_ERROR_PREFIX } from '../bioEntity.constants';
-import { getBioEntity } from '../bioEntity.thunks';
+import { getBioEntity } from './getBioEntity';
+import { fetchReactionsAndGetBioEntitiesIds } from './utils/fetchReactionsAndGetBioEntitiesIds';
 
 type GetMultiBioEntityProps = PerfectMultiSearchParams;
 type GetMultiBioEntityActions = PayloadAction<BioEntityContent[] | undefined | string>[]; // if error thrown, string containing error message is returned
@@ -40,37 +37,11 @@ export const getMultiBioEntity = createAsyncThunk<
       const bioEntityIds = bioEntityContents.map(b => b.bioEntity.elementId);
       dispatch(addNumbersToEntityNumberData(bioEntityIds));
 
-      const bioEntityReactionsIds = (bioEntityContents || [])
-        .filter(c => c?.bioEntity?.idReaction)
-        .map(c => c?.bioEntity?.id)
-        .filter((id): id is number => typeof id === 'number');
-
-      if (bioEntityReactionsIds.length > ZERO) {
-        dispatch(
-          getReactionsByIds({
-            ids: bioEntityReactionsIds,
-            shouldConcat: true,
-          }),
-        ).then(async result => {
-          if (typeof result.payload === 'string') return;
-
-          const reactions = result.payload?.data;
-          if (!reactions || reactions.length === SIZE_OF_EMPTY_ARRAY) return;
-
-          const bioEntitiesIds = reactions
-            .map(reaction => getBioEntitiesIdsFromReaction(reaction))
-            .flat();
-
-          //   dispatch(openReactionDrawerById(reactions[FIRST_ARRAY_ELEMENT].id));
-          dispatch(selectTab(''));
-          await dispatch(
-            getMultiBioEntity({
-              searchQueries: bioEntitiesIds,
-              isPerfectMatch: true,
-            }),
-          );
-        });
-      }
+      const bioEntitiesIds = await fetchReactionsAndGetBioEntitiesIds({
+        bioEntityContents,
+        dispatch,
+      });
+      getMultiBioEntity({ searchQueries: bioEntitiesIds, isPerfectMatch: true });
 
       return bioEntityContents;
     } catch (error) {
diff --git a/src/redux/bioEntity/thunks/utils/fetchReactionsAndGetBioEntitiesIds.ts b/src/redux/bioEntity/thunks/utils/fetchReactionsAndGetBioEntitiesIds.ts
new file mode 100644
index 00000000..0a7beac9
--- /dev/null
+++ b/src/redux/bioEntity/thunks/utils/fetchReactionsAndGetBioEntitiesIds.ts
@@ -0,0 +1,45 @@
+import { getBioEntitiesIdsFromReaction } from '@/components/Map/MapViewer/utils/listeners/mapSingleClick/getBioEntitiesIdsFromReaction';
+import { SIZE_OF_EMPTY_ARRAY, ZERO } from '@/constants/common';
+import { selectTab } from '@/redux/drawer/drawer.slice';
+import { getReactionsByIds } from '@/redux/reactions/reactions.thunks';
+import type { AppDispatch } from '@/redux/store';
+import type { BioEntityContent } from '@/types/models';
+
+interface Args {
+  bioEntityContents: BioEntityContent[];
+  dispatch: AppDispatch;
+}
+
+export const fetchReactionsAndGetBioEntitiesIds = async ({
+  dispatch,
+  bioEntityContents,
+}: Args): Promise<string[]> => {
+  const bioEntityReactionsIds = (bioEntityContents || [])
+    .filter(c => c?.bioEntity?.idReaction)
+    .map(c => c?.bioEntity?.id)
+    .filter((id): id is number => typeof id === 'number');
+
+  if (bioEntityReactionsIds.length === ZERO) {
+    return [];
+  }
+
+  const result = await dispatch(
+    getReactionsByIds({
+      ids: bioEntityReactionsIds,
+      shouldConcat: true,
+    }),
+  );
+  if (typeof result.payload === 'string') {
+    return [];
+  }
+
+  const reactions = result.payload?.data;
+  if (!reactions || reactions.length === SIZE_OF_EMPTY_ARRAY) {
+    return [];
+  }
+
+  const bioEntitiesIds = reactions.map(reaction => getBioEntitiesIdsFromReaction(reaction)).flat();
+  //   dispatch(openReactionDrawerById(reactions[FIRST_ARRAY_ELEMENT].id));
+  dispatch(selectTab(''));
+  return bioEntitiesIds;
+};
-- 
GitLab