Skip to content

Commit

Permalink
chore: example updates for login_app type
Browse files Browse the repository at this point in the history
  • Loading branch information
tomzemp committed Feb 21, 2023
1 parent cba768a commit 2be3f4e
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 35 deletions.
30 changes: 29 additions & 1 deletion adapter/src/components/AppWrapper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import PropTypes from 'prop-types'
import React from 'react'
import { useCurrentUserLocale } from '../utils/useLocale.js'
import {
useCurrentUserLocale,
useSystemDefaultLocale,
} from '../utils/useLocale.js'
import { useVerifyLatestUser } from '../utils/useVerifyLatestUser.js'
import { Alerts } from './Alerts.js'
import { ConnectedHeaderBar } from './ConnectedHeaderBar.js'
Expand Down Expand Up @@ -34,3 +37,28 @@ AppWrapper.propTypes = {
children: PropTypes.node,
plugin: PropTypes.bool,
}

export const LoginAppWrapper = ({ children }) => {
const { loading: localeLoading } = useSystemDefaultLocale()
// cannot check current user for a loginApp (no api/me)

if (localeLoading) {
return <LoadingMask />
}

return (
<div className="app-shell-adapter">
<style jsx>{styles}</style>
<div className="app-shell-app">
<ErrorBoundary onRetry={() => window.location.reload()}>
{children}
</ErrorBoundary>
</div>
<Alerts />
</div>
)
}

LoginAppWrapper.propTypes = {
children: PropTypes.node,
}
43 changes: 28 additions & 15 deletions adapter/src/components/ServerVersionProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const ServerVersionProvider = ({
url,
apiVersion,
pwaEnabled,
loginApp,
children,
}) => {
const offlineInterface = useOfflineInterface()
Expand All @@ -27,28 +28,39 @@ export const ServerVersionProvider = ({
}

setState((state) => (state.loading ? state : { loading: true }))
const request = get(`${url}/api/system/info`)
request
.then((systemInfo) => {
setState({ loading: false, systemInfo })
})
.catch((e) => {
// Todo: If this is a network error, the app cannot load -- handle that gracefully here
// if (e === 'Network error') { ... }
setState({ loading: false, error: e })
})

return () => {
request.abort()
// in reality we probably want a request to get api version
if (loginApp) {
const fakeSystemInfo = { version: '2.40-SNAPSHOT' }
setState({ loading: false, systemInfo: fakeSystemInfo })
} else {
const request = get(`${url}/api/system/info`)
request
.then((systemInfo) => {
setState({ loading: false, systemInfo })
})
.catch((e) => {
// Todo: If this is a network error, the app cannot load -- handle that gracefully here
// if (e === 'Network error') { ... }
setState({ loading: false, error: e })
})

return () => {
request.abort()
}
}
}, [url])
}, [url, loginApp])

if (loading) {
return <LoadingMask />
}

if (error) {
return <LoginModal />
return !loginApp ? (
<LoginModal />
) : (
<p>Specify DHIS2_BASE_URL environment variable</p>
)
}

