Skip to content

Commit bc68bc7

Browse files
committed
STCOR-1027 Use GET mod-settings /locale API to get tenant language & locale settings
1 parent cbf24e4 commit bc68bc7

File tree

3 files changed

+60
-248
lines changed

3 files changed

+60
-248
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* Add `tr` Türkçe (Turkish) locale. Refs STCOR-1026.
3737
* Remove the `/users-keycloak/_self` (and `/bl-users/_self`) requests. They were included in `isAuthenticationRequest`, which caused them to bypass the RTR queue (`getPromise`). Fixes STCOR-1029.
3838
* Define a permission-set containing values necessary for session-init API requests. Refs STCOR-1031.
39+
* Use GET mod-settings `/locale` API to get tenant language & locale settings. Refs STCOR-1027.
3940

4041
## [11.0.0](https://github.com/folio-org/stripes-core/tree/v11.0.0) (2025-02-24)
4142
[Full Changelog](https://github.com/folio-org/stripes-core/compare/v10.2.0...v11.0.0)

src/loginServices.js

Lines changed: 45 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -327,77 +327,13 @@ export async function loadTranslations(store, locale, defaultTranslations = {})
327327
* @param {string} tenant
328328
* @returns {Promise}
329329
*/
330-
async function dispatchLocale(url, store, tenant) {
331-
const response = await fetch(url, {
332-
headers: getHeaders(tenant, store.getState().okapi.token),
333-
credentials: 'include',
334-
mode: 'cors',
335-
});
336-
337-
if (response.ok) {
338-
const json = await response.json();
339-
if (json.configs?.length) {
340-
const localeValues = JSON.parse(json.configs[0].value);
341-
const { locale, timezone, currency } = localeValues;
342-
if (locale) {
343-
await loadTranslations(store, locale);
344-
}
345-
if (timezone) store.dispatch(setTimezone(timezone));
346-
if (currency) store.dispatch(setCurrency(currency));
347-
}
330+
async function dispatchLocale(localeValues, store) {
331+
const { locale, timezone, currency } = localeValues;
332+
if (locale) {
333+
await loadTranslations(store, locale);
348334
}
349-
350-
return response;
351-
}
352-
353-
/**
354-
* getLocale
355-
* return a promise that retrieves the tenant's locale-settings then
356-
* loads the translations and dispatches the timezone and currency.
357-
* @param {string} okapiUrl
358-
* @param {redux store} store
359-
* @param {string} tenant
360-
*
361-
* @returns {Promise}
362-
*/
363-
export async function getLocale(okapiUrl, store, tenant) {
364-
const query = [
365-
'module==ORG',
366-
'configName == localeSettings',
367-
'(cql.allRecords=1 NOT userId="" NOT code="")'
368-
].join(' AND ');
369-
370-
const res = await dispatchLocale(
371-
`${okapiUrl}/configurations/entries?query=(${query})`,
372-
store,
373-
tenant
374-
);
375-
376-
return res;
377-
}
378-
379-
/**
380-
* getUserLocale
381-
* return a promise that retrieves the user's locale-settings then
382-
* loads the translations and dispatches the timezone and currency.
383-
* @param {*} okapiUrl
384-
* @param {*} store
385-
* @param {*} tenant
386-
*
387-
* @returns {Promise}
388-
*/
389-
export async function getUserLocale(okapiUrl, store, tenant, userId) {
390-
const query = Object.entries(userLocaleConfig)
391-
.map(([k, v]) => `"${k}"=="${v}"`)
392-
.join(' AND ');
393-
394-
const res = await dispatchLocale(
395-
`${okapiUrl}/configurations/entries?query=(${query} and userId=="${userId}")`,
396-
store,
397-
tenant
398-
);
399-
400-
return res;
335+
if (timezone) store.dispatch(setTimezone(timezone));
336+
if (currency) store.dispatch(setCurrency(currency));
401337
}
402338

403339
/**
@@ -484,6 +420,7 @@ const fetchLocale = async (url, store, tenant) => {
484420
return res;
485421
};
486422

423+
487424
/**
488425
* Retrieves the tenant's locale setting from the mod-settings.
489426
*
@@ -496,15 +433,23 @@ const fetchLocale = async (url, store, tenant) => {
496433
* @returns {Promise} A promise that resolves with the locale configuration data.
497434
*/
498435
const getTenantLocale = async (url, store, tenant) => {
499-
const query = `scope=="${settings.SCOPE}" and key=="${tenantLocaleConfig.KEY}"`;
436+
const response = await fetch(`${url}/locale`, {
437+
headers: getHeaders(tenant, store.getState().okapi.token),
438+
credentials: 'include',
439+
mode: 'cors',
440+
});
500441

501-
const res = await fetchLocale(
502-
`${url}/settings/entries?query=(${query})`,
503-
store,
504-
tenant
505-
);
442+
if (response.ok) {
443+
const locale = await response.json();
506444

507-
return res;
445+
dispatchLocale(
446+
locale,
447+
store,
448+
tenant
449+
);
450+
}
451+
452+
return response;
508453
};
509454

510455
/**
@@ -523,13 +468,27 @@ const getTenantLocale = async (url, store, tenant) => {
523468
const getUserOwnLocale = async (url, store, tenant, userId) => {
524469
const query = `userId=="${userId}" and scope=="${settings.SCOPE}" and key=="${userOwnLocaleConfig.KEY}"`;
525470

526-
const res = await fetchLocale(
527-
`${url}/settings/entries?query=(${query})`,
528-
store,
529-
tenant
530-
);
471+
const response = await fetch(`${url}/settings/entries?query=(${query})`, {
472+
headers: getHeaders(tenant, store.getState().okapi.token),
473+
credentials: 'include',
474+
mode: 'cors',
475+
});
531476

532-
return res;
477+
if (response.ok) {
478+
const json = await response.json();
479+
480+
if (json.items?.length) {
481+
const localeValues = JSON.parse(json.items[0]?.value);
482+
483+
dispatchLocale(
484+
localeValues,
485+
store,
486+
tenant
487+
);
488+
}
489+
}
490+
491+
return response;
533492
};
534493

535494
/**
@@ -584,7 +543,7 @@ export const getFullLocale = (languageRegion, numberingSystem) => {
584543
* @returns {Promise}
585544
*/
586545
const processLocaleSettings = async (store, tenantLocaleData, userLocaleData) => {
587-
const tenantLocaleSettings = tenantLocaleData?.items[0]?.value;
546+
const tenantLocaleSettings = tenantLocaleData;
588547
const userLocaleSettings = userLocaleData?.items[0]?.value;
589548

590549
const locale = userLocaleSettings?.locale || tenantLocaleSettings?.locale;
@@ -598,15 +557,6 @@ const processLocaleSettings = async (store, tenantLocaleData, userLocaleData) =>
598557
return res;
599558
};
600559

601-
// This function is used to support the deprecated mod-configuration API.
602-
// It is only used when the new mod-settings API returns empty settings.
603-
const getLocalesPromise = (url, store, tenant, userId) => {
604-
return Promise.all([
605-
getLocale(url, store, tenant),
606-
getUserLocale(url, store, tenant, userId),
607-
]);
608-
};
609-
610560
/**
611561
* loadResources
612562
* return a promise that retrieves the tenant's locale, user's locale,
@@ -642,20 +592,15 @@ export async function loadResources(store, tenant, userId) {
642592
getTenantLocale(okapiUrl, store, tenant),
643593
getUserOwnLocale(okapiUrl, store, tenant, userId),
644594
]);
645-
const [tenantLocaleData, userLocaleData] = await Promise.all(responses.map(res => res.value?.json?.()));
646-
hasSetting = tenantLocaleData?.items[0] || userLocaleData?.items[0];
595+
const [tenantLocaleData, userLocaleData] = [responses];
596+
hasSetting = tenantLocaleData || userLocaleData?.items[0].value;
647597

648598
if (hasSetting) {
649599
await processLocaleSettings(store, tenantLocaleData, userLocaleData);
650600
promises.push(responses.map(res => res?.value));
651601
}
652602
}
653603

654-
// only read from legacy mod-config if we haven't already read from mod-settings
655-
if (hasReadConfigPerm && !hasSetting) {
656-
promises.push(getLocalesPromise(okapiUrl, store, tenant, userId));
657-
}
658-
659604
// tenant's locale, plugin, bindings, and user's locale are all stored
660605
// in mod-configuration so we can only retrieve them if the user has
661606
// read-permission for configuration entries.

0 commit comments

Comments
 (0)