Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(web): replace loaders with queries hooks #1452

Merged
merged 6 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions web/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@ import { useInstallerClientStatus } from "~/context/installer";
import { useProduct } from "./context/product";
import { CONFIG, INSTALL, STARTUP } from "~/client/phase";
import { BUSY } from "~/client/status";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { useL10nConfigChanges } from "~/queries/l10n";

const queryClient = new QueryClient();

/**
* Main application component.
*
Expand Down
8 changes: 5 additions & 3 deletions web/src/MainLayout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* find current contact information at www.suse.com.
*/

import React from "react";
import React, { Suspense } from "react";
import { Outlet, NavLink, useNavigate } from "react-router-dom";
import {
Button,
Expand All @@ -28,7 +28,7 @@ import {
Page, PageSidebar, PageSidebarBody, PageToggleButton,
Toolbar, ToolbarContent, ToolbarGroup, ToolbarItem
} from "@patternfly/react-core";
import { Icon } from "~/components/layout";
import { Icon, Loading } from "~/components/layout";
import { About, InstallerOptions, LogsButton } from "~/components/core";
import { _ } from "~/i18n";
import { rootRoutes } from "~/router";
Expand Down Expand Up @@ -134,7 +134,9 @@ export default function Root() {
header={<Header />}
sidebar={<Sidebar />}
>
<Outlet />
<Suspense fallback={<Loading />}>
<Outlet />
</Suspense>
</Page>
);
}
6 changes: 3 additions & 3 deletions web/src/components/l10n/KeyboardSelection.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@

import React, { useState } from "react";
import { Form, FormGroup, Radio, Text } from "@patternfly/react-core";
import { useLoaderData, useNavigate } from "react-router-dom";
import { useNavigate } from "react-router-dom";
import { ListSearch, Page } from "~/components/core";
import { _ } from "~/i18n";
import { useConfigMutation } from "~/queries/l10n";
import { useConfigMutation, useL10n } from "~/queries/l10n";
import textStyles from '@patternfly/react-styles/css/utilities/Text/text';