const serverVersion = parseDHIS2ServerVersion(systemInfo.version)
Expand All @@ -65,7 +77,7 @@ export const ServerVersionProvider = ({
systemInfo,
pwaEnabled,
}}
offlineInterface={offlineInterface}
offlineInterface={loginApp ? null : offlineInterface}
>
{children}
</Provider>
Expand All @@ -77,6 +89,7 @@ ServerVersionProvider.propTypes = {
appVersion: PropTypes.string.isRequired,
apiVersion: PropTypes.number,
children: PropTypes.element,
loginApp: PropTypes.bool,
pwaEnabled: PropTypes.bool,
url: PropTypes.string,
}
48 changes: 38 additions & 10 deletions adapter/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { checkForSWUpdateAndReload } from '@dhis2/pwa'
import PropTypes from 'prop-types'
import React from 'react'
import { AppWrapper } from './components/AppWrapper.js'
import { LoginAppWrapper, AppWrapper } from './components/AppWrapper.js'
import { ErrorBoundary } from './components/ErrorBoundary.js'
import { OfflineInterfaceProvider } from './components/OfflineInterfaceContext.js'
import { PWALoadingBoundary } from './components/PWALoadingBoundary.js'
Expand All @@ -14,30 +14,58 @@ const AppAdapter = ({
apiVersion,
pwaEnabled,
plugin,
loginApp,
children,
}) => (
<ErrorBoundary fullscreen onRetry={checkForSWUpdateAndReload}>
<OfflineInterfaceProvider>
<PWALoadingBoundary>
}) => {
if (loginApp) {
return (
<ErrorBoundary
fullscreen
onRetry={() => {
window.location.reload()
}}
>
<ServerVersionProvider
appName={appName}
appVersion={appVersion}
url={url}
apiVersion={apiVersion}
pwaEnabled={pwaEnabled}
loginApp={true}
>
<AppWrapper plugin={plugin}>{children}</AppWrapper>
<LoginAppWrapper>{children}</LoginAppWrapper>
</ServerVersionProvider>
</PWALoadingBoundary>
</OfflineInterfaceProvider>
</ErrorBoundary>
)
</ErrorBoundary>
)
}
return (
<ErrorBoundary fullscreen onRetry={checkForSWUpdateAndReload}>
<OfflineInterfaceProvider>
<PWALoadingBoundary>
<ServerVersionProvider
appName={appName}
appVersion={appVersion}
url={url}
apiVersion={apiVersion}
pwaEnabled={pwaEnabled}
loginApp={false}
>
<AppWrapper plugin={plugin} loginApp={loginApp}>
{children}
</AppWrapper>
</ServerVersionProvider>
</PWALoadingBoundary>
</OfflineInterfaceProvider>
</ErrorBoundary>
)
}

