Debian Codegraph @

install/freeipa-webui/src/pages/IDViews/IDViewsAppliedToTable.tsx

raw
1 import React, { useEffect } from "react";
2 // PatternFly
3 import { Td, Th, Tr } from "@patternfly/react-table";
4 // Tables
5 import TableLayout from "src/components/layouts/TableLayout";
6 // Layouts
7 import EmptyBodyTable from "src/components/tables/EmptyBodyTable";
8 // React Router DOM
9 import { Link } from "react-router";
10 // Hooks
11 import useShifting from "src/hooks/useShifting";
12
13 interface HostsData {
14 selectedHosts: string[];
15 hostsList: string[];
16 setSelectedHosts: (hosts: string[]) => void;
17 clearSelectedHosts: () => void;
18 }
19
20 interface ButtonsData {
21 updateIsUnapplyButtonDisabled: (value: boolean) => void;
22 isUnapply: boolean;
23 updateIsUnapply: (value: boolean) => void;
24 }
25
26 interface PaginationData {
27 selectedPerPage: number;
28 updateSelectedPerPage: (selected: number) => void;
29 }
30
31 interface PropsToTable {
32 hosts: string[];
33 shownHosts: string[];
34 hostsData: HostsData;
35 buttonsData: ButtonsData;
36 paginationData: PaginationData;
37 }
38
39 const IDViewsAppliedToTable = (props: PropsToTable) => {
40 // Retrieve views data from props
41 const shownHostsList = [...props.shownHosts];
42
43 const isHostSelected = (host: string) => {
44 if (
45 props.hostsData.selectedHosts.find(
46 (selectedHost) => selectedHost === host
47 )
48 ) {
49 return true;
50 } else {
51 return false;
52 }
53 };
54
55 // On selecting one single row
56 const onSelectHost = useShifting(
57 shownHostsList,
58 props.hostsData.setSelectedHosts
59 );
60
61 // Reset 'selectedHosts' array if an apply operation has been done
62 useEffect(() => {
63 if (props.buttonsData.isUnapply) {
64 props.hostsData.clearSelectedHosts();
65 props.buttonsData.updateIsUnapply(false);
66 }
67 }, [props.buttonsData.isUnapply]);
68
69 // Enable 'Un-apply' button (if any host selected)
70 useEffect(() => {
71 if (props.hostsData.selectedHosts.length > 0) {
72 props.buttonsData.updateIsUnapplyButtonDisabled(false);
73 }
74
75 if (props.hostsData.selectedHosts.length === 0) {
76 props.buttonsData.updateIsUnapplyButtonDisabled(true);
77 }
78 }, [props.hostsData.selectedHosts]);
79
80 // Defining table header and body from here to avoid passing specific names to the Table Layout
81 const header = (
82 <Tr>
83 <Th modifier="wrap"></Th>
84 <Th modifier="wrap">Host</Th>
85 </Tr>
86 );
87
88 const body = shownHostsList.map((host, rowIndex) => (
89 <Tr key={host} id={host}>
90 <Td
91 dataLabel="checkbox"
92 select={{
93 rowIndex,
94 onSelect: (_event, isSelecting) =>
95 onSelectHost(host, rowIndex, isSelecting),
96 isSelected: isHostSelected(host),
97 }}
98 />
99 <Td dataLabel={host}>
100 <Link to={"/hosts/" + host} state={host}>
101 {host}
102 </Link>
103 </Td>
104 </Tr>
105 ));
106
107 return (
108 <TableLayout
109 ariaLabel={"ID view applies to table"}
110 variant={"compact"}
111 hasBorders={true}
112 tableId={"id-views-applies-to-table"}
113 isStickyHeader={true}
114 tableHeader={header}
115 tableBody={props.shownHosts.length === 0 ? <EmptyBodyTable /> : body}
116 />
117 );
118 };
119
120 export default IDViewsAppliedToTable;