Skip to content

Commit 2104f00

Browse files
Merge pull request #281 from open-formulieren/bug/5817-profile-preferences-modal-initial-values
Fix initial value of digitalAddressPreferencesModal
2 parents 3440220 + 68dfa98 commit 2104f00

File tree

3 files changed

+68
-8
lines changed

3 files changed

+68
-8
lines changed

src/registry/customerProfile/digitalAddressPreferencesModal.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,20 @@ import Modal from '@/components/modal/Modal';
1414
import PortalUrl from './PortalUrl';
1515
import {useCustomerProfileComponentParameters} from './hooks';
1616

17+
type FormValues = {preference: PreferenceUpdateOptions};
18+
type PreferenceRadioOption = RadioOption & {value: PreferenceUpdateOptions};
19+
1720
interface DigitalAddressPreferencesModalProps extends Pick<ModalProps, 'closeModal' | 'isOpen'> {
1821
onSubmit: (preference: PreferenceUpdateOptions) => void;
1922
digitalAddressType: DigitalAddressType;
23+
initialValue: FormValues;
2024
}
2125

22-
type FormValues = {preference: PreferenceUpdateOptions};
23-
type PreferenceRadioOption = RadioOption & {value: PreferenceUpdateOptions};
24-
2526
const DigitalAddressPreferencesModal: React.FC<DigitalAddressPreferencesModalProps> = ({
2627
closeModal,
2728
isOpen,
2829
onSubmit,
30+
initialValue,
2931
digitalAddressType,
3032
}) => {
3133
const id = useId();
@@ -44,8 +46,7 @@ const DigitalAddressPreferencesModal: React.FC<DigitalAddressPreferencesModalPro
4446
closeModal={closeModal}
4547
>
4648
<Formik<FormValues>
47-
// By default, use the 'useOnlyOnce' option
48-
initialValues={{preference: 'useOnlyOnce'}}
49+
initialValues={initialValue}
4950
onSubmit={({preference}) => onSubmit(preference)}
5051
>
5152
<Form>

src/registry/customerProfile/index.stories.tsx

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,61 @@ export const OpenPreferencesModal: Story = {
149149
},
150150
};
151151

152+
export const OpenPreferencesModalWithInitialValue: Story = {
153+
args: {
154+
componentDefinition: {
155+
id: 'customerProfile',
156+
type: 'customerProfile',
157+
key: 'customerProfile',
158+
label: 'Profile',
159+
digitalAddressTypes: ['email'],
160+
shouldUpdateCustomerData: false,
161+
},
162+
},
163+
parameters: {
164+
formik: {
165+
initialValues: {
166+
customerProfile: [
167+
{
168+
type: 'email',
169+
address: 'test@mail.com',
170+
preferenceUpdate: 'isNewPreferred',
171+
},
172+
],
173+
},
174+
},
175+
},
176+
play: async ({canvasElement, step}) => {
177+
const canvas = within(canvasElement);
178+
const emailField = await canvas.findByLabelText('Email');
179+
180+
// Initial value is set
181+
expect(emailField).toHaveValue('test@mail.com');
182+
183+
// Open preferences modal
184+
await userEvent.click(
185+
await canvas.findByRole('button', {
186+
name: 'Update preferences',
187+
})
188+
);
189+
190+
await step('In preferences modal', async () => {
191+
// Open preferences modal
192+
const modal = within(await canvas.findByRole('dialog'));
193+
const forFutureUseRadio = modal.getByRole('radio', {
194+
name: 'Save my preferences for the next time. You can always change them again later in the portal.',
195+
});
196+
const oneTimeUseRadio = modal.getByRole('radio', {
197+
name: 'Use this email address only for this form.',
198+
});
199+
200+
// Expect the preference from the initial value to be checked
201+
expect(forFutureUseRadio).toBeChecked();
202+
expect(oneTimeUseRadio).not.toBeChecked();
203+
});
204+
},
205+
};
206+
152207
export const OpenPreferencesModalWithoutPortalUrl: Story = {
153208
args: {
154209
componentDefinition: {

src/registry/customerProfile/subFields.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,12 @@ const DigitalAddressTextfield: React.FC<DigitalAddressTextfieldProps> = ({
159159
const {getFieldHelpers, getFieldMeta} = useFormikContext<DigitalAddress>();
160160
const [isModalOpen, setIsModalOpen] = useState(false);
161161
const {updatePreferencesModalEnabled} = useCustomerProfileComponentParameters();
162-
const {setValue: setPreference} = getFieldHelpers<DigitalAddress['preferenceUpdate']>(
163-
`${namePrefix}.preferenceUpdate`
164-
);
162+
163+
const preferenceUpdateFieldName = `${namePrefix}.preferenceUpdate`;
164+
const {value: preference} =
165+
getFieldMeta<DigitalAddress['preferenceUpdate']>(preferenceUpdateFieldName);
166+
const {setValue: setPreference} =
167+
getFieldHelpers<DigitalAddress['preferenceUpdate']>(preferenceUpdateFieldName);
165168

166169
const {value, error} = getFieldMeta<DigitalAddress['address']>(fieldName);
167170

@@ -205,6 +208,7 @@ const DigitalAddressTextfield: React.FC<DigitalAddressTextfieldProps> = ({
205208
</ButtonGroup>
206209
<DigitalAddressPreferencesModal
207210
isOpen={isModalOpen}
211+
initialValue={{preference: preference || 'useOnlyOnce'}}
208212
closeModal={() => setIsModalOpen(false)}
209213
onSubmit={newPreference => {
210214
// Update the preference field

0 commit comments

Comments
 (0)