Created
March 13, 2018 11:50
-
-
Save karlwestin/f02f6584d543ad97a358ed2827d18f89 to your computer and use it in GitHub Desktop.
config without higher order function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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