| 1 |
import React from "react"; |
| 2 |
// PatternFly |
| 3 |
import { |
| 4 |
Flex, |
| 5 |
FlexItem, |
| 6 |
PageSection, |
| 7 |
PaginationVariant, |
| 8 |
ToolbarItemVariant, |
| 9 |
} from "@patternfly/react-core"; |
| 10 |
import { |
| 11 |
InnerScrollContainer, |
| 12 |
OuterScrollContainer, |
| 13 |
Td, |
| 14 |
Th, |
| 15 |
Tr, |
| 16 |
} from "@patternfly/react-table"; |
| 17 |
// Hooks |
| 18 |
import { addAlert } from "src/store/Global/alerts-slice"; |
| 19 |
import useUpdateRoute from "src/hooks/useUpdateRoute"; |
| 20 |
import useListPageSearchParams from "src/hooks/useListPageSearchParams"; |
| 21 |
import useApiError from "src/hooks/useApiError"; |
| 22 |
// Redux |
| 23 |
import { useAppDispatch, useAppSelector } from "src/store/hooks"; |
| 24 |
// RPC |
| 25 |
import { |
| 26 |
useDnsServersFindQuery, |
| 27 |
useSearchDnsServersEntriesMutation, |
| 28 |
} from "src/services/rpcDnsServers"; |
| 29 |
// React router |
| 30 |
import { Link } from "react-router"; |
| 31 |
// Components |
| 32 |
import { FetchBaseQueryError } from "@reduxjs/toolkit/query"; |
| 33 |
import { SerializedError } from "@reduxjs/toolkit"; |
| 34 |
import ToolbarLayout, { |
| 35 |
ToolbarItem, |
| 36 |
} from "src/components/layouts/ToolbarLayout"; |
| 37 |
import SearchInputLayout from "src/components/layouts/SearchInputLayout"; |
| 38 |
import SecondaryButton from "src/components/layouts/SecondaryButton"; |
| 39 |
import HelpTextWithIconLayout from "src/components/layouts/HelpTextWithIconLayout"; |
| 40 |
import PaginationLayout from "src/components/layouts/PaginationLayout"; |
| 41 |
import TitleLayout from "src/components/layouts/TitleLayout"; |
| 42 |
import GlobalErrors from "src/components/errors/GlobalErrors"; |
| 43 |
import TableLayout from "src/components/layouts/TableLayout"; |
| 44 |
import SkeletonOnTableLayout from "src/components/layouts/Skeleton/SkeletonOnTableLayout"; |
| 45 |
|
| 46 |
const DnsServers = () => { |
| 47 |
const dispatch = useAppDispatch(); |
| 48 |
|
| 49 |
// Update current route data to Redux and highlight the current page in the Nav bar |
| 50 |
const { browserTitle } = useUpdateRoute({ |
| 51 |
pathname: "dns-servers", |
| 52 |
}); |
| 53 |
|
| 54 |
// Set the page title to be shown in the browser tab |
| 55 |
React.useEffect(() => { |
| 56 |
document.title = browserTitle; |
| 57 |
}, [browserTitle]); |
| 58 |
|
| 59 |
// Retrieve API version from environment data |
| 60 |
const apiVersion = useAppSelector( |
| 61 |
(state) => state.global.environment.api_version |
| 62 |
) as string; |
| 63 |
|
| 64 |
// URL parameters: page number, page size, search value |
| 65 |
const { page, setPage, perPage, setPerPage, searchValue, setSearchValue } = |
| 66 |
useListPageSearchParams(); |
| 67 |
|
| 68 |
// Handle API calls errors |
| 69 |
const globalErrors = useApiError([]); |
| 70 |
|
| 71 |
// Page indexes |
| 72 |
const firstUserIdx = (page - 1) * perPage; |
| 73 |
const lastUserIdx = page * perPage; |
| 74 |
|
| 75 |
// States |
| 76 |
const [dnsServersId, setDnsServersId] = React.useState<string[]>([]); |
| 77 |
const [isSearchDisabled, setIsSearchDisabled] = |
| 78 |
React.useState<boolean>(false); |
| 79 |
const [totalCount, setTotalCount] = React.useState<number>(0); |
| 80 |
|
| 81 |
// API calls |
| 82 |
const dnsServersResponse = useDnsServersFindQuery({ |
| 83 |
searchValue, |
| 84 |
pkeyOnly: true, |
| 85 |
sizeLimit: 100, |
| 86 |
version: apiVersion, |
| 87 |
}); |
| 88 |
|
| 89 |
const { data, isLoading, error } = dnsServersResponse; |
| 90 |
|
| 91 |
// Handle data when the API call is finished |
| 92 |
React.useEffect(() => { |
| 93 |
if (dnsServersResponse.isFetching) { |
| 94 |
setShowTableRows(false); |
| 95 |
setTotalCount(0); |
| 96 |
globalErrors.clear(); |
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
if ( |
| 101 |
dnsServersResponse.isSuccess && |
| 102 |
dnsServersResponse.data && |
| 103 |
data !== undefined && |
| 104 |
"data" in data |
| 105 |
) { |
| 106 |
setTotalCount(data.data.length || 0); |
| 107 |
setDnsServersId(data.data.slice(firstUserIdx, lastUserIdx) || []); |
| 108 |
setShowTableRows(true); |
| 109 |
} |
| 110 |
}, [dnsServersResponse]); |
| 111 |
|
| 112 |
// Refresh button handling |
| 113 |
const refreshData = () => { |
| 114 |
setShowTableRows(false); |
| 115 |
setTotalCount(0); |
| 116 |
|
| 117 |
dnsServersResponse.refetch().then(() => { |
| 118 |
setShowTableRows(true); |
| 119 |
}); |
| 120 |
}; |
| 121 |
|
| 122 |
// Always refetch data when the component is loaded. |
| 123 |
// This ensures the data is always up-to-date. |
| 124 |
React.useEffect(() => { |
| 125 |
dnsServersResponse.refetch(); |
| 126 |
}, []); |
| 127 |
|
| 128 |
// Show table rows |
| 129 |
const [showTableRows, setShowTableRows] = React.useState<boolean>(!isLoading); |
| 130 |
|
| 131 |
// Show table rows only when data is fully retrieved |
| 132 |
React.useEffect(() => { |
| 133 |
if (dnsServersResponse.isSuccess && dnsServersResponse.data) { |
| 134 |
setShowTableRows(true); |
| 135 |
} |
| 136 |
}, [dnsServersResponse.isSuccess, dnsServersResponse.data]); |
| 137 |
|
| 138 |
// Search API call |
| 139 |
const [searchEntry] = useSearchDnsServersEntriesMutation(); |
| 140 |
|
| 141 |
const submitSearchValue = () => { |
| 142 |
searchEntry({ |
| 143 |
searchValue: searchValue, |
| 144 |
pkeyOnly: true, |
| 145 |
sizeLimit: 100, |
| 146 |
version: apiVersion, |
| 147 |
}).then((result) => { |
| 148 |
if ("data" in result && result.data !== undefined) { |
| 149 |
const searchError = result.data?.error as |
| 150 |
| FetchBaseQueryError |
| 151 |
| SerializedError; |
| 152 |
|
| 153 |
if (searchError) { |
| 154 |
// Error |
| 155 |
let error: string | undefined = ""; |
| 156 |
if ("error" in searchError) { |
| 157 |
error = searchError.error; |
| 158 |
} |
| 159 |
dispatch( |
| 160 |
addAlert({ |
| 161 |
name: "submit-search-value-error", |
| 162 |
title: error || "Error when searching for elements", |
| 163 |
variant: "danger", |
| 164 |
}) |
| 165 |
); |
| 166 |
} else { |
| 167 |
// Success |
| 168 |
const dnsServers = result.data || []; |
| 169 |
|
| 170 |
setTotalCount(totalCount); |
| 171 |
setDnsServersId( |
| 172 |
dnsServers.data.slice(firstUserIdx, lastUserIdx) || [] |
| 173 |
); |
| 174 |
setShowTableRows(true); |
| 175 |
} |
| 176 |
setIsSearchDisabled(false); |
| 177 |
} |
| 178 |
}); |
| 179 |
}; |
| 180 |
|
| 181 |
// Data wrappers |
| 182 |
// TODO: Better separation of concerts |
| 183 |
// - 'PaginationLayout' |
| 184 |
const paginationData = { |
| 185 |
page, |
| 186 |
perPage, |
| 187 |
updatePage: setPage, |
| 188 |
updatePerPage: setPerPage, |
| 189 |
updateSelectedPerPage: () => {}, |
| 190 |
updateShownElementsList: setDnsServersId, |
| 191 |
totalCount, |
| 192 |
}; |
| 193 |
|
| 194 |
// SearchInputLayout |
| 195 |
const searchValueData = { |
| 196 |
searchValue, |
| 197 |
updateSearchValue: setSearchValue, |
| 198 |
submitSearchValue, |
| 199 |
}; |
| 200 |
|
| 201 |
// List of Toolbar items |
| 202 |
const toolbarItems: ToolbarItem[] = [ |
| 203 |
{ |
| 204 |
key: 0, |
| 205 |
element: ( |
| 206 |
<SearchInputLayout |
| 207 |
dataCy="search" |
| 208 |
name="search" |
| 209 |
ariaLabel="Search dns servers" |
| 210 |
placeholder="Search" |
| 211 |
searchValueData={searchValueData} |
| 212 |
isDisabled={isSearchDisabled} |
| 213 |
/> |
| 214 |
), |
| 215 |
toolbarItemVariant: ToolbarItemVariant.label, |
| 216 |
toolbarItemGap: { default: "gapMd" }, |
| 217 |
}, |
| 218 |
{ |
| 219 |
key: 1, |
| 220 |
toolbarItemVariant: ToolbarItemVariant.separator, |
| 221 |
}, |
| 222 |
{ |
| 223 |
key: 2, |
| 224 |
element: ( |
| 225 |
<SecondaryButton |
| 226 |
dataCy="dns-servers-button-refresh" |
| 227 |
onClickHandler={refreshData} |
| 228 |
isDisabled={!showTableRows} |
| 229 |
> |
| 230 |
Refresh |
| 231 |
</SecondaryButton> |
| 232 |
), |
| 233 |
}, |
| 234 |
{ |
| 235 |
key: 3, |
| 236 |
toolbarItemVariant: ToolbarItemVariant.separator, |
| 237 |
}, |
| 238 |
{ |
| 239 |
key: 4, |
| 240 |
element: <HelpTextWithIconLayout textContent="Help" />, |
| 241 |
}, |
| 242 |
{ |
| 243 |
key: 5, |
| 244 |
element: ( |
| 245 |
<PaginationLayout |
| 246 |
list={dnsServersId} |
| 247 |
paginationData={paginationData} |
| 248 |
widgetId="pagination-options-menu-top" |
| 249 |
isCompact={true} |
| 250 |
/> |
| 251 |
), |
| 252 |
toolbarItemAlignment: { default: "alignEnd" }, |
| 253 |
}, |
| 254 |
]; |
| 255 |
|
| 256 |
// Table header |
| 257 |
const header = ( |
| 258 |
<Tr key="header" id="table-header"> |
| 259 |
<Th modifier="wrap" key="idnsserverid"> |
| 260 |
Server name |
| 261 |
</Th> |
| 262 |
</Tr> |
| 263 |
); |
| 264 |
|
| 265 |
const body = ( |
| 266 |
<> |
| 267 |
{dnsServersId.map((dnsServerId) => ( |
| 268 |
<Tr key={`body-row-${dnsServerId}`} id={dnsServerId}> |
| 269 |
<Td key={`idnsserverid-${dnsServerId}`}> |
| 270 |
<Link to={`/dns-servers/${dnsServerId}`}>{dnsServerId}</Link> |
| 271 |
</Td> |
| 272 |
</Tr> |
| 273 |
))} |
| 274 |
</> |
| 275 |
); |
| 276 |
|
| 277 |
const skeleton = ( |
| 278 |
<SkeletonOnTableLayout |
| 279 |
rows={4} |
| 280 |
colSpan={9} |
| 281 |
screenreaderText={"Loading table rows"} |
| 282 |
/> |
| 283 |
); |
| 284 |
|
| 285 |
// Render component |
| 286 |
return ( |
| 287 |
<div> |
| 288 |
<PageSection hasBodyWrapper={false}> |
| 289 |
<TitleLayout |
| 290 |
id="DNS servers page" |
| 291 |
headingLevel="h1" |
| 292 |
text="DNS servers" |
| 293 |
/> |
| 294 |
</PageSection> |
| 295 |
<PageSection hasBodyWrapper={false} isFilled={false}> |
| 296 |
<Flex direction={{ default: "column" }}> |
| 297 |
<FlexItem> |
| 298 |
<ToolbarLayout toolbarItems={toolbarItems} /> |
| 299 |
</FlexItem> |
| 300 |
<FlexItem style={{ flex: "0 0 auto" }}> |
| 301 |
<OuterScrollContainer> |
| 302 |
<InnerScrollContainer |
| 303 |
style={{ height: "55vh", overflow: "auto" }} |
| 304 |
> |
| 305 |
{error !== undefined && error ? ( |
| 306 |
<GlobalErrors errors={globalErrors.getAll()} /> |
| 307 |
) : ( |
| 308 |
<TableLayout |
| 309 |
ariaLabel={"DNS servers table"} |
| 310 |
variant={"compact"} |
| 311 |
hasBorders={true} |
| 312 |
classes={"pf-v6-u-mt-md"} |
| 313 |
tableId={"dns-servers-table"} |
| 314 |
isStickyHeader={true} |
| 315 |
tableHeader={header} |
| 316 |
tableBody={!showTableRows ? skeleton : body} |
| 317 |
/> |
| 318 |
)} |
| 319 |
</InnerScrollContainer> |
| 320 |
</OuterScrollContainer> |
| 321 |
</FlexItem> |
| 322 |
<FlexItem style={{ flex: "0 0 auto", position: "sticky", bottom: 0 }}> |
| 323 |
<PaginationLayout |
| 324 |
list={dnsServersId} |
| 325 |
paginationData={paginationData} |
| 326 |
variant={PaginationVariant.bottom} |
| 327 |
widgetId="pagination-options-menu-bottom" |
| 328 |
/> |
| 329 |
</FlexItem> |
| 330 |
</Flex> |
| 331 |
</PageSection> |
| 332 |
</div> |
| 333 |
); |
| 334 |
}; |
| 335 |
|
| 336 |
export default DnsServers; |