From 5d0ee49b07adf3dbdb872aae9851be1ee778db92 Mon Sep 17 00:00:00 2001 From: Austin McGee <947888+amcgee@users.noreply.github.com> Date: Thu, 26 Sep 2019 21:43:41 +0200 Subject: [PATCH 1/3] feat: add support for standalone mode (default in development) --- cli/src/commands/build.js | 8 ++-- cli/src/lib/shell/env.js | 25 ------------ docs/scripts/start.md | 2 +- examples/simple-app/d2.config.js | 2 + shell/adapter/i18n/en.pot | 9 +++-- shell/adapter/src/AuthBoundary/LoginModal.js | 41 ++++++++++++++------ shell/adapter/src/AuthBoundary/index.js | 6 +-- shell/src/App.js | 4 +- 8 files changed, 48 insertions(+), 49 deletions(-) diff --git a/cli/src/commands/build.js b/cli/src/commands/build.js index ed4206537..e6c8798fd 100644 --- a/cli/src/commands/build.js +++ b/cli/src/commands/build.js @@ -52,9 +52,11 @@ const handler = async ({ const config = parseConfig(paths) const shell = makeShell({ config, paths }) - process.env.PUBLIC_URL = - process.env.PUBLIC_URL || - `/api/apps/${getUrlFriendlyName(config.title)}` + process.env.PUBLIC_URL = process.env.PUBLIC_URL || '.' + if (!config.standalone) { + process.env.DHIS2_BASE_URL = + process.env.DHIS2_BASE_URL || config.coreApp ? `..` : `../../..` + } await fs.remove(paths.buildOutput) diff --git a/cli/src/lib/shell/env.js b/cli/src/lib/shell/env.js index 0a05dad99..6d0eeb82d 100644 --- a/cli/src/lib/shell/env.js +++ b/cli/src/lib/shell/env.js @@ -23,30 +23,6 @@ const prefixEnvForCRA = env => {} ) -const getDHISConfig = () => { - const dhisConfigPath = - process.env.DHIS2_HOME && `${process.env.DHIS2_HOME}/config` - - let dhisConfig - try { - dhisConfig = require(dhisConfigPath) - } catch (e) { - // Failed to load config file - use default config - reporter.debug(`\nWARNING! Failed to load DHIS config:`, e.message) - dhisConfig = { - baseUrl: 'http://localhost:8080', - authorization: 'Basic YWRtaW46ZGlzdHJpY3Q=', // admin:district - } - } - - return dhisConfig -} - -const envFromDHISConfig = config => ({ - DHIS2_BASE_URL: config.baseUrl, - DHIS2_AUTHORIZATION: config.authorization, -}) - const makeShellEnv = vars => Object.entries(vars).reduce( (out, [key, value]) => ({ @@ -59,7 +35,6 @@ const makeShellEnv = vars => module.exports = vars => { const env = { ...prefixEnvForCRA({ - ...envFromDHISConfig(getDHISConfig()), ...filterEnv(), ...makeShellEnv(vars), }), diff --git a/docs/scripts/start.md b/docs/scripts/start.md index 8326f2b04..3fb1665c8 100644 --- a/docs/scripts/start.md +++ b/docs/scripts/start.md @@ -1,6 +1,6 @@ # d2 app scripts start -Run a live-reloading development server on `localhost:3000` +Run a live-reloading development server on localhost > **NOTE**: This command is currently unsupported for libraries diff --git a/examples/simple-app/d2.config.js b/examples/simple-app/d2.config.js index 0b57ffcfd..0bf536ec9 100644 --- a/examples/simple-app/d2.config.js +++ b/examples/simple-app/d2.config.js @@ -3,6 +3,8 @@ const config = { title: 'Simple Example App', description: 'This is a simple example application', + standalone: true, // Don't bake-in a DHIS2 base URL, allow the user to choose + entryPoints: { app: './src/App', }, diff --git a/shell/adapter/i18n/en.pot b/shell/adapter/i18n/en.pot index c3307eade..e96ec3510 100644 --- a/shell/adapter/i18n/en.pot +++ b/shell/adapter/i18n/en.pot @@ -5,12 +5,15 @@ msgstr "" "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n != 1)\n" -"POT-Creation-Date: 2019-06-18T09:28:13.445Z\n" -"PO-Revision-Date: 2019-06-18T09:28:13.445Z\n" +"POT-Creation-Date: 2019-09-26T16:40:43.483Z\n" +"PO-Revision-Date: 2019-09-26T16:40:43.483Z\n" msgid "Please sign in" msgstr "" +msgid "Server" +msgstr "" + msgid "Username" msgstr "" @@ -20,7 +23,7 @@ msgstr "" msgid "Sign in" msgstr "" -msgid "An error occurred in the DHIS2 Maps application." +msgid "An error occurred in the DHIS2 application." msgstr "" msgid "Something went wrong" diff --git a/shell/adapter/src/AuthBoundary/LoginModal.js b/shell/adapter/src/AuthBoundary/LoginModal.js index bf7da921f..fb60dee22 100644 --- a/shell/adapter/src/AuthBoundary/LoginModal.js +++ b/shell/adapter/src/AuthBoundary/LoginModal.js @@ -2,7 +2,12 @@ import React, { useState } from 'react' import i18n from '../locales' import { Modal, Button, InputField } from '@dhis2/ui-core' +const staticUrl = process.env.REACT_APP_DHIS2_BASE_URL + export const LoginModal = ({ url }) => { + const [server, setServer] = useState( + staticUrl || window.localStorage.DHIS2_BASE_URL || '' + ) const [username, setUsername] = useState('') const [password, setPassword] = useState('') const [isDirty, setIsDirty] = useState(false) @@ -12,18 +17,22 @@ export const LoginModal = ({ url }) => { const onSubmit = async e => { e.preventDefault() setIsDirty(true) - if (isValid(username) && isValid(password)) { + if (isValid(server) && isValid(username) && isValid(password)) { + window.localStorage.DHIS2_BASE_URL = server try { - await fetch(`${url}/dhis-web-commons-security/login.action`, { - method: 'POST', - credentials: 'include', - body: `j_username=${username}&j_password=${password}`, - headers: { - 'X-Requested-With': 'XMLHttpRequest', - Accept: 'application/json', - 'Content-Type': 'application/x-www-form-urlencoded', - }, - }) + await fetch( + `${server}/dhis-web-commons-security/login.action`, + { + method: 'POST', + credentials: 'include', + body: `j_username=${username}&j_password=${password}`, + headers: { + 'X-Requested-With': 'XMLHttpRequest', + Accept: 'application/json', + 'Content-Type': 'application/x-www-form-urlencoded', + }, + } + ) } catch (e) { console.log( 'TODO: This will always error and cancel the request until we get a real login endpoint!' @@ -40,6 +49,16 @@ export const LoginModal = ({ url }) => {