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 4 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
86 changes: 48 additions & 38 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 { apiGetConsultingType } from './apiGetConsultingType';

export const apiAgencySelection = async (
{
fetchConsultingTypeDetails,
...params
}: {
postcode: string;
postcode?: string;
consultingType: number | undefined;
topicId?: number;
age?: number;
Expand All @@ -18,50 +17,61 @@ 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;
}

// 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;
}
}))
);
});
};
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
})
};
});
};
32 changes: 22 additions & 10 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 { apiGetConsultingType } from './apiGetConsultingType';

export const apiGetConsultant = async (
consultantId: string,
Expand All @@ -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
)
}))
}));
});
};
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
Loading
Loading