Debian Codegraph @

install/freeipa-webui/src/pages/Configuration/ConfigObjectclassTable.tsx

raw
1 import React from "react";
2 import {
3 Button,
4 Form,
5 FormGroup,
6 Modal,
7 ModalBody,
8 ModalFooter,
9 ModalHeader,
10 } from "@patternfly/react-core";
11 import { Table, TableText, Tr, Tbody, Td } from "@patternfly/react-table";
12 // Utils
13 import {
14 IPAParamDefinition,
15 getParamProperties,
16 updateIpaObject,
17 } from "../../utils/ipaObjectUtils";
18 import InputWithValidation from "src/components/layouts/InputWithValidation";
19
20 interface PropsToTable extends IPAParamDefinition {
21 title: string;
22 }
23
24 const ConfigObjectclassTable = (props: PropsToTable) => {
25 const { value } = getParamProperties(props);
26 const values = value ? (value as string[]) : [];
27
28 const [isOpen, setIsOpen] = React.useState<boolean>(false);
29 const [newOC, setNewOC] = React.useState<string>("");
30
31 const removeOC = (name: string) => {
32 if (props.ipaObject !== undefined && props.onChange !== undefined) {
33 const newOCList = values.filter((item) => item !== name);
34 updateIpaObject(props.ipaObject, props.onChange, newOCList, props.name);
35 }
36 };
37
38 const addOC = () => {
39 if (props.ipaObject !== undefined && props.onChange !== undefined) {
40 const newOCList = [...values, newOC];
41 updateIpaObject(props.ipaObject, props.onChange, newOCList, props.name);
42 }
43 setIsOpen(false);
44 };
45
46 return (
47 <React.Fragment>
48 <Table aria-label="Objectclass table" variant="compact">
49 <Tbody>
50 {values.map((item) => {
51 const removeButton = (
52 <TableText>
53 <Button
54 data-cy="configuration-table-button-remove"
55 id={props.name + "-" + item}
56 onClick={() => removeOC(item)}
57 variant="secondary"
58 size="sm"
59 isDisabled={values.length < 2}
60 >
61 Remove
62 </Button>
63 </TableText>
64 );
65
66 return (
67 <Tr key={item}>
68 <Td>{item}</Td>
69 <Td>{removeButton}</Td>
70 </Tr>
71 );
72 })}
73 </Tbody>
74 </Table>
75 <Button
76 data-cy="configuration-table-button-add"
77 id={props.name + "-" + "addoc"}
78 onClick={() => {
79 setNewOC("");
80 setIsOpen(true);
81 }}
82 className="pf-v6-u-mt-md"
83 size="sm"
84 >
85 Add objectclass
86 </Button>
87 <Modal
88 data-cy="add-objectclass-modal"
89 variant="small"
90 isOpen={isOpen}
91 onClose={() => setIsOpen(false)}
92 >
93 <ModalHeader title={"Add objectclass"} labelId="add-oc-modal-title" />
94 <ModalBody id="add-oc-modal-body">
95 <Form id={"add-oc-modal"}>
96 <FormGroup
97 key={"oc"}
98 label={"New objectclass"}
99 fieldId={"oc"}
100 isRequired
101 >
102 <InputWithValidation
103 dataCy="modal-textbox-objectclass"
104 id="oc"
105 name="oc"
106 value={newOC}
107 onChange={setNewOC}
108 isRequired
109 rules={[
110 {
111 id: "unique",
112 message: "Must be unique",
113 validate: (value: string) =>
114 values.indexOf(value.toLowerCase()) === -1,
115 },
116 ]}
117 />
118 </FormGroup>
119 </Form>
120 </ModalBody>
121 <ModalFooter>
122 <Button
123 data-cy="modal-button-add"
124 key="add-new-oc"
125 isDisabled={
126 newOC === "" || values.indexOf(newOC.toLowerCase()) !== -1
127 }
128 onClick={addOC}
129 >
130 Add
131 </Button>
132 ,
133 <Button
134 data-cy="modal-button-cancel"
135 key="cancel-new-host"
136 variant="link"
137 onClick={() => setIsOpen(false)}
138 >
139 Cancel
140 </Button>
141 </ModalFooter>
142 </Modal>
143 </React.Fragment>
144 );
145 };
146
147 export default ConfigObjectclassTable;