AppAdapter.propTypes = {
appName: PropTypes.string.isRequired,
appVersion: PropTypes.string.isRequired,
apiVersion: PropTypes.number,
children: PropTypes.element,
loginApp: PropTypes.bool,
plugin: PropTypes.bool,
pwaEnabled: PropTypes.bool,
url: PropTypes.string,
Expand Down
7 changes: 7 additions & 0 deletions adapter/src/utils/useLocale.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,10 @@ export const useCurrentUserLocale = () => {

return { loading: loading || !locale, locale }
}

export const useSystemDefaultLocale = () => {
// TO-DO: system language query (not currently available)
const defaultLocale = window.navigator.language
const locale = useLocale(defaultLocale)
return { loading: !locale, locale }
}
9 changes: 5 additions & 4 deletions cli/src/commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const generateManifests = require('../lib/generateManifests')
const i18n = require('../lib/i18n')
const loadEnvFiles = require('../lib/loadEnvFiles')
const parseConfig = require('../lib/parseConfig')
const { appTypes } = require('../lib/parseConfig')
const makePaths = require('../lib/paths')
const makePlugin = require('../lib/plugin')
const { injectPrecacheManifest } = require('../lib/pwa')
Expand Down Expand Up @@ -70,7 +71,7 @@ const handler = async ({
const shell = makeShell({ config, paths })
const plugin = makePlugin({ config, paths })

if (config.type === 'app') {
if (appTypes.includes(config.type)) {
setAppParameters(standalone, config)
}

Expand Down Expand Up @@ -105,7 +106,7 @@ const handler = async ({
paths,
})

if (config.type === 'app') {
if (appTypes.includes(config.type)) {
reporter.info('Bootstrapping local appShell...')
await shell.bootstrap({ shell: shellSource, force })
}
Expand All @@ -114,7 +115,7 @@ const handler = async ({
`Building ${config.type} ${chalk.bold(config.name)}...`
)

if (config.type === 'app') {
if (appTypes.includes(config.type)) {
await compile({
config,
paths,
Expand Down Expand Up @@ -167,7 +168,7 @@ const handler = async ({
}
)

if (config.type === 'app') {
if (appTypes.includes(config.type)) {
if (!fs.pathExistsSync(paths.shellBuildOutput)) {
reporter.error('No build output found')
process.exit(1)
Expand Down
3 changes: 2 additions & 1 deletion cli/src/commands/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const generateManifests = require('../lib/generateManifests')
const i18n = require('../lib/i18n')
const loadEnvFiles = require('../lib/loadEnvFiles')
const parseConfig = require('../lib/parseConfig')
const { appTypes } = require('../lib/parseConfig')
const makePaths = require('../lib/paths')
const makePlugin = require('../lib/plugin')
const createProxyServer = require('../lib/proxy')
Expand All @@ -32,7 +33,7 @@ const handler = async ({
const shell = makeShell({ config, paths })
const plugin = makePlugin({ config, paths })

if (config.type !== 'app') {
if (!appTypes.includes(config.type)) {
reporter.error(
`The command ${chalk.bold(
'd2-app-scripts start'
Expand Down
3 changes: 2 additions & 1 deletion cli/src/lib/compiler/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { reporter, prettyPrint } = require('@dhis2/cli-helpers-engine')
const chokidar = require('chokidar')
const fs = require('fs-extra')
const makeBabelConfig = require('../../../config/makeBabelConfig.js')
const { appTypes } = require('../parseConfig')
const {
verifyEntrypoints,
createAppEntrypointWrapper,
Expand Down Expand Up @@ -67,7 +68,7 @@ const compile = async ({
mode = 'development',
watch = false,
}) => {
const isApp = config.type === 'app'
const isApp = appTypes.includes(config.type)

verifyEntrypoints({ config, paths })
if (isApp) {
Expand Down
3 changes: 2 additions & 1 deletion cli/src/lib/compiler/entrypoints.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const path = require('path')
const { reporter, chalk } = require('@dhis2/cli-helpers-engine')
const fs = require('fs-extra')
const { appTypes } = require('../parseConfig')
const { normalizeExtension } = require('./extensionHelpers.js')

const verifyEntrypoint = ({ entrypoint, basePath, resolveModule }) => {
Expand All @@ -26,7 +27,7 @@ exports.verifyEntrypoints = ({
paths,
resolveModule = require.resolve,
}) => {
if (config.type === 'app') {
if (appTypes.includes(config.type)) {
if (
!config.entryPoints ||
(!config.entryPoints.app && !config.entryPoints.plugin)
Expand Down
8 changes: 7 additions & 1 deletion cli/src/lib/parseConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ const parseAuthorString = require('parse-author')

const requiredConfigFields = {
app: ['name', 'version', 'title', 'entryPoints.app'],
login_app: ['name', 'version', 'title', 'entryPoints.app'],
lib: ['name', 'version', 'entryPoints.lib'],
}

const appTypes = ['app', 'login_app']

const parseAuthor = (author) => {
if (isPlainObject(author)) {
return {
Expand Down Expand Up @@ -58,7 +61,7 @@ const parseConfigObjects = (
config = defaultsDeep(config, defaults)

// Add PWA defaults to apps
if (type === 'app') {
if (appTypes.includes(type)) {
config = defaultsDeep(config, defaultsPWA)
}

Expand Down Expand Up @@ -109,3 +112,6 @@ const parseConfig = (paths) => {
module.exports = parseConfig

module.exports.parseConfigObjects = parseConfigObjects

module.exports.parseConfigObjects = parseConfigObjects
module.exports.appTypes = appTypes
4 changes: 3 additions & 1 deletion cli/src/lib/pwa/getPWAEnvVars.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { appTypes } = require('../parseConfig')

/** Preps string literals for regex conversion by escaping special chars */
function escapeForRegex(string) {
return string.replace(/[-\\^$*+?.()|[\]{}]/g, '\\$&')
Expand Down Expand Up @@ -31,7 +33,7 @@ function stringifyPatterns(patternsList) {
* @param {Object} config
*/
function getPWAEnvVars(config) {
if (config.type !== 'app' || !config.pwa.enabled) {
if (!appTypes.includes(config.type) || !config.pwa.enabled) {
return null
}
return {
Expand Down
1 change: 1 addition & 0 deletions cli/src/lib/shell/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = ({ config, paths }) => {
const baseEnvVars = {
name: config.title,
version: config.version,
loginApp: config.type === 'login_app',
}

return {
Expand Down
1 change: 1 addition & 0 deletions shell/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const appConfig = {
apiVersion: parseInt(process.env.REACT_APP_DHIS2_API_VERSION),
pwaEnabled: process.env.REACT_APP_DHIS2_APP_PWA_ENABLED === 'true',
plugin: process.env.REACT_APP_DHIS2_APP_PLUGIN === 'true',
loginApp: process.env.REACT_APP_DHIS2_APP_LOGINAPP === 'true',
}

const App = () => (
Expand Down

0 comments on commit 2be3f4e

Please sign in to comment.