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

Fix restartRequired context handling #734

Merged
merged 1 commit into from
Aug 25, 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
14 changes: 6 additions & 8 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,12 @@ export function App(): JSX.Element {
return (
<>
<RestartRequiredProvider>
<>
<NavPathContext.Provider value={navPath}>
<Header routes={routes} />
</NavPathContext.Provider>
{routes.length === 0 ? null : (
<Router onChange={handleRouteChange}>{routeComponents}</Router>
)}
</>
<NavPathContext.Provider value={navPath}>
<Header routes={routes} />
</NavPathContext.Provider>
{routes.length === 0 ? null : (
<Router onChange={handleRouteChange}>{routeComponents}</Router>
)}
</RestartRequiredProvider>
</>
);
Expand Down
19 changes: 8 additions & 11 deletions frontend/src/common/RestartRequiredContext.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import { createContext, JSX } from "preact";
import { createContext } from "preact";
import { useState } from "preact/hooks";

export interface RestartRequiredContextProps {
restartRequired: boolean;
setRestartRequired: (value: boolean) => void;
}

export const RestartRequiredContext = createContext<RestartRequiredContextProps>({
restartRequired: false,
setRestartRequired: () => {},
});
export const RestartRequiredContext =
createContext<RestartRequiredContextProps>({
restartRequired: false,
setRestartRequired: () => {},
});

export const RestartRequiredProvider = ({
children,
}: {
children: JSX.Element;
}) => {
const [restartRequired, setRestartRequired] = useState<boolean>(false);
export const RestartRequiredProvider = ({ children }) => {
const [restartRequired, setRestartRequired] = useState(false);

return (
<RestartRequiredContext.Provider
Expand Down
19 changes: 10 additions & 9 deletions frontend/src/pages/System/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ function SystemSettingsCard({
children,
}: SystemSettingsCardProps): JSX.Element {
const [saving, setSaving] = useState<boolean>(false);
const { restartRequired, setRestartRequired } =
useContext<RestartRequiredContextProps>(RestartRequiredContext);

const id = useId();

async function handleSave(e: MouseEvent): Promise<void> {
const { restartRequired, setRestartRequired } =
useContext<RestartRequiredContextProps>(RestartRequiredContext);
e.preventDefault();
setSaving(true);
await handleSaveData();
Expand Down Expand Up @@ -126,6 +126,8 @@ function DeviceNameCard(): JSX.Element {
const [origData, setOrigData] = useState<JsonObject>({});
const [data, setData] = useState<JsonObject>({});
const [errorMessage, setErrorMessage] = useState<string>("");
const { restartRequired, setRestartRequired } =
useContext<RestartRequiredContextProps>(RestartRequiredContext);

const id = useId();

Expand All @@ -141,9 +143,6 @@ function DeviceNameCard(): JSX.Element {
};

async function handleSaveData(): Promise<void> {
const { restartRequired, setRestartRequired } =
useContext<RestartRequiredContextProps>(RestartRequiredContext);

const success = await saveConfigData(
configPath,
JSON.stringify(data),
Expand Down Expand Up @@ -199,6 +198,9 @@ function AuthCard(): JSX.Element {
const [origData, setOrigData] = useState<JsonObject>({});
const [data, setData] = useState<JsonObject>({});
const [errorMessage, setErrorMessage] = useState<string>("");
const { restartRequired, setRestartRequired } =
useContext<RestartRequiredContextProps>(RestartRequiredContext);

const id = useId();

const configPath = "/system/httpserver";
Expand All @@ -221,9 +223,6 @@ function AuthCard(): JSX.Element {
}, []);

async function handleSaveData(): Promise<void> {
const { restartRequired, setRestartRequired } =
useContext<RestartRequiredContextProps>(RestartRequiredContext);

const success = await saveConfigData(
configPath,
JSON.stringify(data),
Expand Down Expand Up @@ -355,7 +354,9 @@ function RestartCard(): JSX.Element {
<ToastMessage
color="text-bg-warning"
show={showRestartToast}
onHide={() => {setShowRestartToast(false)}}
onHide={() => {
setShowRestartToast(false);
}}
>
<p>Restarting the device...</p>
</ToastMessage>
Expand Down
10 changes: 6 additions & 4 deletions frontend/src/pages/WiFi/WiFiSettingsPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { RestartRequiredContext } from "common/RestartRequiredContext";
import {
RestartRequiredContext,
RestartRequiredContextProps,
} from "common/RestartRequiredContext";
import { fetchConfigData, saveConfigData } from "common/configAPIClient";
import { JsonValue } from "common/jsonTypes";
import { CollapseCard } from "components/Card";
Expand Down Expand Up @@ -28,6 +31,8 @@ export function WiFiSettingsPanel(): JSX.Element {
new WiFiSettingsConfig(),
);
const [errorText, setErrorText] = useState("");
const { restartRequired, setRestartRequired } =
useContext<RestartRequiredContextProps>(RestartRequiredContext);

const id = useId();

Expand All @@ -36,9 +41,6 @@ export function WiFiSettingsPanel(): JSX.Element {
}

async function handleSave(): Promise<void> {
const { restartRequired, setRestartRequired } = useContext(
RestartRequiredContext,
);
try {
await saveConfigData(
"/System/WiFi Settings",
Expand Down
Loading
Loading