Skip to content

Commit

Permalink
MWPW-147989 and MWPW-151786: handle localized path for quiz spreadshe…
Browse files Browse the repository at this point in the history
…ets and doc files (#2416)

* MWPW-147601: sending ML field text to Analytics

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

* update code according to feedbacks
  • Loading branch information
JackySun9 authored Jun 10, 2024
1 parent ca8ba03 commit 366c1fe
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
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';
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
17 changes: 12 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,9 @@ 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, ietf = 'en-US' } = locale || {};
return ietf !== 'en-US' && !originalURL.startsWith(`${prefix}/`) ? `${prefix}${originalURL}` : originalURL;
};
24 changes: 23 additions & 1 deletion test/blocks/quiz/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const {
findAndStoreResultData,
} = await import('../../../libs/blocks/quiz/utils.js');

const locales = { '': { ietf: 'en-US', tk: 'hah7vzn.css' } };
let locales = { '': { ietf: 'en-US', tk: 'hah7vzn.css' } };
const conf = { locales };
const QUIZ_BASE_PATH = 'https://mockdata/path/to/quiz';

Expand Down Expand Up @@ -245,6 +245,28 @@ 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' } };
setConfig({ locales, pathname: '/de' });

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

expect(getLocalizedURL('/path/to/quiz/uar-results')).to.equal('/de/path/to/quiz/uar-results');
expect(getLocalizedURL('/de/path/to/quiz/uar-results')).to.equal('/de/path/to/quiz/uar-results');
});

it('Testing getLocalizedURL without locale define', async () => {
locales = { '': { } };
setConfig({ locales, pathname: '/de' });

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

expect(getLocalizedURL('/path/to/quiz/uar-results')).to.equal('/path/to/quiz/uar-results');
expect(getLocalizedURL('/de/path/to/quiz/uar-results')).to.equal('/de/path/to/quiz/uar-results');
});

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

0 comments on commit 366c1fe

Please sign in to comment.