Skip to content

Commit a771f1f

Browse files
authored
Fixed issue with load default lang (#12333)
### Related Ticket(s) [Noitce & Choice w3 Publisher](https://w3.ibm.com/w3publisher/urx/notice-and-choice-v23) ### Description Notice & Choice is a legally required component to create a form where IBM collects its customer information. This section shows the specific product's legal links, terms, and conditions. Along with this, it collects users' consent regarding communication preferences. ### Changelog **New** **Changed** - Resolved the issue related to loading the language. **Removed** <!-- React and Web Component deploy previews are enabled by default. --> <!-- To enable additional available deploy previews, apply the following --> <!-- labels for the corresponding package: --> <!-- *** "test: e2e": Codesandbox examples and e2e integration tests --> <!-- *** "package: services": Services --> <!-- *** "package: utilities": Utilities --> <!-- *** "RTL": React / Web Components (RTL) --> <!-- *** "feature flag": React / Web Components (experimental) -->
1 parent 7887307 commit a771f1f

1 file changed

Lines changed: 88 additions & 69 deletions

File tree

packages/web-components/src/components/notice-choice/notice-choice.ts

Lines changed: 88 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -178,106 +178,125 @@ class NoticeChoice extends StableSelectorMixin(LitElement) {
178178
});
179179
}
180180

181+
private handleEmailChange(
182+
value: unknown,
183+
oldValue: unknown,
184+
hasValue: boolean
185+
) {
186+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
187+
this.emailValid = false;
188+
189+
if (
190+
hasValue &&
191+
typeof value === 'string' &&
192+
value.trim() &&
193+
oldValue !== value &&
194+
emailRegex.test(value.trim())
195+
) {
196+
this.email = value.trim();
197+
this.onEmailChange();
198+
} else {
199+
this.showCheckBox = false;
200+
}
201+
}
202+
203+
private loadContentWithFallback(lang: string) {
204+
loadContent(
205+
lang,
206+
this.environment,
207+
(ncData) => {
208+
this.isLoading = false;
209+
this.ncData = ncData;
210+
this.prepareCheckboxes();
211+
},
212+
() => this.defaultLoadContent()
213+
);
214+
}
215+
216+
private handleLanguageOrEnvironmentChange(value: string) {
217+
if (['stage', 'prod'].includes(value)) {
218+
this.environment = value;
219+
} else {
220+
this.language = value;
221+
}
222+
223+
const langPart = this.language?.split(/[-_]/)[0];
224+
this.isLoading = true;
225+
226+
loadSettings(
227+
this.environment,
228+
(settings) => {
229+
this.countrySettings = settings.preferences;
230+
this.noticeOnly = settings.noticeOnly || ['us'];
231+
this.supportedLanguages = settings.supportedLanguages || {};
232+
233+
const supportedLang =
234+
this.supportedLanguages[this.language?.toLowerCase() || ''] ||
235+
(langPart && this.supportedLanguages[langPart.toLowerCase()]) ||
236+
'en';
237+
238+
this.loadContentWithFallback(supportedLang);
239+
},
240+
(err) => {
241+
console.error('Error loading settings', err);
242+
this.loadContentWithFallback('en');
243+
}
244+
);
245+
}
246+
181247
private _dispatchChange(
182248
field: string,
183249
value: unknown,
184250
hasValue: boolean,
185251
oldValue?: unknown
186252
) {
253+
const stringValue = typeof value === 'string' ? value : '';
254+
187255
switch (field) {
188-
case 'questionchoices': {
256+
case 'questionchoices':
189257
this.prepareCheckboxes();
190258
this.setDefaultSelections();
191-
break;
192-
}
259+
return;
193260

194261
case 'language':
195-
case 'environment': {
196-
if (['stage', 'prod'].includes(value as string)) {
197-
this.environment = value as string;
198-
} else {
199-
this.language = value as string;
200-
}
201-
const langPart = (this.language as string | undefined)?.split(
202-
/[-_]/
203-
)[0];
204-
const supportedLang =
205-
(this.language &&
206-
this.supportedLanguages[this.language?.toLowerCase()]) ||
207-
(langPart && this.supportedLanguages[langPart?.toLowerCase()]) ||
208-
'en';
209-
210-
this.isLoading = true;
211-
loadSettings(
212-
this.environment,
213-
(settings) => {
214-
this.countrySettings = settings.preferences;
215-
this.noticeOnly = settings.noticeOnly || ['us'];
216-
this.supportedLanguages = settings.supportedLanguages || {};
217-
},
218-
(err) => console.error('error loading settings', err)
219-
);
220-
221-
loadContent(
222-
supportedLang,
223-
this.environment,
224-
(ncData) => {
225-
this.isLoading = false;
226-
this.ncData = ncData;
227-
this.prepareCheckboxes();
228-
},
229-
() => this.defaultLoadContent()
230-
);
231-
break;
232-
}
262+
case 'environment':
263+
this.handleLanguageOrEnvironmentChange(stringValue);
264+
return;
233265

234-
case 'country': {
266+
case 'country':
235267
if (
236268
hasValue &&
237-
typeof value === 'string' &&
269+
stringValue &&
238270
oldValue !== value &&
239-
this.countrySettings?.[value.toLowerCase()]
271+
this.countrySettings?.[stringValue.toLowerCase()]
240272
) {
241273
this.countryChanged();
242274
}
243-
break;
244-
}
275+
return;
245276

246-
case 'enableAllOptIn': {
277+
case 'enableAllOptIn':
247278
if (oldValue !== value && typeof value === 'string') {
248279
this.enableAllOptIn = JSON.parse(value);
249280
this.setDefaultSelections();
250281
}
251-
break;
252-
}
282+
return;
253283

254-
case 'hideErrorMessage': {
284+
case 'hideErrorMessage':
255285
if (oldValue !== value && typeof value === 'string') {
256286
this.hideErrorMessage = JSON.parse(value);
257287
this.countryBasedLegalNotice();
258288
}
259-
break;
260-
}
289+
return;
261290

262-
case 'email': {
263-
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
264-
this.emailValid = false;
265-
if (
266-
hasValue &&
267-
oldValue !== value &&
268-
typeof value === 'string' &&
269-
emailRegex.test(value.trim()) &&
270-
value.trim() !== ''
271-
) {
272-
this.email = value.trim();
273-
this.onEmailChange();
274-
} else {
275-
this.showCheckBox = false;
276-
}
277-
break;
278-
}
291+
case 'email':
292+
this.handleEmailChange(value, oldValue, hasValue);
293+
return;
294+
295+
default:
296+
return;
279297
}
280298
}
299+
281300
onEmailChange() {
282301
const email = this.email;
283302
const oneYearAgo = new Date(Date.now() - 365 * 24 * 60 * 60 * 1000);

0 commit comments

Comments
 (0)