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