Consolidate UI configuration in one place, allowing it to be overridden from the server

This commit is contained in:
Deluan 2020-04-07 16:58:02 -04:00 committed by Deluan Quintão
parent d0188db4f9
commit a0f389fc3e
4 changed files with 28 additions and 4 deletions

View File

@ -1,11 +1,12 @@
import jwtDecode from 'jwt-decode'
import md5 from 'md5-hex'
import baseUrl from './utils/baseUrl'
import config from './config'
const authProvider = {
login: ({ username, password }) => {
let url = baseUrl('/app/login')
if (localStorage.getItem('initialAccountCreation')) {
if (config.firstTime) {
url = baseUrl('/app/createAdmin')
}
const request = new Request(url, {

20
ui/src/config.js Normal file
View File

@ -0,0 +1,20 @@
const defaultConfig = {
firstTime: false,
baseURL: '',
loginBackgroundURL: 'https://source.unsplash.com/random/1600x900?music'
}
let config
try {
const appConfig = JSON.parse(window.__APP_CONFIG__)
config = {
...defaultConfig,
...appConfig
}
} catch (e) {
config = defaultConfig
}
export default config

View File

@ -15,6 +15,7 @@ import LockIcon from '@material-ui/icons/Lock'
import { Notification, useLogin, useNotify, useTranslate } from 'react-admin'
import LightTheme from '../themes/light'
import config from '../config'
const useStyles = makeStyles((theme) => ({
main: {
@ -23,7 +24,7 @@ const useStyles = makeStyles((theme) => ({
minHeight: '100vh',
alignItems: 'center',
justifyContent: 'flex-start',
background: 'url(https://source.unsplash.com/random/1600x900?music)',
background: `url(${config.loginBackgroundURL})`,
backgroundRepeat: 'no-repeat',
backgroundSize: 'cover',
backgroundPosition: 'center'
@ -253,7 +254,7 @@ const Login = ({ location }) => {
return errors
}
if (localStorage.getItem('initialAccountCreation') === 'true') {
if (config.firstTime) {
return (
<FormSignUp
handleSubmit={handleSubmit}

View File

@ -1,5 +1,7 @@
import config from '../config'
const baseUrl = (path) => {
const base = localStorage.getItem('baseURL') || ''
const base = config.baseURL || ''
const parts = [base]
parts.push(path.replace(/^\//, ''))
return parts.join('/')