dependsOn now should check every update

This commit is contained in:
srgooglo 2022-05-31 00:45:47 +02:00
parent 5e9678aa3b
commit 3aaf668903

View File

@ -32,6 +32,7 @@ const SettingItem = (props) => {
const [loading, setLoading] = React.useState(true) const [loading, setLoading] = React.useState(true)
const [value, setValue] = React.useState(item.defaultValue ?? false) const [value, setValue] = React.useState(item.defaultValue ?? false)
const [delayedValue, setDelayedValue] = React.useState(null) const [delayedValue, setDelayedValue] = React.useState(null)
const [disabled, setDisabled] = React.useState(false)
let SettingComponent = item.component let SettingComponent = item.component
@ -99,6 +100,20 @@ const SettingItem = (props) => {
} }
} }
const checkDependsValidation = () => {
return !Boolean(Object.keys(item.dependsOn).every((key) => {
const storagedValue = window.app.settings.get(key)
console.debug(`Checking validation for [${key}] with now value [${storagedValue}]`)
if (typeof item.dependsOn[key] === "function") {
return item.dependsOn[key](storagedValue)
}
return storagedValue === item.dependsOn[key]
}))
}
const settingInitialization = async () => { const settingInitialization = async () => {
if (item.storaged) { if (item.storaged) {
const storagedValue = window.app.settings.get(item.id) const storagedValue = window.app.settings.get(item.id)
@ -110,17 +125,15 @@ const SettingItem = (props) => {
} }
if (typeof item.dependsOn === "object") { if (typeof item.dependsOn === "object") {
const dependsOptionsKeys = Object.keys(item.dependsOn) // create a event handler to watch changes
Object.keys(item.dependsOn).forEach((key) => {
window.app.eventBus.on(`setting.update.${key}`, () => {
setDisabled(checkDependsValidation())
})
})
item.props.disabled = !Boolean(dependsOptionsKeys.every((key) => { // by default check depends validation
const storagedValue = window.app.settings.get(key) item.props.disabled = checkDependsValidation()
if (typeof item.dependsOn[key] === "function") {
return item.dependsOn[key](storagedValue)
}
return storagedValue === item.dependsOn[key]
}))
} }
if (typeof item.listenUpdateValue === "string") { if (typeof item.listenUpdateValue === "string") {
@ -141,7 +154,6 @@ const SettingItem = (props) => {
settingInitialization() settingInitialization()
}, []) }, [])
if (typeof SettingComponent === "string") { if (typeof SettingComponent === "string") {
if (typeof ItemTypes[SettingComponent] === "undefined") { if (typeof ItemTypes[SettingComponent] === "undefined") {
console.error(`Item [${item.id}] has an invalid component: ${item.component}`) console.error(`Item [${item.id}] has an invalid component: ${item.component}`)
@ -194,8 +206,10 @@ const SettingItem = (props) => {
if (!item.props.children) { if (!item.props.children) {
item.props.children = item.title ?? item.id item.props.children = item.title ?? item.id
} }
item.props.value = item.defaultValue item.props.value = item.defaultValue
item.props.onClick = (event) => onUpdateItem(event) item.props.onClick = (event) => onUpdateItem(event)
break break
} }
} }
@ -204,6 +218,8 @@ const SettingItem = (props) => {
SettingComponent = ItemTypes[SettingComponent] SettingComponent = ItemTypes[SettingComponent]
} }
item.props["disabled"] = disabled
return <div key={item.id} className="settingItem"> return <div key={item.id} className="settingItem">
<div className="header"> <div className="header">
<div className="title"> <div className="title">