From 23f4f7bb002d0fe29b16994f8225e56302826d3c Mon Sep 17 00:00:00 2001 From: mabashian Date: Tue, 20 Sep 2022 10:50:50 -0400 Subject: [PATCH] Remove duplicate Limit detail on schedule Bumps migration number from 0168 to 0169 Make labels and IGs requests synchronously when getting launch data Moves label creation out to a util --- ...=> 0169_jt_prompt_everything_on_launch.py} | 2 +- .../components/LaunchButton/LaunchButton.js | 30 ++++++---- .../components/LaunchPrompt/LaunchPrompt.js | 44 ++------------ .../Schedule/ScheduleDetail/ScheduleDetail.js | 1 - .../Schedule/ScheduleEdit/ScheduleEdit.js | 53 ++++------------- awx/ui/src/util/labels.js | 57 +++++++++++++++++++ 6 files changed, 94 insertions(+), 93 deletions(-) rename awx/main/migrations/{0168_jt_prompt_everything_on_launch.py => 0169_jt_prompt_everything_on_launch.py} (99%) create mode 100644 awx/ui/src/util/labels.js diff --git a/awx/main/migrations/0168_jt_prompt_everything_on_launch.py b/awx/main/migrations/0169_jt_prompt_everything_on_launch.py similarity index 99% rename from awx/main/migrations/0168_jt_prompt_everything_on_launch.py rename to awx/main/migrations/0169_jt_prompt_everything_on_launch.py index 46f254f780..8704b94446 100644 --- a/awx/main/migrations/0168_jt_prompt_everything_on_launch.py +++ b/awx/main/migrations/0169_jt_prompt_everything_on_launch.py @@ -9,7 +9,7 @@ import django.db.models.deletion class Migration(migrations.Migration): dependencies = [ - ('main', '0167_project_signature_validation_credential'), + ('main', '0168_inventoryupdate_scm_revision'), ] operations = [ diff --git a/awx/ui/src/components/LaunchButton/LaunchButton.js b/awx/ui/src/components/LaunchButton/LaunchButton.js index ad133d64b7..061462042b 100644 --- a/awx/ui/src/components/LaunchButton/LaunchButton.js +++ b/awx/ui/src/components/LaunchButton/LaunchButton.js @@ -71,12 +71,26 @@ function LaunchButton({ resource, children }) { setSurveyConfig(data); } - if (launch.ask_labels_on_launch) { - const { - data: { results }, - } = await readLabels; + const relatedPromises = []; - const allLabels = results.map((label) => ({ + if (launch.ask_labels_on_launch) { + relatedPromises.push(readLabels); + } else { + relatedPromises.push(null); + } + + if (launch.ask_instance_groups_on_launch) { + relatedPromises.push(JobTemplatesAPI.readInstanceGroups(resource.id)); + } else { + relatedPromises.push(null); + } + + const [labelsResponse, instanceGroupsResponse] = await Promise.all( + relatedPromises + ); + + if (launch.ask_labels_on_launch) { + const allLabels = labelsResponse?.data?.results.map((label) => ({ ...label, isReadOnly: true, })); @@ -85,11 +99,7 @@ function LaunchButton({ resource, children }) { } if (launch.ask_instance_groups_on_launch) { - const { - data: { results }, - } = await JobTemplatesAPI.readInstanceGroups(resource.id); - - setInstanceGroups(results); + setInstanceGroups(instanceGroupsResponse?.data?.results); } if (canLaunchWithoutPrompt(launch)) { diff --git a/awx/ui/src/components/LaunchPrompt/LaunchPrompt.js b/awx/ui/src/components/LaunchPrompt/LaunchPrompt.js index c177405e59..4bc4c8313c 100644 --- a/awx/ui/src/components/LaunchPrompt/LaunchPrompt.js +++ b/awx/ui/src/components/LaunchPrompt/LaunchPrompt.js @@ -2,10 +2,10 @@ import React, { useState } from 'react'; import { ExpandableSection, Wizard } from '@patternfly/react-core'; import { t } from '@lingui/macro'; import { Formik, useFormikContext } from 'formik'; -import { LabelsAPI, OrganizationsAPI } from 'api'; import { useDismissableError } from 'hooks/useRequest'; import mergeExtraVars from 'util/prompt/mergeExtraVars'; import getSurveyValues from 'util/prompt/getSurveyValues'; +import createNewLabels from 'util/labels'; import ContentLoading from '../ContentLoading'; import ContentError from '../ContentError'; import useLaunchSteps from './useLaunchSteps'; @@ -76,44 +76,10 @@ function PromptModalForm({ } if (launchConfig.ask_labels_on_launch) { - const labelIds = []; - const newLabels = []; - const labelRequests = []; - let organizationId = resource.organization; - values.labels.forEach((label) => { - if (typeof label.id !== 'number') { - newLabels.push(label); - } else { - labelIds.push(label.id); - } - }); - - if (newLabels.length > 0) { - if (!organizationId) { - // eslint-disable-next-line no-useless-catch - try { - const { - data: { results }, - } = await OrganizationsAPI.read(); - organizationId = results[0].id; - } catch (err) { - throw err; - } - } - } - - newLabels.forEach((label) => { - labelRequests.push( - LabelsAPI.create({ - name: label.name, - organization: organizationId, - }).then(({ data }) => { - labelIds.push(data.id); - }) - ); - }); - - await Promise.all(labelRequests); + const { labelIds } = createNewLabels( + values.labels, + resource.organization + ); setValue('labels', labelIds); } diff --git a/awx/ui/src/components/Schedule/ScheduleDetail/ScheduleDetail.js b/awx/ui/src/components/Schedule/ScheduleDetail/ScheduleDetail.js index bbad60baa3..a5650ac238 100644 --- a/awx/ui/src/components/Schedule/ScheduleDetail/ScheduleDetail.js +++ b/awx/ui/src/components/Schedule/ScheduleDetail/ScheduleDetail.js @@ -473,7 +473,6 @@ function ScheduleDetail({ hasDaysToKeepField, schedule, surveyConfig }) { )} {ask_forks_on_launch && } - {ask_limit_on_launch && } {ask_verbosity_on_launch && ( { - if (typeof label.id !== 'number') { - newLabels.push(label); - } else { - labelIds.push(label.id); - } - }); + const { labelIds, error } = createNewLabels( + values.labels, + resource.organization + ); + + if (error) { + setFormSubmitError(error); + } else { + submitValues.labels = labelIds; } - - if (newLabels.length > 0) { - if (!organizationId) { - // eslint-disable-next-line no-useless-catch - try { - const { - data: { results }, - } = await OrganizationsAPI.read(); - organizationId = results[0].id; - } catch (err) { - throw err; - } - } - } - - newLabels.forEach((label) => { - labelRequests.push( - LabelsAPI.create({ - name: label.name, - organization: organizationId, - }).then(({ data }) => { - labelIds.push(data.id); - }) - ); - }); - - await Promise.all(labelRequests); - - submitValues.labels = labelIds; } const ruleSet = buildRuleSet(values); diff --git a/awx/ui/src/util/labels.js b/awx/ui/src/util/labels.js new file mode 100644 index 0000000000..8e973d5836 --- /dev/null +++ b/awx/ui/src/util/labels.js @@ -0,0 +1,57 @@ +import { LabelsAPI, OrganizationsAPI } from '../api'; + +async function createNewLabels(labels = [], organization = null) { + let error = null; + const labelIds = []; + + try { + const newLabels = []; + const labelRequests = []; + let organizationId = organization; + if (labels) { + labels.forEach((label) => { + if (typeof label.id !== 'number') { + newLabels.push(label); + } else { + labelIds.push(label.id); + } + }); + } + + if (newLabels.length > 0) { + if (!organizationId) { + // eslint-disable-next-line no-useless-catch + try { + const { + data: { results }, + } = await OrganizationsAPI.read(); + organizationId = results[0].id; + } catch (err) { + throw err; + } + } + } + + newLabels.forEach((label) => { + labelRequests.push( + LabelsAPI.create({ + name: label.name, + organization: organizationId, + }).then(({ data }) => { + labelIds.push(data.id); + }) + ); + }); + + await Promise.all(labelRequests); + } catch (err) { + error = err; + } + + return { + labelIds, + error, + }; +} + +export default createNewLabels;