Skip to content

Commit

Permalink
fix: Block users from submitting updates when environment variable ve…
Browse files Browse the repository at this point in the history
…rification fails

Signed-off-by: TheYoungManLi <cjl@kubesphere.io>
  • Loading branch information
weili520 committed Jan 12, 2022
1 parent 94a7116 commit 4d6520d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import React from 'react'
import { Form } from '@kube-design/components'
import { EnvironmentInput } from 'components/Inputs'
import RootStore from 'stores/root'
import { PATTERN_ENV_NAME } from 'utils/constants'

import { lazy } from 'utils'

Expand Down Expand Up @@ -47,6 +48,20 @@ export default class Environments extends React.Component {
)
}

validKeys = (rule, value, callback) => {
if (!value) {
callback()
}
if (value.some(item => item.name === '' && item.value === '')) {
callback()
}
const keyInvalid = value.some(item => !PATTERN_ENV_NAME.test(item.name))
const noEmpty = [].concat(value).every(item => item.name !== '')
if (noEmpty && !keyInvalid) {
callback()
}
}

render() {
const {
checkable,
Expand All @@ -62,7 +77,7 @@ export default class Environments extends React.Component {
desc={t('CONTAINER_ENVIRONMENT_DESC')}
checkable={checkable}
>
<Form.Item>
<Form.Item rules={[{ validator: this.validKeys }]}>
<EnvironmentInput
rootStore={this.rootStore}
name={`${this.prefix}env`}
Expand Down
6 changes: 4 additions & 2 deletions src/components/Inputs/EnvironmentInput/Item.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -235,14 +235,16 @@ export default class EnvironmentInputItem extends React.Component {
}
}, 300)

handleValueChange = ({ name, value }) => {
handleValueChange = debounce(({ name, value }) => {
if (name === '' && value === '') {
this.props.handleKeyError()
this.setState({
keyError: false,
})
} else {
this.validEnvKey(name, value)
}
}
}, 300)

render() {
const { value = {}, onChange } = this.props
Expand Down

0 comments on commit 4d6520d

Please sign in to comment.