Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ROX-12152: added field for aws account number in central instance creation modal #31

Merged
merged 1 commit into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/components/SelectSingle.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function SelectSingle({
isCreatable = false,
variant = null,
placeholderText = '',
menuAppendTo = '',
}) {
const [isOpen, setIsOpen] = useState(false);

Expand All @@ -39,6 +40,7 @@ function SelectSingle({
isCreatable={isCreatable}
placeholderText={placeholderText}
toggleId={id}
menuAppendTo={menuAppendTo}
>
{children}
</Select>
Expand Down
14 changes: 14 additions & 0 deletions src/hooks/apis/useCloudAccounts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useQuery } from 'react-query';

import apiRequest from '../../services/apiRequest';

export const queryKey = 'cloud_accounts';

const getCloudAccounts = async () => {
const { data } = await apiRequest.get(`/api/rhacs/v1/cloud_accounts`);
return data;
Comment on lines +8 to +9
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we're not leveraging the ability to see any error, or loading state.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea that's true. I can resolve that separately

};

export default function useCloudAccounts() {
return useQuery([queryKey], () => getCloudAccounts());
}
67 changes: 51 additions & 16 deletions src/routes/InstancesPage/CreateInstanceModal.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable react/prop-types */
import React, { useState } from 'react';
import {
Alert,
Button,
Expand All @@ -12,7 +13,6 @@ import {
ToggleGroup,
ToggleGroupItem,
} from '@patternfly/react-core';
import React, { useState } from 'react';

import { regionOptions } from '../../utils/region';
import SelectSingle from '../../components/SelectSingle';
Expand All @@ -22,9 +22,15 @@ const defaultFormValues = {
cloud_provider: 'aws',
region: 'us-east-1',
availabilityZones: 'multi',
aws_account_number: '',
};

function CreateInstanceModal({ isOpen, onClose, onRequestCreate }) {
function CreateInstanceModal({
isOpen,
onClose,
onRequestCreate,
cloudAccountIds,
}) {
const [errorMessage, setErrorMessage] = useState(null);
const [formValues, setFormValues] = useState(defaultFormValues);
const [isRequestingCreate, setIsRequestingCreate] = useState(false);
Expand All @@ -37,6 +43,19 @@ function CreateInstanceModal({ isOpen, onClose, onRequestCreate }) {
onClose();
}

async function onRequestCreateHandler() {
setIsRequestingCreate(true);
const result = await onRequestCreate(formValues);
setIsRequestingCreate(false);
if (result instanceof Error) {
const errorMessage = result.response.data.reason;
setErrorMessage(errorMessage);
} else {
setFormValues(defaultFormValues);
onClose();
}
}

function onChangeAvailabilityZones(isSelected, event) {
const { id } = event.currentTarget;
setFormValues((prevFormValues) => ({
Expand All @@ -52,24 +71,18 @@ function CreateInstanceModal({ isOpen, onClose, onRequestCreate }) {
}));
}

function onInputChange(value) {
function onChangeAWSAccountNumber(id, selection) {
setFormValues((prevFormValues) => ({
...prevFormValues,
name: value,
aws_account_number: selection,
}));
}

async function onRequestCreateHandler() {
setIsRequestingCreate(true);
const result = await onRequestCreate(formValues);
setIsRequestingCreate(false);
if (result instanceof Error) {
const errorMessage = result.response.data.reason;
setErrorMessage(errorMessage);
} else {
setFormValues(defaultFormValues);
onClose();
}
function onNameChange(value) {
setFormValues((prevFormValues) => ({
...prevFormValues,
name: value,
}));
}

return (
Expand Down Expand Up @@ -116,7 +129,7 @@ function CreateInstanceModal({ isOpen, onClose, onRequestCreate }) {
id="name"
name="name"
value={formValues.name}
onChange={onInputChange}
onChange={onNameChange}
/>
</FormGroup>
<FormGroup label="Cloud provider" isRequired fieldId="cloud_provider">
Expand Down Expand Up @@ -161,6 +174,28 @@ function CreateInstanceModal({ isOpen, onClose, onRequestCreate }) {
/>
</ToggleGroup>
</FormGroup>
<FormGroup label="AWS account number" fieldId="aws_account_number">
<SelectSingle
id="aws_account_number"
value={formValues.aws_account_number}
handleSelect={onChangeAWSAccountNumber}
placeholderText={
cloudAccountIds.length === 0
? 'No accounts available'
: 'Select an AWS Account'
}
menuAppendTo="parent"
isDisabled={cloudAccountIds.length === 0}
>
{cloudAccountIds.map((cloudAccountId) => {
return (
<SelectOption key={cloudAccountId} value={cloudAccountId}>
{cloudAccountId}
</SelectOption>
);
})}
</SelectSingle>
</FormGroup>
</Form>
</Modal>
);
Expand Down
9 changes: 9 additions & 0 deletions src/routes/InstancesPage/InstancesPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import usePagination from '../../hooks/usePagination';
import useInstances from '../../hooks/apis/useInstances';
import useCreateInstance from '../../hooks/apis/useCreateInstance';
import useDeleteInstance from '../../hooks/apis/useDeleteInstance';
import useCloudAccounts from '../../hooks/apis/useCloudAccounts';

import CreateInstanceModal from './CreateInstanceModal';
import DeleteInstanceModal from './DeleteInstanceModal';
Expand Down Expand Up @@ -78,6 +79,12 @@ function InstancesPage() {
});
const [filters, setFilters] = useState({});

const { data: cloudAccountsData } = useCloudAccounts();
const cloudAccountIds =
cloudAccountsData?.cloudAccounts?.map(
(cloudAccount) => cloudAccount.cloudAccountId
) || [];

const { data, isFetching } = useInstances({
query: {
page,
Expand Down Expand Up @@ -109,6 +116,7 @@ function InstancesPage() {
cloud_provider: values.cloud_provider,
name: values.name,
multi_az: values.availabilityZones === 'multi',
aws_account_number: values.aws_account_number,
});
return response.catch((error) => {
return error;
Expand Down Expand Up @@ -332,6 +340,7 @@ function InstancesPage() {
isOpen={!!creatingInstance}
onClose={closeCreateInstanceModal}
onRequestCreate={onRequestCreate}
cloudAccountIds={cloudAccountIds}
/>
<DeleteInstanceModal
instance={deletingInstance}
Expand Down