From a40c4803a39a1f9ccb235c620cf896879b9625fa Mon Sep 17 00:00:00 2001
From: Piotr Gawron <p.gawron@atcomp.pl>
Date: Thu, 5 Sep 2024 08:34:27 +0200
Subject: [PATCH] use different endpoint when looking for info about element

---
 src/redux/bioEntity/bioEntity.constants.ts    |  4 ++--
 src/redux/bioEntity/bioEntity.selectors.ts    |  3 +--
 src/redux/bioEntity/bioEntity.types.ts        |  4 ++--
 .../thunks/getSubmapConnectionsBioEntity.ts   | 19 +++++++++----------
 4 files changed, 14 insertions(+), 16 deletions(-)

diff --git a/src/redux/bioEntity/bioEntity.constants.ts b/src/redux/bioEntity/bioEntity.constants.ts
index b267180f..70b92fc4 100644
--- a/src/redux/bioEntity/bioEntity.constants.ts
+++ b/src/redux/bioEntity/bioEntity.constants.ts
@@ -1,5 +1,5 @@
 import { FetchDataState } from '@/types/fetchDataState';
-import { BioEntityContent } from '@/types/models';
+import { BioEntity } from '@/types/models';
 import { BioEntityContentsState } from './bioEntity.types';
 
 export const DEFAULT_BIOENTITY_PARAMS = {
@@ -9,7 +9,7 @@ export const DEFAULT_BIOENTITY_PARAMS = {
 export const BIO_ENTITY_FETCHING_ERROR_PREFIX = 'Failed to fetch bio entity';
 export const MULTI_BIO_ENTITY_FETCHING_ERROR_PREFIX = 'Failed to fetch multi bio entity';
 
-export const BIOENTITY_SUBMAP_CONNECTIONS_INITIAL_STATE: FetchDataState<BioEntityContent[]> = {
+export const BIOENTITY_SUBMAP_CONNECTIONS_INITIAL_STATE: FetchDataState<BioEntity[]> = {
   data: [],
   loading: 'idle',
   error: { name: '', message: '' },
diff --git a/src/redux/bioEntity/bioEntity.selectors.ts b/src/redux/bioEntity/bioEntity.selectors.ts
index ee3b893c..e5b70204 100644
--- a/src/redux/bioEntity/bioEntity.selectors.ts
+++ b/src/redux/bioEntity/bioEntity.selectors.ts
@@ -51,8 +51,7 @@ export const bioEntityDataListSelector = createSelector(bioEntityDataSelector, b
 
 export const allSubmapConnectionsBioEntitySelector = createSelector(
   bioEntitySelector,
-  (bioEntityData): BioEntity[] =>
-    (bioEntityData?.submapConnections?.data || []).map(({ bioEntity }) => bioEntity),
+  (bioEntityData): BioEntity[] => bioEntityData?.submapConnections?.data || [],
 );
 
 export const allSubmapConnectionsBioEntityOfCurrentSubmapSelector = createSelector(
diff --git a/src/redux/bioEntity/bioEntity.types.ts b/src/redux/bioEntity/bioEntity.types.ts
index 11c3418f..9540daab 100644
--- a/src/redux/bioEntity/bioEntity.types.ts
+++ b/src/redux/bioEntity/bioEntity.types.ts
@@ -1,9 +1,9 @@
 import { FetchDataState, MultiFetchDataState } from '@/types/fetchDataState';
-import { BioEntityContent } from '@/types/models';
+import { BioEntity, BioEntityContent } from '@/types/models';
 import { PayloadAction } from '@reduxjs/toolkit';
 
 export type BioEntityContentsState = MultiFetchDataState<BioEntityContent[]> & {
-  submapConnections?: FetchDataState<BioEntityContent[]>;
+  submapConnections?: FetchDataState<BioEntity[]>;
   isContentTabOpened?: boolean;
 };
 
diff --git a/src/redux/bioEntity/thunks/getSubmapConnectionsBioEntity.ts b/src/redux/bioEntity/thunks/getSubmapConnectionsBioEntity.ts
index e5018993..554dfde5 100644
--- a/src/redux/bioEntity/thunks/getSubmapConnectionsBioEntity.ts
+++ b/src/redux/bioEntity/thunks/getSubmapConnectionsBioEntity.ts
@@ -1,15 +1,15 @@
-import { bioEntityContentSchema } from '@/models/bioEntityContentSchema';
 import { submapConnection } from '@/models/submapConnection';
 import { apiPath } from '@/redux/apiPath';
 import { axiosInstance, axiosInstanceNewAPI } from '@/services/api/utils/axiosInstance';
-import { BioEntityContent, BioEntityResponse, SubmapConnection } from '@/types/models';
+import { BioEntity, SubmapConnection } from '@/types/models';
 import { validateDataUsingZodSchema } from '@/utils/validateDataUsingZodSchema';
 import { createAsyncThunk } from '@reduxjs/toolkit';
 import { z } from 'zod';
 import { getError } from '@/utils/error-report/getError';
+import { bioEntitySchema } from '@/models/bioEntitySchema';
 import { MULTI_BIO_ENTITY_FETCHING_ERROR_PREFIX } from '../bioEntity.constants';
 
-export const getSubmapConnectionsBioEntity = createAsyncThunk<BioEntityContent[]>(
+export const getSubmapConnectionsBioEntity = createAsyncThunk<BioEntity[]>(
   'project/getSubmapConnectionsBioEntity',
   async () => {
     try {
@@ -20,21 +20,20 @@ export const getSubmapConnectionsBioEntity = createAsyncThunk<BioEntityContent[]
         throw new Error('Submap connections validation error');
       }
 
-      const searchQueries = response.data.map(({ from }) => `${from.id}`);
+      const targetElements = response.data.map(({ from }) => from);
 
-      const asyncFetchBioEntityFunctions = searchQueries.map(searchQuery =>
-        axiosInstanceNewAPI.get<BioEntityResponse>(
-          apiPath.getBioEntityContentsStringWithQuery({ searchQuery, isPerfectMatch: true }),
+      const asyncFetchBioEntityFunctions = targetElements.map(targetElement =>
+        axiosInstanceNewAPI.get<BioEntity>(
+          apiPath.getElementById(targetElement.id, targetElement.modelId),
         ),
       );
 
       const bioEntityContentResponse = await Promise.all(asyncFetchBioEntityFunctions);
 
       const bioEntityContents = bioEntityContentResponse
-        .map(contentResponse => contentResponse?.data?.content || [])
+        .map(contentResponse => contentResponse.data)
         .flat()
-        .filter(content => bioEntityContentSchema.safeParse(content).success)
-        .filter(payload => 'bioEntity' in payload || {});
+        .filter(content => bioEntitySchema.safeParse(content).success);
 
       return bioEntityContents;
     } catch (error) {
-- 
GitLab