diff --git a/web/src/App.jsx b/web/src/App.jsx index 50e09eedff..f54c50ce9e 100644 --- a/web/src/App.jsx +++ b/web/src/App.jsx @@ -24,7 +24,7 @@ import { Outlet } from "react-router-dom"; import { _ } from "~/i18n"; import { useInstallerClient, useInstallerClientStatus } from "~/context/installer"; -import { useSoftware } from "./context/software"; +import { useProduct } from "./context/product"; import { STARTUP, INSTALL } from "~/client/phase"; import { BUSY } from "~/client/status"; @@ -57,7 +57,7 @@ const ATTEMPTS = 3; function App() { const client = useInstallerClient(); const { attempt } = useInstallerClientStatus(); - const { products } = useSoftware(); + const { products } = useProduct(); const { language } = useL10n(); const [status, setStatus] = useState(undefined); const [phase, setPhase] = useState(undefined); diff --git a/web/src/App.test.jsx b/web/src/App.test.jsx index 84092633b6..59b0753521 100644 --- a/web/src/App.test.jsx +++ b/web/src/App.test.jsx @@ -31,9 +31,9 @@ jest.mock("~/client"); // list of available products let mockProducts; -jest.mock("~/context/software", () => ({ - ...jest.requireActual("~/context/software"), - useSoftware: () => { +jest.mock("~/context/product", () => ({ + ...jest.requireActual("~/context/product"), + useProduct: () => { return { products: mockProducts, selectedProduct: null diff --git a/web/src/context/product.jsx b/web/src/context/product.jsx index 1d79959fdd..4f476f710a 100644 --- a/web/src/context/product.jsx +++ b/web/src/context/product.jsx @@ -72,7 +72,7 @@ function useProduct() { } const { products = [], selectedId } = context; - const selectedProduct = products.find(p => p.id === selectedId); + const selectedProduct = products.find(p => p.id === selectedId) || null; return { ...context, selectedProduct }; } diff --git a/web/src/context/software.jsx b/web/src/context/software.jsx deleted file mode 100644 index 8e53f91405..0000000000 --- a/web/src/context/software.jsx +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright (c) [2022] SUSE LLC - * - * All Rights Reserved. - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of version 2 of the GNU General Public License as published - * by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, contact SUSE LLC. - * - * To contact SUSE LLC about this file by physical or electronic mail, you may - * find current contact information at www.suse.com. - */ - -import React, { useEffect } from "react"; -import { useCancellablePromise } from "~/utils"; -import { useInstallerClient } from "./installer"; - -const SoftwareContext = React.createContext([]); - -function SoftwareProvider({ children }) { - const client = useInstallerClient(); - const { cancellablePromise } = useCancellablePromise(); - const [products, setProducts] = React.useState(undefined); - const [selectedId, setSelectedId] = React.useState(undefined); - - useEffect(() => { - const loadProducts = async () => { - const available = await cancellablePromise(client.software.getProducts()); - const selected = await cancellablePromise(client.software.getSelectedProduct()); - setProducts(available); - setSelectedId(selected?.id || null); - }; - - if (client) { - loadProducts().catch(console.error); - } - }, [client, setProducts, setSelectedId, cancellablePromise]); - - useEffect(() => { - if (!client) return; - - return client.software.onProductChange(setSelectedId); - }, [client, setSelectedId]); - - const value = [products, selectedId]; - return {children}; -} - -function useSoftware() { - const context = React.useContext(SoftwareContext); - - if (!context) { - throw new Error("useSoftware must be used within a SoftwareProvider"); - } - - const [products, selectedId] = context; - - let selectedProduct = selectedId; - if (selectedId && products) { - selectedProduct = products.find(p => p.id === selectedId) || null; - } - - return { - products, - selectedProduct - }; -} - -export { SoftwareProvider, useSoftware };