mirror of
https://github.com/ansible/awx.git
synced 2026-03-02 09:18:48 -03:30
Use new lifecycle method to update component state.
- Use component state to handle rendering of Ansible Select dropdown based on the number of custom virtualenvs the API returns.
This commit is contained in:
11
src/App.jsx
11
src/App.jsx
@@ -76,6 +76,8 @@ class App extends React.Component {
|
|||||||
const isNavOpen = typeof window !== 'undefined' && window.innerWidth >= parseInt(breakpointMd.value, 10);
|
const isNavOpen = typeof window !== 'undefined' && window.innerWidth >= parseInt(breakpointMd.value, 10);
|
||||||
this.state = {
|
this.state = {
|
||||||
isNavOpen,
|
isNavOpen,
|
||||||
|
config: {},
|
||||||
|
error: false,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,9 +96,14 @@ class App extends React.Component {
|
|||||||
|
|
||||||
async componentDidMount() {
|
async componentDidMount() {
|
||||||
// Grab our config data from the API and store in state
|
// Grab our config data from the API and store in state
|
||||||
const { data } = await api.get(API_CONFIG);
|
try {
|
||||||
this.setState({ config: data });
|
const { data } = await api.get(API_CONFIG);
|
||||||
|
this.setState({ config: data });
|
||||||
|
} catch (error) {
|
||||||
|
this.setState({ error });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { isNavOpen, config } = this.state;
|
const { isNavOpen, config } = this.state;
|
||||||
const { logo, loginInfo, history } = this.props;
|
const { logo, loginInfo, history } = this.props;
|
||||||
|
|||||||
@@ -12,12 +12,25 @@ class AnsibleSelect extends React.Component {
|
|||||||
this.onSelectChange = this.onSelectChange.bind(this);
|
this.onSelectChange = this.onSelectChange.bind(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
state = {
|
||||||
|
count: 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
static getDerivedStateFromProps(nexProps, _) {
|
||||||
|
if (nexProps.data) {
|
||||||
|
return {
|
||||||
|
count: nexProps.data.length,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
onSelectChange(val, _) {
|
onSelectChange(val, _) {
|
||||||
this.props.selectChange(val);
|
this.props.selectChange(val);
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
if (this.props.data.length > 1) {
|
const { count } = this.state;
|
||||||
|
if (count > 1) {
|
||||||
return (
|
return (
|
||||||
<FormGroup label={this.props.labelName} fieldId="ansible-select">
|
<FormGroup label={this.props.labelName} fieldId="ansible-select">
|
||||||
<Select value={this.props.selected} onChange={this.onSelectChange} aria-label="Select Input">
|
<Select value={this.props.selected} onChange={this.onSelectChange} aria-label="Select Input">
|
||||||
|
|||||||
Reference in New Issue
Block a user