Remove aws sourcedata duplication

This commit is contained in:
Amir 2024-02-29 12:16:34 +02:00 committed by Lucas Garfield
parent 45d50b2440
commit 6572e15f53
7 changed files with 40 additions and 39 deletions

View file

@ -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));

View file

@ -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 (
<TextContent>

View file

@ -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"
/>
</FormGroup>

View file

@ -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<Element, 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"

View file

@ -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'));
}}

View file

@ -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')

View file

@ -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<AwsShareMethod>) => {
state.aws.shareMethod = action.payload;
},
changeAwsSource: (
state,
action: PayloadAction<V1ListSourceResponseItem | undefined>
) => {
state.aws.source = action.payload;
changeAwsSourceId: (state, action: PayloadAction<string | undefined>) => {
state.aws.sourceId = action.payload;
},
changeAzureTenantId: (state, action: PayloadAction<string>) => {
state.azure.tenantId = action.payload;
@ -398,7 +394,7 @@ export const {
changeImageTypes,
changeAwsAccountId,
changeAwsShareMethod,
changeAwsSource,
changeAwsSourceId,
changeAzureTenantId,
changeAzureShareMethod,
changeAzureSubscriptionId,