Skip to content

Commit

Permalink
πŸ§‘β€πŸ’»(frontend) split settings file
Browse files Browse the repository at this point in the history
All settings may be changeable in development without interfering with
production settings.
  • Loading branch information
rlecellier committed Feb 22, 2024
1 parent cec322c commit d1aefca
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,4 @@ docker/files/etc/nginx/ssl/*
storybook-static

# Frontend dev settings
src/frontend/js/settings.dev.ts
src/frontend/js/settings/settings.dev.ts
4 changes: 2 additions & 2 deletions src/frontend/js/api/lms/dummy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export type DevDemoUser = keyof typeof JOANIE_DEV_DEMO_USER_JWT_TOKENS;

export const RICHIE_DUMMY_IS_LOGGED_IN = 'RICHIE_DUMMY_IS_LOGGED_IN';

function getUserInfo(username: DevDemoUser): Maybe<User> {
function getUserInfo(username: DevDemoUser): User {
const accessToken = JOANIE_DEV_DEMO_USER_JWT_TOKENS[username];
const JWTPayload: JWTPayload = JSON.parse(base64Decode(accessToken.split('.')[1]));

Expand Down Expand Up @@ -78,7 +78,7 @@ const API = (APIConf: LMSBackend | AuthenticationBackend): APILms => {
if (!localStorage.getItem(RICHIE_DUMMY_IS_LOGGED_IN)) {
return null;
}
return getUserInfo(CURRENT_JOANIE_DEV_DEMO_USER) || null;
return CURRENT_JOANIE_DEV_DEMO_USER ? getUserInfo(CURRENT_JOANIE_DEV_DEMO_USER) : null;
},
login: () => {
localStorage.setItem(RICHIE_DUMMY_IS_LOGGED_IN, 'true');
Expand Down
3 changes: 0 additions & 3 deletions src/frontend/js/settings.dev.dist.ts

This file was deleted.

38 changes: 38 additions & 0 deletions src/frontend/js/settings/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { mergeWith } from 'lodash-es';
import * as prodSettings from './settings.prod';
import * as testSettings from './settings.test';

let settingsOverride = {};
if (process.env.NODE_ENV === 'development') {
try {
settingsOverride = require('./settings.local.ts');
} catch {
// no local settings found, do nothing
}
} else if (process.env.NODE_ENV === 'test') {
settingsOverride = testSettings;
}

try {
settingsOverride = require('./settings.local.ts');
} catch {
// no local settings found, do nothing
}

const settings = mergeWith({}, prodSettings, settingsOverride);

export const {
API_LIST_DEFAULT_PARAMS,
EDX_CSRF_TOKEN_COOKIE_NAME,
RICHIE_USER_TOKEN,
RICHIE_LTI_ANONYMOUS_USER_ID_CACHE_KEY,
JOANIE_API_VERSION,
REACT_QUERY_SETTINGS,
PAYMENT_SETTINGS,
CONTRACT_SETTINGS,
CONTRACT_DOWNLOAD_SETTINGS,
PER_PAGE,
MOCK_SERVICE_WORKER_ENABLED,
DEBUG_UNION_RESOURCES_HOOK,
CURRENT_JOANIE_DEV_DEMO_USER,
} = settings;
25 changes: 25 additions & 0 deletions src/frontend/js/settings/settings.dev.dist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// This configuration file can be overridden with settings.dev.ts file.
// `$ cp settings.local.dist.ts settings.local.ts`, then change what's needed
// in local environment in settings.local.ts.
import { DevDemoUser } from 'api/lms/dummy';

// disable react query cache
// export const REACT_QUERY_SETTINGS = {
// staleTimes: {
// session: 0,
// sessionItems: 0,
// },
// };

/**
* Available users:
* * admin
* * user0
* * user1
* * user2
* * user3
* * user4
* * organization_owner
* * student_user
*/
export const CURRENT_JOANIE_DEV_DEMO_USER: DevDemoUser = 'admin';
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import * as defaultDevSettings from './settings.dev.dist';

export const API_LIST_DEFAULT_PARAMS = {
limit: '21',
offset: '0',
Expand Down Expand Up @@ -61,15 +59,5 @@ export const PER_PAGE = {
useOrdersEnrollments: DEFAULT_PER_PAGE,
};

let devSettings;
try {
devSettings = require('settings.dev.ts');
} catch {
devSettings = defaultDevSettings;
}

export const MOCK_SERVICE_WORKER_ENABLED = false;

export const DEBUG_UNION_RESOURCES_HOOK = process.env.NODE_ENV !== 'production' && false;

export const { CURRENT_JOANIE_DEV_DEMO_USER } = devSettings;
export const DEBUG_UNION_RESOURCES_HOOK = false;
16 changes: 16 additions & 0 deletions src/frontend/js/settings/settings.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// This confiuration file is used for testing.
// Mostly usefull to test our test tools.
import { DevDemoUser } from 'api/lms/dummy';

/**
* Available users:
* * admin
* * user0
* * user1
* * user2
* * user3
* * user4
* * organization_owner
* * student_user
*/
export const CURRENT_JOANIE_DEV_DEMO_USER: DevDemoUser = 'admin';

0 comments on commit d1aefca

Please sign in to comment.