Skip to content

Commit

Permalink
refactor(web) move and improve network routes file
Browse files Browse the repository at this point in the history
Related to 1555cc4
  • Loading branch information
dgdavid committed Jul 24, 2024
1 parent 624e611 commit 6c21d01
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 33 deletions.
8 changes: 4 additions & 4 deletions web/src/components/network/ConnectionsTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
*/

import React from "react";
import { useNavigate } from "react-router-dom";
import { useNavigate, generatePath } from "react-router-dom";
import { Table, Thead, Tr, Th, Tbody, Td } from "@patternfly/react-table";
import { sprintf } from "sprintf-js";

import { RowActions } from "~/components/core";
import { Icon } from "~/components/layout";
import { formatIp } from "~/client/network/utils";
import { PATHS } from "~/routes/network";
import { sprintf } from "sprintf-js";
import { _ } from "~/i18n";

/**
Expand Down Expand Up @@ -76,7 +76,7 @@ export default function ConnectionsTable({ connections, devices, onForget }) {
role: "link",
// TRANSLATORS: %s is replaced by a network connection name
"aria-label": sprintf(_("Edit connection %s"), connection.id),
onClick: () => navigate(`connections/${connection.id}/edit`),
onClick: () => navigate(generatePath(PATHS.editConnection, { id: connection.id })),
},
typeof onForget === "function" && {
title: _("Forget"),
Expand Down
11 changes: 6 additions & 5 deletions web/src/components/network/NetworkPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ import React from "react";
import { CardBody, Grid, GridItem } from "@patternfly/react-core";
import { ButtonLink, CardField, EmptyState, Page } from "~/components/core";
import { ConnectionsTable } from "~/components/network";
import { _ } from "~/i18n";
import { formatIp } from "~/client/network/utils";
import { sprintf } from "sprintf-js";
import { useNetwork, useNetworkConfigChanges } from "~/queries/network";
import { PATHS } from "~/routes/network";
import { sprintf } from "sprintf-js";
import { _ } from "~/i18n";

/**
* Page component holding Network settings
Expand Down Expand Up @@ -70,7 +71,7 @@ export default function NetworkPage() {
<CardField
label={_("Wi-Fi")}
actions={
<ButtonLink isPrimary={!activeConnection} to="wifis">
<ButtonLink isPrimary={!activeConnection} to={PATHS.wifis}>
{activeConnection ? _("Change") : _("Connect")}
</ButtonLink>
}
Expand Down Expand Up @@ -101,8 +102,8 @@ export default function NetworkPage() {
return (
<CardField label={total > 0 && _("Wired")}>
<CardBody>
{total === 0 && (<EmptyState title={_("No wired connections found")} icon="warning" />)}
{total !== 0 && (<ConnectionsTable connections={wiredConnections} devices={devices} />)}
{total === 0 && <EmptyState title={_("No wired connections found")} icon="warning" />}
{total !== 0 && <ConnectionsTable connections={wiredConnections} devices={devices} />}
</CardBody>
</CardField>
);
Expand Down
24 changes: 13 additions & 11 deletions web/src/components/network/WifiNetworksListPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,17 @@ import {
Split,
Stack,
} from "@patternfly/react-core";
import { generatePath } from "react-router-dom";
import { useQueryClient } from "@tanstack/react-query";
import { Icon } from "~/components/layout";
import { WifiConnectionForm } from "~/components/network";
import { ButtonLink } from "~/components/core";
import { DeviceState } from "~/client/network/model";
import { useInstallerClient } from "~/context/installer";
import { _ } from "~/i18n";
import { formatIp } from "~/client/network/utils";
import { useQueryClient } from "@tanstack/react-query";
import { useInstallerClient } from "~/context/installer";
import { useSelectedWifi, useSelectedWifiChange } from "~/queries/network";
import { PATHS } from "~/routes/network";
import { _ } from "~/i18n";

const HIDDEN_NETWORK = Object.freeze({ hidden: true });

Expand Down Expand Up @@ -92,7 +94,7 @@ const WifiDrawerPanelBody = ({ network, onCancel }) => {
const { data } = useSelectedWifi();
const forgetNetwork = async () => {
await client.network.deleteConnection(network.settings.id);
queryClient.invalidateQueries({ queryKey: ["network", "connections"] })
queryClient.invalidateQueries({ queryKey: ["network", "connections"] });
};

if (!network) return;
Expand All @@ -109,7 +111,9 @@ const WifiDrawerPanelBody = ({ network, onCancel }) => {
<ButtonLink onClick={async () => await client.network.connectTo(network.settings)}>
{_("Connect")}
</ButtonLink>
<ButtonLink to={`/network/connections/${network.settings.id}/edit`}>{_("Edit")}</ButtonLink>
<ButtonLink to={generatePath(PATHS.editConnection, { id: network.settings.id })}>
{_("Edit")}
</ButtonLink>
<Button variant="secondary" isDanger onClick={forgetNetwork}>
{_("Forget")}
</Button>
Expand All @@ -131,7 +135,7 @@ const WifiDrawerPanelBody = ({ network, onCancel }) => {
<ButtonLink onClick={async () => await client.network.disconnect(network.settings)}>
{_("Disconnect")}
</ButtonLink>
<ButtonLink to={`/network/connections/${network.settings.id}/edit`}>
<ButtonLink to={generatePath(PATHS.editConnection, { id: network.settings.id })}>
{_("Edit")}
</ButtonLink>
<Button
Expand Down Expand Up @@ -187,7 +191,8 @@ const NetworkListName = ({ network }) => {
*/
function WifiNetworksListPage({ networks = [] }) {
const { data } = useSelectedWifi();
const selected = data.ssid === undefined ? HIDDEN_NETWORK : networks.find(n => n.ssid === data.ssid);
const selected =
data.ssid === undefined ? HIDDEN_NETWORK : networks.find((n) => n.ssid === data.ssid);
const changeSelected = useSelectedWifiChange();

const selectHiddneNetwork = () => {
Expand Down Expand Up @@ -247,10 +252,7 @@ function WifiNetworksListPage({ networks = [] }) {
</DrawerActions>
</DrawerHead>
<DrawerPanelBody>
<WifiDrawerPanelBody
network={selected}
onCancel={unselectNetwork}
/>
<WifiDrawerPanelBody network={selected} onCancel={unselectNetwork} />
</DrawerPanelBody>
</DrawerPanelContent>
}
Expand Down
4 changes: 2 additions & 2 deletions web/src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { LoginPage } from "~/components/core";
import { OverviewPage } from "~/components/overview";
import { _, N_ } from "~/i18n";
import l10nRoutes from "~/routes/l10n";
import networkRoutes from "~/components/network/routes";
import networkRoutes from "~/routes/network";
import productsRoutes from "~/routes/products";
import storageRoutes from "~/routes/storage";
import softwareRoutes from "~/routes/software";
Expand All @@ -48,7 +48,7 @@ const rootRoutes = [
handle: { name: N_("Overview"), icon: "list_alt" },
},
l10nRoutes(),
networkRoutes,
networkRoutes(),
storageRoutes(),
softwareRoutes(),
usersRoutes(),
Expand Down
27 changes: 16 additions & 11 deletions web/src/components/network/routes.js → web/src/routes/network.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,33 @@
*/

import React from "react";
import NetworkPage from "./NetworkPage";
import IpSettingsForm from "./IpSettingsForm";
import WifiSelectorPage from "./WifiSelectorPage";
import { NetworkPage, IpSettingsForm, WifiSelectorPage } from "~/components/network";
import { N_ } from "~/i18n";

const routes = {
path: "/network",
const PATHS = {
root: "/network",
editConnection: "/network/connections/:id/edit",
wifis: "/network/wifis",
};

const routes = () => ({
path: PATHS.root,
handle: {
name: N_("Network"),
icon: "settings_ethernet",
},
children: [
{ index: true, element: <NetworkPage /> },
{
path: "connections/:id/edit",
element: <IpSettingsForm />
path: PATHS.editConnection,
element: <IpSettingsForm />,
},
{
path: "wifis",
path: PATHS.wifis,
element: <WifiSelectorPage />,
}
]
};
},
],
});

export default routes;
export { PATHS };

0 comments on commit 6c21d01

Please sign in to comment.