Create a configs plugin

This starts to migrate configs to state
This commit is contained in:
Josh Ponelat
2017-10-18 13:06:33 +02:00
parent c2bdb3af24
commit 50b318c5d9
6 changed files with 106 additions and 51 deletions

View File

@@ -58,13 +58,12 @@ module.exports = function SwaggerUI(opts) {
plugins: [ plugins: [
], ],
// Initial state
initialState: { },
// Inline Plugin // Inline Plugin
fn: { }, fn: { },
components: { }, components: { },
state: { },
// Override some core configs... at your own risk
store: { },
} }
let queryConfig = parseSearch() let queryConfig = parseSearch()
@@ -74,12 +73,12 @@ module.exports = function SwaggerUI(opts) {
const constructorConfig = deepExtend({}, defaults, opts, queryConfig) const constructorConfig = deepExtend({}, defaults, opts, queryConfig)
const storeConfigs = deepExtend({}, constructorConfig.store, { const storeConfigs = {
system: { system: {
configs: constructorConfig.configs configs: constructorConfig.configs
}, },
plugins: constructorConfig.presets, plugins: constructorConfig.presets,
state: { state: deepExtend({
layout: { layout: {
layout: constructorConfig.layout, layout: constructorConfig.layout,
filter: constructorConfig.filter filter: constructorConfig.filter
@@ -88,8 +87,8 @@ module.exports = function SwaggerUI(opts) {
spec: "", spec: "",
url: constructorConfig.url url: constructorConfig.url
} }
}, constructorConfig.initialState)
} }
})
let inlinePlugin = ()=> { let inlinePlugin = ()=> {
return { return {

View File

@@ -10,6 +10,7 @@ import auth from "core/plugins/auth"
import util from "core/plugins/util" import util from "core/plugins/util"
import SplitPaneModePlugin from "core/plugins/split-pane-mode" import SplitPaneModePlugin from "core/plugins/split-pane-mode"
import downloadUrlPlugin from "core/plugins/download-url" import downloadUrlPlugin from "core/plugins/download-url"
import configsPlugin from "plugins/configs"
import deepLinkingPlugin from "core/plugins/deep-linking" import deepLinkingPlugin from "core/plugins/deep-linking"
import App from "core/components/app" import App from "core/components/app"
@@ -122,6 +123,7 @@ export default function() {
} }
return [ return [
configsPlugin,
util, util,
logs, logs,
view, view,

View 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,
}
}

View File

@@ -1,5 +1,8 @@
import YAML from "js-yaml" import YAML from "js-yaml"
import yamlConfig from "../../../swagger-config.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) => { const parseYamlConfig = (yaml, system) => {
try { try {
@@ -13,11 +16,8 @@ const parseYamlConfig = (yaml, system) => {
} }
export default function configPlugin (toolbox) { const specActions = {
let { fn } = toolbox downloadConfig: (url) => ({fn}) => {
const actions = {
downloadConfig: (url) => () => {
let {fetch} = fn let {fetch} = fn
return fetch(url) return fetch(url)
}, },
@@ -39,18 +39,28 @@ export default function configPlugin (toolbox) {
} }
} }
} }
} }
const selectors = { const specSelectors = {
getLocalConfig: () => { getLocalConfig: () => {
return parseYamlConfig(yamlConfig) return parseYamlConfig(yamlConfig)
} }
} }
export default function configsPlugin() {
return { return {
statePlugins: { statePlugins: {
spec: { actions, selectors } spec: {
actions: specActions,
selectors: specSelectors,
},
configs: {
reducers,
actions,
selectors,
}
} }
} }
} }

View 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)
},
}

View 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])
}