Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: agency list update registration #1014

Merged
merged 5 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cypress/e2e/login.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
Expand Down
18 changes: 6 additions & 12 deletions cypress/e2e/registration/topic.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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');
});
});
});
Expand Down
68 changes: 29 additions & 39 deletions src/api/apiAgencySelection.ts
Original file line number Diff line number Diff line change
@@ -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 { loadConsultingTypesForAgencies } from '../utils/loadConsultingTypesForAgencies';

export const apiAgencySelection = async (
{
fetchConsultingTypeDetails,
...params
}: {
postcode: string;
postcode?: string;
consultingType: number | undefined;
topicId?: number;
age?: number;
Expand All @@ -18,50 +17,41 @@ export const apiAgencySelection = async (
fetchConsultingTypeDetails?: boolean;
},
signal?: AbortSignal
): Promise<Array<AgencyDataInterface> | null> => {
): Promise<AgencyDataInterface[] | null> => {
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;
}

return Promise.all(
agencies.map(
async (agency) =>
await loadConsultingTypeForAgency(agency)
)
);
});
} else {
return null;
}
return loadConsultingTypesForAgencies(agencies);
});
};
9 changes: 7 additions & 2 deletions src/api/apiGetAgencyId.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -21,6 +21,11 @@ export const apiGetAgencyById = async (
return agency;
}

return await loadConsultingTypeForAgency(agency);
return {
...agency,
consultingTypeRel: await apiGetConsultingType({
consultingTypeId: agency?.consultingType
})
};
});
};
18 changes: 6 additions & 12 deletions src/api/apiGetConsultant.ts
Original file line number Diff line number Diff line change
@@ -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 { loadConsultingTypesForAgencies } from '../utils/loadConsultingTypesForAgencies';

export const apiGetConsultant = async (
consultantId: string,
Expand All @@ -14,20 +14,14 @@ export const apiGetConsultant = async (
method: FETCH_METHODS.GET,
skipAuth: true,
responseHandling: [FETCH_ERRORS.CATCH_ALL]
}).then((user) => {
}).then(async (user: ConsultantDataInterface) => {
if (!fetchConsultingTypeDetails) {
return user;
}

return Promise.all(
user.agencies.map(
async (agency) => await loadConsultingTypeForAgency(agency)
)
).then(
(agencies): ConsultantDataInterface => ({
...user,
agencies
})
);
return {
...user,
agencies: await loadConsultingTypesForAgencies(user.agencies)
};
});
};
2 changes: 0 additions & 2 deletions src/components/agencySelection/AgencySelection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -218,23 +228,24 @@ export const ConsultingTypeAgencySelection = ({
</div>
)}

{selectedConsultingTypeOption && agencyOptions.length > 1 && (
<div className="agencySelection">
{consultingTypeOptions.length <= 1 && (
<Text
text={translate(
'registration.consultingTypeAgencySelection.agency.infoText'
)}
type="standard"
{(selectedConsultingTypeOption || selectedTopicOption) &&
agencyOptions.length > 1 && (
<div className="agencySelection">
{consultingTypeOptions.length <= 1 && (
<Text
text={translate(
'registration.consultingTypeAgencySelection.agency.infoText'
)}
type="standard"
/>
)}
<AgencySelection
agencies={agencyOptions}
onChange={handleChange}
selectedAgency={agency}
/>
)}
<AgencySelection
agencies={agencyOptions}
onChange={handleChange}
selectedAgency={agency}
/>
</div>
)}
</div>
)}
</div>
);
};
Expand Down
2 changes: 1 addition & 1 deletion src/components/formAccordion/FormAccordion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const FormAccordion = ({
const { setSpecificAgency, specificAgency } = useContext(
AgencySpecificContext
);
const { consultingTypes } = useConsultantRegistrationData();
const { consultingTypes } = useConsultantRegistrationData({});

const [activeItem, setActiveItem] = useState<number>(1);

Expand Down
2 changes: 1 addition & 1 deletion src/components/login/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export const Login = () => {
agencies: possibleAgencies,
consultingTypes: possibleConsultingTypes,
topicIds: possibleTopicIds
} = useConsultantRegistrationData();
} = useConsultantRegistrationData({});

const registerOverlay = useMemo(
(): OverlayItem => ({
Expand Down
18 changes: 12 additions & 6 deletions src/containers/registration/hooks/useAgenciesForRegistration.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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<AgencyDataInterface[]>([]);
Expand Down Expand Up @@ -75,8 +78,6 @@ export const useAgenciesForRegistration = ({
}

uniqueAgencies = uniqueAgencies
// Filter by preselected agency

// Filter by consultingType
.filter(
(agency) =>
Expand Down Expand Up @@ -116,15 +117,20 @@ 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;
}

setIsLoading(true);
apiAgencySelection(
{
postcode: autoSelectPostcode ? DEFAULT_POSTCODE : postcode,
...(autoSelectPostcode ? {} : { postcode }),
consultingType: consultingType?.id,
topicId: topic?.id,
fetchConsultingTypeDetails: true
Expand Down
Loading
Loading