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

pre work for replacing IconComponent usages with iconPartial #870

Merged
merged 5 commits into from
Jul 1, 2021
Merged
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
2 changes: 1 addition & 1 deletion commands/helpers/utils/argumentmetadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ Object.freeze(ArgumentType);
class ArgumentMetadata {
constructor(type, description, isRequired, defaultValue, itemType) {
this._type = type;
this._description = description;
this._isRequired = isRequired;
this._defaultValue = defaultValue;
this._description = description;
this._itemType = itemType;
}

Expand Down
5 changes: 3 additions & 2 deletions hbshelpers/relativePathHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ const isNonRelativeUrl = require('./isNonRelativeUrl');
* If the url is not relative, return it. If it is relative,
* append relativePath to it.
*
* @param {string} url
* @param {import('handlebars').HelperOptions} options
* @param {string} options.hash.relativePath
* @param {string} options.hash.url
* @returns {string}
*/
module.exports = function relativePathHandler(options) {
const { relativePath, url } = options.hash || {};
if (isNonRelativeUrl(url) || !relativePath) {
if (isNonRelativeUrl(url) || !relativePath || !url) {
return url;
}
return relativePath + '/' + url;
Expand Down
4 changes: 3 additions & 1 deletion hbshelpers/sdkAssetUrl.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const RELEASE_BRANCH_REGEX = /^release\/v[0-9.]+$/;
const HOTFIX_BRANCH_REGEX = /^hotfix\/v[0-9.]+$/;
const I18N_FEATURE_BRANCH_REGEX = /^feature\/.+-i18n$/;
const SEM_VER_REGEX = /^[1-9]+$|^[1-9]+\.[0-9]+$|^[1-9]+\.[0-9]+\.[0-9]+$/;

/**
Expand All @@ -25,8 +26,9 @@ module.exports = function sdkAssetUrl(branch, locale, assetName) {

const isPreReleaseBranch =
RELEASE_BRANCH_REGEX.test(branch) || HOTFIX_BRANCH_REGEX.test(branch);
const isI18nFeatureBranch = I18N_FEATURE_BRANCH_REGEX.test(branch);
const isLocalizationSupported =
(isReleasedBranch || isPreReleaseBranch) &&
(isReleasedBranch || isPreReleaseBranch || isI18nFeatureBranch) &&
!(locale.startsWith('en') || assetName === 'answers.css') ;

const parsedAssetName = isLocalizationSupported ?
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
},
"jest": {
"collectCoverageFrom": [
"static/**/*.js"
"static/**/*.js",
"hbshelpers/*.js"
],
"verbose": true,
"moduleFileExtensions": [
Expand Down
22 changes: 20 additions & 2 deletions script/core.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,32 @@
* Determine whether a URL is absolute or not.
* Common examples: "mailto:slapshot@gmail.com", "//yext.com", "https://yext.com"
*/
ANSWERS.registerHelper('isNonRelativeUrl', function(str) {
function isNonRelativeUrl(str) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's kind of crummy that we have to basically duplicate isNonRelativeUrl and relativePathUrlHandler. It'd be great if the JS in hbshelpers could be inlined as a partial here. Would save us having to update the same code-block twice. Is there any non-janky way to do that?

Copy link
Contributor Author

@oshi97 oshi97 Jun 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You know, at first I thought this was going to be hard because it's hard to get the path from entry.js to the hbshelpers folder. That path depends on the jambo config, and we don't want to use dynamic imports here (with like a require.resolve or something). BUT I'm remembering now that webpack lets you add resolve aliases. Will add in a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I lied. Webpack doesn't like it when you mix ES6 and commonjs, and unfortunately jambo only understands hbs helpers that are commonjs. So putting it into the bundle is probably out of the window for now.
I tried thinking about how to do the inlining method as well with just HBS, but obviously module.exports is not something hbs understands. We could do jank where we create an empty object called "module", then loop over the desired helpers, continually reassigning the value of module.exports and calling ANSWERS.registerHelper on it. jank !== not janky though

Copy link
Contributor Author

@oshi97 oshi97 Jun 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sad developer noises this was the pr #871
For whatever reason it works fine with our dev webpack build, just borks on prod ("it" being the first commit before I started trying random ideas)

const absoluteURLRegex = /^(\/|[a-zA-Z]+:)/;
return str && str.match(absoluteURLRegex);
});
}
ANSWERS.registerHelper('isNonRelativeUrl', isNonRelativeUrl);

ANSWERS.registerHelper('close-card-svg', () => {
return ANSWERS.renderer.SafeString({{{stringifyPartial (read 'static/assets/images/close-card') }}});
});

/**
* If the url is not relative, return it. If it is relative,
* append relativePath to it.
*
* @param {import('handlebars').HelperOptions} options
* @param {string} options.hash.relativePath
* @param {string} options.hash.url
* @returns {string}
*/
ANSWERS.registerHelper('relativePathHandler', function relativePathHandler(options) {
const { relativePath, url } = options.hash || {};
if (isNonRelativeUrl(url) || !relativePath || !url) {
return url;
}
return relativePath + '/' + url;
});
}
}).catch(err => {
window.AnswersExperience.AnswersInitializedPromise.reject('Answers failed to initialized.');
Expand Down
22 changes: 12 additions & 10 deletions test-site/.gitignore
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
public/*
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for reasons apparently git is finicky and whether or not you put a star at the end of a path matters
https://stackoverflow.com/questions/5533050/gitignore-exclude-folder-but-include-specific-subfolder

config/*
pages/*
cards/*
directanswercards/*
Gruntfile.js
package-lock.json
package.json
webpack-config.js

!cards/custom-cta-icons/
!config/global_config.json
!config/locale_config.json
!config/index.json
!pages/index.html.hbs
!public/iframe_test.html
!public/overlay.html
public/
config/
pages/
cards/
directanswercards/
Gruntfile.js
package-lock.json
package.json
webpack-config.js
!public/overlay.html
56 changes: 56 additions & 0 deletions test-site/cards/custom-cta-icons/component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{{> cards/card_component componentName='custom-cta-icons' }}

class custom_cta_iconsCardComponent extends BaseCard['custom-cta-icons'] {
constructor(config = {}, systemConfig = {}) {
super(config, systemConfig);
}

dataForRender(profile) {
const linkTarget = AnswersExperience.runtimeConfig.get('linkTarget') || '_top';

return {
title: profile.name,
url: profile.website,
target: linkTarget,
titleEventOptions: this.addDefaultEventOptions(),
date: Formatter.bigDate(profile),
subtitle: Formatter.dateRange(profile),
details: profile.description,
CTA1: {
label: 'Hitchhiker Thumb!',
/**
* @Test a custom icon url without iconName in the CTA
* @Expect the custom icon to show up
*/
iconUrl: 'static/assets/hitchhiker-thumb.jpeg',
url: 'testUrl',
target: linkTarget,
eventType: 'RSVP',
eventOptions: this.addDefaultEventOptions(),
},
CTA2: {
label: 'AYAYA',
iconName: 'star',
/**
* @Test a custom icon url hen an iconName already exists
* @Expect the custom icon to show up
*/
iconUrl: 'static/assets/ayaya.png',
url: 'testUrl',
target: linkTarget,
eventType: 'DRIVING_DIRECTIONS',
eventOptions: this.addDefaultEventOptions(),
}
};
}

