Skip to content

Commit

Permalink
dynamicConfig: use 'function' syntax for big fns
Browse files Browse the repository at this point in the history
To be more consistent with the rest of the codebase, as a rule of thumb we're having top-level functions use 'function' synax while one-liners or nested functions are arrow functions.

Also, resetStoredConfig was renamed to _test_resetStoredConfig because that change is coming anyway in e-mission#1113 and this will make it easier to resolve merge conflicts
  • Loading branch information
JGreenlee committed Dec 9, 2023
1 parent 8c31b65 commit 0527569
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions www/__tests__/enketoHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '../js/survey/enketo/enketoHelper';
import { mockBEMUserCache } from '../__mocks__/cordovaMocks';
import { mockLogger } from '../__mocks__/globalMocks';
import { getConfig, resetStoredConfig } from '../../www/js/config/dynamicConfig';
import { getConfig, _test_resetStoredConfig } from '../../www/js/config/dynamicConfig';
import fakeConfig from '../__mocks__/fakeConfig.json';

import initializedI18next from '../js/i18nextInit';
Expand All @@ -21,7 +21,7 @@ global.URL = require('url').URL;
global.Blob = require('node:buffer').Blob;

beforeEach(() => {
resetStoredConfig();
_test_resetStoredConfig();
});

it('gets the survey config', async () => {
Expand Down
28 changes: 14 additions & 14 deletions www/js/config/dynamicConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ export let storedConfig = null;
export let configChanged = false;
export const setConfigChanged = (b) => (configChanged = b);

//used test multiple configs, not used outside of test
export const resetStoredConfig = function () {
// used to test multiple configs, not used outside of test
export const _test_resetStoredConfig = () => {
storedConfig = null;
};

const _getStudyName = function (connectUrl) {
function _getStudyName(connectUrl) {
const orig_host = new URL(connectUrl).hostname;
const first_domain = orig_host.split('.')[0];
if (first_domain == 'openpath-stage') {
Expand All @@ -28,18 +28,18 @@ const _getStudyName = function (connectUrl) {
}
const study_name = first_domain.substr(0, openpath_index);
return study_name;
};
}

const _fillStudyName = (config: Partial<AppConfig>): AppConfig => {
function _fillStudyName(config: Partial<AppConfig>): AppConfig {
if (config.name) return config as AppConfig;
if (config.server) {
return { ...config, name: _getStudyName(config.server.connectUrl) } as AppConfig;
} else {
return { ...config, name: 'dev' } as AppConfig;
}
};
}

const _backwardsCompatSurveyFill = (config: Partial<AppConfig>): AppConfig => {
function _backwardsCompatSurveyFill(config: Partial<AppConfig>): AppConfig {
if (config.survey_info) return config as AppConfig;
return {
...config,
Expand All @@ -59,13 +59,13 @@ const _backwardsCompatSurveyFill = (config: Partial<AppConfig>): AppConfig => {
'trip-labels': 'MULTILABEL',
},
} as AppConfig;
};
}

/* Fetch and cache any surveys resources that are referenced by URL in the config,
as well as the label_options config if it is present.
This way they will be available when the user needs them, and we won't have to
fetch them again unless local storage is cleared. */
const cacheResourcesFromConfig = (config) => {
function cacheResourcesFromConfig(config) {
if (config.survey_info?.surveys) {
Object.values(config.survey_info.surveys).forEach((survey) => {
if (!survey?.['formPath']) throw new Error(i18next.t('config.survey-missing-formpath'));
Expand All @@ -75,9 +75,9 @@ const cacheResourcesFromConfig = (config) => {
if (config.label_options) {
fetchUrlCached(config.label_options);
}
};
}

const readConfigFromServer = async (label) => {
async function readConfigFromServer(label) {
const fetchedConfig = await fetchConfig(label);
logDebug(`Successfully found config,
fetchedConfig = ${JSON.stringify(fetchedConfig).substring(0, 10)}`);
Expand All @@ -94,9 +94,9 @@ const readConfigFromServer = async (label) => {
deployment_name = ${filledConfig.intro.translated_text.en.deployment_name};
connectionURL = ${fetchedConfig.server ? fetchedConfig.server.connectUrl : 'dev defaults'}`);
return filledConfig;
};
}

const fetchConfig = async (label, alreadyTriedLocal = false) => {
async function fetchConfig(label, alreadyTriedLocal = false) {
logDebug('Received request to join ' + label);
const downloadURL = `https://raw.githubusercontent.com/e-mission/nrel-openpath-deploy-configs/main/configs/${label}.nrel-op.json`;
if (!__DEV__ || alreadyTriedLocal) {
Expand All @@ -115,7 +115,7 @@ const fetchConfig = async (label, alreadyTriedLocal = false) => {
return fetchConfig(label, true);
}
}
};
}

/*
* We want to support both old style and new style tokens.
Expand Down

0 comments on commit 0527569

Please sign in to comment.