|
| 1 | +import { Form, Input, Label, RadioInput } from "@canonical/react-components"; |
| 2 | +import type { FC } from "react"; |
| 3 | +import GroupSelection from "pages/permissions/panels/GroupSelection"; |
| 4 | +import { useAuthGroups } from "context/useAuthGroups"; |
| 5 | +import type { FormikProps } from "formik/dist/types"; |
| 6 | + |
| 7 | +export interface ClusterLinkFormValues { |
| 8 | + name: string; |
| 9 | + description?: string; |
| 10 | + token?: string; |
| 11 | + tokenType?: "generate" | "consume"; |
| 12 | + authGroups: string[]; |
| 13 | + isCreating: boolean; |
| 14 | +} |
| 15 | + |
| 16 | +interface Props { |
| 17 | + formik: FormikProps<ClusterLinkFormValues>; |
| 18 | +} |
| 19 | + |
| 20 | +const ClusterLinkForm: FC<Props> = ({ formik }) => { |
| 21 | + const { data: authGroups = [] } = useAuthGroups(); |
| 22 | + |
| 23 | + return ( |
| 24 | + <Form onSubmit={formik.handleSubmit}> |
| 25 | + {/* hidden submit to enable enter key in inputs */} |
| 26 | + <Input type="submit" hidden value="Hidden input" /> |
| 27 | + {formik.values.isCreating && ( |
| 28 | + <Input |
| 29 | + {...formik.getFieldProps("name")} |
| 30 | + type="text" |
| 31 | + label="Name" |
| 32 | + placeholder="Enter name" |
| 33 | + required |
| 34 | + autoFocus |
| 35 | + error={formik.touched.name ? formik.errors.name : null} |
| 36 | + /> |
| 37 | + )} |
| 38 | + <Input |
| 39 | + {...formik.getFieldProps("description")} |
| 40 | + type="text" |
| 41 | + label="Description" |
| 42 | + placeholder="Enter description" |
| 43 | + /> |
| 44 | + {formik.values.isCreating && ( |
| 45 | + <> |
| 46 | + <div className="u-sv1"> |
| 47 | + <RadioInput |
| 48 | + inline |
| 49 | + labelClassName="margin-right" |
| 50 | + label="Generate token" |
| 51 | + checked={formik.values.tokenType === "generate"} |
| 52 | + onClick={() => { |
| 53 | + formik.setFieldValue("tokenType", "generate"); |
| 54 | + }} |
| 55 | + /> |
| 56 | + <RadioInput |
| 57 | + inline |
| 58 | + label="Consume token" |
| 59 | + checked={formik.values.tokenType === "consume"} |
| 60 | + onClick={() => { |
| 61 | + formik.setFieldValue("tokenType", "consume"); |
| 62 | + }} |
| 63 | + /> |
| 64 | + </div> |
| 65 | + {formik.values.tokenType === "consume" && ( |
| 66 | + <Input |
| 67 | + {...formik.getFieldProps("token")} |
| 68 | + type="text" |
| 69 | + label="Token" |
| 70 | + placeholder="Enter token" |
| 71 | + /> |
| 72 | + )} |
| 73 | + </> |
| 74 | + )} |
| 75 | + <Label className="u-sv-2">Auth groups</Label> |
| 76 | + <p className="u-text--muted u-sv-1"> |
| 77 | + Control access for incoming request on the cluster link. |
| 78 | + </p> |
| 79 | + <GroupSelection |
| 80 | + groups={authGroups} |
| 81 | + modifiedGroups={new Set(formik.values.authGroups)} |
| 82 | + parentItemName="cluster link" |
| 83 | + selectedGroups={new Set(formik.values.authGroups)} |
| 84 | + setSelectedGroups={(val, isUnselectAll) => { |
| 85 | + if (isUnselectAll) { |
| 86 | + formik.setFieldValue("authGroups", []); |
| 87 | + } else { |
| 88 | + formik.setFieldValue("authGroups", val); |
| 89 | + } |
| 90 | + }} |
| 91 | + toggleGroup={(group) => { |
| 92 | + const currentGroups = formik.values.authGroups; |
| 93 | + if (currentGroups.includes(group)) { |
| 94 | + formik.setFieldValue( |
| 95 | + "authGroups", |
| 96 | + currentGroups.filter((g) => g !== group), |
| 97 | + ); |
| 98 | + } else { |
| 99 | + formik.setFieldValue("authGroups", [...currentGroups, group]); |
| 100 | + } |
| 101 | + }} |
| 102 | + scrollDependencies={[formik]} |
| 103 | + /> |
| 104 | + </Form> |
| 105 | + ); |
| 106 | +}; |
| 107 | + |
| 108 | +export default ClusterLinkForm; |
0 commit comments