Skip to content

Commit

Permalink
Update main.tsx logic
Browse files Browse the repository at this point in the history
  • Loading branch information
joelvdavies committed Aug 14, 2024
1 parent 2a7c104 commit 01d4550
Showing 1 changed file with 14 additions and 25 deletions.
39 changes: 14 additions & 25 deletions src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from 'axios';
import * as log from 'loglevel';
import log from 'loglevel';
import { SetupWorker } from 'msw/browser';
import React from 'react';
import ReactDOMClient from 'react-dom/client';
Expand Down Expand Up @@ -100,6 +100,7 @@ export function unmount(props: unknown): Promise<void> {
});
}

// TODO JOEL: Add tests for this like OG?
// only export this for testing
export const fetchSettings =
(): Promise<InventoryManagementSystemSettings | void> => {
Expand Down Expand Up @@ -168,7 +169,12 @@ export const fetchSettings =
};

async function prepare() {
if (import.meta.env.DEV || import.meta.env.VITE_APP_INCLUDE_MSW === 'true') {
// When in dev, only use MSW if the api url is given, otherwise load MSW as it must have been explicitly requested
const settingsResult = await settings;
if (
import.meta.env.VITE_APP_INCLUDE_MSW === 'true' ||
settingsResult?.apiUrl === ''
) {
// Need to use require instead of import as import breaks when loaded in SG
const { worker } = await import('./mocks/browser');
return (worker as SetupWorker).start({
Expand All @@ -181,12 +187,10 @@ async function prepare() {
print.warning();
},
});
}
return Promise.resolve();
} else return Promise.resolve();
}

const settings = fetchSettings();

setSettings(settings);

/* Renders only if we're not being loaded by SG */
Expand All @@ -196,27 +200,12 @@ const conditionalSciGatewayRender = () => {
}
};

if (import.meta.env.DEV) {
// When in dev, only use MSW if the api url or otherwise if MSW is explicitly requested
settings
.then((settings) => {
if (
(settings && settings.apiUrl !== '') ||
import.meta.env.VITE_APP_INCLUDE_MSW === 'false'
)
conditionalSciGatewayRender();
else prepare().then(() => conditionalSciGatewayRender());
})
.catch((error) => log.error(`Got error: ${error.message}`));
if (import.meta.env.DEV || import.meta.env.VITE_APP_INCLUDE_MSW === 'true') {
prepare().then(() => conditionalSciGatewayRender());

log.setDefaultLevel(log.levels.DEBUG);
} else {
// When in production, only use MSW if explicitly requested
if (import.meta.env.VITE_APP_INCLUDE_MSW === 'true') {
prepare().then(() => conditionalSciGatewayRender());
log.setDefaultLevel(log.levels.DEBUG);
} else {
conditionalSciGatewayRender();
log.setDefaultLevel(log.levels.ERROR);
}
conditionalSciGatewayRender();

log.setDefaultLevel(log.levels.ERROR);
}

0 comments on commit 01d4550

Please sign in to comment.