Gracefully handle missing summary fields

Not all templates have `modified_by`, `created_by` fields or
other summary_fields. Avoid page load error by only referencing these
fields if they exist.
This commit is contained in:
Jake McDermott
2019-10-03 21:43:19 -04:00
parent 9d2c877143
commit 85909c4264
2 changed files with 55 additions and 16 deletions

View File

@@ -70,12 +70,10 @@ class JobTemplateDetail extends Component {
job_slice_count, job_slice_count,
job_tags, job_tags,
job_type, job_type,
inventory,
name, name,
limit, limit,
modified, modified,
playbook, playbook,
project,
skip_tags, skip_tags,
timeout, timeout,
summary_fields, summary_fields,
@@ -107,6 +105,32 @@ class JobTemplateDetail extends Component {
const renderOptionsField = const renderOptionsField =
become_enabled || host_config_key || allow_simultaneous || use_fact_cache; become_enabled || host_config_key || allow_simultaneous || use_fact_cache;
let createdBy = '';
if (created) {
if (summary_fields.created_by && summary_fields.created_by.username) {
createdBy = i18n._(
t`${formatDateString(created)} by ${
summary_fields.created_by.username
}`
);
} else {
createdBy = formatDateString(created);
}
}
let modifiedBy = '';
if (modified) {
if (summary_fields.modified_by && summary_fields.modified_by.username) {
modifiedBy = i18n._(
t`${formatDateString(modified)} by ${
summary_fields.modified_by.username
}`
);
} else {
modifiedBy = formatDateString(modified);
}
}
const renderOptions = ( const renderOptions = (
<TextList component={TextListVariants.ul}> <TextList component={TextListVariants.ul}>
{become_enabled && ( {become_enabled && (
@@ -147,13 +171,13 @@ class JobTemplateDetail extends Component {
<Detail label={i18n._(t`Name`)} value={name} /> <Detail label={i18n._(t`Name`)} value={name} />
<Detail label={i18n._(t`Description`)} value={description} /> <Detail label={i18n._(t`Description`)} value={description} />
<Detail label={i18n._(t`Job Type`)} value={job_type} /> <Detail label={i18n._(t`Job Type`)} value={job_type} />
{inventory && ( {summary_fields.inventory && (
<Detail <Detail
label={i18n._(t`Inventory`)} label={i18n._(t`Inventory`)}
value={summary_fields.inventory.name} value={summary_fields.inventory.name}
/> />
)} )}
{project && ( {summary_fields.project && (
<Detail <Detail
label={i18n._(t`Project`)} label={i18n._(t`Project`)}
value={summary_fields.project.name} value={summary_fields.project.name}
@@ -167,18 +191,18 @@ class JobTemplateDetail extends Component {
value={verbosityDetails[0].details} value={verbosityDetails[0].details}
/> />
<Detail label={i18n._(t`Timeout`)} value={timeout || '0'} /> <Detail label={i18n._(t`Timeout`)} value={timeout || '0'} />
<Detail {createdBy && (
label={i18n._(t`Created`)} <Detail
value={`${formatDateString(created)} by ${ label={i18n._(t`Created`)}
summary_fields.created_by.username value={createdBy} // TODO: link to user in users
}`} // TODO: link to user in users />
/> )}
<Detail {modifiedBy && (
label={i18n._(t`Last Modified`)} <Detail
value={`${formatDateString(modified)} by ${ label={i18n._(t`Last Modified`)}
summary_fields.modified_by.username value={modifiedBy} // TODO: link to user in users
}`} // TODO: link to user in users />
/> )}
<Detail <Detail
label={i18n._(t`Show Changes`)} label={i18n._(t`Show Changes`)}
value={diff_mode ? 'On' : 'Off'} value={diff_mode ? 'On' : 'Off'}

View File

@@ -56,6 +56,21 @@ describe('<JobTemplateDetail />', () => {
); );
expect(wrapper).toMatchSnapshot(); expect(wrapper).toMatchSnapshot();
}); });
test('Can load with missing summary fields', async done => {
const mockTemplate = { ...template };
mockTemplate.summary_fields = { user_capabilities: {} };
const wrapper = mountWithContexts(
<JobTemplateDetail template={mockTemplate} />
);
await waitForElement(
wrapper,
'Detail[label="Description"]',
el => el.length === 1
);
done();
});
test('When component mounts API is called to get instance groups', async done => { test('When component mounts API is called to get instance groups', async done => {
const wrapper = mountWithContexts( const wrapper = mountWithContexts(
<JobTemplateDetail template={template} /> <JobTemplateDetail template={template} />