Skip to content

Instantly share code, notes, and snippets.

@karlwestin
Created March 13, 2018 11:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karlwestin/f02f6584d543ad97a358ed2827d18f89 to your computer and use it in GitHub Desktop.
Save karlwestin/f02f6584d543ad97a358ed2827d18f89 to your computer and use it in GitHub Desktop.
config without higher order function
const common = {}
let config = {}
export function setup = url => {
van: {
...common,
remoteDbUrl: process.env.REACT_APP_VAN_COUCHDB_URL || 'http://localhost:5984/',
dateFormats: {
long: 'D MMM, YYYY',
week: 'YYYY-[W]WW'
},
appName: 'VAN',
logoUrl: '',
theme: '',
hosts: [
'dev.van.field.partners',
'dev.van.field.local:3000',
'staging.van.field.partners',
'training.van.field.partners',
'van.field.partners'
]
},
psm: {
...common,
remoteDbUrl: process.env.REACT_APP_VAN_COUCHDB_URL || 'http://localhost:5984/',
dateFormats: {
long: 'D MMM, YYYY',
week: 'YYYY-[W]WW'
},
appName: 'National Stock System',
logoUrl: 'https://s3-eu-west-1.amazonaws.com/field-psm/assets/coat-of-arms-nigeria.svg',
theme: 'psm',
hosts: [
'dev.nscip.field.supply',
'dev.nscip.field.local:3000',
'staging.nscip.field.supply',
'nscip.field.supply'
]
}
// somehow generate the config based on the passed in url
// replace the config variable on module level
config = generateFromUrl(url)
}
// this is used to access the config object
export const getConfig () => {
return config
}
// For using in one of the sub-apps
import { setConfig } from '@fielded/van-shared/build-lib/providers/config'
import Root from './root/Root'
setConfig(window.location)
render(<Root />, rootElement)
import { getConfig } from '@fielded/van-shared/build-lib/providers/config'
const MyComponent = ({ config }) => {
const config = getConfig()
return (<div>
<pre>{JSON.stringify(config, false, 2)}</pre>
</div>)
}
export default MyComponentWithConfig
/*
testing
*/
it('choses van style sheet', t => {
setConfig(vanurl)
// render and make assertions
})
it('choses psm style sheet', t => {
setConfig(psmurl)
// render and make assertions
})
import React, { Component } from 'react'
import { setConfig } from '@fielded/van-shared/build-lib/providers/config'
import Loading from '../common/Loading'
import PreloadedStateProvider from './PreloadedStateProvider'
import { setConfig } from './common/config'
import { getPreloadedState } from './reducer'
class Root extends Component {
state = {}
constructor () {
super()
setConfig(window.location)
}
componentDidMount () {
if (!this.state.preloadedState) {
return getPreloadedState()
.then(preloadedState => this.setState({preloadedState}))
}
}
render () {
if (!this.state.preloadedState) {
return <Loading />
}
// ConfigProvider calls `resolveConfigFromLocation` passing the location object
// and config options and populates the context.config with the correct values
return (
<PreloadedStateProvider
preloadedState={this.state.preloadedState}
/>
)
}
}
export default Root
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment