Skip to content

Commit b540c5b

Browse files
[wip] cloudConfigPage: toggle aws config
This is still a wip since the form fields aren't yet disabled when the config toggle is set to off.
1 parent 4f67dc7 commit b540c5b

File tree

2 files changed

+63
-22
lines changed

2 files changed

+63
-22
lines changed

src/Components/CloudProviderConfig/AWSConfig.tsx

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import {
77
MenuToggleElement,
88
Select,
99
SelectOption,
10+
Switch,
1011
} from '@patternfly/react-core';
1112

1213
import { useIsAwsBucketValid, useIsAwsCredsPathValid } from './validators';
1314

1415
import {
1516
changeAWSBucketName,
17+
changeAWSConfig,
1618
changeAWSCredsPath,
1719
changeAWSRegion,
1820
selectAWSBucketName,
@@ -40,35 +42,58 @@ const AWS_REGIONS = [
4042
'us-gov-west-1',
4143
];
4244

43-
type FormGroupProps = {
44-
value: string | undefined;
45-
setValue: (value: string) => void;
45+
type FormGroupProps<T> = {
46+
value: T | undefined;
47+
onChange: (value: T) => void;
48+
isDisabled?: boolean;
4649
};
4750

48-
const AWSBucket = ({ value, setValue }: FormGroupProps) => {
51+
const AWSConfigToggle = ({ value, onChange }: FormGroupProps<boolean>) => {
52+
const handleChange = (
53+
_event: React.FormEvent<HTMLInputElement>,
54+
checked: boolean
55+
) => {
56+
onChange(checked);
57+
};
58+
59+
return (
60+
<FormGroup label="Configure AWS Uploads">
61+
<Switch
62+
id="aws-config-switch"
63+
ouiaId="aws-config-switch"
64+
// empty label so there is no icon
65+
label=""
66+
isChecked={value}
67+
onChange={handleChange}
68+
/>
69+
</FormGroup>
70+
);
71+
};
72+
73+
const AWSBucket = ({ value, onChange, isDisabled }: FormGroupProps<string>) => {
4974
const isValid = useIsAwsBucketValid();
5075

5176
return (
52-
<FormGroup label="AWS Bucket">
77+
<FormGroup label="AWS Bucket" disabled={isDisabled}>
5378
<ValidatedInput
5479
ariaLabel="aws-bucket"
5580
value={value || ''}
5681
validator={() => isValid}
57-
onChange={(_event, value) => setValue(value)}
82+
onChange={(_event, value) => onChange(value)}
5883
helperText="Invalid AWS bucket name"
5984
/>
6085
</FormGroup>
6186
);
6287
};
6388

64-
const AWSRegion = ({ value, setValue }: FormGroupProps) => {
89+
const AWSRegion = ({ value, onChange, isDisabled }: FormGroupProps<string>) => {
6590
const [isOpen, setIsOpen] = useState(false);
6691

6792
const onSelect = (
6893
_event: React.MouseEvent<Element, MouseEvent> | undefined,
6994
value: string | number | undefined
7095
) => {
71-
setValue(value as string);
96+
onChange(value as string);
7297
setIsOpen(false);
7398
};
7499

@@ -88,7 +113,7 @@ const AWSRegion = ({ value, setValue }: FormGroupProps) => {
88113
);
89114

90115
return (
91-
<FormGroup label="AWS Region">
116+
<FormGroup label="AWS Region" disabled={isDisabled}>
92117
<Select
93118
isOpen={isOpen}
94119
selected={value}
@@ -107,45 +132,61 @@ const AWSRegion = ({ value, setValue }: FormGroupProps) => {
107132
);
108133
};
109134

110-
const AWSCredsPath = ({ value, setValue }: FormGroupProps) => {
135+
const AWSCredsPath = ({
136+
value,
137+
onChange,
138+
isDisabled,
139+
}: FormGroupProps<string>) => {
111140
const isValid = useIsAwsCredsPathValid();
112141

113142
return (
114-
<FormGroup label="AWS Credentials Filepath">
143+
<FormGroup label="AWS Credentials Filepath" disabled={isDisabled}>
115144
<ValidatedInput
116145
ariaLabel="aws-creds-path"
117146
value={value || ''}
118147
validator={() => isValid}
119-
onChange={(_event, value) => setValue(value)}
148+
onChange={(_event, value) => onChange(value)}
120149
helperText="Invalid filepath for AWS credentials"
121150
/>
122151
</FormGroup>
123152
);
124153
};
125154

126-
export const AWSConfig = () => {
155+
// TODO: fix this type
156+
export const AWSConfig = ({ refetch }: { refetch: any }) => {
127157
const dispatch = useAppDispatch();
128158
const bucket = useAppSelector(selectAWSBucketName);
129159
const region = useAppSelector(selectAWSRegion);
130160
const credentials = useAppSelector(selectAWSCredsPath);
131-
132-
// TODO: maybe add a radio button to toggle AWS configuration
133-
// on or off - this might simplify validation & the overall
134-
// experience
161+
const [enabled, setEnabled] = useState<boolean>(true);
162+
163+
const onToggle = async (v: boolean) => {
164+
let awsConfig = undefined;
165+
if (v) {
166+
const { data } = await refetch();
167+
awsConfig = data?.aws;
168+
}
169+
dispatch(changeAWSConfig(awsConfig));
170+
setEnabled(v);
171+
};
135172

136173
return (
137174
<Form>
175+
<AWSConfigToggle value={enabled} onChange={onToggle} />
138176
<AWSBucket
139177
value={bucket}
140-
setValue={(v) => dispatch(changeAWSBucketName(v))}
178+
onChange={(v) => dispatch(changeAWSBucketName(v))}
179+
isDisabled={!enabled}
141180
/>
142181
<AWSRegion
143182
value={region}
144-
setValue={(v) => dispatch(changeAWSRegion(v))}
183+
onChange={(v) => dispatch(changeAWSRegion(v))}
184+
isDisabled={!enabled}
145185
/>
146186
<AWSCredsPath
147187
value={credentials}
148-
setValue={(v) => dispatch(changeAWSCredsPath(v))}
188+
onChange={(v) => dispatch(changeAWSCredsPath(v))}
189+
isDisabled={!enabled}
149190
/>
150191
</Form>
151192
);

src/Components/CloudProviderConfig/CloudProviderConfig.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const CloudProviderConfig = () => {
1717
const dispatch = useAppDispatch();
1818
const handleClose = () => navigate(resolveRelPath(''));
1919

20-
const { data, error } = useGetWorkerConfigQuery({});
20+
const { data, error, refetch } = useGetWorkerConfigQuery({});
2121
const isAwsStepValid = useIsAwsStepValid();
2222

2323
useEffect(() => {
@@ -49,7 +49,7 @@ export const CloudProviderConfig = () => {
4949
onBack: () => navigate(resolveRelPath('')),
5050
}}
5151
>
52-
<AWSConfig />
52+
<AWSConfig refetch={refetch} />
5353
</WizardStep>
5454
</Wizard>
5555
</PageSection>

0 commit comments

Comments
 (0)