@salesforcedevs/dw-components 1.3.148-canary.5 → 1.3.148-canary.7

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@salesforcedevs/dw-components",
3
- "version": "1.3.148-canary.5",
3
+ "version": "1.3.148-canary.7",
4
4
  "description": "Lightning web components for https://developer.salesforce.com",
5
5
  "license": "MIT",
6
6
  "engines": {
@@ -35,10 +35,10 @@
35
35
  <dw-j2s-form
36
36
  id={stepDatum.formData.formId}
37
37
  countries={countries}
38
- drm-recaptcha-site-key={drmRecaptchaSiteKey}
39
38
  form-metadata={stepDatum.formMetadata}
40
39
  incoming-user-info={formIncomingUserInfo}
41
40
  onformvaliditychange={handleFormValidityChange}
41
+ recaptcha-token={recaptchaToken}
42
42
  >
43
43
  <div slot="form-captcha-slot">
44
44
  <slot name="captcha-slot"></slot>
@@ -51,7 +51,6 @@ export default class J2sCardSequence extends LightningElement {
51
51
  _stepsData: StepDatum[] = [];
52
52
  @api animateTransitions = false;
53
53
  @api drmDomain: string = "https://drm.my.salesforce-sites.com";
54
- @api drmRecaptchaSiteKey: string = "";
55
54
  @api formHandler: string =
56
55
  "https://formhandlermule-1s9xrb.5sc6y6-4.usa-e2.cloudhub.io/form_handler";
57
56
  @api tbidApiBaseUrl: string | undefined;
@@ -91,6 +90,7 @@ export default class J2sCardSequence extends LightningElement {
91
90
 
92
91
  private countries = "[]";
93
92
  private currentSequenceStep = 0;
93
+ private recaptchaToken = "";
94
94
  private sessionStorageId = "j2s-card-sequence";
95
95
  private tbidUserInfo: any; // TODO typings?
96
96
 
@@ -158,6 +158,16 @@ export default class J2sCardSequence extends LightningElement {
158
158
  );
159
159
  }
160
160
 
161
+ @api
162
+ recaptchaCallback(responseToken: string) {
163
+ this.recaptchaToken = responseToken;
164
+ }
165
+
166
+ @api
167
+ recaptchaExpiredCallback() {
168
+ this.recaptchaToken = "";
169
+ }
170
+
161
171
  connectedCallback() {
162
172
  this.getCountryList();
163
173
  this.getTbidUserInfo();
@@ -203,8 +213,8 @@ export default class J2sCardSequence extends LightningElement {
203
213
  }
204
214
  }
205
215
 
206
- handleFormSubmit(evt: Event) {
207
- if (!this.formHandler || this.isJ2sFormInvalid) {
216
+ async handleFormSubmit(evt: Event) {
217
+ if (!this.formHandler || this.isJ2sFormInvalid || !this.isLoggedIn) {
208
218
  return;
209
219
  }
210
220
 
@@ -221,10 +231,32 @@ export default class J2sCardSequence extends LightningElement {
221
231
  if (typeof formData.Privacy_Statement__c !== "boolean") {
222
232
  formData.Privacy_Statement__c = true;
223
233
  }
224
- console.log("would submit", JSON.stringify(formData, null, 2));
225
- this.jumpToStepIndex(2);
234
+
235
+ console.log("submitting", JSON.stringify(formData, null, 2));
236
+ const res = await fetch(this.formHandler, {
237
+ method: "POST",
238
+ headers: {
239
+ "Captcha-Response": this.recaptchaToken
240
+ },
241
+ body: JSON.stringify({
242
+ ...formData,
243
+ handlerName: "J2S Registration Page",
244
+ TBID__c: this.tbidUserInfo.user_id
245
+ })
246
+ });
247
+
248
+ if (res.ok) {
249
+ const resData = await res.json();
250
+ if (resData.status === "success") {
251
+ this.jumpToStepIndex(2);
252
+ } else {
253
+ throw new Error(
254
+ `Form submission failed. (status: ${resData.status}, message: ${resData.errorMessage})`
255
+ );
256
+ }
257
+ }
226
258
  } catch (ex) {
227
- console.log(ex); // TODO
259
+ console.error(ex); // TODO
228
260
  }
229
261
 
230
262
  this._isFormSubmitting = false;
@@ -44,3 +44,9 @@
44
44
  .form-terms-container {
45
45
  padding-top: var(--dx-g-spacing-sm);
46
46
  }
47
+
48
+ .form-captcha-slot {
49
+ align-items: center;
50
+ display: flex;
51
+ justify-content: center;
52
+ }
@@ -154,7 +154,7 @@
154
154
  class="legal-terms no-checkbox"
155
155
  ></span>
156
156
  </div>
157
- <slot name="form-captcha-slot"></slot>
157
+ <slot class="form-captcha-slot" name="form-captcha-slot"></slot>
158
158
  </div>
159
159
  </form>
160
160
  </template>
@@ -33,15 +33,13 @@ export type J2sFormState = {
33
33
  export default class J2sForm extends LightningElementWithState<J2sFormState> {
34
34
  private _countriesJson = "";
35
35
  private _countriesLookup: any = {}; // TODO typings
36
- private _didRenderRecaptcha = false;
37
36
  private _didSetFormId = false;
38
37
  private _formMetadata: any = {}; // TODO typings
39
38
  private _incomingUserInfo: any = {}; // TODO typings
40
39
  private _isInitialRender = true;
40
+ private _recaptchaToken = "";
41
41
 
42
- @api drmRecaptchaSiteKey = "";
43
42
  @api id = "";
44
- private recaptcha: any = null;
45
43
  @api referralText = "";
46
44
 
47
45
  @api
@@ -82,16 +80,27 @@ export default class J2sForm extends LightningElementWithState<J2sFormState> {
82
80
  // but our custom form elements don't seem to implement what it is required for that currently. See:
83
81
  // https://web.dev/more-capable-form-controls/#defining-a-form-associated-custom-element
84
82
  const formData: any = {};
85
- this.template.querySelectorAll("[name]").forEach((namedElement) => {
86
- const typedElement = namedElement as HTMLInputElement;
87
- formData[typedElement.name] =
88
- typeof typedElement.checked === "boolean"
89
- ? typedElement.checked
90
- : typedElement.value;
91
- });
83
+ this.template
84
+ .querySelectorAll("form [name]")
85
+ .forEach((namedElement) => {
86
+ const typedElement = namedElement as HTMLInputElement;
87
+ formData[typedElement.name] =
88
+ typeof typedElement.checked === "boolean"
89
+ ? typedElement.checked
90
+ : typedElement.value;
91
+ });
92
92
  return formData;
93
93
  }
94
94
 
95
+ @api
96
+ get recaptchaToken() {
97
+ return this._recaptchaToken;
98
+ }
99
+ set recaptchaToken(value: string) {
100
+ this._recaptchaToken = value;
101
+ this.handleRequiredFieldUpdate();
102
+ }
103
+
95
104
  private participatingCountryCodes = ["US", "CA", "IN"];
96
105
  private countriesWithReferralInput = ["IN"];
97
106
  private countriesWithRequiredJobRole = ["CA"];
@@ -138,12 +147,6 @@ export default class J2sForm extends LightningElementWithState<J2sFormState> {
138
147
  }
139
148
 
140
149
  this.state = initialState as J2sFormState;
141
-
142
- if ((window as any).grecaptcha) {
143
- this.setRecaptcha();
144
- } else {
145
- window.addEventListener("recaptchaloaded", this.setRecaptcha);
146
- }
147
150
  }
148
151
 
149
152
  renderedCallback(): void {
@@ -197,31 +200,8 @@ export default class J2sForm extends LightningElementWithState<J2sFormState> {
197
200
  this._didSetFormId = true;
198
201
  this.template.querySelector("form")?.setAttribute("id", this.id);
199
202
  }
200
-
201
- // if (!this._didRenderRecaptcha && this.recaptcha) {
202
- // this._didRenderRecaptcha = true;
203
- // this.recaptcha.render(this.template.querySelector(".captcha"), {
204
- // sitekey: this.drmRecaptchaSiteKey,
205
- // callback: (recaptchaToken: string) => {
206
- // console.log("got captcha", recaptchaToken);
207
- // },
208
- // "expired-callback": () => {},
209
- // "error-callback": (...args: any[]) => {
210
- // console.log("RECAPTCHA ERROR");
211
- // console.log(...args);
212
- // }
213
- // });
214
- // }
215
203
  }
216
204
 
217
- disconnectedCallback() {
218
- window.removeEventListener("recaptchaloaded", this.setRecaptcha);
219
- }
220
-
221
- setRecaptcha = () => {
222
- this.recaptcha = (window as any).grecaptcha;
223
- };
224
-
225
205
  handleRequiredFieldUpdate() {
226
206
  const isFormNowValid = this.checkValidity();
227
207
  if (this.state.isFormValid !== isFormNowValid) {
@@ -271,6 +251,7 @@ export default class J2sForm extends LightningElementWithState<J2sFormState> {
271
251
 
272
252
  private checkValidity() {
273
253
  return (
254
+ Boolean(this.recaptchaToken) &&
274
255
  this.isParticipatingCountry(this.state.country) &&
275
256
  this.requiredFormFields.every(
276
257
  (requiredFormField) =>