Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
* Add `tr` Türkçe (Turkish) locale. Refs STCOR-1026.
* 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.
* Define a permission-set containing values necessary for session-init API requests. Refs STCOR-1031.
* Use GET mod-settings `/locale` API to get tenant language & locale settings. Refs STCOR-1027.

## [11.0.0](https://github.com/folio-org/stripes-core/tree/v11.0.0) (2025-02-24)
[Full Changelog](https://github.com/folio-org/stripes-core/compare/v10.2.0...v11.0.0)
Expand Down
145 changes: 45 additions & 100 deletions src/loginServices.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,77 +327,13 @@
* @param {string} tenant
* @returns {Promise}
*/
async function dispatchLocale(url, store, tenant) {
const response = await fetch(url, {
headers: getHeaders(tenant, store.getState().okapi.token),
credentials: 'include',
mode: 'cors',
});

if (response.ok) {
const json = await response.json();
if (json.configs?.length) {
const localeValues = JSON.parse(json.configs[0].value);
const { locale, timezone, currency } = localeValues;
if (locale) {
await loadTranslations(store, locale);
}
if (timezone) store.dispatch(setTimezone(timezone));
if (currency) store.dispatch(setCurrency(currency));
}
async function dispatchLocale(localeValues, store) {
const { locale, timezone, currency } = localeValues;
if (locale) {
await loadTranslations(store, locale);
}

return response;
}

/**
* getLocale
* return a promise that retrieves the tenant's locale-settings then
* loads the translations and dispatches the timezone and currency.
* @param {string} okapiUrl
* @param {redux store} store
* @param {string} tenant
*
* @returns {Promise}
*/
export async function getLocale(okapiUrl, store, tenant) {
const query = [
'module==ORG',
'configName == localeSettings',
'(cql.allRecords=1 NOT userId="" NOT code="")'
].join(' AND ');

const res = await dispatchLocale(
`${okapiUrl}/configurations/entries?query=(${query})`,
store,
tenant
);

return res;
}

/**
* getUserLocale
* return a promise that retrieves the user's locale-settings then
* loads the translations and dispatches the timezone and currency.
* @param {*} okapiUrl
* @param {*} store
* @param {*} tenant
*
* @returns {Promise}
*/
export async function getUserLocale(okapiUrl, store, tenant, userId) {
const query = Object.entries(userLocaleConfig)
.map(([k, v]) => `"${k}"=="${v}"`)
.join(' AND ');

const res = await dispatchLocale(
`${okapiUrl}/configurations/entries?query=(${query} and userId=="${userId}")`,
store,
tenant
);

return res;
if (timezone) store.dispatch(setTimezone(timezone));
if (currency) store.dispatch(setCurrency(currency));
}

/**
Expand Down Expand Up @@ -474,7 +410,7 @@
* @param {string} tenant - The tenant name for which the locale settings are being requested.
* @returns {Promise} A promise that resolves to the fetch API's response.
*/
const fetchLocale = async (url, store, tenant) => {

Check warning on line 413 in src/loginServices.js

View workflow job for this annotation

GitHub Actions / ui / Install and lint / Install and lint

'fetchLocale' is assigned a value but never used. Allowed unused vars must match /React/u
const res = await fetch(url, {
headers: getHeaders(tenant, store.getState().okapi.token),
credentials: 'include',
Expand All @@ -484,6 +420,7 @@
return res;
};


/**
* Retrieves the tenant's locale setting from the mod-settings.
*
Expand All @@ -496,15 +433,23 @@
* @returns {Promise} A promise that resolves with the locale configuration data.
*/
const getTenantLocale = async (url, store, tenant) => {
const query = `scope=="${settings.SCOPE}" and key=="${tenantLocaleConfig.KEY}"`;
const response = await fetch(`${url}/locale`, {
headers: getHeaders(tenant, store.getState().okapi.token),
credentials: 'include',
mode: 'cors',
});

const res = await fetchLocale(
`${url}/settings/entries?query=(${query})`,
store,
tenant
);
if (response.ok) {
const locale = await response.json();

return res;
dispatchLocale(
locale,
store,
tenant
);
}

return response;
};

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

const res = await fetchLocale(
`${url}/settings/entries?query=(${query})`,
store,
tenant
);
const response = await fetch(`${url}/settings/entries?query=(${query})`, {
headers: getHeaders(tenant, store.getState().okapi.token),
credentials: 'include',
mode: 'cors',
});

return res;
if (response.ok) {
const json = await response.json();

if (json.items?.length) {
const localeValues = JSON.parse(json.items[0]?.value);

dispatchLocale(
localeValues,
store,
tenant
);
}
}

return response;
};

/**
Expand Down Expand Up @@ -584,7 +543,7 @@
* @returns {Promise}
*/
const processLocaleSettings = async (store, tenantLocaleData, userLocaleData) => {
const tenantLocaleSettings = tenantLocaleData?.items[0]?.value;
const tenantLocaleSettings = tenantLocaleData;
const userLocaleSettings = userLocaleData?.items[0]?.value;

const locale = userLocaleSettings?.locale || tenantLocaleSettings?.locale;
Expand All @@ -598,15 +557,6 @@
return res;
};

// This function is used to support the deprecated mod-configuration API.
// It is only used when the new mod-settings API returns empty settings.
const getLocalesPromise = (url, store, tenant, userId) => {
return Promise.all([
getLocale(url, store, tenant),
getUserLocale(url, store, tenant, userId),
]);
};

/**
* loadResources
* return a promise that retrieves the tenant's locale, user's locale,
Expand Down Expand Up @@ -642,20 +592,15 @@
getTenantLocale(okapiUrl, store, tenant),
getUserOwnLocale(okapiUrl, store, tenant, userId),
]);
const [tenantLocaleData, userLocaleData] = await Promise.all(responses.map(res => res.value?.json?.()));
hasSetting = tenantLocaleData?.items[0] || userLocaleData?.items[0];
const [tenantLocaleData, userLocaleData] = [responses];
hasSetting = tenantLocaleData || userLocaleData?.items[0].value;

if (hasSetting) {
await processLocaleSettings(store, tenantLocaleData, userLocaleData);
promises.push(responses.map(res => res?.value));
}
}

// only read from legacy mod-config if we haven't already read from mod-settings
if (hasReadConfigPerm && !hasSetting) {
promises.push(getLocalesPromise(okapiUrl, store, tenant, userId));
}

// tenant's locale, plugin, bindings, and user's locale are all stored
// in mod-configuration so we can only retrieve them if the user has
// read-permission for configuration entries.
Expand Down
Loading
Loading