2025-10-02 22:53:38 +01:00
|
|
|
import { useQuery, useMutation } from "@tanstack/react-query";
|
|
|
|
|
import { CAM_BASE } from "../utils/config";
|
2025-10-03 10:19:49 +01:00
|
|
|
import type { ModemConfig, WifiConfig } from "../types/types";
|
2025-10-06 14:21:56 +01:00
|
|
|
import { useEffect } from "react";
|
|
|
|
|
import { toast } from "sonner";
|
2025-11-04 16:09:24 +00:00
|
|
|
const camBase = import.meta.env.MODE !== "development" ? CAM_BASE : "";
|
2025-10-02 22:53:38 +01:00
|
|
|
|
|
|
|
|
const getWiFiSettings = async () => {
|
2025-11-04 16:09:24 +00:00
|
|
|
const response = await fetch(`${camBase}/api/fetch-config?id=ModemAndWifiManager-wifi`, {
|
2025-11-05 16:30:27 +00:00
|
|
|
signal: AbortSignal.timeout(300000),
|
2025-10-15 11:00:52 +01:00
|
|
|
});
|
2025-10-02 22:53:38 +01:00
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error("Cannot fetch Wifi settings");
|
|
|
|
|
}
|
|
|
|
|
return response.json();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const updateWifiSettings = async (wifiConfig: WifiConfig) => {
|
2025-11-10 09:05:08 +00:00
|
|
|
const response = await fetch(`${camBase}/api/update-config`, {
|
2025-10-15 11:00:52 +01:00
|
|
|
method: "POST",
|
|
|
|
|
headers: { "Content-Type": "application/json" },
|
|
|
|
|
body: JSON.stringify(wifiConfig),
|
|
|
|
|
});
|
2025-10-02 22:53:38 +01:00
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error("Cannot update wifi settings");
|
|
|
|
|
}
|
|
|
|
|
return response.json();
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-03 10:19:49 +01:00
|
|
|
const getModemSettings = async () => {
|
2025-11-04 16:09:24 +00:00
|
|
|
const response = await fetch(`${camBase}/api/fetch-config?id=ModemAndWifiManager-modem`, {
|
2025-11-05 16:30:27 +00:00
|
|
|
signal: AbortSignal.timeout(300000),
|
2025-10-15 11:00:52 +01:00
|
|
|
});
|
2025-10-03 10:19:49 +01:00
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error("Cannot fetch modem settings");
|
|
|
|
|
}
|
|
|
|
|
return response.json();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const updateModemSettings = async (modemConfig: ModemConfig) => {
|
2025-11-10 09:05:08 +00:00
|
|
|
const response = await fetch(`${camBase}/api/update-config`, {
|
2025-10-15 11:00:52 +01:00
|
|
|
method: "POST",
|
|
|
|
|
headers: { "Content-Type": "application/json" },
|
|
|
|
|
body: JSON.stringify(modemConfig),
|
|
|
|
|
});
|
2025-10-03 10:19:49 +01:00
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error("cannot update modem settings");
|
|
|
|
|
}
|
|
|
|
|
return response.json();
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-02 22:53:38 +01:00
|
|
|
export const useWifiAndModem = () => {
|
|
|
|
|
const wifiQuery = useQuery({
|
|
|
|
|
queryKey: ["getWifiSettings"],
|
|
|
|
|
queryFn: getWiFiSettings,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const wifiMutation = useMutation({
|
|
|
|
|
mutationKey: ["updateWifiSettings"],
|
|
|
|
|
mutationFn: (wifiConfig: WifiConfig) => updateWifiSettings(wifiConfig),
|
2025-10-06 14:21:56 +01:00
|
|
|
onError: (error) => {
|
2025-10-06 15:18:58 +01:00
|
|
|
toast.error("Failed to update WiFi settings", { id: "wiFiSettings" });
|
2025-10-06 14:21:56 +01:00
|
|
|
console.error(error);
|
|
|
|
|
},
|
|
|
|
|
onSuccess: () => {
|
2025-10-06 15:18:58 +01:00
|
|
|
toast.success("WiFi settings updated successfully", {
|
|
|
|
|
id: "wiFiSettings",
|
|
|
|
|
});
|
2025-10-06 14:21:56 +01:00
|
|
|
},
|
2025-10-03 10:19:49 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const modemQuery = useQuery({
|
|
|
|
|
queryKey: ["getModemSettings"],
|
|
|
|
|
queryFn: getModemSettings,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const modemMutation = useMutation({
|
|
|
|
|
mutationKey: ["updateModemSettings"],
|
|
|
|
|
mutationFn: (modemConfig: ModemConfig) => updateModemSettings(modemConfig),
|
2025-10-06 14:21:56 +01:00
|
|
|
onError: (error) => {
|
2025-10-06 15:18:58 +01:00
|
|
|
toast.error("Failed to update Modem settings", { id: "modemSettings" });
|
2025-10-06 14:21:56 +01:00
|
|
|
console.error(error);
|
|
|
|
|
},
|
|
|
|
|
onSuccess: () => {
|
2025-10-06 15:18:58 +01:00
|
|
|
toast.success("Modem settings updated successfully", {
|
|
|
|
|
id: "modemSettings",
|
|
|
|
|
});
|
2025-10-06 14:21:56 +01:00
|
|
|
},
|
2025-10-02 22:53:38 +01:00
|
|
|
});
|
|
|
|
|
|
2025-10-06 14:21:56 +01:00
|
|
|
useEffect(() => {
|
2025-10-15 11:00:52 +01:00
|
|
|
if (wifiQuery.isError) toast.error("Cannot get WiFi settings", { id: "wiFiSettings" });
|
2025-10-06 14:21:56 +01:00
|
|
|
}, [wifiQuery?.error?.message, wifiQuery.isError]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2025-10-15 11:00:52 +01:00
|
|
|
if (modemQuery.isError) toast.error("Cannot get Modem settings", { id: "modemSettings" });
|
2025-10-06 14:21:56 +01:00
|
|
|
}, [modemQuery?.error?.message, modemQuery.isError]);
|
2025-10-02 22:53:38 +01:00
|
|
|
return {
|
|
|
|
|
wifiQuery,
|
|
|
|
|
wifiMutation,
|
2025-10-03 10:19:49 +01:00
|
|
|
modemQuery,
|
|
|
|
|
modemMutation,
|
2025-10-02 22:53:38 +01:00
|
|
|
};
|
|
|
|
|
};
|