@salesforcedevs/dw-components 1.3.147 → 1.3.148-canary.0

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.
@@ -0,0 +1,225 @@
1
+ import { toJson } from "dxUtils/normalizers";
2
+ import { api } from "lwc";
3
+ import { LightningElementWithState } from "dxBaseElements/lightningElementWithState";
4
+
5
+ // Hard-coding for now because we can't get information about an endpoint for this
6
+ const tbidValidRoles = [
7
+ "Developer",
8
+ "Administrator",
9
+ "Architect",
10
+ "Sales Representative / Manager",
11
+ "Marketer",
12
+ "Service Agent / Supervisor",
13
+ "Data Analyst",
14
+ "Consultant",
15
+ "Business Operations Manager",
16
+ "Designer",
17
+ "Educator",
18
+ "Student",
19
+ "Executive",
20
+ "Product Manager"
21
+ ];
22
+
23
+ export type J2sFormState = {
24
+ country: string;
25
+ legalText: string;
26
+ marketingText: string;
27
+ learnMore: string;
28
+ showLegalCheckbox: boolean;
29
+ showMarketingText: boolean;
30
+ isFormValid: boolean;
31
+ };
32
+
33
+ export default class J2sForm extends LightningElementWithState<J2sFormState> {
34
+ private _didSetFormId = false;
35
+ private _isInitialRender = true;
36
+ private _incomingUserInfo: any = {}; // TODO typings
37
+ private _formMetadata: any = {}; // TODO typings
38
+ private _countriesJson = "";
39
+ private _countriesLookup: any = {}; // TODO typings
40
+ @api id = "";
41
+ @api referralText = "";
42
+
43
+ @api
44
+ get countries() {
45
+ return this._countriesJson || "[]";
46
+ }
47
+ set countries(value: string) {
48
+ this._countriesJson = value;
49
+
50
+ // Store a lookup table of country data by value so that we don't have to loop all the time
51
+ const countries = toJson(value);
52
+ const countriesLookup: any = {};
53
+ countries.forEach((country: any) => {
54
+ countriesLookup[country.value] = country;
55
+ });
56
+ this._countriesLookup = countriesLookup;
57
+ }
58
+
59
+ @api
60
+ get incomingUserInfo() {
61
+ return this._incomingUserInfo;
62
+ }
63
+ set incomingUserInfo(value: string) {
64
+ this._incomingUserInfo = toJson(value);
65
+ }
66
+
67
+ @api
68
+ get formMetadata() {
69
+ return this._formMetadata;
70
+ }
71
+ set formMetadata(value: string) {
72
+ this._formMetadata = toJson(value);
73
+ }
74
+
75
+ private participatingCountryCodes = ["US", "CA", "IN"];
76
+ private countriesWithReferralInput = ["IN"];
77
+ private countriesWithRequiredJobRole = ["CA"];
78
+ private requiredFormFields: HTMLElement[] = [];
79
+
80
+ get isNotParticipatingCountry() {
81
+ return !this.isParticipatingCountry(this.state.country);
82
+ }
83
+
84
+ get isCountryWithReferralInput() {
85
+ return this.countriesWithReferralInput.includes(this.state.country);
86
+ }
87
+
88
+ get isJobRoleRequired() {
89
+ return this.countriesWithRequiredJobRole.includes(this.state.country);
90
+ }
91
+
92
+ get notAvailableAlertInfo() {
93
+ return (this.formMetadata as any).notAvailableAlertInfo;
94
+ }
95
+
96
+ get jobRoles() {
97
+ return tbidValidRoles.map((role) => ({
98
+ label: role,
99
+ value: role
100
+ }));
101
+ }
102
+
103
+ connectedCallback() {
104
+ const initialState: Partial<J2sFormState> = {
105
+ ...this._incomingUserInfo,
106
+ isFormValid: false,
107
+ showMarketingText: true
108
+ };
109
+ const initialCountry = initialState.country;
110
+ const initialCountryData =
111
+ initialCountry && this._countriesLookup[initialCountry];
112
+ if (initialCountryData) {
113
+ initialState.showLegalCheckbox =
114
+ initialCountryData.showLegalCheckbox;
115
+ initialState.legalText = initialCountryData.legalText;
116
+ initialState.marketingText = initialCountryData.marketingText;
117
+ initialState.learnMore = initialCountryData.learnMore;
118
+ }
119
+
120
+ this.state = initialState as J2sFormState;
121
+ }
122
+
123
+ renderedCallback(): void {
124
+ if (this._isInitialRender) {
125
+ this.updateInnerHtml(
126
+ "span.j2s-terms",
127
+ (this.formMetadata as any).j2sTerms
128
+ );
129
+ }
130
+
131
+ if (
132
+ this.state.country &&
133
+ this.state.country !== this.prevState.country
134
+ ) {
135
+ // Re-render new country's terms
136
+ this.updateInnerHtml(
137
+ "span.marketing-terms",
138
+ this.state.marketingText
139
+ );
140
+ this.updateInnerHtml("span.legal-terms", this.state.legalText);
141
+ if (this.state.learnMore) {
142
+ this.updateInnerHtml(
143
+ "span.legal-terms-learn-more",
144
+ this.state.learnMore
145
+ );
146
+ } else {
147
+ this.updateInnerHtml("span.legal-terms-learn-more", "");
148
+ this.template
149
+ .querySelector(".legal-terms-learn-more")
150
+ ?.classList.add("hidden");
151
+ }
152
+ }
153
+
154
+ if (this.state.isFormValid !== this.prevState.isFormValid) {
155
+ this.dispatchEvent(
156
+ new CustomEvent("formvaliditychange", {
157
+ detail: {
158
+ isValid: this.state.isFormValid
159
+ },
160
+ bubbles: true,
161
+ composed: true
162
+ })
163
+ );
164
+ }
165
+
166
+ this.requiredFormFields = Array.from(
167
+ this.template.querySelectorAll("[required]")
168
+ );
169
+
170
+ if (this.id && !this._didSetFormId) {
171
+ this._didSetFormId = true;
172
+ this.template.querySelector("form")?.setAttribute("id", this.id);
173
+ }
174
+ }
175
+
176
+ private updateInnerHtml(selector: string, html: string): void {
177
+ const element = this.template.querySelector(selector);
178
+ if (element) {
179
+ element.innerHTML = html;
180
+ }
181
+ }
182
+
183
+ private isParticipatingCountry(country: string) {
184
+ return this.participatingCountryCodes.includes(country);
185
+ }
186
+
187
+ handleRequiredFieldUpdate() {
188
+ const isFormNowValid = this.checkValidity(this.state.country);
189
+ if (this.state.isFormValid !== isFormNowValid) {
190
+ this.setState({
191
+ isFormValid: isFormNowValid
192
+ });
193
+ }
194
+ }
195
+
196
+ handleCountryChange({ detail }: CustomEvent) {
197
+ const { value } = detail;
198
+ const countryData = this._countriesLookup[value];
199
+ this.setState({
200
+ country: value,
201
+ isFormValid: this.checkValidity(value),
202
+ learnMore: countryData.learnMore,
203
+ legalText: countryData.legalText,
204
+ marketingText: countryData.marketingText,
205
+ showLegalCheckbox: countryData.showLegalCheckbox
206
+ });
207
+ }
208
+
209
+ handleLearnMore() {
210
+ this.template
211
+ .querySelector(".legal-terms-learn-more")
212
+ ?.classList.toggle("hidden");
213
+ }
214
+
215
+ private checkValidity(country: string) {
216
+ return (
217
+ this.isParticipatingCountry(country) &&
218
+ this.requiredFormFields.every(
219
+ (requiredFormField) =>
220
+ // @ts-ignore
221
+ requiredFormField.checkValidity?.() === true
222
+ )
223
+ );
224
+ }
225
+ }