From 85909c4264283904dfba6e14b5a9e333c3fd4712 Mon Sep 17 00:00:00 2001 From: Jake McDermott Date: Thu, 3 Oct 2019 21:43:19 -0400 Subject: [PATCH] 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. --- .../JobTemplateDetail/JobTemplateDetail.jsx | 56 +++++++++++++------ .../JobTemplateDetail.test.jsx | 15 +++++ 2 files changed, 55 insertions(+), 16 deletions(-) diff --git a/awx/ui_next/src/screens/Template/JobTemplateDetail/JobTemplateDetail.jsx b/awx/ui_next/src/screens/Template/JobTemplateDetail/JobTemplateDetail.jsx index 9fadf59be6..2c18c0296e 100644 --- a/awx/ui_next/src/screens/Template/JobTemplateDetail/JobTemplateDetail.jsx +++ b/awx/ui_next/src/screens/Template/JobTemplateDetail/JobTemplateDetail.jsx @@ -70,12 +70,10 @@ class JobTemplateDetail extends Component { job_slice_count, job_tags, job_type, - inventory, name, limit, modified, playbook, - project, skip_tags, timeout, summary_fields, @@ -107,6 +105,32 @@ class JobTemplateDetail extends Component { const renderOptionsField = 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 = ( {become_enabled && ( @@ -147,13 +171,13 @@ class JobTemplateDetail extends Component { - {inventory && ( + {summary_fields.inventory && ( )} - {project && ( + {summary_fields.project && ( - - + {createdBy && ( + + )} + {modifiedBy && ( + + )} ', () => { ); expect(wrapper).toMatchSnapshot(); }); + + test('Can load with missing summary fields', async done => { + const mockTemplate = { ...template }; + mockTemplate.summary_fields = { user_capabilities: {} }; + + const wrapper = mountWithContexts( + + ); + await waitForElement( + wrapper, + 'Detail[label="Description"]', + el => el.length === 1 + ); + done(); + }); test('When component mounts API is called to get instance groups', async done => { const wrapper = mountWithContexts(