Skip to content

Commit

Permalink
refactor(web): improve route files
Browse files Browse the repository at this point in the history
By moving them from ~/components/{namespace}/routs.js to
~/routes/{namespace}.tsx

It also adds a constant PATHS per namespace and uses them instead of
directly typing the route path again when needed.
  • Loading branch information
dgdavid committed Jul 24, 2024
1 parent 22d0125 commit 1555cc4
Show file tree
Hide file tree
Showing 17 changed files with 165 additions and 168 deletions.
9 changes: 5 additions & 4 deletions web/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ import { useProduct, useProductChanges } from "./queries/software";
import { CONFIG, INSTALL, STARTUP } from "~/client/phase";
import { BUSY } from "~/client/status";
import { useL10nConfigChanges } from "~/queries/l10n";
import { useIssues, useIssuesChanges } from "./queries/issues";
import { useIssuesChanges } from "./queries/issues";
import { PATHS as PRODUCT_PATHS } from "./routes/products";

/**
* Main application component.
Expand Down Expand Up @@ -61,12 +62,12 @@ function App() {
return <Loading />;
}

if (selectedProduct === undefined && location.pathname !== "/products") {
if (selectedProduct === undefined && location.pathname !== PRODUCT_PATHS.root) {
return <Navigate to="/products" />;
}

if (phase === CONFIG && status === BUSY && location.pathname !== "/products/progress") {
return <Navigate to="/products/progress" />;
if (phase === CONFIG && status === BUSY && location.pathname !== PRODUCT_PATHS.progress) {
return <Navigate to={PRODUCT_PATHS.progress} />;
}

return <Outlet />;
Expand Down
10 changes: 3 additions & 7 deletions web/src/components/l10n/L10nPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ import React from "react";
import { Gallery, GalleryItem } from "@patternfly/react-core";
import { useLoaderData } from "react-router-dom";
import { ButtonLink, CardField, Page } from "~/components/core";
import {
LOCALE_SELECTION_PATH,
KEYMAP_SELECTION_PATH,
TIMEZONE_SELECTION_PATH,
} from "~/routes/l10n";
import { PATHS } from "~/routes/l10n";
import { _ } from "~/i18n";
import { useL10n } from "~/queries/l10n";

Expand Down Expand Up @@ -60,15 +56,15 @@ export default function L10nPage() {
label={_("Language")}
value={locale ? `${locale.name} - ${locale.territory}` : _("Not selected yet")}
>
<ButtonLink to={LOCALE_SELECTION_PATH} isPrimary={!locale}>
<ButtonLink to={PATHS.localeSelection} isPrimary={!locale}>
{locale ? _("Change") : _("Select")}
</ButtonLink>
</Section>
</GalleryItem>

<GalleryItem>
<Section label={_("Keyboard")} value={keymap ? keymap.name : _("Not selected yet")}>
<ButtonLink to={KEYMAP_SELECTION_PATH} isPrimary={!keymap}>
<ButtonLink to={PATHS.keymapSelection} isPrimary={!keymap}>
{keymap ? _("Change") : _("Select")}
</ButtonLink>
</Section>
Expand Down
35 changes: 0 additions & 35 deletions web/src/components/overview/routes.js

This file was deleted.

3 changes: 2 additions & 1 deletion web/src/components/product/ProductSelectionProgress.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { useProduct } from "~/queries/software";
import { ProgressReport } from "~/components/core";
import { IDLE } from "~/client/status";
import { useInstallerClient } from "~/context/installer";
import { PATHS } from "~/router";

/**
* @component
Expand All @@ -42,7 +43,7 @@ function ProductSelectionProgress() {
return manager.onStatusChange(setStatus);
}, [manager, setStatus]);

if (status === IDLE) return <Navigate to="/" replace />;
if (status === IDLE) return <Navigate to={PATHS.root} replace />;

return (
<ProgressReport
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@
* find current contact information at www.suse.com.
*/

export { default as ProductRegistrationPage } from "./ProductRegistrationPage";
export { default as ProductSelectionPage } from "./ProductSelectionPage";
export { default as ProductSelectionProgress } from "./ProductSelectionProgress";
3 changes: 2 additions & 1 deletion web/src/components/software/SoftwarePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { useIssues } from "~/queries/issues";
import { usePatterns, useProposal, useProposalChanges } from "~/queries/software";
import { Pattern, SelectedBy } from "~/types/software";
import { _ } from "~/i18n";
import { PATHS } from "~/routes/software";

