46 lines
1.1 KiB
TypeScript
46 lines
1.1 KiB
TypeScript
|
|
import { useQuery, useMutation } from "@tanstack/react-query";
|
||
|
|
import { CAM_BASE } from "../utils/config";
|
||
|
|
import type { WifiConfig } from "../types/types";
|
||
|
|
|
||
|
|
const getWiFiSettings = async () => {
|
||
|
|
const response = await fetch(
|
||
|
|
`${CAM_BASE}/api/fetch-config?id=ModemAndWifiManager-wifi`
|
||
|
|
);
|
||
|
|
if (!response.ok) {
|
||
|
|
throw new Error("Cannot fetch Wifi settings");
|
||
|
|
}
|
||
|
|
return response.json();
|
||
|
|
};
|
||
|
|
|
||
|
|
const updateWifiSettings = async (wifiConfig: WifiConfig) => {
|
||
|
|
const response = await fetch(
|
||
|
|
`${CAM_BASE}/api/update-config?id=ModemAndWifiManager-wifi`,
|
||
|
|
{
|
||
|
|
method: "POST",
|
||
|
|
headers: { "Content-Type": "application/json" },
|
||
|
|
body: JSON.stringify(wifiConfig),
|
||
|
|
}
|
||
|
|
);
|
||
|
|
if (!response.ok) {
|
||
|
|
throw new Error("Cannot update wifi settings");
|
||
|
|
}
|
||
|
|
return response.json();
|
||
|
|
};
|
||
|
|
|
||
|
|
export const useWifiAndModem = () => {
|
||
|
|
const wifiQuery = useQuery({
|
||
|
|
queryKey: ["getWifiSettings"],
|
||
|
|
queryFn: getWiFiSettings,
|
||
|
|
});
|
||
|
|
|
||
|
|
const wifiMutation = useMutation({
|
||
|
|
mutationKey: ["updateWifiSettings"],
|
||
|
|
mutationFn: (wifiConfig: WifiConfig) => updateWifiSettings(wifiConfig),
|
||
|
|
});
|
||
|
|
|
||
|
|
return {
|
||
|
|
wifiQuery,
|
||
|
|
wifiMutation,
|
||
|
|
};
|
||
|
|
};
|