Skip to content

Commit 9e7ae82

Browse files
authored
Merge pull request #1661 from FromDoppler/doi-2928-do-not-allow-delete-email-field
[DOI-2928] Do not allow delete email field
2 parents f7af82c + 47fc23e commit 9e7ae82

4 files changed

Lines changed: 90 additions & 53 deletions

File tree

src/customJs/tools/promotional/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import {
1111
richTextProperty,
1212
textProperty,
1313
} from '../../properties/helpers';
14-
import { availableFields } from '../smartforms/helper';
14+
import {
15+
applyFieldsWidgetModalRestrictions,
16+
availableFields,
17+
} from '../smartforms/helper';
1518
import { subscriptionListProperty } from '../../properties/subscription_list';
1619
import { buttonGroupProperty } from '../../properties/button_group';
1720
import { labeledAutoWidthProperty } from '../../properties/labeled_auto_width';
@@ -70,6 +73,11 @@ export const getPromotionalToolDefinition: () =>
7073
label: $t('_dp.promotional.label'),
7174
icon: `${ASSETS_BASE_URL}/promotion_code_v2.svg`,
7275
usageLimit: 1,
76+
modalUpdate: addEventListener(
77+
'click',
78+
applyFieldsWidgetModalRestrictions,
79+
true,
80+
),
7381
Component: PromotionalViewer,
7482
values: {
7583
containerPadding: '0px',

src/customJs/tools/smartforms/helper.ts

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { $t } from '../../localization';
22
import { dropdownProperty } from '../../properties/helpers';
33
import { UnlayerProperty } from '../../types';
4-
import { CustomField, SmartFormAction } from './types';
4+
import { CustomField, SmartFormAction, UnlayerField } from './types';
55

66
export const behaviorListProperty: () => UnlayerProperty<string> = () =>
77
dropdownProperty({
@@ -69,19 +69,20 @@ const optionToString = (options: string[] | undefined) => {
6969
?.slice(0, -1);
7070
};
7171

72-
export const availableFields = userData.fields?.map((field: CustomField) => {
73-
return {
74-
meta_data: {
75-
...field,
76-
},
77-
name: field.name,
78-
type: getFieldCompatibleType(field.type),
79-
label: field.name,
80-
required: field.required,
81-
show_label: true,
82-
options: optionToString(field.allowedValues),
83-
};
84-
});
72+
export const availableFields: UnlayerField[] =
73+
userData.fields?.map((field: CustomField) => {
74+
return {
75+
meta_data: {
76+
...field,
77+
},
78+
name: field.name,
79+
type: getFieldCompatibleType(field.type),
80+
label: field.name,
81+
required: field.required,
82+
show_label: true,
83+
options: optionToString(field.allowedValues),
84+
};
85+
}) ?? [];
8586

8687
export const isValidUrl = (url: string) => {
8788
/* eslint-disable no-useless-escape */
@@ -90,3 +91,53 @@ export const isValidUrl = (url: string) => {
9091
);
9192
return URL_VALID_REGEX.test(url);
9293
};
94+
95+
export const applyFieldsWidgetModalRestrictions = () => {
96+
window.requestAnimationFrame(() => {
97+
const modal = document.querySelector('.blockbuilder-fields-widget-modal');
98+
if (!(modal instanceof HTMLElement)) {
99+
return;
100+
}
101+
102+
const modalBody = modal.querySelector('.modal-content');
103+
const modalFooter = modal.querySelector('.modal-footer');
104+
if (
105+
!(modalBody instanceof HTMLElement) ||
106+
!(modalFooter instanceof HTMLElement)
107+
) {
108+
return;
109+
}
110+
111+
const typeButton = modalBody.querySelector('button.dropdown-button');
112+
const fieldNameInput = modalBody.querySelector('input[name="name"]');
113+
const inputOption = modalBody.querySelector('textarea');
114+
const fieldName =
115+
fieldNameInput instanceof HTMLInputElement
116+
? fieldNameInput.value
117+
: undefined;
118+
const currentField = availableFields.find(
119+
(field: UnlayerField) => field.name === fieldName,
120+
);
121+
122+
if (typeButton instanceof HTMLButtonElement && currentField) {
123+
typeButton.disabled = currentField.type !== 'text';
124+
}
125+
126+
if (inputOption instanceof HTMLTextAreaElement && currentField) {
127+
inputOption.disabled = currentField.type !== 'text';
128+
}
129+
130+
const footerButtons = modalFooter.querySelectorAll('button');
131+
const buttonEliminate = footerButtons[1];
132+
if (
133+
buttonEliminate instanceof HTMLButtonElement &&
134+
fieldNameInput instanceof HTMLInputElement
135+
) {
136+
buttonEliminate.disabled = fieldNameInput.value === 'EMAIL';
137+
}
138+
139+
if (fieldNameInput instanceof HTMLInputElement) {
140+
fieldNameInput.closest('form')?.remove();
141+
}
142+
});
143+
};

src/customJs/tools/smartforms/index.ts

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { $t } from '../../localization';
22
import { SmartFormViewer } from './smartFormViewer';
33
import { ReactToolDefinitionFrom } from '../../types';
4-
import { SmartFormBase, SmartFormValues, UnlayerField } from './types';
4+
import { SmartFormBase, SmartFormValues } from './types';
55
import { ASSETS_BASE_URL } from '../../constants';
66
import {
77
alignmentProperty,
@@ -14,6 +14,7 @@ import {
1414
richTextProperty,
1515
} from '../../properties/helpers';
1616
import {
17+
applyFieldsWidgetModalRestrictions,
1718
behaviorListProperty,
1819
congratsBehaviorListProperty,
1920
availableFields,
@@ -32,7 +33,11 @@ export const getSmartFormToolDefinition: () =>
3233
label: $t('_dp.smart_forms.label'),
3334
icon: `${ASSETS_BASE_URL}/form1.svg`,
3435
usageLimit: 1,
35-
modalUpdate: addEventListener('click', getClick, true),
36+
modalUpdate: addEventListener(
37+
'click',
38+
applyFieldsWidgetModalRestrictions,
39+
true,
40+
),
3641
Component: SmartFormViewer,
3742
options: {
3843
behavior: {
@@ -300,38 +305,3 @@ export const getSmartFormToolDefinition: () =>
300305
},
301306
};
302307
};
303-
304-
/* Note: temporal function for manipulating UNLAYER field edit modal */
305-
const getClick = (event: any) => {
306-
if (event.target.className == 'col-12') {
307-
const modalBody = document.querySelectorAll(
308-
'.blockbuilder-fields-widget-modal > div > div ',
309-
)[1];
310-
if (!modalBody) {
311-
return;
312-
}
313-
const modalfooter = document.querySelectorAll(
314-
'.blockbuilder-fields-widget-modal > div > div ',
315-
)[2];
316-
const forms = modalBody.getElementsByTagName('form');
317-
const inputs = modalBody.getElementsByTagName('input');
318-
const inputName = inputs[0].value;
319-
const currentField = availableFields.find((field: UnlayerField) => {
320-
return field.name == inputName;
321-
});
322-
323-
//disabled change type update
324-
const buttonType = modalBody.getElementsByTagName('button')[0];
325-
buttonType['disabled'] = currentField.type !== 'text';
326-
327-
//disabled dropdown values update
328-
const inputOption = modalBody.getElementsByTagName('textarea')[0];
329-
if (inputOption) inputOption['disabled'] = currentField.type !== 'text';
330-
331-
const buttonEliminate = modalfooter.getElementsByTagName('button')[1];
332-
buttonEliminate['disabled'] = inputName === 'EMAIL';
333-
334-
//remove name fieldset
335-
forms[0].remove();
336-
}
337-
};

src/customJs/tools/wheel/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import {
88
} from '../../properties/helpers';
99
import { subscriptionListProperty } from '../../properties/subscription_list';
1010
import { wheelListProperty } from '../../properties/wheel_list';
11-
import { availableFields } from '../smartforms/helper';
11+
import {
12+
applyFieldsWidgetModalRestrictions,
13+
availableFields,
14+
} from '../smartforms/helper';
1215
import { WheelFortuneValues, WheelSlice } from './types';
1316
import { buttonGroupProperty } from '../../properties/button_group';
1417

@@ -71,6 +74,11 @@ export const getWheelFortuneToolDefinition: () =>
7174
label: $t('_dp.wheel_fortune.label'),
7275
icon: `${ASSETS_BASE_URL}/roulette2.svg`,
7376
usageLimit: 1,
77+
modalUpdate: addEventListener(
78+
'click',
79+
applyFieldsWidgetModalRestrictions,
80+
true,
81+
),
7482

7583
Component: WheelFortuneViewer,
7684
options: {

0 commit comments

Comments
 (0)