// TODO: Add documentation and typechecking
// TODO: Evaluate if worth it extracting the selector
export default function KeyboardSelection() {
const navigate = useNavigate();
const setConfig = useConfigMutation();
const { keymaps, keymap: currentKeymap } = useLoaderData();
const { keymaps, selectedKeymap: currentKeymap } = useL10n();
const [selected, setSelected] = useState(currentKeymap.id);
const [filteredKeymaps, setFilteredKeymaps] = useState(
keymaps.sort((k1, k2) => k1.name > k2.name ? 1 : -1)
Expand Down
4 changes: 2 additions & 2 deletions web/src/components/l10n/KeyboardSelection.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ const mockConfigMutation = {

jest.mock("~/queries/l10n", () => ({
...jest.requireActual("~/queries/l10n"),
useConfigMutation: () => mockConfigMutation
useConfigMutation: () => mockConfigMutation,
useL10n: () => ({ keymaps, selectedKeymap: keymaps[0] })
}));

jest.mock("react-router-dom", () => ({
...jest.requireActual("react-router-dom"),
useNavigate: () => mockNavigateFn,
useLoaderData: () => ({ keymaps, keymap: keymaps[0] })
}));

it("allows changing the keyboard", async () => {
Expand Down
7 changes: 6 additions & 1 deletion web/src/components/l10n/L10nPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ 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 { _ } from "~/i18n";
import { useL10n } from "~/queries/l10n";

const Section = ({ label, value, children }) => {
return (
Expand All @@ -45,7 +46,11 @@ const Section = ({ label, value, children }) => {
* @component
*/
export default function L10nPage() {
const { locale, timezone, keymap } = useLoaderData();
const {
selectedLocale: locale,
selectedTimezone: timezone,
selectedKeymap: keymap
} = useL10n();

return (
<>
Expand Down
35 changes: 28 additions & 7 deletions web/src/components/l10n/L10nPage.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,39 @@ import L10nPage from "~/components/l10n/L10nPage";

let mockLoadedData;

const locales = [
{ id: "en_US.UTF-8", name: "English", territory: "United States" },
{ id: "es_ES.UTF-8", name: "Spanish", territory: "Spain" }
];

const keymaps = [
{ id: "us", name: "English" },
{ id: "es", name: "Spanish" }
];

const timezones = [
{ id: "Europe/Berlin", parts: ["Europe", "Berlin"] },
{ id: "Europe/Madrid", parts: ["Europe", "Madrid"] }
];

jest.mock('react-router-dom', () => ({
...jest.requireActual("react-router-dom"),
useLoaderData: () => mockLoadedData,
// TODO: mock the link because it needs a working router.
Link: ({ children }) => <button>{children}</button>
}));

jest.mock("~/queries/l10n", () => ({
useL10n: () => mockLoadedData
}));

beforeEach(() => {
mockLoadedData = {
locale: { id: "en_US.UTF-8", name: "English", territory: "United States" },
keymap: { id: "us", name: "English" },
timezone: { id: "Europe/Berlin", parts: ["Europe", "Berlin"] }
locales,
keymaps,
timezones,
selectedLocale: locales[0],
selectedKeymap: keymaps[0],
selectedTimezone: timezones[0],
};
});

Expand All @@ -49,7 +70,7 @@ it("renders a section for configuring the language", () => {

describe("if there is no selected language", () => {
beforeEach(() => {
mockLoadedData.locale = undefined;
mockLoadedData.selectedLocale = undefined;
});

it("renders a button for selecting a language", () => {
Expand All @@ -69,7 +90,7 @@ it("renders a section for configuring the keyboard", () => {

describe("if there is no selected keyboard", () => {
beforeEach(() => {
mockLoadedData.keymap = undefined;
mockLoadedData.selectedKeymap = undefined;
});

it("renders a button for selecting a keyboard", () => {
Expand All @@ -89,7 +110,7 @@ it("renders a section for configuring the time zone", () => {

describe("if there is no selected time zone", () => {
beforeEach(() => {
mockLoadedData.timezone = undefined;
mockLoadedData.selectedTimezone = undefined;
});

it("renders a button for selecting a time zone", () => {
Expand Down
6 changes: 3 additions & 3 deletions web/src/components/l10n/LocaleSelection.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@

import React, { useState } from "react";
import { Flex, Form, FormGroup, Radio } from "@patternfly/react-core";
import { useLoaderData, useNavigate } from "react-router-dom";
import { useNavigate } from "react-router-dom";
import { ListSearch, Page } from "~/components/core";
import { _ } from "~/i18n";
import { useConfigMutation } from "~/queries/l10n";
import { useConfigMutation, useL10n } from "~/queries/l10n";
import textStyles from '@patternfly/react-styles/css/utilities/Text/text';

// TODO: Add documentation and typechecking
// TODO: Evaluate if worth it extracting the selector
export default function LocaleSelection() {
const navigate = useNavigate();
const setConfig = useConfigMutation();
const { locales, locale: currentLocale } = useLoaderData();
const { locales, selectedLocale: currentLocale } = useL10n();
const [selected, setSelected] = useState(currentLocale.id);
const [filteredLocales, setFilteredLocales] = useState(locales);

Expand Down
4 changes: 2 additions & 2 deletions web/src/components/l10n/LocaleSelection.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ const mockConfigMutation = {

jest.mock("~/queries/l10n", () => ({
...jest.requireActual("~/queries/l10n"),
useL10n: () => ({ locales, selectedLocale: locales[0] }),
useConfigMutation: () => mockConfigMutation
}));

jest.mock("react-router-dom", () => ({
...jest.requireActual("react-router-dom"),
useNavigate: () => mockNavigateFn,
useLoaderData: () => ({ locales, locale: locales[0] })
useNavigate: () => mockNavigateFn
}));

it("allows changing the keyboard", async () => {
Expand Down
6 changes: 3 additions & 3 deletions web/src/components/l10n/TimezoneSelection.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@

import React, { useState } from "react";
import { Divider, Flex, Form, FormGroup, Radio, Text } from "@patternfly/react-core";
import { useLoaderData, useNavigate } from "react-router-dom";
import { useNavigate } from "react-router-dom";
import { ListSearch, Page } from "~/components/core";
import { _ } from "~/i18n";
import { timezoneTime } from "~/utils";
import { useConfigMutation } from "~/queries/l10n";
import { useConfigMutation, useL10n } from "~/queries/l10n";
import textStyles from '@patternfly/react-styles/css/utilities/Text/text';

let date;
Expand Down Expand Up @@ -56,7 +56,7 @@ export default function TimezoneSelection() {
date = new Date();
const navigate = useNavigate();
const setConfig = useConfigMutation();
const { timezones, timezone: currentTimezone } = useLoaderData();
const { timezones, selectedTimezone: currentTimezone } = useL10n();
const displayTimezones = timezones.map(timezoneWithDetails);
const [selected, setSelected] = useState(currentTimezone.id);
const [filteredTimezones, setFilteredTimezones] = useState(sortedTimezones(displayTimezones));
Expand Down
6 changes: 3 additions & 3 deletions web/src/components/l10n/TimezoneSelection.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ const mockConfigMutation = {

jest.mock("~/queries/l10n", () => ({
...jest.requireActual("~/queries/l10n"),
useConfigMutation: () => mockConfigMutation
useConfigMutation: () => mockConfigMutation,
useL10n: () => ({ timezones, selectedTimezone: timezones[0] })
}));

jest.mock("react-router-dom", () => ({
...jest.requireActual("react-router-dom"),
useNavigate: () => mockNavigateFn,
useLoaderData: () => ({ timezones, timezone: timezones[0] })
useNavigate: () => mockNavigateFn
}));

it("allows changing the keyboard", async () => {
Expand Down
10 changes: 2 additions & 8 deletions web/src/components/overview/L10nSection.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,10 @@ import React from "react";
import { TextContent, Text, TextVariants } from "@patternfly/react-core";
import { Em } from "~/components/core";
import { _ } from "~/i18n";
import { localesQuery, configQuery } from "~/queries/l10n";
import { useQuery } from "@tanstack/react-query";
import { useL10n } from "~/queries/l10n";

export default function L10nSection() {
const { isPending: isLocalesPending, data: locales } = useQuery(localesQuery());
const { isPending: isConfigPending, data: config } = useQuery(configQuery());

if (isLocalesPending || isConfigPending) return;

const locale = locales.find((l) => l.id === config?.locales[0]);
const { selectedLocale: locale } = useL10n();

// TRANSLATORS: %s will be replaced by a language name and territory, example:
// "English (United States)".
Expand Down
9 changes: 1 addition & 8 deletions web/src/components/overview/L10nSection.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,7 @@ const locales = [
];

jest.mock("~/queries/l10n", () => ({
localesQuery: () => ({
queryKey: ["l10n", "locales"],
queryFn: jest.fn().mockResolvedValue(locales)
}),
configQuery: () => ({
queryKey: ["l10n", "config"],
queryFn: jest.fn().mockResolvedValue({ locales: ["en_US.UTF-8"] })
})
useL10n: () => ({ locales, selectedLocale: locales[0] }),
}));

it("displays the selected locale", async () => {
Expand Down
2 changes: 1 addition & 1 deletion web/src/context/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ function AppProviders({ children }) {
);
}

export { AppProviders, queryClient };
export { AppProviders };
65 changes: 0 additions & 65 deletions web/src/queries/hooks.js

This file was deleted.

Loading
Loading