Skip to content

Commit

Permalink
Merge pull request #734 from SignalK/restart_required_context_fix
Browse files Browse the repository at this point in the history
Fix restartRequired context handling
  • Loading branch information
mairas authored Aug 25, 2024
2 parents 178703e + 2db3095 commit 318e26a
Show file tree
Hide file tree
Showing 5 changed files with 1,979 additions and 1,981 deletions.
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

0 comments on commit 318e26a

Please sign in to comment.