/**
* List of selected patterns.
Expand Down Expand Up @@ -66,7 +67,7 @@ const SelectedPatterns = ({ patterns }): React.ReactNode => (
<CardField
label={_("Selected patterns")}
actions={
<ButtonLink to="patterns/select" isPrimary>
<ButtonLink to={PATHS.patternsSelection} isPrimary>
{_("Change selection")}
</ButtonLink>
}
Expand Down
3 changes: 2 additions & 1 deletion web/src/components/storage/BootConfigField.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { _ } from "~/i18n";
import { sprintf } from "sprintf-js";
import { deviceLabel } from "~/components/storage/utils";
import { Icon } from "~/components/layout";
import { PATHS } from "~/routes/storage";

/**
* @typedef {import ("~/client/storage").StorageDevice} StorageDevice
Expand All @@ -42,7 +43,7 @@ import { Icon } from "~/components/layout";
const Link = ({ isBold = false }) => {
const text = _("Change boot options");

return <RouterLink to="booting-partition">{isBold ? <b>{text}</b> : text}</RouterLink>;
return <RouterLink to={PATHS.bootingPartition}>{isBold ? <b>{text}</b> : text}</RouterLink>;
};

/**
Expand Down
3 changes: 2 additions & 1 deletion web/src/components/storage/InstallationDeviceField.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import React from "react";
import { Skeleton } from "@patternfly/react-core";
import { ButtonLink, CardField } from "~/components/core";
import { deviceLabel } from "~/components/storage/utils";
import { PATHS } from "~/routes/storage";
import { _ } from "~/i18n";
import { sprintf } from "sprintf-js";

Expand Down Expand Up @@ -104,7 +105,7 @@ export default function InstallationDeviceField({
isLoading ? (
<Skeleton fontSize="sm" width="100px" />
) : (
<ButtonLink to="target-device" isPrimary={false}>
<ButtonLink to={PATHS.targetDevice} isPrimary={false}>
{_("Change")}
</ButtonLink>
)
Expand Down
3 changes: 2 additions & 1 deletion web/src/components/storage/ProposalActionsSummary.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import DevicesManager from "~/components/storage/DevicesManager";
import { _, n_ } from "~/i18n";
import { sprintf } from "sprintf-js";
import textStyles from "@patternfly/react-styles/css/utilities/Text/text";
import { PATHS } from "~/routes/storage";

/**
* @typedef {import ("~/client/storage").Action} Action
Expand Down Expand Up @@ -235,7 +236,7 @@ export default function ProposalActionsSummary({
isLoading ? (
<Skeleton fontSize="sm" width="100px" />
) : (
<ButtonLink to="space-policy">{_("Change")}</ButtonLink>
<ButtonLink to={PATHS.spacePolicy}>{_("Change")}</ButtonLink>
)
}
cardProps={{ isFullHeight: false }}
Expand Down
65 changes: 0 additions & 65 deletions web/src/components/storage/routes.js

This file was deleted.

5 changes: 3 additions & 2 deletions web/src/components/users/FirstUser.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { useNavigate } from "react-router-dom";
import { RowActions, ButtonLink } from "~/components/core";
import { _ } from "~/i18n";
import { useFirstUser, useFirstUserChanges, useRemoveFirstUserMutation } from "~/queries/users";
import { PATHS } from "~/routes/users";

const UserNotDefined = ({ actionCb }) => {
return (
Expand All @@ -40,7 +41,7 @@ const UserNotDefined = ({ actionCb }) => {
</strong>
</div>
<Split hasGutter>
<ButtonLink to="first" isPrimary>
<ButtonLink to={PATHS.firstUser.create} isPrimary>
{_("Define a user now")}
</ButtonLink>
</Split>
Expand Down Expand Up @@ -83,7 +84,7 @@ export default function FirstUser() {
const actions = [
{
title: _("Edit"),
onClick: () => navigate("/users/first/edit"),
onClick: () => navigate(PATHS.firstUser.edit),
},
{
title: _("Discard"),
Expand Down
39 changes: 24 additions & 15 deletions web/src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,36 @@ import MainLayout from "~/MainLayout";
import SimpleLayout from "./SimpleLayout";
import { LoginPage } from "~/components/core";
import { OverviewPage } from "~/components/overview";
import { _ } from "~/i18n";
import overviewRoutes from "~/components/overview/routes";
import { _, N_ } from "~/i18n";
import l10nRoutes from "~/routes/l10n";
import networkRoutes from "~/components/network/routes";
import productsRoutes from "~/routes/products";
import storageRoutes from "~/components/storage/routes";
import softwareRoutes from "~/components/software/routes";
import usersRoutes from "~/components/users/routes";
import storageRoutes from "~/routes/storage";
import softwareRoutes from "~/routes/software";
import usersRoutes from "~/routes/users";

const PATHS = {
root: "/",
login: "/login",
overview: "/overview",
};

const rootRoutes = [
overviewRoutes,
l10nRoutes,
{
path: "/overview",
element: <OverviewPage />,
handle: { name: N_("Overview"), icon: "list_alt" },
},
l10nRoutes(),
networkRoutes,
storageRoutes,
softwareRoutes,
usersRoutes,
storageRoutes(),
softwareRoutes(),
usersRoutes(),
];

const protectedRoutes = [
{
path: "/",
path: PATHS.root,
element: <App />,
children: [
{
Expand All @@ -62,15 +71,15 @@ const protectedRoutes = [
},
{
element: <SimpleLayout showInstallerOptions />,
children: [productsRoutes],
children: [productsRoutes()],
},
],
},
];

const routes = [
{
path: "/login",
path: PATHS.login,
exact: true,
element: <SimpleLayout />,
children: [
Expand All @@ -81,12 +90,12 @@ const routes = [
],
},
{
path: "/",
path: PATHS.root,
element: <Protected />,
children: [...protectedRoutes],
},
];

const router = createHashRouter(routes);

export { router, rootRoutes };
export { router, rootRoutes, PATHS };
Loading

0 comments on commit 1555cc4

Please sign in to comment.