1
0
mirror of https://github.com/ansible/awx.git synced 2024-10-31 15:21:13 +03:00

Properly show template add button and options based on OPTIONS response on both job_templates and workflow_job_templates endpoints.

This commit is contained in:
mabashian 2019-12-02 14:51:22 -05:00
parent 9bdd49bec5
commit bd7248d21c

View File

@ -110,19 +110,34 @@ class TemplatesList extends Component {
async loadTemplates() {
const { location } = this.props;
const { actions: cachedActions } = this.state;
const {
jtActions: cachedJTActions,
wfjtActions: cachedWFJTActions,
} = this.state;
const params = parseQueryString(QS_CONFIG, location.search);
let optionsPromise;
if (cachedActions) {
optionsPromise = Promise.resolve({ data: { actions: cachedActions } });
let jtOptionsPromise;
if (cachedJTActions) {
jtOptionsPromise = Promise.resolve({
data: { actions: cachedJTActions },
});
} else {
optionsPromise = UnifiedJobTemplatesAPI.readOptions();
jtOptionsPromise = JobTemplatesAPI.readOptions();
}
let wfjtOptionsPromise;
if (cachedWFJTActions) {
wfjtOptionsPromise = Promise.resolve({
data: { actions: cachedWFJTActions },
});
} else {
wfjtOptionsPromise = WorkflowJobTemplatesAPI.readOptions();
}
const promises = Promise.all([
UnifiedJobTemplatesAPI.read(params),
optionsPromise,
jtOptionsPromise,
wfjtOptionsPromise,
]);
this.setState({ contentError: null, hasContentLoading: true });
@ -133,12 +148,16 @@ class TemplatesList extends Component {
data: { count, results },
},
{
data: { actions },
data: { actions: jtActions },
},
{
data: { actions: wfjtActions },
},
] = await promises;
this.setState({
actions,
jtActions,
wfjtActions,
itemCount: count,
templates: results,
selected: [],
@ -158,27 +177,31 @@ class TemplatesList extends Component {
templates,
itemCount,
selected,
actions,
jtActions,
wfjtActions,
} = this.state;
const { match, i18n } = this.props;
const canAdd =
actions && Object.prototype.hasOwnProperty.call(actions, 'POST');
const canAddJT =
jtActions && Object.prototype.hasOwnProperty.call(jtActions, 'POST');
const canAddWFJT =
wfjtActions && Object.prototype.hasOwnProperty.call(wfjtActions, 'POST');
const addButtonOptions = [];
if (canAddJT) {
addButtonOptions.push({
label: i18n._(t`Template`),
url: `${match.url}/job_template/add/`,
});
}
if (canAddWFJT) {
addButtonOptions.push({
label: i18n._(t`Workflow Template`),
url: `${match.url}/workflow_job_template/add/`,
});
}
const isAllSelected =
selected.length === templates.length && selected.length > 0;
const addButton = (
<AddDropDownButton
key="add"
dropdownItems={[
{
label: i18n._(t`Template`),
url: `${match.url}/template/add/`,
},
{
label: i18n._(t`Workflow Template`),
url: `${match.url}/_workflow/add/`,
},
]}
/>
<AddDropDownButton key="add" dropdownItems={addButtonOptions} />
);
return (
<PageSection>
@ -225,7 +248,7 @@ class TemplatesList extends Component {
itemsToDelete={selected}
pluralizedItemName="Templates"
/>,
canAdd && addButton,
(canAddJT || canAddWFJT) && addButton,
]}
/>
)}
@ -239,7 +262,7 @@ class TemplatesList extends Component {
isSelected={selected.some(row => row.id === template.id)}
/>
)}
emptyStateControls={canAdd && addButton}
emptyStateControls={(canAddJT || canAddWFJT) && addButton}
/>
</Card>
<AlertModal