static defaultTemplateName (config) {
return 'cards/custom-cta-icons';
}
}

ANSWERS.registerTemplate(
'cards/custom-cta-icons',
{{{stringifyPartial (read 'cards/event-standard/template') }}}
);
ANSWERS.registerComponentType(custom_cta_iconsCardComponent);
8 changes: 8 additions & 0 deletions test-site/config-overrides/events_custom_cta_icons.es.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"verticalsToConfig": {
"events": {
"cardType": "custom-cta-icons",
"label": "Eventos"
}
}
}
7 changes: 7 additions & 0 deletions test-site/config-overrides/events_custom_cta_icons.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"verticalsToConfig": {
"events": {
"cardType": "custom-cta-icons"
}
}
}
2 changes: 1 addition & 1 deletion test-site/config/global_config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"sdkVersion": "1.9", // The version of the Answers SDK to use
"sdkVersion": "feature/icon-partial-i18n", // The version of the Answers SDK to use
"apiKey": "2d8c550071a64ea23e263118a2b0680b", // The answers api key found on the experiences page. This will be provided automatically by the Yext CI system
// "experienceVersion": "<REPLACE ME>", // the Answers Experience version to use for API requests. This will be provided automatically by the Yext CI system
// "businessId": "<REPLACE ME>", // The business ID of the account. This will be provided automatically by the Yext CI system
Expand Down
2 changes: 1 addition & 1 deletion test-site/config/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"mapConfig": {
"mapProvider": "Google"
},
"icon": "static/assets/slapshot.png"
"iconUrl": "static/assets/slapshot.png"
},
"products": {
"universalSectionTemplate": "grid-three-columns",
Expand Down
11 changes: 10 additions & 1 deletion test-site/scripts/create-verticals.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ const verticalConfiguration = {
template: 'vertical-standard',
cardName: 'event-custom'
},
events_custom_cta_icons: {
verticalKey: 'events',
template: 'vertical-standard'
},
faqs: {
verticalKey: 'faq',
template: 'vertical-standard',
Expand Down Expand Up @@ -90,7 +94,12 @@ const testSiteDir = path.resolve(__dirname, '..');
process.chdir(testSiteDir);

Object.entries(verticalConfiguration).forEach(([pageName, config]) => {
execSync(`npx jambo vertical --name ${pageName} --verticalKey ${config.verticalKey} --template ${config.template} --cardName ${config.cardName} --locales es`);
let verticalCommand =
`npx jambo vertical --name ${pageName} --verticalKey ${config.verticalKey} --template ${config.template} --locales es`
if (config.cardName) {
verticalCommand += ` --cardName ${config.cardName}`
}
execSync(verticalCommand);
configMerger.mergeConfigForPage(pageName);
pagePatcher.applyPatchToPage(pageName);
configMerger.mergeConfigForPage(pageName + '.es');
Expand Down
Binary file added test-site/static/assets/ayaya.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test-site/static/assets/hitchhiker-thumb.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 56 additions & 0 deletions tests/hbshelpers/relativePathHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import relativePathHandler from '../../hbshelpers/relativePathHandler';

it('works for undefined url', () => {
const hash = {
url: undefined,
relativePath: '.'
};
expect(relativePathHandler({ hash })).toEqual(undefined);
});

it('works for null url', () => {
const hash = {
url: null,
relativePath: '.'
};
expect(relativePathHandler({ hash })).toEqual(null);
});

it('works for blank string url', () => {
const hash = {
url: '',
relativePath: '.'
};
expect(relativePathHandler({ hash })).toEqual('');
});

it('works for relative url with relativePath', () => {
const hash = {
url: 'mySpecialAsset.jpg',
relativePath: '.'
};
expect(relativePathHandler({ hash })).toEqual('./mySpecialAsset.jpg');
});

it('works for relative url with relativePath that points back a level', () => {
const hash = {
url: 'mySpecialAsset.jpg',
relativePath: '../..'
};
expect(relativePathHandler({ hash })).toEqual('../../mySpecialAsset.jpg');
});

it('works when no relativePath', () => {
const hash = {
url: 'mySpecialAsset.jpg'
};
expect(relativePathHandler({ hash })).toEqual('mySpecialAsset.jpg');
});

it('works with absolute url', () => {
const hash = {
url: '/mySpecialAsset.jpg',
relativePath: '../../'
};
expect(relativePathHandler({ hash })).toEqual('/mySpecialAsset.jpg');
});
3 changes: 3 additions & 0 deletions tests/percy/photographer.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class Photographer {
await this._pageNavigator.gotoVerticalPage('events', { query: 'vrginia' });
await this._camera.snapshot('vertical-search--spellcheck');

await this._pageNavigator.gotoVerticalPage('events_custom_cta_icons');
await this._camera.snapshot('vertical-search--custom-cta-icons');

await this._pageNavigator.gotoVerticalPage('financial_professionals', { query: 'connor' });
await this._camera.snapshot('vertical-search--financial-professional-location');

Expand Down