@salesforcedevs/dw-components 1.3.151-canary.1 → 1.3.155

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,319 +0,0 @@
1
- import { toJson } from "dxUtils/normalizers";
2
- import { api } from "lwc";
3
- import debounce from "debounce";
4
- import { LightningElementWithState } from "dxBaseElements/lightningElementWithState";
5
- import { DrmFormData } from "dw/j2sCardSequence";
6
-
7
- // Hard-coding for now because we can't get information about an endpoint for this
8
- const tbidValidRoles = [
9
- "Developer",
10
- "Administrator",
11
- "Architect",
12
- "Sales Representative / Manager",
13
- "Marketer",
14
- "Service Agent / Supervisor",
15
- "Data Analyst",
16
- "Consultant",
17
- "Business Operations Manager",
18
- "Designer",
19
- "Educator",
20
- "Student",
21
- "Executive",
22
- "Product Manager"
23
- ];
24
-
25
- export type J2sFormState = DrmFormData & {
26
- legalText: string;
27
- marketingText: string;
28
- learnMore: string;
29
- showLegalCheckbox: boolean;
30
- showMarketingText: boolean;
31
- isFormValid: boolean;
32
- };
33
-
34
- export default class J2sForm extends LightningElementWithState<J2sFormState> {
35
- private _countriesJson = "";
36
- private _countriesLookup: any = {}; // TODO typings
37
- private _didSetFormId = false;
38
- private _isInitialRender = true;
39
- private _recaptchaToken = "";
40
-
41
- @api formMetadata: any = {};
42
- @api id = "";
43
- @api incomingUserInfo = {} as DrmFormData;
44
- @api referralText = "";
45
-
46
- @api
47
- get countries() {
48
- return this._countriesJson || "[]";
49
- }
50
- set countries(value: string) {
51
- this._countriesJson = value;
52
-
53
- // Store a lookup table of country data by value so that we don't have to loop all the time
54
- const countries = toJson(value);
55
- const countriesLookup: any = {};
56
- countries.forEach((country: any) => {
57
- countriesLookup[country.value] = country;
58
- });
59
- this._countriesLookup = countriesLookup;
60
- }
61
-
62
- @api
63
- get formDataForDrm(): Partial<DrmFormData> {
64
- // We should be able to just get the formData from the form automatically using `new FormData(theForm)`,
65
- // but our custom form elements don't seem to implement what it is required for that currently. See:
66
- // https://web.dev/more-capable-form-controls/#defining-a-form-associated-custom-element
67
- const formData: any = {};
68
- this.template
69
- .querySelectorAll("form [name]")
70
- .forEach((namedElement) => {
71
- const typedElement = namedElement as HTMLInputElement;
72
- formData[typedElement.name] =
73
- typeof typedElement.checked === "boolean"
74
- ? typedElement.checked
75
- : typedElement.value;
76
- });
77
- return formData;
78
- }
79
-
80
- @api
81
- get recaptchaToken() {
82
- return this._recaptchaToken;
83
- }
84
- set recaptchaToken(value: string) {
85
- this._recaptchaToken = value;
86
- if (!this._isInitialRender) {
87
- // Treat recaptcha checkbox as a required form field even though not technically marked as such:
88
- this.setState({
89
- isFormValid: this.checkValidity()
90
- });
91
- }
92
- }
93
-
94
- private participatingCountryCodes = ["US", "CA", "IN"];
95
- private countriesWithReferralInput = ["IN"];
96
- private requiredFormFields: HTMLElement[] = [];
97
- private showSubmitFailureAlert = false;
98
-
99
- get isNotParticipatingCountry() {
100
- return !this.isParticipatingCountry(this.state.MailingCountry);
101
- }
102
-
103
- get isCountryWithReferralInput() {
104
- return this.countriesWithReferralInput.includes(
105
- this.state.MailingCountry
106
- );
107
- }
108
-
109
- get notAvailableAlertInfo() {
110
- return this.formMetadata.notAvailableAlertInfo;
111
- }
112
-
113
- get formSubmitFailureAlertInfo() {
114
- return this.formMetadata.formSubmitFailureAlertInfo;
115
- }
116
-
117
- get jobRoles() {
118
- return tbidValidRoles.map((role) => ({
119
- label: role,
120
- value: role
121
- }));
122
- }
123
-
124
- connectedCallback() {
125
- const initialState: Partial<J2sFormState> = {
126
- ...this.incomingUserInfo,
127
- isFormValid: false,
128
- showMarketingText: true
129
- };
130
- const initialCountry = initialState.MailingCountry;
131
- const initialCountryData =
132
- initialCountry && this._countriesLookup[initialCountry];
133
- if (initialCountryData) {
134
- initialState.showLegalCheckbox =
135
- initialCountryData.showLegalCheckbox;
136
- initialState.showMarketingText =
137
- initialCountryData.showMarketingText;
138
- initialState.legalText = initialCountryData.legalText;
139
- initialState.marketingText = initialCountryData.marketingText;
140
- initialState.learnMore = initialCountryData.learnMore;
141
- }
142
-
143
- this.state = initialState as J2sFormState;
144
- }
145
-
146
- renderedCallback(): void {
147
- if (this._isInitialRender) {
148
- this.updateInnerHtml("span.j2s-terms", this.formMetadata.j2sTerms);
149
- this._isInitialRender = false;
150
- }
151
-
152
- if (
153
- this.state.MailingCountry &&
154
- this.state.MailingCountry !== this.prevState.MailingCountry
155
- ) {
156
- // Re-render new country's terms
157
- this.updateInnerHtml(
158
- "span.marketing-terms",
159
- this.state.marketingText
160
- );
161
- this.updateInnerHtml("span.legal-terms", this.state.legalText);
162
- if (this.state.learnMore) {
163
- this.updateInnerHtml(
164
- "span.legal-terms-learn-more",
165
- this.state.learnMore
166
- );
167
- } else {
168
- this.updateInnerHtml("span.legal-terms-learn-more", "");
169
- this.template
170
- .querySelector(".legal-terms-learn-more")
171
- ?.classList.add("hidden");
172
- }
173
- }
174
-
175
- if (this.state.isFormValid !== this.prevState.isFormValid) {
176
- this.dispatchEvent(
177
- new CustomEvent("formvaliditychange", {
178
- detail: {
179
- isValid: this.state.isFormValid
180
- },
181
- bubbles: true,
182
- composed: true
183
- })
184
- );
185
- }
186
-
187
- this.requiredFormFields = Array.from(
188
- this.template.querySelectorAll("[required]")
189
- );
190
-
191
- if (this.id && !this._didSetFormId) {
192
- this._didSetFormId = true;
193
- this.template.querySelector("form")?.setAttribute("id", this.id);
194
- }
195
- }
196
-
197
- handleLearnMore() {
198
- this.template
199
- .querySelector(".legal-terms-learn-more")
200
- ?.classList.toggle("hidden");
201
- }
202
-
203
- handleFormFieldChange = (evt: CustomEvent) => {
204
- // Event target information must be read prior to debouncing; the target is gone after the debounce.
205
- const { target = {}, detail } = evt;
206
- const { name: targetName, required: isRequiredField } =
207
- target as HTMLInputElement;
208
- const value =
209
- typeof detail === "string"
210
- ? detail
211
- : typeof detail.checked === "boolean"
212
- ? detail.checked
213
- : detail.value;
214
-
215
- // Perform the real work after a debounce, in order to prevent unnecessary work on rapid form value changes:
216
- this.callbackWithDebounce(() => {
217
- if (targetName === "MailingCountry") {
218
- this.handleCountryChange(value);
219
- } else {
220
- const nextState: Partial<J2sFormState> = {};
221
- if (targetName) {
222
- nextState[targetName as keyof J2sFormState] = value;
223
- }
224
- if (isRequiredField) {
225
- nextState.isFormValid = this.checkValidity();
226
- }
227
- this.setState(nextState);
228
- }
229
-
230
- this.unsetSubmitFailure();
231
- });
232
- };
233
-
234
- @api
235
- notifySubmitFailure(formSubmissionFailureState: Partial<DrmFormData>) {
236
- this.showSubmitFailureAlert = true;
237
- // After form submission failure, restore at most the following values to the form:
238
- const {
239
- FirstName,
240
- LastName,
241
- Email,
242
- MailingCountry,
243
- Company__c,
244
- DRM_Role__c,
245
- Referral_Email__c
246
- } = formSubmissionFailureState;
247
- this.setState({
248
- FirstName,
249
- LastName,
250
- Email,
251
- MailingCountry,
252
- Company__c,
253
- DRM_Role__c,
254
- Referral_Email__c
255
- });
256
- }
257
-
258
- private unsetSubmitFailure() {
259
- if (this.showSubmitFailureAlert) {
260
- this.showSubmitFailureAlert = false;
261
- }
262
- }
263
-
264
- private callbackWithDebounce = debounce((callback: Function) => {
265
- callback();
266
- }, 1000 / 15 /* 15fps seems reasonable */);
267
-
268
- private handleCountryChange(countryCode: string) {
269
- const countryData = this._countriesLookup[countryCode];
270
-
271
- this.setState({
272
- MailingCountry: countryCode,
273
- learnMore: countryData.learnMore,
274
- legalText: countryData.legalText,
275
- marketingText: countryData.marketingText,
276
- showLegalCheckbox: countryData.showLegalCheckbox,
277
- showMarketingText: countryData.showMarketingText
278
- });
279
-
280
- this.dispatchEvent(
281
- new CustomEvent("countrychange", {
282
- bubbles: true,
283
- composed: true,
284
- detail: countryCode
285
- })
286
- );
287
-
288
- // Because updating the country can update the required fields, we need to re-set form
289
- // validity _after_ render
290
- requestAnimationFrame(() =>
291
- this.setState({
292
- isFormValid: this.checkValidity()
293
- })
294
- );
295
- }
296
-
297
- private updateInnerHtml(selector: string, html: string): void {
298
- const element = this.template.querySelector(selector);
299
- if (element) {
300
- element.innerHTML = html;
301
- }
302
- }
303
-
304
- private isParticipatingCountry(country: string) {
305
- return this.participatingCountryCodes.includes(country);
306
- }
307
-
308
- private checkValidity() {
309
- return (
310
- Boolean(this.recaptchaToken) &&
311
- this.isParticipatingCountry(this.state.MailingCountry) &&
312
- this.requiredFormFields.every(
313
- (requiredFormField) =>
314
- // @ts-ignore
315
- requiredFormField.checkValidity?.() === true
316
- )
317
- );
318
- }
319
- }