Create a configs plugin
This starts to migrate configs to state
This commit is contained in:
20
src/plugins/configs/actions.js
Normal file
20
src/plugins/configs/actions.js
Normal file
@@ -0,0 +1,20 @@
|
||||
export const UPDATE_CONFIGS = "configs_update"
|
||||
export const TOGGLE_CONFIGS = "configs_toggle"
|
||||
|
||||
// Update the configs, with a merge ( not deep )
|
||||
export function update(configName, configValue) {
|
||||
return {
|
||||
type: UPDATE_CONFIGS,
|
||||
payload: {
|
||||
[configName]: configValue
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Toggle's the config, by name
|
||||
export function toggle(configName) {
|
||||
return {
|
||||
type: TOGGLE_CONFIGS,
|
||||
payload: configName,
|
||||
}
|
||||
}
|
||||
@@ -1,56 +1,66 @@
|
||||
import YAML from "js-yaml"
|
||||
import yamlConfig from "../../../swagger-config.yaml"
|
||||
import * as actions from "./actions"
|
||||
import * as selectors from "./selectors"
|
||||
import reducers from "./reducers"
|
||||
|
||||
const parseYamlConfig = (yaml, system) => {
|
||||
try {
|
||||
return YAML.safeLoad(yaml)
|
||||
} catch(e) {
|
||||
if (system) {
|
||||
system.errActions.newThrownErr( new Error(e) )
|
||||
}
|
||||
return {}
|
||||
try {
|
||||
return YAML.safeLoad(yaml)
|
||||
} catch(e) {
|
||||
if (system) {
|
||||
system.errActions.newThrownErr( new Error(e) )
|
||||
}
|
||||
return {}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export default function configPlugin (toolbox) {
|
||||
let { fn } = toolbox
|
||||
|
||||
const actions = {
|
||||
downloadConfig: (url) => () => {
|
||||
let {fetch} = fn
|
||||
return fetch(url)
|
||||
},
|
||||
|
||||
getConfigByUrl: (configUrl, cb)=> ({ specActions }) => {
|
||||
if (configUrl) {
|
||||
return specActions.downloadConfig(configUrl).then(next, next)
|
||||
}
|
||||
|
||||
function next(res) {
|
||||
if (res instanceof Error || res.status >= 400) {
|
||||
specActions.updateLoadingStatus("failedConfig")
|
||||
specActions.updateLoadingStatus("failedConfig")
|
||||
specActions.updateUrl("")
|
||||
console.error(res.statusText + " " + configUrl)
|
||||
cb(null)
|
||||
} else {
|
||||
cb(parseYamlConfig(res.text))
|
||||
}
|
||||
}
|
||||
}
|
||||
const specActions = {
|
||||
downloadConfig: (url) => ({fn}) => {
|
||||
let {fetch} = fn
|
||||
return fetch(url)
|
||||
},
|
||||
|
||||
getConfigByUrl: (configUrl, cb)=> ({ specActions }) => {
|
||||
if (configUrl) {
|
||||
return specActions.downloadConfig(configUrl).then(next, next)
|
||||
}
|
||||
|
||||
const selectors = {
|
||||
getLocalConfig: () => {
|
||||
return parseYamlConfig(yamlConfig)
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
statePlugins: {
|
||||
spec: { actions, selectors }
|
||||
}
|
||||
function next(res) {
|
||||
if (res instanceof Error || res.status >= 400) {
|
||||
specActions.updateLoadingStatus("failedConfig")
|
||||
specActions.updateLoadingStatus("failedConfig")
|
||||
specActions.updateUrl("")
|
||||
console.error(res.statusText + " " + configUrl)
|
||||
cb(null)
|
||||
} else {
|
||||
cb(parseYamlConfig(res.text))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const specSelectors = {
|
||||
getLocalConfig: () => {
|
||||
return parseYamlConfig(yamlConfig)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export default function configsPlugin() {
|
||||
|
||||
return {
|
||||
statePlugins: {
|
||||
spec: {
|
||||
actions: specActions,
|
||||
selectors: specSelectors,
|
||||
},
|
||||
configs: {
|
||||
reducers,
|
||||
actions,
|
||||
selectors,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
20
src/plugins/configs/reducers.js
Normal file
20
src/plugins/configs/reducers.js
Normal file
@@ -0,0 +1,20 @@
|
||||
import { fromJS } from "immutable"
|
||||
|
||||
import {
|
||||
UPDATE_CONFIGS,
|
||||
TOGGLE_CONFIGS,
|
||||
} from "./actions"
|
||||
|
||||
export default {
|
||||
|
||||
[UPDATE_CONFIGS]: (state, action) => {
|
||||
return state.merge(fromJS(action.payload))
|
||||
},
|
||||
|
||||
[TOGGLE_CONFIGS]: (state, action) => {
|
||||
const configName = action.payload
|
||||
const oriVal = state.get(configName)
|
||||
return state.set(configName, !oriVal)
|
||||
},
|
||||
|
||||
}
|
||||
4
src/plugins/configs/selectors.js
Normal file
4
src/plugins/configs/selectors.js
Normal file
@@ -0,0 +1,4 @@
|
||||
// Just get the config value ( it can possibly be an immutable object)
|
||||
export const get = (state, path) => {
|
||||
return state.getIn(Array.isArray(path) ? path : [path])
|
||||
}
|
||||
Reference in New Issue
Block a user