Skip to content

Commit

Permalink
feat: added feature toggle logic for counselling relation
Browse files Browse the repository at this point in the history
  • Loading branch information
web-mi committed Feb 27, 2024
1 parent ad6937c commit 864fc7d
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 85 deletions.
21 changes: 11 additions & 10 deletions src/extensions/components/askerInfo/AskerInfoContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@ export const AskerInfoContent = () => {
);
}, [activeSession, type, userData]);

const translateKeys = {
gender: `profile.gender.options.${sessionData?.gender?.toLowerCase()}`,
counselling: `profile.counsellingRelation.${sessionData?.counsellingRelation?.toLowerCase()}`
};

return (
<>
{!sessionData && (
Expand All @@ -88,12 +83,18 @@ export const AskerInfoContent = () => {
/>
<ProfileDataItem
title="profile.gender.title"
content={translate(translateKeys.gender)}
/>
<ProfileDataItem
title="profile.status"
content={translate(translateKeys.counselling)}
content={translate(
`profile.gender.options.${sessionData?.gender?.toLowerCase()}`
)}
/>
{tenant.settings.featureCounsellingRelationsEnabled && (
<ProfileDataItem
title="profile.status"
content={translate(
`profile.counsellingRelation.${sessionData?.counsellingRelation?.toLowerCase()}`
)}
/>
)}
<ProfileDataItem
title="profile.postalCode"
content={sessionData?.postcode}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Field, FieldContext } from 'rc-field-form';

Check failure on line 1 in src/extensions/components/registration/AgencyFields/AgencySelection/index.tsx

View workflow job for this annotation

GitHub Actions / Prettier

src/extensions/components/registration/AgencyFields/AgencySelection/index.tsx#L1

There are issues with this file's formatting, please run Prettier to fix the errors
import { HOOK_MARK } from 'rc-field-form/lib/FieldContext';
import React, { useEffect, useState } from 'react';
import React, { useContext, useEffect, useState } from 'react';
import { apiAgencySelection, FETCH_ERRORS } from '../../../../../api';
import {
ConsultingTypeBasicInterface,
Expand All @@ -17,6 +17,7 @@ import { NoAgencyFound } from '../NoAgencyFound';
import './agencySelection.styles.scss';
import { useTranslation } from 'react-i18next';
import { setValueInCookie } from '../../../../../components/sessionCookie/accessSessionCookie';
import { TenantContext } from '../../../../../globalState';

interface AgencySelectionFormFieldProps {
preselectedAgencies?: AgencyDataInterface[];
Expand Down Expand Up @@ -91,7 +92,8 @@ export const AgencySelection = ({
consultingType,
preselectedAgencies
}: AgencySelectionFormFieldProps) => {
const field = React.useContext(FieldContext);
const field = useContext(FieldContext);
const { tenant } = useContext(TenantContext);
const [isLoading, setIsLoading] = useState(false);
const [agencies, setAgencies] = useState<AgencyDataInterface[]>([
...(preselectedAgencies || [])
Expand All @@ -103,12 +105,14 @@ export const AgencySelection = ({
postCode: postcode,
counsellingRelation
} = field.getFieldsValue();

const isValidToRequestData =
preselectedAgencies.length === 0 &&
Number(mainTopicId) >= 0 &&
age &&
gender &&
counsellingRelation &&
(!tenant.settings.featureCounsellingRelationsEnabled ||
counsellingRelation) &&
!!postcode?.match(REGEX_POSTCODE);

const { t: translate } = useTranslation();
Expand Down Expand Up @@ -168,12 +172,12 @@ export const AgencySelection = ({
? [
'registration.agencyPreselected.intro.point1',
'registration.agencyPreselected.intro.point2'
]
]
: [
'registration.agencySelection.intro.point1',
'registration.agencySelection.intro.point2',
'registration.agencySelection.intro.point3'
];
];

return (
<div className="agencySelection">
Expand All @@ -183,10 +187,10 @@ export const AgencySelection = ({
preselectedAgencies.length
? translate(
'registration.agencyPreselected.intro.overline'
)
)
: translate(
'registration.agencySelection.intro.overline'
)
)
}
type="infoLargeAlternative"
/>
Expand All @@ -196,10 +200,10 @@ export const AgencySelection = ({
preselectedAgencies.length
? translate(
'registration.agencyPreselected.intro.subline'
)
)
: translate(
'registration.agencySelection.intro.subline'
)
)
}
type="infoLargeAlternative"
/>
Expand Down
9 changes: 6 additions & 3 deletions src/extensions/components/registration/AgencyFields/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useContext } from 'react';
import { FieldContext } from 'rc-field-form';
import {
ConsultingTypeBasicInterface,
Expand All @@ -8,6 +8,7 @@ import { InputFormField } from '../InputFormField';
import { AgencySelection } from './AgencySelection';
import { useTranslation } from 'react-i18next';
import { Text } from '../../../../components/text/Text';
import { TenantContext } from '../../../../globalState';

interface AgencySelectionFormFieldProps {
preselectedAgencies: AgencyDataInterface[];
Expand All @@ -18,7 +19,8 @@ export const AgencySelectionFormField = ({
consultingType,
preselectedAgencies
}: AgencySelectionFormFieldProps) => {
const field = React.useContext(FieldContext);
const field = useContext(FieldContext);
const { tenant } = useContext(TenantContext);
const { t: translate } = useTranslation();
const { mainTopicId, gender, age, counsellingRelation } =
field.getFieldsValue();
Expand All @@ -29,7 +31,8 @@ export const AgencySelectionFormField = ({
Number(mainTopicId) >= 0 &&
gender &&
age &&
counsellingRelation
(!tenant.settings.featureCounsellingRelationsEnabled ||
counsellingRelation)
) || preselectedAgencies.length > 0 ? (
<AgencySelection
consultingType={consultingType}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { RadioBoxGroup } from '../RadioBoxGroup';
import * as React from 'react';
import { useTranslation } from 'react-i18next';
import { getUrlParameter } from '../../../../utils/getUrlParameter';
import { useMemo } from 'react';

export enum COUNSELLING_RELATIONS {
Self = 'SELF_COUNSELLING',
Relative = 'RELATIVE_COUNSELLING',
Parental = 'PARENTAL_COUNSELLING'
}

const CounsellingRelation = () => {
const { t: translate } = useTranslation();
const counsellingRelationParam = getUrlParameter('counsellingRelation');

// Get the counselling relation from the query parameter
const counsellingRelation = useMemo((): string | null => {
if (!counsellingRelationParam) return null;
const fullRelation = `${counsellingRelationParam.toUpperCase()}_COUNSELLING`;
const allRelations: string[] = Object.values(COUNSELLING_RELATIONS);
return allRelations.includes(fullRelation) ? fullRelation : null;
}, [counsellingRelationParam]);

return (
<RadioBoxGroup
name="counsellingRelation"
options={Object.values(COUNSELLING_RELATIONS).map((value) => ({
label: translate(
`registrationDigi.counsellingRelation.options.${value.toLowerCase()}`
),
value
}))}
preset={counsellingRelation}
/>
);
};

export default CounsellingRelation;
89 changes: 32 additions & 57 deletions src/extensions/components/registration/RegistrationForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,9 @@ import LegalLinks from '../../../components/legalLinks/LegalLinks';
import { FormAccordion } from './FormAccordion/FormAccordion';
import { FormAccordionItem } from './FormAccordion/FormAccordionItem';
import { UrlParamsContext } from '../../../globalState/provider/UrlParamsProvider';

enum CounsellingRelation {
Self = 'SELF_COUNSELLING',
Relative = 'RELATIVE_COUNSELLING',
Parental = 'PARENTAL_COUNSELLING'
}
import CounsellingRelation, {
COUNSELLING_RELATIONS
} from './CounsellingRelation';

enum Gender {
Male = 'MALE',
Expand All @@ -55,7 +52,7 @@ interface FormData {
'topicIds[]': number[];
'mainTopicId': number;
'gender': Gender;
'counsellingRelation': CounsellingRelation;
'counsellingRelation': COUNSELLING_RELATIONS;
}

export const RegistrationForm = () => {
Expand Down Expand Up @@ -141,29 +138,13 @@ export const RegistrationForm = () => {
const getValidRef = (ref: string) =>
ref.replace(/[^a-zA-Z0-9]/g, '').substring(0, 8);

// Get the counselling relation from the query parameter
const getCounsellingRelation = (): string | null => {
const queryRelation = urlQuery.get('counsellingRelation');

if (!queryRelation) return null;

const fullRelation = `${queryRelation.toUpperCase()}_COUNSELLING`;
const allRelations: string[] = Object.values(CounsellingRelation);

if (allRelations.includes(fullRelation)) {
return fullRelation;
}

return null;
};

const preselectedAgencies = useMemo(
() =>
agency
? [agency]
: consultant?.agencies
? consultant?.agencies
: [],
? consultant?.agencies
: [],
[agency, consultant?.agencies]
);

Expand All @@ -180,8 +161,10 @@ export const RegistrationForm = () => {
gender: formValues.gender,
age: Number(formValues.age),
topicIds: formValues['topicIds[]'].map(Number),
counsellingRelation: formValues.counsellingRelation,
consultingType: formValues.consultingTypeId,
...(tenant.settings.featureCounsellingRelationsEnabled && {
counsellingRelation: formValues.counsellingRelation
}),
...(consultant && { consultantId: consultant.consultantId }),
referer: urlQuery.get('ref')
? getValidRef(urlQuery.get('ref'))
Expand Down Expand Up @@ -352,37 +335,29 @@ export const RegistrationForm = () => {
/>
</div>
</FormAccordionItem>,
<FormAccordionItem
id={`step-counsellingRelation`}
key={`step-counsellingRelation`}
title={translate(
'registrationDigi.counsellingRelation.step.title'
)}
formFields={['counsellingRelation']}
errorOnTouchExtraFields={[
'counsellingRelation',
'topicIds[]',
'mainTopicId',
'agencyId',
'postCode',
'username',
'password'
]}
{...props}
>
<RadioBoxGroup
name="counsellingRelation"
options={Object.values(
CounsellingRelation
).map((value) => ({
label: translate(
`registrationDigi.counsellingRelation.options.${value.toLowerCase()}`
),
value
}))}
preset={getCounsellingRelation()}
/>
</FormAccordionItem>,
tenant.settings
.featureCounsellingRelationsEnabled && (
<FormAccordionItem
id={`step-counsellingRelation`}
key={`step-counsellingRelation`}
title={translate(
'registrationDigi.counsellingRelation.step.title'
)}
formFields={['counsellingRelation']}
errorOnTouchExtraFields={[
'counsellingRelation',
'topicIds[]',
'mainTopicId',
'agencyId',
'postCode',
'username',
'password'
]}
{...props}
>
<CounsellingRelation />
</FormAccordionItem>
),
<FormAccordionItem
id={`step-topics`}
key={`step-topics`}
Expand Down
53 changes: 47 additions & 6 deletions src/extensions/cypress/e2e/registration.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,26 @@ describe('registration', () => {
data
).as('agencies');
});

cy.intercept(endpoints.topicsData, [
{
id: 1,
name: 'Alkohol'
}
]);
});

describe('addiction', () => {
describe('Counselling relation enabled', () => {
beforeEach(() => {
cy.intercept(endpoints.topicsData, [
cy.willReturn(
'service.tenant.public',
{
id: 1,
name: 'Alkohol'
}
]);
settings: {
featureCounsellingRelationsEnabled: true
}
},
true
);
});

it('should have all generic registration page elements', () => {
Expand All @@ -55,4 +65,35 @@ describe('registration', () => {
cy.get('.stageLayout__toLogin').should('exist');
});
});
describe('Counselling relation disabled', () => {
beforeEach(() => {
cy.willReturn(
'service.tenant.public',
{
settings: {
featureCounsellingRelationsEnabled: false
}
},
true
);
});

it('should have all generic registration page elements', () => {
cy.visit('/suchtberatung/registration');
cy.wait('@consultingTypeServiceBySlugFull');
cy.get('[data-cy="close-welcome-screen"]').click();

cy.get('.registrationFormDigi__Input').should('exist');
cy.get('input[name="gender"]').should('exist');
cy.get('input[name="counsellingRelation"]').should('not.exist');
cy.get(
'.registrationFormDigi__InputTopicIdsContainer input'
).should('exist');
cy.get('#username').should('exist');
cy.get('#passwordInput').should('exist');
cy.get('#passwordConfirmation').should('exist');
cy.get('.button__primary').should('exist');
cy.get('.stageLayout__toLogin').should('exist');
});
});
});

0 comments on commit 864fc7d

Please sign in to comment.