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

fix: strf-9535 Add fallback for shopper language default #804

Merged
merged 1 commit into from
Dec 6, 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
37 changes: 21 additions & 16 deletions lib/stencil-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,28 @@ class StencilStart {
storeInfoFromAPI,
);

this._storeSettingsLocale = await this.getStoreSettingsLocale(
cliOptions,
updatedStencilConfig,
);

await this.startLocalServer(cliOptions, updatedStencilConfig);

this._logger.log(this.getStartUpInfo(updatedStencilConfig));

await this.startBrowserSync(cliOptions, updatedStencilConfig, browserSyncPort);
}

async getStoreSettingsLocale(cliOptions, stencilConfig) {
const { accessToken } = stencilConfig;
const apiHost = cliOptions.apiHost || stencilConfig.apiHost;
return this._storeSettingsApiClient.getStoreSettingsLocale({
storeHash: this.storeHash,
accessToken,
apiHost,
});
}

async getChannelUrl(stencilConfig, cliOptions) {
const { accessToken } = stencilConfig;
const apiHost = cliOptions.apiHost || stencilConfig.apiHost;
Expand All @@ -97,16 +112,6 @@ class StencilStart {
return foundChannel ? foundChannel.url : null;
}

async getDefaultShopperLanguage(cliOptions, stencilConfig) {
const { accessToken } = stencilConfig;
const apiHost = cliOptions.apiHost || stencilConfig.apiHost;
return this._storeSettingsApiClient.getDefaultShopperLanguage({
storeHash: this.storeHash,
accessToken,
apiHost,
});
}

/**
* @param {Object} cliOptions
*/
Expand Down Expand Up @@ -148,6 +153,7 @@ class StencilStart {
useCache: cliOptions.cache,
themePath: this._themeConfigManager.themePath,
stencilCliVersion: PACKAGE_INFO.version,
storeSettingsLocale: this._storeSettingsLocale,
});
}

Expand All @@ -156,10 +162,6 @@ class StencilStart {
const DEFAULT_WATCH_IGNORED = ['/assets/scss', '/assets/css'];
const { themePath, configPath } = this._themeConfigManager;
const { watchOptions } = this._buildConfigManager;
const defaultShopperLanguage = await this.getDefaultShopperLanguage(
cliOptions,
stencilConfig,
);

// Watch sccs directory and automatically reload all css files if a file changes
const stylesPath = path.join(themePath, 'assets/scss');
Expand Down Expand Up @@ -203,7 +205,10 @@ class StencilStart {
this._browserSync.watch(langsPath, async (event) => {
try {
if (event === 'change') {
await this.checkLangFiles(langsPath, defaultShopperLanguage);
await this.checkLangFiles(
langsPath,
this._storeSettingsLocale.default_shopper_language,
);
}
} catch (e) {
this._logger.error(e);
Expand Down Expand Up @@ -247,7 +252,7 @@ class StencilStart {
this._buildConfigManager.initWorker().development(this._browserSync);
}

await this.checkLangFiles(langsPath, defaultShopperLanguage);
await this.checkLangFiles(langsPath, this._storeSettingsLocale.default_shopper_language);
}

/**
Expand Down
20 changes: 14 additions & 6 deletions lib/store-settings-api-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,32 @@ const NetworkUtils = require('./utils/NetworkUtils');

const networkUtils = new NetworkUtils();

async function getDefaultShopperLanguage({ apiHost, storeHash, accessToken }) {
async function getStoreSettingsLocale({ apiHost, storeHash, accessToken }) {
try {
const response = await networkUtils.sendApiRequest({
url: `${apiHost}/stores/${storeHash}/v3/settings/store/locale`,
accessToken,
});
if (!response.data.data || !response.data.data.default_shopper_language) {

if (!response.data.data) {
throw new Error('Received empty store locale in the server response'.red);
} else if (!response.data.data.default_shopper_language) {
throw new Error(
'Received empty default_shopper_language field in the server response'.red,
);
} else if (!response.data.data.shopper_language_selection_method) {
throw new Error(
'Received empty default_shopper_language value in the server response'.red,
'Received empty shopper_language_selection_method field in the server response'.red,
);
}
return response.data.data.default_shopper_language;

return response.data.data;
} catch (err) {
err.name = 'DefaultShopperLanguageError';
err.name = 'StoreSettingsLocaleError';
throw err;
}
}

module.exports = {
getDefaultShopperLanguage,
getStoreSettingsLocale,
};
2 changes: 2 additions & 0 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ function buildManifest(srcManifest, options) {
options.dotStencilFile.customLayouts;
pluginsByName['./plugins/renderer/renderer.module'].themePath = options.themePath;
pluginsByName['./plugins/renderer/renderer.module'].storeUrl = storeUrl;
pluginsByName['./plugins/renderer/renderer.module'].storeSettingsLocale =
options.storeSettingsLocale;
pluginsByName['./plugins/theme-assets/theme-assets.module'].themePath = options.themePath;

resManifest.register.plugins = _.reduce(
Expand Down
12 changes: 11 additions & 1 deletion server/plugins/renderer/renderer.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,16 @@ internals.getTemplatePath = (requestPath, data) => {
return templatePath || data.template_file;
};

function getAcceptLanguageHeader(request) {
if (
internals.options.storeSettingsLocale.shopper_language_selection_method ===
'default_shopper_language'
) {
return internals.options.storeSettingsLocale.default_shopper_language;
}
return request.headers['accept-language'];
}

/**
* Creates a new Pencil Response object and returns it.
*
Expand Down Expand Up @@ -400,7 +410,7 @@ internals.getPencilResponse = (data, request, response, configuration, renderedR
context,
translations: data.translations,
method: request.method,
acceptLanguage: request.headers['accept-language'],
acceptLanguage: getAcceptLanguageHeader(request),
headers: response.headers,
statusCode: response.status,
},
Expand Down