Skip to content

Commit

Permalink
add "Refresh App Configuration" row to Profile tab
Browse files Browse the repository at this point in the history
This feature allows the user to refresh the app configuration without having to log out and back in.
(This will be handy for our alpha "run-through" of the fermata project, e-mission/nrel-openpath-deploy-configs#89.
we will likely need to update beacon + vehicle info periodically.

dynamicConfig.ts
-- in fetchConfig, pass an option to the fetch API so it does not use a cached copy of the config. We want to make sure it's actually checking for the latest config.
-- export function loadNewConfig so it can be used in ProfileSettings.tsx

ProfileSettings.tsx
--add function refreshConfig, which calls loadNewConfig and triggers a hard refresh if the config has changed. Toast messages to guide the user through the process.
--add the new row itself: "Refresh App Configuration" (which also shows the current version of the app config)

appConfigTypes.ts
--add prop 'version' to config type. Every config has this property.

Testing done:
On a local dev environment with locally hosted configs, I was signed into an nrel-commute opcode. I updated the local config file, changing "version" from 1 to 2 and changing "use_imperial" from true to false.
In the UI Profile tab, the new row showed "Current version: 1". After clicking the row, the app reloads and UI now shows 'km' instead of 'miles'. I went back to the Profile tab and the new row now shows "Current version: 2". Clicking the row a second time triggers a toast message saying "Already up to date!"
  • Loading branch information
JGreenlee committed Apr 14, 2024
1 parent fbedee4 commit 3fd2863
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
6 changes: 5 additions & 1 deletion www/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@
"reminders-time-of-day": "Time of Day for Reminders ({{time}})",
"upcoming-notifications": "Upcoming Notifications",
"dummy-notification": "Dummy Notification in 5 Seconds",
"log-out": "Log Out"
"log-out": "Log Out",
"refresh-app-config": "Refresh App Configuration",
"current-version": "Current version: {{version}}",
"refreshing-app-config": "Refreshing app configuration, please wait...",
"already-up-to-date": "Already up to date!"
},

"general-settings": {
Expand Down
9 changes: 5 additions & 4 deletions www/js/config/dynamicConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,17 @@ async function readConfigFromServer(studyLabel: string) {
*/
async function fetchConfig(studyLabel: string, alreadyTriedLocal?: boolean) {
logDebug('Received request to join ' + studyLabel);
const downloadURL = `https://raw.githubusercontent.com/e-mission/nrel-openpath-deploy-configs/main/configs/${studyLabel}.nrel-op.json`;
let downloadURL = `https://raw.githubusercontent.com/e-mission/nrel-openpath-deploy-configs/main/configs/${studyLabel}.nrel-op.json`;
if (!__DEV__ || alreadyTriedLocal) {
logDebug('Fetching config from github');
const r = await fetch(downloadURL);
const r = await fetch(downloadURL, { cache: 'reload' });
if (!r.ok) throw new Error('Unable to fetch config from github');
return r.json(); // TODO: validate, make sure it has required fields
} else {
logDebug('Running in dev environment, checking for locally hosted config');
try {
const r = await fetch('http://localhost:9090/configs/' + studyLabel + '.nrel-op.json');
downloadURL = `http://localhost:9090/configs/${studyLabel}.nrel-op.json`;
const r = await fetch(downloadURL, { cache: 'reload' });
if (!r.ok) throw new Error('Local config not found');
return r.json();
} catch (err) {
Expand Down Expand Up @@ -227,7 +228,7 @@ function extractSubgroup(token: string, config: AppConfig): string | undefined {
* @param existingVersion If the new config's version is the same, we won't update
* @returns boolean representing whether the config was updated or not
*/
function loadNewConfig(newToken: string, existingVersion?: number): Promise<boolean> {
export function loadNewConfig(newToken: string, existingVersion?: number): Promise<boolean> {
const newStudyLabel = extractStudyName(newToken);
return readConfigFromServer(newStudyLabel)
.then((downloadedConfig) => {
Expand Down
17 changes: 16 additions & 1 deletion www/js/control/ProfileSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import ControlCollectionHelper, {
helperToggleLowAccuracy,
forceTransition,
} from './ControlCollectionHelper';
import { resetDataAndRefresh } from '../config/dynamicConfig';
import { loadNewConfig, resetDataAndRefresh } from '../config/dynamicConfig';
import { AppContext } from '../App';
import { shareQR } from '../components/QrCode';
import { storageClear } from '../plugin/storage';
Expand Down Expand Up @@ -307,6 +307,16 @@ const ProfileSettings = () => {
}, 1500);
}

async function refreshConfig() {
AlertManager.addMessage({ text: t('control.refreshing-app-config') });
const updated = await loadNewConfig(authSettings.opcode, appConfig?.version);
if (updated) {
window.location.reload();
} else {
AlertManager.addMessage({ text: t('control.already-up-to-date') });
}
}

//Platform.OS returns "web" now, but could be used once it's fully a Native app
//for now, use window.cordova.platformId

Expand Down Expand Up @@ -434,6 +444,11 @@ const ProfileSettings = () => {
textKey="control.email-log"
iconName="email"
action={() => sendEmail('loggerDB')}></SettingRow>
<SettingRow
textKey="control.refresh-app-config"
desc={t('control.current-version', { version: appConfig?.version })}
iconName="cog-refresh"
action={refreshConfig}></SettingRow>
<ExpansionSection sectionTitle="control.dev-zone">
<BluetoothScanSettingRow />
<SettingRow
Expand Down
1 change: 1 addition & 0 deletions www/js/types/appConfigTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// examples of configs: https://github.com/e-mission/nrel-openpath-deploy-configs/tree/main/configs

export type AppConfig = {
version: number;
server: ServerConnConfig;
intro: IntroConfig;
survey_info: {
Expand Down

0 comments on commit 3fd2863

Please sign in to comment.