Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Block users submit updates when environment variable verification fails #2907

Merged
merged 1 commit into from
Jan 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const getActions = lazy(() =>
export default class Environments extends React.Component {
rootStore = new RootStore()

envError = ''

static defaultProps = {
prefix: '',
checkable: true,
Expand All @@ -47,6 +49,16 @@ export default class Environments extends React.Component {
)
}

handleErrorStatus = (err = '') => {
this.envError = err
}

envValidator = (rule, value, callback) => {
if (this.envError === '') {
callback()
}
}

render() {
const {
checkable,
Expand All @@ -62,14 +74,15 @@ export default class Environments extends React.Component {
desc={t('CONTAINER_ENVIRONMENT_DESC')}
checkable={checkable}
>
<Form.Item>
<Form.Item rules={[{ validator: this.envValidator }]}>
<EnvironmentInput
rootStore={this.rootStore}
name={`${this.prefix}env`}
namespace={namespace}
isFederated={isFederated}
cluster={cluster}
projectDetail={projectDetail}
handleInputError={this.handleErrorStatus}
/>
</Form.Item>
</Form.Group>
Expand Down
11 changes: 10 additions & 1 deletion src/components/Inputs/ArrayInput/Item.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ import { Button } from '@kube-design/components'

import styles from './index.scss'

const Item = ({ component, index, value, arrayValue, onChange, onDelete }) => {
const Item = ({
component,
index,
value,
arrayValue,
onChange,
onDelete,
handleInputError,
}) => {
const [keyErrorTip, setKeyError] = useState('')

const handleKeyError = info => {
Expand All @@ -37,6 +45,7 @@ const Item = ({ component, index, value, arrayValue, onChange, onDelete }) => {
arrayValue,
onChange,
handleKeyError,
handleInputError,
})

return (
Expand Down
6 changes: 5 additions & 1 deletion src/components/Inputs/ArrayInput/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default class ArrayInput extends React.Component {
name: PropTypes.string,
value: PropTypes.array,
onChange: PropTypes.func,
handleInputError: PropTypes.func,
addText: PropTypes.string,
itemType: PropTypes.oneOf(['string', 'object']),
children: PropTypes.node.isRequired,
Expand All @@ -41,6 +42,7 @@ export default class ArrayInput extends React.Component {
name: '',
value: [''],
onChange() {},
handleInputError() {},
addText: t('ADD'),
itemType: 'string',
}
Expand Down Expand Up @@ -109,7 +111,7 @@ export default class ArrayInput extends React.Component {
}

renderItems() {
const { value, children, id } = this.props
const { value, children, id, handleInputError } = this.props
if (id && id.includes("metadata.annotations['kubesphere.io/")) {
return value.map((item, index) => (
<Form.Item
Expand All @@ -124,6 +126,7 @@ export default class ArrayInput extends React.Component {
component={children}
onChange={this.handleChange.bind(this, index)}
onDelete={this.handleDelete.bind(this, index)}
handleInputError={handleInputError}
/>
</Form.Item>
))
Expand All @@ -138,6 +141,7 @@ export default class ArrayInput extends React.Component {
component={children}
onChange={this.handleChange.bind(this, index)}
onDelete={this.handleDelete.bind(this, index)}
handleInputError={handleInputError}
/>
))
}
Expand Down
24 changes: 14 additions & 10 deletions src/components/Inputs/EnvironmentInput/Item.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -210,39 +210,43 @@ export default class EnvironmentInputItem extends React.Component {
}

validEnvKey = debounce((value, target = {}) => {
const { handleKeyError } = this.props
const { handleKeyError, handleInputError } = this.props
const status = !PATTERN_ENV_NAME.test(value)
if (value === '' && target.value === '') {
handleKeyError()
handleInputError()
this.setState({
keyError: false,
})
} else {
if (status) {
value !== ''
? handleKeyError({
message: t('ENVIRONMENT_INVALID_TIP'),
})
: handleKeyError({
message: t('ENVIRONMENT_CANNOT_BE_EMPTY'),
})
const message =
value !== ''
? t('ENVIRONMENT_INVALID_TIP')
: t('ENVIRONMENT_CANNOT_BE_EMPTY')
handleInputError({ message })
handleKeyError({ message })
} else {
handleKeyError()
handleInputError()
}
this.setState({
keyError: status,
})
}
}, 300)

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

render() {
const { value = {}, onChange } = this.props
Expand Down
2 changes: 2 additions & 0 deletions src/components/Inputs/EnvironmentInput/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ export default class EnvironmentInput extends React.Component {
name: PropTypes.string,
value: PropTypes.array,
onChange: PropTypes.func,
handleInputError: PropTypes.func,
}

static defaultProps = {
name: '',
onChange() {},
handleInputError() {},
}

componentDidMount() {
Expand Down