From 6572e15f53464860e4fd7f0ea60f5e93101ba81e Mon Sep 17 00:00:00 2001 From: Amir Date: Thu, 29 Feb 2024 12:16:34 +0200 Subject: [PATCH] Remove aws sourcedata duplication --- .../CreateImageWizardV2/CreateImageWizard.tsx | 4 ++-- .../steps/Review/ReviewStepTextLists.tsx | 14 +++++++++-- .../TargetEnvironment/Aws/AwsAccountId.tsx | 10 ++++---- .../Aws/AwsSourcesSelect.tsx | 23 +++++++------------ .../steps/TargetEnvironment/Aws/index.tsx | 6 ++--- .../utilities/requestMapper.tsx | 6 +++-- src/store/wizardSlice.ts | 16 +++++-------- 7 files changed, 40 insertions(+), 39 deletions(-) diff --git a/src/Components/CreateImageWizardV2/CreateImageWizard.tsx b/src/Components/CreateImageWizardV2/CreateImageWizard.tsx index e2d97f25..6bdb5a22 100644 --- a/src/Components/CreateImageWizardV2/CreateImageWizard.tsx +++ b/src/Components/CreateImageWizardV2/CreateImageWizard.tsx @@ -37,7 +37,7 @@ import { selectActivationKey, selectAwsAccountId, selectAwsShareMethod, - selectAwsSource, + selectAwsSourceId, selectAzureResourceGroup, selectAzureShareMethod, selectAzureSource, @@ -110,7 +110,7 @@ const CreateImageWizard = ({ startStepIndex = 1 }: CreateImageWizardProps) => { // AWS const awsShareMethod = useAppSelector((state) => selectAwsShareMethod(state)); const awsAccountId = useAppSelector((state) => selectAwsAccountId(state)); - const awsSourceId = useAppSelector((state) => selectAwsSource(state)); + const awsSourceId = useAppSelector((state) => selectAwsSourceId(state)); // GCP const gcpShareMethod = useAppSelector((state) => selectGcpShareMethod(state)); const gcpEmail = useAppSelector((state) => selectGcpEmail(state)); diff --git a/src/Components/CreateImageWizardV2/steps/Review/ReviewStepTextLists.tsx b/src/Components/CreateImageWizardV2/steps/Review/ReviewStepTextLists.tsx index e80836bc..b7b1ffe1 100644 --- a/src/Components/CreateImageWizardV2/steps/Review/ReviewStepTextLists.tsx +++ b/src/Components/CreateImageWizardV2/steps/Review/ReviewStepTextLists.tsx @@ -35,12 +35,12 @@ import { selectArchitecture, selectAwsAccountId, selectAwsShareMethod, - selectAwsSource, selectAzureShareMethod, selectAzureSource, selectAzureResourceGroup, selectAzureSubscriptionId, selectAzureTenantId, + selectAwsSourceId, selectBlueprintDescription, selectBlueprintName, selectCustomRepositories, @@ -116,7 +116,17 @@ export const TargetEnvAWSList = () => { }); const awsAccountId = useAppSelector((state) => selectAwsAccountId(state)); const awsShareMethod = useAppSelector((state) => selectAwsShareMethod(state)); - const source = useAppSelector((state) => selectAwsSource(state)); + const sourceId = useAppSelector((state) => selectAwsSourceId(state)); + const { source } = useGetSourceListQuery( + { + provider: 'aws', + }, + { + selectFromResult: ({ data }) => ({ + source: data?.data?.find((source) => source.id === sourceId), + }), + } + ); return ( diff --git a/src/Components/CreateImageWizardV2/steps/TargetEnvironment/Aws/AwsAccountId.tsx b/src/Components/CreateImageWizardV2/steps/TargetEnvironment/Aws/AwsAccountId.tsx index 7d9fb665..032c9de0 100644 --- a/src/Components/CreateImageWizardV2/steps/TargetEnvironment/Aws/AwsAccountId.tsx +++ b/src/Components/CreateImageWizardV2/steps/TargetEnvironment/Aws/AwsAccountId.tsx @@ -12,18 +12,18 @@ import { useAppDispatch, useAppSelector } from '../../../../../store/hooks'; import { useGetSourceUploadInfoQuery } from '../../../../../store/provisioningApi'; import { changeAwsAccountId, - selectAwsSource, + selectAwsSourceId, } from '../../../../../store/wizardSlice'; export const AwsAccountId = () => { const dispatch = useAppDispatch(); - const source = useAppSelector((state) => selectAwsSource(state)); + const sourceId = useAppSelector((state) => selectAwsSourceId(state)); const { data, isError } = useGetSourceUploadInfoQuery( { - id: parseInt(source?.id as string), + id: parseInt(sourceId as string), }, - { skip: source === undefined } + { skip: sourceId === undefined || sourceId === '' } ); useEffect(() => { @@ -37,7 +37,7 @@ export const AwsAccountId = () => { readOnlyVariant="default" isRequired id="aws-account-id" - value={source && data ? data.aws?.account_id : ''} + value={sourceId && data ? data.aws?.account_id : ''} aria-label="aws account id" /> diff --git a/src/Components/CreateImageWizardV2/steps/TargetEnvironment/Aws/AwsSourcesSelect.tsx b/src/Components/CreateImageWizardV2/steps/TargetEnvironment/Aws/AwsSourcesSelect.tsx index e4d7afe1..8f597c5b 100644 --- a/src/Components/CreateImageWizardV2/steps/TargetEnvironment/Aws/AwsSourcesSelect.tsx +++ b/src/Components/CreateImageWizardV2/steps/TargetEnvironment/Aws/AwsSourcesSelect.tsx @@ -1,4 +1,4 @@ -import React, { useState, useEffect } from 'react'; +import React, { useState } from 'react'; import { Alert, Spinner } from '@patternfly/react-core'; import { FormGroup } from '@patternfly/react-core'; @@ -11,14 +11,14 @@ import { import { useAppDispatch, useAppSelector } from '../../../../../store/hooks'; import { useGetSourceListQuery } from '../../../../../store/provisioningApi'; import { - changeAwsSource, - selectAwsSource, + changeAwsSourceId, + selectAwsSourceId, } from '../../../../../store/wizardSlice'; export const AwsSourcesSelect = () => { const dispatch = useAppDispatch(); const [isOpen, setIsOpen] = useState(false); - const source = useAppSelector((state) => selectAwsSource(state)); + const sourceId = useAppSelector((state) => selectAwsSourceId(state)); const { data, isFetching, isLoading, isSuccess, isError, refetch } = useGetSourceListQuery({ @@ -26,26 +26,19 @@ export const AwsSourcesSelect = () => { }); const sources = data?.data; - - useEffect(() => { - // when the source is already initialized by id (i.e editing / import) - if (sources && sources.length > 0) { - if (source?.id && !!source?.name) - dispatch(changeAwsSource(sources.find((s) => s.id === source.id))); - } - }, [sources, source, dispatch]); + const chosenSource = sources?.find((source) => source.id === sourceId); const handleSelect = ( _event: React.MouseEvent, value: string ) => { const source = sources?.find((source) => source.name === value); - dispatch(changeAwsSource(source)); + dispatch(changeAwsSourceId(source?.id)); setIsOpen(false); }; const handleClear = () => { - dispatch(changeAwsSource(undefined)); + dispatch(changeAwsSourceId(undefined)); }; const handleToggle = () => { @@ -80,7 +73,7 @@ export const AwsSourcesSelect = () => { onToggle={handleToggle} onSelect={handleSelect} onClear={handleClear} - selections={source?.name} + selections={chosenSource?.name} isOpen={isOpen} placeholderText="Select source" typeAheadAriaLabel="Select source" diff --git a/src/Components/CreateImageWizardV2/steps/TargetEnvironment/Aws/index.tsx b/src/Components/CreateImageWizardV2/steps/TargetEnvironment/Aws/index.tsx index cea09848..4c56f541 100644 --- a/src/Components/CreateImageWizardV2/steps/TargetEnvironment/Aws/index.tsx +++ b/src/Components/CreateImageWizardV2/steps/TargetEnvironment/Aws/index.tsx @@ -22,7 +22,7 @@ import { useAppDispatch, useAppSelector } from '../../../../../store/hooks'; import { changeAwsAccountId, changeAwsShareMethod, - changeAwsSource, + changeAwsSourceId, selectAwsAccountId, selectAwsShareMethod, } from '../../../../../store/wizardSlice'; @@ -73,7 +73,7 @@ const Aws = () => { description="Use a configured sources to launch environments directly from the console." isChecked={shareMethod === 'sources'} onChange={() => { - dispatch(changeAwsSource(undefined)); + dispatch(changeAwsSourceId(undefined)); dispatch(changeAwsAccountId('')); dispatch(changeAwsShareMethod('sources')); }} @@ -85,7 +85,7 @@ const Aws = () => { name="radio-8" isChecked={shareMethod === 'manual'} onChange={() => { - dispatch(changeAwsSource(undefined)); + dispatch(changeAwsSourceId(undefined)); dispatch(changeAwsAccountId('')); dispatch(changeAwsShareMethod('manual')); }} diff --git a/src/Components/CreateImageWizardV2/utilities/requestMapper.tsx b/src/Components/CreateImageWizardV2/utilities/requestMapper.tsx index 8d1191e4..51462bf6 100644 --- a/src/Components/CreateImageWizardV2/utilities/requestMapper.tsx +++ b/src/Components/CreateImageWizardV2/utilities/requestMapper.tsx @@ -18,7 +18,7 @@ import { selectArchitecture, selectAwsAccountId, selectAwsShareMethod, - selectAwsSource, + selectAwsSourceId, selectAzureResourceGroup, selectAzureShareMethod, selectAzureSource, @@ -131,9 +131,11 @@ export const mapRequestToState = (request: BlueprintResponse): wizardState => { accountId: awsUploadOptions?.share_with_accounts?.[0] || '', shareMethod: awsUploadOptions?.share_with_sources ? 'sources' : 'manual', source: { id: awsUploadOptions?.share_with_sources?.[0] }, + sourceId: awsUploadOptions?.share_with_sources?.[0], }, repositories: { customRepositories: request.customizations.custom_repositories || [], + payloadRepositories: request.customizations.payload_repositories || [], }, registration: { registrationType: request.customizations?.subscription?.rhc @@ -200,7 +202,7 @@ const getImageOptions = ( switch (imageType) { case 'aws': if (selectAwsShareMethod(state) === 'sources') - return { share_with_sources: [selectAwsSource(state)?.id || ''] }; + return { share_with_sources: [selectAwsSourceId(state) || ''] }; else return { share_with_accounts: [selectAwsAccountId(state)] }; case 'azure': if (selectAzureShareMethod(state) === 'sources') diff --git a/src/store/wizardSlice.ts b/src/store/wizardSlice.ts index b29c33e2..f5428e54 100644 --- a/src/store/wizardSlice.ts +++ b/src/store/wizardSlice.ts @@ -40,6 +40,7 @@ export type wizardState = { accountId: string; shareMethod: AwsShareMethod; source: V1ListSourceResponseItem | undefined; + sourceId?: string; }; azure: { shareMethod: AzureShareMethod; @@ -153,10 +154,8 @@ export const selectAwsAccountId = (state: RootState): string => { return state.wizard.aws.accountId; }; -export const selectAwsSource = ( - state: RootState -): V1ListSourceResponseItem | undefined => { - return state.wizard.aws.source; +export const selectAwsSourceId = (state: RootState): string | undefined => { + return state.wizard.aws.sourceId; }; export const selectAwsShareMethod = (state: RootState) => { @@ -282,11 +281,8 @@ export const wizardSlice = createSlice({ changeAwsShareMethod: (state, action: PayloadAction) => { state.aws.shareMethod = action.payload; }, - changeAwsSource: ( - state, - action: PayloadAction - ) => { - state.aws.source = action.payload; + changeAwsSourceId: (state, action: PayloadAction) => { + state.aws.sourceId = action.payload; }, changeAzureTenantId: (state, action: PayloadAction) => { state.azure.tenantId = action.payload; @@ -398,7 +394,7 @@ export const { changeImageTypes, changeAwsAccountId, changeAwsShareMethod, - changeAwsSource, + changeAwsSourceId, changeAzureTenantId, changeAzureShareMethod, changeAzureSubscriptionId,