Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MWPW-147989 and MWPW-151786: handle localized path for quiz spreadsheets and doc files #2416

Merged
merged 10 commits into from
Jun 10, 2024
Merged
4 changes: 2 additions & 2 deletions libs/blocks/quiz-results/quiz-results.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createTag, getConfig } from '../../utils/utils.js';
import { handleStyle } from '../section-metadata/section-metadata.js';
import { getNormalizedMetadata } from '../quiz/utils.js';
import { getNormalizedMetadata, getLocalizedURL } from '../quiz/utils.js';
import { decorateSectionAnalytics } from '../../martech/attributes.js';

export const LOADING_ERROR = 'Could not load quiz results:';
Expand All @@ -19,7 +19,7 @@ async function loadFragments(el, experiences) {
}

function redirectPage(quizUrl, debug, message) {
const url = (quizUrl) ? quizUrl.text : 'https://adobe.com';
const url = (quizUrl) ? getLocalizedURL(quizUrl.text) : 'https://adobe.com';
JackySun9 marked this conversation as resolved.
Show resolved Hide resolved
window.lana.log(message, { tags: 'errorType=error,module=quiz-results' });

if (debug === 'quiz-results') {
Expand Down
3 changes: 2 additions & 1 deletion libs/blocks/quiz/quiz.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { DecorateBlockBackground, DecorateBlockForeground } from './quizcontaine
import {
initConfigPathGlob, handleResultFlow, handleNext, transformToFlowData, getQuizData,
getAnalyticsDataForBtn, getUrlParams, isValidUrl,
getLocalizedURL,
} from './utils.js';
import StepIndicator from './stepIndicator.js';

Expand Down Expand Up @@ -265,7 +266,7 @@ const App = ({
};
const fragmentURL = getStringValue('footerFragment');
if (fragmentURL) {
loadFragments(fragmentURL);
loadFragments(getLocalizedURL(fragmentURL));
}
const iconBg = getStringValue('icon-background-color');
if (iconBg) {
Expand Down
25 changes: 20 additions & 5 deletions libs/blocks/quiz/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const initConfigPath = (quizMetaData) => {
const quizConfigPath = quizMetaData.data.text;
const urlParams = new URLSearchParams(window.location.search);
const stringsPath = urlParams.get('quiz-data');
return (filepath) => `${stringsPath || quizConfigPath}${filepath}`;
return (filepath) => `${stringsPath || getLocalizedURL(quizConfigPath)}${filepath}`;
};

async function fetchContentOfFile(path) {
Expand Down Expand Up @@ -84,7 +84,8 @@ export const findAndStoreResultData = async (answers = []) => {
let umbrellaProduct = '';

if (resultData.matchedResults.length > 0) {
destinationPage = resultData.matchedResults[0].url;
destinationPage = getLocalizedURL(resultData.matchedResults[0].url);

primaryProductCodes = resultData.primary;
secondaryProductCodes = resultData.secondary;
umbrellaProduct = resultData.matchedResults[0]['umbrella-result'];
Expand Down Expand Up @@ -168,11 +169,11 @@ export const structuredFragments = (
resultResources?.data?.forEach((row) => {
if (umbrellaProduct) {
if (umbrellaProduct && row.product === umbrellaProduct) {
structureFragments.push(row[fragment]);
structureFragments.push(getLocalizedURL(row[fragment]));
}
} else if (primaryProducts?.length > 0 && primaryProducts.includes(row.product)
&& row[fragment]) {
structureFragments.push(row[fragment]);
structureFragments.push(getLocalizedURL(row[fragment]));
}
});
});
Expand Down Expand Up @@ -233,7 +234,7 @@ const getNestedFragments = (resultResources, productCodes, fragKey) => {

function insertFragment() {
row[fragKey]?.split(',').forEach((val) => {
fragArray.push(val.trim());
fragArray.push(getLocalizedURL(val.trim()));
});
}
});
Expand Down Expand Up @@ -497,3 +498,17 @@ export const getAnalyticsDataForLocalStorage = (config) => {
export const isValidUrl = (url) => VALID_URL_RE.test(url);

export const getNormalizedMetadata = (el) => normalizeKeys(getMetadata(el));

export const getLocalizedURL = (originalURL) => {
const { locale } = getConfig();
const prefix = locale?.prefix;
const localCode = locale?.ietf ?? 'en-US';

let localizedURL = originalURL;

if (localCode !== 'en-US' && !originalURL.startsWith(`${prefix}/`)) {
localizedURL = `${prefix}${originalURL}`;
}

return localizedURL;
JackySun9 marked this conversation as resolved.
Show resolved Hide resolved
};
45 changes: 43 additions & 2 deletions test/blocks/quiz/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ const {
findAndStoreResultData,
} = await import('../../../libs/blocks/quiz/utils.js');

const locales = { '': { ietf: 'en-US', tk: 'hah7vzn.css' } };
const conf = { locales };
let locales = { '': { ietf: 'en-US', tk: 'hah7vzn.css' } };
let conf = { locales };
JackySun9 marked this conversation as resolved.
Show resolved Hide resolved
const QUIZ_BASE_PATH = 'https://mockdata/path/to/quiz';

setConfig(conf);
Expand Down Expand Up @@ -245,6 +245,47 @@ describe('Quiz', () => {
expect(flowData).to.be.an('array').of.length(5);
});

it('Testing getLocalizedURL with country code or without country code', async () => {
locales = { '': { ietf: 'de-DE', tk: 'hah7vzn.css' } };
conf = { locales };
conf.pathname = '/de';
setConfig(conf);
JackySun9 marked this conversation as resolved.
Show resolved Hide resolved

// Import getLocalizedURL function
const { getLocalizedURL } = await import('../../../libs/blocks/quiz/utils.js');

// Setup the URL and test the function
const fragmentURL = '/path/to/quiz/uar-results';
const modifiedURL = getLocalizedURL(fragmentURL);

// Assert the result
expect(modifiedURL).to.equal('/de/path/to/quiz/uar-results');
JackySun9 marked this conversation as resolved.
Show resolved Hide resolved

// Setup the URL and test the function
const fragmentURL2 = '/de/path/to/quiz/uar-results';
const modifiedURL2 = getLocalizedURL(fragmentURL2);

// Assert the result
expect(modifiedURL2).to.equal('/de/path/to/quiz/uar-results');
JackySun9 marked this conversation as resolved.
Show resolved Hide resolved
});

it('Testing getLocalizedURL without locale define', async () => {
locales = { '': { } };
conf = { locales };
conf.pathname = '/de';
setConfig(conf);
JackySun9 marked this conversation as resolved.
Show resolved Hide resolved

// Import getLocalizedURL function
const { getLocalizedURL } = await import('../../../libs/blocks/quiz/utils.js');

// Setup the URL and test the function
const fragmentURL = '/path/to/quiz/uar-results';
const modifiedURL = getLocalizedURL(fragmentURL);

// Assert the result
expect(modifiedURL).to.equal('/path/to/quiz/uar-results');
JackySun9 marked this conversation as resolved.
Show resolved Hide resolved
});

describe('Testing storeResultInLocalStorage with empty results as input', async () => {
let resultToDelegate;
const primaryProductCodes = [];
Expand Down
Loading