From 52a204f4aa0e1bcef7f11cd8315caef77e52a5f5 Mon Sep 17 00:00:00 2001 From: Marc Itzenthaler Date: Wed, 28 Feb 2024 02:14:26 +0100 Subject: [PATCH 1/4] fix: agency list update registration --- .../ConsultingTypeAgencySelection.tsx | 47 ++++++++++++------- .../formAccordion/FormAccordion.tsx | 2 +- src/components/login/Login.tsx | 2 +- .../hooks/useAgenciesForRegistration.ts | 5 +- .../hooks/useConsultantRegistrationData.ts | 19 +++++++- src/resources/i18n/de/common.json | 7 +++ src/resources/i18n/de@informal/common.json | 4 ++ src/resources/i18n/en/common.json | 5 ++ 8 files changed, 69 insertions(+), 22 deletions(-) diff --git a/src/components/consultingTypeSelection/ConsultingTypeAgencySelection.tsx b/src/components/consultingTypeSelection/ConsultingTypeAgencySelection.tsx index 07bebf79f..70d649d2e 100644 --- a/src/components/consultingTypeSelection/ConsultingTypeAgencySelection.tsx +++ b/src/components/consultingTypeSelection/ConsultingTypeAgencySelection.tsx @@ -66,7 +66,13 @@ export const ConsultingTypeAgencySelection = ({ agencies: possibleAgencies, consultingTypes: possibleConsultingTypes, topicIds: possibleTopicIds - } = useConsultantRegistrationData(); + } = useConsultantRegistrationData({ + consultingTypeId: + selectedConsultingTypeOption?.value && + parseInt(selectedConsultingTypeOption.value), + topicId: + selectedTopicOption?.value && parseInt(selectedTopicOption.value) + }); useEffect(() => { apiGetTopicsData() @@ -185,7 +191,11 @@ export const ConsultingTypeAgencySelection = ({ defaultValue: selectedTopicOption }; - if (possibleAgencies.length <= 1 && possibleConsultingTypes.length <= 1) { + if ( + possibleAgencies.length <= 1 && + possibleConsultingTypes.length <= 1 && + possibleTopicIds.length <= 1 + ) { return null; } @@ -218,23 +228,24 @@ export const ConsultingTypeAgencySelection = ({ )} - {selectedConsultingTypeOption && agencyOptions.length > 1 && ( -
- {consultingTypeOptions.length <= 1 && ( - 1 && ( +
+ {consultingTypeOptions.length <= 1 && ( + + )} + - )} - -
- )} +
+ )} ); }; diff --git a/src/components/formAccordion/FormAccordion.tsx b/src/components/formAccordion/FormAccordion.tsx index ad14fc6f4..d8eb9be38 100644 --- a/src/components/formAccordion/FormAccordion.tsx +++ b/src/components/formAccordion/FormAccordion.tsx @@ -71,7 +71,7 @@ export const FormAccordion = ({ const { setSpecificAgency, specificAgency } = useContext( AgencySpecificContext ); - const { consultingTypes } = useConsultantRegistrationData(); + const { consultingTypes } = useConsultantRegistrationData({}); const [activeItem, setActiveItem] = useState(1); diff --git a/src/components/login/Login.tsx b/src/components/login/Login.tsx index 3d2d1ca88..92f346b69 100644 --- a/src/components/login/Login.tsx +++ b/src/components/login/Login.tsx @@ -198,7 +198,7 @@ export const Login = () => { agencies: possibleAgencies, consultingTypes: possibleConsultingTypes, topicIds: possibleTopicIds - } = useConsultantRegistrationData(); + } = useConsultantRegistrationData({}); const registerOverlay = useMemo( (): OverlayItem => ({ diff --git a/src/containers/registration/hooks/useAgenciesForRegistration.ts b/src/containers/registration/hooks/useAgenciesForRegistration.ts index 35a209e54..2621251fc 100644 --- a/src/containers/registration/hooks/useAgenciesForRegistration.ts +++ b/src/containers/registration/hooks/useAgenciesForRegistration.ts @@ -38,7 +38,10 @@ export const useAgenciesForRegistration = ({ const { agencies: consultantAgencies, consultingTypes: consultantConsultingTypes - } = useConsultantRegistrationData(); + } = useConsultantRegistrationData({ + topicId: topic?.id, + consultingTypeId: consultingType?.id + }); const [isLoading, setIsLoading] = useState(true); const [agencies, setAgencies] = useState([]); diff --git a/src/containers/registration/hooks/useConsultantRegistrationData.ts b/src/containers/registration/hooks/useConsultantRegistrationData.ts index 2c470cd0f..1bf30cea9 100644 --- a/src/containers/registration/hooks/useConsultantRegistrationData.ts +++ b/src/containers/registration/hooks/useConsultantRegistrationData.ts @@ -8,7 +8,15 @@ import { import { useAppConfig } from '../../../hooks/useAppConfig'; import { UrlParamsContext } from '../../../globalState/provider/UrlParamsProvider'; -export const useConsultantRegistrationData = () => { +interface ConsultantRegistrationDataArgs { + consultingTypeId?: number; + topicId?: number; +} + +export const useConsultantRegistrationData = ({ + consultingTypeId, + topicId +}: ConsultantRegistrationDataArgs) => { const settings = useAppConfig(); const { consultingType: preselectedConsultingType, @@ -89,12 +97,19 @@ export const useConsultantRegistrationData = () => { .filter((agency) => consultingTypeIds.includes(agency.consultingType) ) + // Filter agencies by selected topic + .filter( + (a) => + !consultingTypeId || a.consultingType === consultingTypeId + ) // Filter agencies by preselected topic .filter( (a) => !preselectedTopic || a.topicIds?.includes(preselectedTopic?.id) ) + // Filter agencies by selected topic + .filter((a) => !topicId || a.topicIds?.includes(topicId)) // Filter agencies by preselected agency .filter((a) => !preselectedAgency || preselectedAgency.id === a.id); setAgencies(possibleAgencies); @@ -106,6 +121,8 @@ export const useConsultantRegistrationData = () => { : consultingTypes ); }, [ + topicId, + consultingTypeId, consultant, preselectedConsultingType, preselectedAgency, diff --git a/src/resources/i18n/de/common.json b/src/resources/i18n/de/common.json index 8d762db8a..169ee1aa2 100644 --- a/src/resources/i18n/de/common.json +++ b/src/resources/i18n/de/common.json @@ -1810,6 +1810,13 @@ "select": { "label": "Themenfeld" } + }, + "topic": { + "headline": "Bitte wählen Sie ein Themenfeld", + "infoText": "Ihr_e Berater_in ist in mehreren Themenfeldern tätig. Bitte wählen Sie Ihr gewünschtes Themenfeld.", + "select": { + "label": "Themenfeld" + } } }, "dataProtection": { diff --git a/src/resources/i18n/de@informal/common.json b/src/resources/i18n/de@informal/common.json index 025aad168..7b2629d6c 100644 --- a/src/resources/i18n/de@informal/common.json +++ b/src/resources/i18n/de@informal/common.json @@ -618,6 +618,10 @@ "consultingType": { "headline": "Bitte wähle ein Themenfeld", "infoText": "Dein_e Berater_in ist in mehreren Themenfeldern tätig. Bitte wähle Dein gewünschtes Themenfeld." + }, + "topic": { + "headline": "Bitte wähle ein Themenfeld", + "infoText": "Dein_e Berater_in ist in mehreren Themenfeldern tätig. Bitte wähle Dein gewünschtes Themenfeld." } }, "mainTopic": { diff --git a/src/resources/i18n/en/common.json b/src/resources/i18n/en/common.json index 0c8c8cfc9..0dbf02480 100644 --- a/src/resources/i18n/en/common.json +++ b/src/resources/i18n/en/common.json @@ -1789,6 +1789,11 @@ "headline": "Please select a topic", "infoText": "Your consultant is active in several subject areas. Please select your desired subject area.", "select.label": "Topic" + }, + "topic": { + "headline": "Please select a topic", + "infoText": "Your consultant is active in several subject areas. Please select your desired subject area.", + "select.label": "Topic" } }, "dataProtection": { From 65b8745c24811a9ce91a4eeaf2af21720d09d05c Mon Sep 17 00:00:00 2001 From: Marc Itzenthaler Date: Wed, 28 Feb 2024 03:10:27 +0100 Subject: [PATCH 2/4] fix: agency list update registration --- cypress/e2e/login.cy.ts | 2 +- cypress/e2e/registration/topic.cy.ts | 18 ++++++------------ .../hooks/useConsultantRegistrationData.ts | 4 +++- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/cypress/e2e/login.cy.ts b/cypress/e2e/login.cy.ts index e96c8a5a1..be54d30d4 100644 --- a/cypress/e2e/login.cy.ts +++ b/cypress/e2e/login.cy.ts @@ -30,7 +30,7 @@ describe('Login', () => { cy.contains('Datenschutzerklärung'); }); - it.only('displays the login for resorts', () => { + it('displays the login for resorts', () => { cy.visit('/suchtberatung'); cy.contains('Login'); }); diff --git a/cypress/e2e/registration/topic.cy.ts b/cypress/e2e/registration/topic.cy.ts index 2f79f17c4..955d21b70 100644 --- a/cypress/e2e/registration/topic.cy.ts +++ b/cypress/e2e/registration/topic.cy.ts @@ -871,23 +871,20 @@ describe('Registration', () => { cy.get('.agencySelection__proposedAgency').should( 'have.length', - 5 + 2 ); cy.get( '.agencySelection__proposedAgency .radioButton__input#agency-11' - ).should('exist'); + ).should('not.exist'); cy.get( '.agencySelection__proposedAgency .radioButton__input#agency-13' - ).should('exist'); + ).should('not.exist'); cy.get( '.agencySelection__proposedAgency .radioButton__input#agency-21' ).should('exist'); cy.get( '.agencySelection__proposedAgency .radioButton__input#agency-22' ).should('exist'); - cy.get( - '.agencySelection__proposedAgency .radioButton__input#agency-23' - ).should('exist'); }); }); describe('With valid cid parameters', () => { @@ -1080,23 +1077,20 @@ describe('Registration', () => { cy.get('.agencySelection__proposedAgency').should( 'have.length', - 5 + 2 ); cy.get( '.agencySelection__proposedAgency .radioButton__input#agency-11' - ).should('exist'); + ).should('not.exist'); cy.get( '.agencySelection__proposedAgency .radioButton__input#agency-13' - ).should('exist'); + ).should('not.exist'); cy.get( '.agencySelection__proposedAgency .radioButton__input#agency-21' ).should('exist'); cy.get( '.agencySelection__proposedAgency .radioButton__input#agency-22' ).should('exist'); - cy.get( - '.agencySelection__proposedAgency .radioButton__input#agency-23' - ).should('exist'); }); }); }); diff --git a/src/containers/registration/hooks/useConsultantRegistrationData.ts b/src/containers/registration/hooks/useConsultantRegistrationData.ts index 1bf30cea9..33b474d39 100644 --- a/src/containers/registration/hooks/useConsultantRegistrationData.ts +++ b/src/containers/registration/hooks/useConsultantRegistrationData.ts @@ -100,7 +100,9 @@ export const useConsultantRegistrationData = ({ // Filter agencies by selected topic .filter( (a) => - !consultingTypeId || a.consultingType === consultingTypeId + slugFallback || + !consultingTypeId || + a.consultingType === consultingTypeId ) // Filter agencies by preselected topic .filter( From 2e02954d747401a1591e455420694d0884e5d47f Mon Sep 17 00:00:00 2001 From: Marc Itzenthaler Date: Wed, 28 Feb 2024 16:10:53 +0100 Subject: [PATCH 3/4] fix: default postcode optional, request reduction --- src/api/apiAgencySelection.ts | 86 +++++++++++-------- src/api/apiGetAgencyId.ts | 9 +- src/api/apiGetConsultant.ts | 32 ++++--- .../agencySelection/AgencySelection.tsx | 2 - .../hooks/useAgenciesForRegistration.ts | 13 +-- src/utils/loadConsultingTypeForAgency.ts | 11 --- 6 files changed, 85 insertions(+), 68 deletions(-) delete mode 100644 src/utils/loadConsultingTypeForAgency.ts diff --git a/src/api/apiAgencySelection.ts b/src/api/apiAgencySelection.ts index e32f19379..1b48eb94e 100644 --- a/src/api/apiAgencySelection.ts +++ b/src/api/apiAgencySelection.ts @@ -1,15 +1,14 @@ import { endpoints } from '../resources/scripts/endpoints'; import { fetchData, FETCH_METHODS, FETCH_ERRORS } from './fetchData'; -import { VALID_POSTCODE_LENGTH } from '../components/agencySelection/agencySelectionHelpers'; import { AgencyDataInterface } from '../globalState/interfaces'; -import { loadConsultingTypeForAgency } from '../utils/loadConsultingTypeForAgency'; +import { apiGetConsultingType } from './apiGetConsultingType'; export const apiAgencySelection = async ( { fetchConsultingTypeDetails, ...params }: { - postcode: string; + postcode?: string; consultingType: number | undefined; topicId?: number; age?: number; @@ -18,50 +17,61 @@ export const apiAgencySelection = async ( fetchConsultingTypeDetails?: boolean; }, signal?: AbortSignal -): Promise | null> => { +): Promise => { let queryStr = Object.keys(params) .filter((key) => params[key] !== undefined) .map((key) => key + '=' + params[key]) .join('&'); const url = endpoints.agencyServiceBase + '?' + queryStr; - if (params.postcode.length === VALID_POSTCODE_LENGTH) { - return fetchData({ - url: url, - method: FETCH_METHODS.GET, - skipAuth: true, - responseHandling: [FETCH_ERRORS.EMPTY], - ...(signal && { signal: signal }) - }) - .then((result) => { - if (result) { - // External agencies should only be returned - // if there are no internal ones. - const internalAgencies = result.filter( - (agency) => !agency.external - ); - if (internalAgencies.length > 0) { - return internalAgencies; - } else { - return result; - } + return fetchData({ + url: url, + method: FETCH_METHODS.GET, + skipAuth: true, + responseHandling: [FETCH_ERRORS.EMPTY], + ...(signal && { signal: signal }) + }) + .then((result) => { + if (result) { + // External agencies should only be returned + // if there are no internal ones. + const internalAgencies = result.filter( + (agency) => !agency.external + ); + if (internalAgencies.length > 0) { + return internalAgencies; } else { return result; } - }) - .then((agencies) => { - if (!fetchConsultingTypeDetails) { - return agencies; - } + } else { + return result; + } + }) + .then((agencies: AgencyDataInterface[]) => { + if (!fetchConsultingTypeDetails) { + return agencies; + } + + // Get unique consultingTypes to prevent multiple requests to api + const uniqueConsultingTypeIds = [ + ...new Set( + agencies.map((a) => a?.consultingType).filter(Boolean) + ) + ]; - return Promise.all( - agencies.map( - async (agency) => - await loadConsultingTypeForAgency(agency) + return Promise.all( + uniqueConsultingTypeIds.map((consultingTypeId) => + apiGetConsultingType({ + consultingTypeId + }) + ) + ).then((consultingTypes) => + agencies.map((a) => ({ + ...a, + consultingTypeRel: consultingTypes.find( + (c) => c.id === a.consultingType ) - ); - }); - } else { - return null; - } + })) + ); + }); }; diff --git a/src/api/apiGetAgencyId.ts b/src/api/apiGetAgencyId.ts index a9d8c2dfb..a1c7aa259 100644 --- a/src/api/apiGetAgencyId.ts +++ b/src/api/apiGetAgencyId.ts @@ -1,7 +1,7 @@ import { endpoints } from '../resources/scripts/endpoints'; import { fetchData, FETCH_METHODS, FETCH_ERRORS } from './fetchData'; import { AgencyDataInterface } from '../globalState/interfaces'; -import { loadConsultingTypeForAgency } from '../utils/loadConsultingTypeForAgency'; +import { apiGetConsultingType } from './apiGetConsultingType'; export const apiGetAgencyById = async ( agencyId: number, @@ -21,6 +21,11 @@ export const apiGetAgencyById = async ( return agency; } - return await loadConsultingTypeForAgency(agency); + return { + ...agency, + consultingTypeRel: await apiGetConsultingType({ + consultingTypeId: agency?.consultingType + }) + }; }); }; diff --git a/src/api/apiGetConsultant.ts b/src/api/apiGetConsultant.ts index c61ffad79..4177688a9 100644 --- a/src/api/apiGetConsultant.ts +++ b/src/api/apiGetConsultant.ts @@ -1,7 +1,7 @@ import { endpoints } from '../resources/scripts/endpoints'; import { fetchData, FETCH_METHODS, FETCH_ERRORS } from './fetchData'; import { ConsultantDataInterface } from '../globalState/interfaces'; -import { loadConsultingTypeForAgency } from '../utils/loadConsultingTypeForAgency'; +import { apiGetConsultingType } from './apiGetConsultingType'; export const apiGetConsultant = async ( consultantId: string, @@ -14,20 +14,32 @@ export const apiGetConsultant = async ( method: FETCH_METHODS.GET, skipAuth: true, responseHandling: [FETCH_ERRORS.CATCH_ALL] - }).then((user) => { + }).then((user: ConsultantDataInterface) => { if (!fetchConsultingTypeDetails) { return user; } + // Get unique consultingTypes to prevent multiple requests to api + const uniqueConsultingTypeIds = [ + ...new Set( + user.agencies.map((a) => a?.consultingType).filter(Boolean) + ) + ]; + return Promise.all( - user.agencies.map( - async (agency) => await loadConsultingTypeForAgency(agency) + uniqueConsultingTypeIds.map((consultingTypeId) => + apiGetConsultingType({ + consultingTypeId + }) ) - ).then( - (agencies): ConsultantDataInterface => ({ - ...user, - agencies - }) - ); + ).then((consultingTypes) => ({ + ...user, + agencies: user.agencies.map((a) => ({ + ...a, + consultingTypeRel: consultingTypes.find( + (c) => c.id === a.consultingType + ) + })) + })); }); }; diff --git a/src/components/agencySelection/AgencySelection.tsx b/src/components/agencySelection/AgencySelection.tsx index 391b38f27..3c0d219a2 100644 --- a/src/components/agencySelection/AgencySelection.tsx +++ b/src/components/agencySelection/AgencySelection.tsx @@ -10,7 +10,6 @@ import { InputField, InputFieldItem } from '../inputField/InputField'; import { VALID_POSTCODE_LENGTH } from './agencySelectionHelpers'; import './agencySelection.styles'; import '../profile/profile.styles'; -import { DEFAULT_POSTCODE } from '../registration/prefillPostcode'; import { RadioButton } from '../radioButton/RadioButton'; import { Loading } from '../app/Loading'; import { Text, LABEL_TYPES } from '../text/Text'; @@ -85,7 +84,6 @@ export const AgencySelection = (props: AgencySelectionProps) => { try { if (autoSelectAgency) { const response = await apiAgencySelection({ - postcode: DEFAULT_POSTCODE, consultingType: props.consultingType.id, topicId: props?.mainTopicId, age: props?.age, diff --git a/src/containers/registration/hooks/useAgenciesForRegistration.ts b/src/containers/registration/hooks/useAgenciesForRegistration.ts index 2621251fc..fbe24654c 100644 --- a/src/containers/registration/hooks/useAgenciesForRegistration.ts +++ b/src/containers/registration/hooks/useAgenciesForRegistration.ts @@ -1,7 +1,6 @@ import unionBy from 'lodash/unionBy'; import { useContext, useEffect, useMemo, useState } from 'react'; import { apiAgencySelection } from '../../../api'; -import { DEFAULT_POSTCODE } from '../../../components/registration/prefillPostcode'; import { useTenant } from '../../../globalState'; import { AgencyDataInterface, @@ -11,6 +10,7 @@ import { import { useConsultantRegistrationData } from './useConsultantRegistrationData'; import { ConsultingTypeRegistrationDefaults } from '../components/ProposedAgencies/ProposedAgencies'; import { UrlParamsContext } from '../../../globalState/provider/UrlParamsProvider'; +import { VALID_POSTCODE_LENGTH } from '../../../components/agencySelection/agencySelectionHelpers'; interface AgenciesForRegistrationArgs { consultingType: ConsultingTypeInterface; @@ -78,8 +78,6 @@ export const useAgenciesForRegistration = ({ } uniqueAgencies = uniqueAgencies - // Filter by preselected agency - // Filter by consultingType .filter( (agency) => @@ -119,7 +117,12 @@ export const useAgenciesForRegistration = ({ useEffect(() => { const abortController = new AbortController(); // if we already have information from consulting types we can ignore the request - if (consultant || preselectedAgency || topicsEnabledAndUnSelected) { + if ( + consultant || + preselectedAgency || + topicsEnabledAndUnSelected || + (!autoSelectPostcode && postcode?.length !== VALID_POSTCODE_LENGTH) + ) { setIsLoading(false); return; } @@ -127,7 +130,7 @@ export const useAgenciesForRegistration = ({ setIsLoading(true); apiAgencySelection( { - postcode: autoSelectPostcode ? DEFAULT_POSTCODE : postcode, + ...(autoSelectPostcode ? {} : { postcode }), consultingType: consultingType?.id, topicId: topic?.id, fetchConsultingTypeDetails: true diff --git a/src/utils/loadConsultingTypeForAgency.ts b/src/utils/loadConsultingTypeForAgency.ts deleted file mode 100644 index 8d605db53..000000000 --- a/src/utils/loadConsultingTypeForAgency.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { apiGetConsultingType } from '../api'; -import { AgencyDataInterface } from '../globalState/interfaces'; - -export const loadConsultingTypeForAgency = async ( - agency: AgencyDataInterface -): Promise => ({ - ...agency, - consultingTypeRel: await apiGetConsultingType({ - consultingTypeId: agency?.consultingType - }) -}); From 623432ad3ecc234e7c5d250cfa63be833abe1d24 Mon Sep 17 00:00:00 2001 From: Marc Itzenthaler Date: Wed, 28 Feb 2024 16:27:52 +0100 Subject: [PATCH 4/4] feat: unique duplicate logic --- src/api/apiAgencySelection.ts | 24 ++---------------- src/api/apiGetConsultant.ts | 28 ++++----------------- src/utils/loadConsultingTypesForAgencies.ts | 26 +++++++++++++++++++ 3 files changed, 33 insertions(+), 45 deletions(-) create mode 100644 src/utils/loadConsultingTypesForAgencies.ts diff --git a/src/api/apiAgencySelection.ts b/src/api/apiAgencySelection.ts index 1b48eb94e..b96f99f98 100644 --- a/src/api/apiAgencySelection.ts +++ b/src/api/apiAgencySelection.ts @@ -1,7 +1,7 @@ import { endpoints } from '../resources/scripts/endpoints'; import { fetchData, FETCH_METHODS, FETCH_ERRORS } from './fetchData'; import { AgencyDataInterface } from '../globalState/interfaces'; -import { apiGetConsultingType } from './apiGetConsultingType'; +import { loadConsultingTypesForAgencies } from '../utils/loadConsultingTypesForAgencies'; export const apiAgencySelection = async ( { @@ -52,26 +52,6 @@ export const apiAgencySelection = async ( return agencies; } - // Get unique consultingTypes to prevent multiple requests to api - const uniqueConsultingTypeIds = [ - ...new Set( - agencies.map((a) => a?.consultingType).filter(Boolean) - ) - ]; - - return Promise.all( - uniqueConsultingTypeIds.map((consultingTypeId) => - apiGetConsultingType({ - consultingTypeId - }) - ) - ).then((consultingTypes) => - agencies.map((a) => ({ - ...a, - consultingTypeRel: consultingTypes.find( - (c) => c.id === a.consultingType - ) - })) - ); + return loadConsultingTypesForAgencies(agencies); }); }; diff --git a/src/api/apiGetConsultant.ts b/src/api/apiGetConsultant.ts index 4177688a9..31e126b3e 100644 --- a/src/api/apiGetConsultant.ts +++ b/src/api/apiGetConsultant.ts @@ -1,7 +1,7 @@ import { endpoints } from '../resources/scripts/endpoints'; import { fetchData, FETCH_METHODS, FETCH_ERRORS } from './fetchData'; import { ConsultantDataInterface } from '../globalState/interfaces'; -import { apiGetConsultingType } from './apiGetConsultingType'; +import { loadConsultingTypesForAgencies } from '../utils/loadConsultingTypesForAgencies'; export const apiGetConsultant = async ( consultantId: string, @@ -14,32 +14,14 @@ export const apiGetConsultant = async ( method: FETCH_METHODS.GET, skipAuth: true, responseHandling: [FETCH_ERRORS.CATCH_ALL] - }).then((user: ConsultantDataInterface) => { + }).then(async (user: ConsultantDataInterface) => { if (!fetchConsultingTypeDetails) { return user; } - // Get unique consultingTypes to prevent multiple requests to api - const uniqueConsultingTypeIds = [ - ...new Set( - user.agencies.map((a) => a?.consultingType).filter(Boolean) - ) - ]; - - return Promise.all( - uniqueConsultingTypeIds.map((consultingTypeId) => - apiGetConsultingType({ - consultingTypeId - }) - ) - ).then((consultingTypes) => ({ + return { ...user, - agencies: user.agencies.map((a) => ({ - ...a, - consultingTypeRel: consultingTypes.find( - (c) => c.id === a.consultingType - ) - })) - })); + agencies: await loadConsultingTypesForAgencies(user.agencies) + }; }); }; diff --git a/src/utils/loadConsultingTypesForAgencies.ts b/src/utils/loadConsultingTypesForAgencies.ts new file mode 100644 index 000000000..09a183fea --- /dev/null +++ b/src/utils/loadConsultingTypesForAgencies.ts @@ -0,0 +1,26 @@ +import { apiGetConsultingType } from '../api'; +import { AgencyDataInterface } from '../globalState/interfaces'; + +export const loadConsultingTypesForAgencies = async ( + agencies: AgencyDataInterface[] +): Promise => { + // Get unique consultingTypes to prevent multiple requests to api + const uniqueConsultingTypeIds = [ + ...new Set(agencies.map((a) => a?.consultingType).filter(Boolean)) + ]; + + return Promise.all( + uniqueConsultingTypeIds.map((consultingTypeId) => + apiGetConsultingType({ + consultingTypeId + }) + ) + ).then((consultingTypes) => + agencies.map((a) => ({ + ...a, + consultingTypeRel: consultingTypes.find( + (c) => c.id === a.consultingType + ) + })) + ); +};