mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-08 00:39:50 +03:00
Alova 3 now working
This commit is contained in:
@@ -5,8 +5,7 @@ import ForwardIcon from '@mui/icons-material/Forward';
|
||||
import { Box, Button, Paper, Typography } from '@mui/material';
|
||||
|
||||
import * as AuthenticationApi from 'components/routing/authentication';
|
||||
// import { useRequest } from 'alova/client' // TODO replace when Alova 3 is released
|
||||
import { useRequest } from 'alova';
|
||||
import { useRequest } from 'alova/client';
|
||||
import type { ValidateFieldsError } from 'async-validator';
|
||||
import {
|
||||
LanguageSelector,
|
||||
@@ -32,26 +31,12 @@ const SignIn = () => {
|
||||
const [processing, setProcessing] = useState<boolean>(false);
|
||||
const [fieldErrors, setFieldErrors] = useState<ValidateFieldsError>();
|
||||
|
||||
// TODO ALova 3 code...
|
||||
// const { send: callSignIn } = useRequest(
|
||||
// (request: SignInRequest) => AuthenticationApi.signIn(request),
|
||||
// {
|
||||
// immediate: false
|
||||
// }
|
||||
// ).onSuccess((response) => {
|
||||
// if (response.data) {
|
||||
// authenticationContext.signIn(response.data.access_token);
|
||||
// }
|
||||
// });
|
||||
|
||||
const { send: callSignIn, onSuccess } = useRequest(
|
||||
const { send: callSignIn } = useRequest(
|
||||
(request: SignInRequest) => AuthenticationApi.signIn(request),
|
||||
{
|
||||
immediate: false
|
||||
}
|
||||
);
|
||||
|
||||
onSuccess((response) => {
|
||||
).onSuccess((response) => {
|
||||
if (response.data) {
|
||||
authenticationContext.signIn(response.data.access_token);
|
||||
}
|
||||
|
||||
@@ -65,8 +65,7 @@ export const readDeviceEntities = (id: number) =>
|
||||
alovaInstance.Get<DeviceEntity[]>(`/rest/deviceEntities`, {
|
||||
params: { id },
|
||||
responseType: 'arraybuffer',
|
||||
// transform(data) { // TODO alova 3
|
||||
transformData(data) {
|
||||
transform(data) {
|
||||
return (data as DeviceEntity[]).map((de: DeviceEntity) => ({
|
||||
...de,
|
||||
o_m: de.m,
|
||||
@@ -89,8 +88,7 @@ export const writeDeviceName = (data: { id: number; name: string }) =>
|
||||
// SettingsScheduler
|
||||
export const readSchedule = () =>
|
||||
alovaInstance.Get<ScheduleItem[]>('/rest/schedule', {
|
||||
// transform(data) { // TODO alova 3
|
||||
transformData(data) {
|
||||
transform(data) {
|
||||
return (data as Schedule).schedule.map((si: ScheduleItem) => ({
|
||||
...si,
|
||||
o_id: si.id,
|
||||
@@ -110,8 +108,7 @@ export const writeSchedule = (data: Schedule) =>
|
||||
// Modules
|
||||
export const readModules = () =>
|
||||
alovaInstance.Get<ModuleItem[]>('/rest/modules', {
|
||||
// transform(data) { // TODO alova 3
|
||||
transformData(data) {
|
||||
transform(data) {
|
||||
return (data as Modules).modules.map((mi: ModuleItem) => ({
|
||||
...mi,
|
||||
o_enabled: mi.enabled,
|
||||
@@ -128,8 +125,7 @@ export const writeModules = (data: {
|
||||
// SettingsEntities
|
||||
export const readCustomEntities = () =>
|
||||
alovaInstance.Get<EntityItem[]>('/rest/customEntities', {
|
||||
// transform(data) { // TODO alova 3
|
||||
transformData(data) {
|
||||
transform(data) {
|
||||
return (data as Entities).entities.map((ei: EntityItem) => ({
|
||||
...ei,
|
||||
o_id: ei.id,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { xhrRequestAdapter } from '@alova/adapter-xhr';
|
||||
import { type AlovaXHRResponse, xhrRequestAdapter } from '@alova/adapter-xhr';
|
||||
import { createAlova } from 'alova';
|
||||
import ReactHook from 'alova/react';
|
||||
|
||||
@@ -8,10 +8,13 @@ export const ACCESS_TOKEN = 'access_token';
|
||||
|
||||
export const alovaInstance = createAlova({
|
||||
statesHook: ReactHook,
|
||||
timeout: 3000, // 3 seconds but throwing a timeout error
|
||||
localCache: null,
|
||||
// TODO Alova v3 code...
|
||||
// cacheFor: null,
|
||||
timeout: 3000, // 3 seconds before throwing a timeout error
|
||||
cacheFor: {
|
||||
GET: {
|
||||
mode: 'memory',
|
||||
expire: 60 * 10 * 1000 // 60 seconds in cache
|
||||
}
|
||||
},
|
||||
requestAdapter: xhrRequestAdapter(),
|
||||
beforeRequest(method) {
|
||||
if (localStorage.getItem(ACCESS_TOKEN)) {
|
||||
@@ -21,7 +24,7 @@ export const alovaInstance = createAlova({
|
||||
},
|
||||
|
||||
responded: {
|
||||
onSuccess: async (response) => {
|
||||
onSuccess: async (response: AlovaXHRResponse) => {
|
||||
// if (response.status === 202) {
|
||||
// throw new Error('Wait'); // wifi scan in progress
|
||||
// } else
|
||||
|
||||
@@ -24,29 +24,15 @@ export const fetchLog = () => alovaInstance.Post('/rest/fetchLog');
|
||||
export const fetchLogES = () => alovaInstance.Get('/es/log');
|
||||
|
||||
// Get versions from github
|
||||
|
||||
// Alova 3 code...
|
||||
// export const getStableVersion = () =>
|
||||
// alovaInstanceGH.Get('latest', {
|
||||
// transform(response: { data: { name: string } }) {
|
||||
// return response.data.name.substring(1);
|
||||
// }
|
||||
// });
|
||||
// export const getDevVersion = () =>
|
||||
// alovaInstanceGH.Get('tags/latest', {
|
||||
// transform(response: { data: { name: string } }) {
|
||||
// return response.data.name.split(/\s+/).splice(-1)[0].substring(1);
|
||||
// }
|
||||
// });
|
||||
export const getStableVersion = () =>
|
||||
alovaInstanceGH.Get('latest', {
|
||||
transformData(response: { data: { name: string } }) {
|
||||
transform(response: { data: { name: string } }) {
|
||||
return response.data.name.substring(1);
|
||||
}
|
||||
});
|
||||
export const getDevVersion = () =>
|
||||
alovaInstanceGH.Get('tags/latest', {
|
||||
transformData(response: { data: { name: string } }) {
|
||||
transform(response: { data: { name: string } }) {
|
||||
return response.data.name.split(/\s+/).splice(-1)[0].substring(1);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -19,8 +19,7 @@ import {
|
||||
Table
|
||||
} from '@table-library/react-table-library/table';
|
||||
import { useTheme } from '@table-library/react-table-library/theme';
|
||||
import { updateState, useRequest } from 'alova';
|
||||
// import { updateState, useRequest } from 'alova/client'; // TODO replace when Alova 3 is released
|
||||
import { updateState, useRequest } from 'alova/client';
|
||||
import {
|
||||
BlockNavigation,
|
||||
ButtonRow,
|
||||
|
||||
@@ -39,8 +39,7 @@ import {
|
||||
} from '@table-library/react-table-library/table';
|
||||
import { useTheme } from '@table-library/react-table-library/theme';
|
||||
import { dialogStyle } from 'CustomTheme';
|
||||
import { useRequest } from 'alova';
|
||||
// import { useRequest } from 'alova/client' // TODO replace when Alova 3 is released
|
||||
import { useRequest } from 'alova/client';
|
||||
import RestartMonitor from 'app/status/RestartMonitor';
|
||||
import {
|
||||
BlockNavigation,
|
||||
@@ -111,25 +110,13 @@ const Customizations = () => {
|
||||
}
|
||||
);
|
||||
|
||||
// TODO Alova 3 code...
|
||||
// const { send: sendDeviceEntities } = useRequest(
|
||||
// (data: number) => readDeviceEntities(data),
|
||||
// {
|
||||
// initialData: [],
|
||||
// immediate: false
|
||||
// }
|
||||
// ).onSuccess((event) => {
|
||||
// setOriginalSettings(event.data);
|
||||
// });
|
||||
|
||||
const { send: sendDeviceEntities, onSuccess } = useRequest(
|
||||
const { send: sendDeviceEntities } = useRequest(
|
||||
(data: number) => readDeviceEntities(data),
|
||||
{
|
||||
initialData: [],
|
||||
immediate: false
|
||||
}
|
||||
);
|
||||
onSuccess((event) => {
|
||||
).onSuccess((event) => {
|
||||
setOriginalSettings(event.data);
|
||||
});
|
||||
|
||||
|
||||
@@ -56,8 +56,7 @@ import {
|
||||
import { useTheme } from '@table-library/react-table-library/theme';
|
||||
import type { Action, State } from '@table-library/react-table-library/types/common';
|
||||
import { dialogStyle } from 'CustomTheme';
|
||||
// import { useRequest } from 'alova/client' // TODO replace when Alova 3 is released
|
||||
import { useRequest } from 'alova';
|
||||
import { useRequest } from 'alova/client';
|
||||
import { ButtonRow, MessageBox, SectionContent, useLayoutTitle } from 'components';
|
||||
import { AuthenticatedContext } from 'contexts/authentication';
|
||||
import { useI18nContext } from 'i18n/i18n-react';
|
||||
|
||||
@@ -17,8 +17,7 @@ import {
|
||||
Typography
|
||||
} from '@mui/material';
|
||||
|
||||
// import { useRequest } from 'alova/client' // TODO replace when Alova 3 is released
|
||||
import { useRequest } from 'alova';
|
||||
import { useRequest } from 'alova/client';
|
||||
import { SectionContent, useLayoutTitle } from 'components';
|
||||
import { useI18nContext } from 'i18n/i18n-react';
|
||||
|
||||
@@ -29,44 +28,23 @@ const Help = () => {
|
||||
const { LL } = useI18nContext();
|
||||
useLayoutTitle(LL.HELP_OF(''));
|
||||
|
||||
const { send: getAPI, onSuccess: onGetAPI } = useRequest(
|
||||
(data: APIcall) => API(data),
|
||||
{
|
||||
immediate: false
|
||||
}
|
||||
);
|
||||
onGetAPI((event) => {
|
||||
const { send: getAPI } = useRequest((data: APIcall) => API(data), {
|
||||
immediate: false
|
||||
}).onSuccess((event) => {
|
||||
const anchor = document.createElement('a');
|
||||
anchor.href = URL.createObjectURL(
|
||||
new Blob([JSON.stringify(event.data, null, 2)], {
|
||||
type: 'text/plain'
|
||||
})
|
||||
);
|
||||
|
||||
anchor.download =
|
||||
'emsesp_' + event.sendArgs[0].device + '_' + event.sendArgs[0].entity + '.txt';
|
||||
'emsesp_' + event.args[0].device + '_' + event.args[0].entity + '.txt';
|
||||
anchor.click();
|
||||
URL.revokeObjectURL(anchor.href);
|
||||
toast.info(LL.DOWNLOAD_SUCCESSFUL());
|
||||
});
|
||||
|
||||
// Alova 3 code...
|
||||
// const { send: getAPI } = useRequest((data: APIcall) => API(data), {
|
||||
// immediate: false
|
||||
// }).onSuccess((event) => {
|
||||
// const anchor = document.createElement('a');
|
||||
// anchor.href = URL.createObjectURL(
|
||||
// new Blob([JSON.stringify(event.data, null, 2)], {
|
||||
// type: 'text/plain'
|
||||
// })
|
||||
// );
|
||||
//
|
||||
// anchor.download =
|
||||
// 'emsesp_' + event.args[0].device + '_' + event.args[0].entity + '.txt';
|
||||
// anchor.click();
|
||||
// URL.revokeObjectURL(anchor.href);
|
||||
// toast.info(LL.DOWNLOAD_SUCCESSFUL());
|
||||
// });
|
||||
|
||||
const callAPI = async (device: string, entity: string) => {
|
||||
await getAPI({ device, entity, id: 0 }).catch((error: Error) => {
|
||||
toast.error(error.message);
|
||||
|
||||
@@ -17,8 +17,7 @@ import {
|
||||
Table
|
||||
} from '@table-library/react-table-library/table';
|
||||
import { useTheme } from '@table-library/react-table-library/theme';
|
||||
import { updateState, useRequest } from 'alova';
|
||||
// import { updateState, useRequest } from 'alova/client'; // TODO replace when Alova 3 is released
|
||||
import { updateState, useRequest } from 'alova/client';
|
||||
import {
|
||||
BlockNavigation,
|
||||
ButtonRow,
|
||||
|
||||
@@ -18,8 +18,7 @@ import {
|
||||
Table
|
||||
} from '@table-library/react-table-library/table';
|
||||
import { useTheme } from '@table-library/react-table-library/theme';
|
||||
// import { updateState, useRequest } from 'alova/client'; // TODO replace when Alova 3 is released
|
||||
import { updateState, useRequest } from 'alova';
|
||||
import { updateState, useRequest } from 'alova/client';
|
||||
import {
|
||||
BlockNavigation,
|
||||
ButtonRow,
|
||||
|
||||
@@ -20,8 +20,7 @@ import {
|
||||
} from '@table-library/react-table-library/table';
|
||||
import { useTheme } from '@table-library/react-table-library/theme';
|
||||
import type { State } from '@table-library/react-table-library/types/common';
|
||||
// import { useRequest } from 'alova/client' // TODO replace when Alova 3 is released
|
||||
import { useRequest } from 'alova';
|
||||
import { useRequest } from 'alova/client';
|
||||
import { ButtonRow, SectionContent, useLayoutTitle } from 'components';
|
||||
import { AuthenticatedContext } from 'contexts/authentication';
|
||||
import { useI18nContext } from 'i18n/i18n-react';
|
||||
|
||||
@@ -18,8 +18,7 @@ import {
|
||||
|
||||
import { readHardwareStatus, restart } from 'api/system';
|
||||
|
||||
// import { useRequest } from 'alova/client' // TODO replace when Alova 3 is released
|
||||
import { useRequest } from 'alova';
|
||||
import { useRequest } from 'alova/client';
|
||||
import RestartMonitor from 'app/status/RestartMonitor';
|
||||
import type { ValidateFieldsError } from 'async-validator';
|
||||
import {
|
||||
@@ -82,15 +81,12 @@ const ApplicationSettings = () => {
|
||||
|
||||
const [fieldErrors, setFieldErrors] = useState<ValidateFieldsError>();
|
||||
|
||||
const {
|
||||
loading: processingBoard,
|
||||
send: readBoardProfile,
|
||||
onSuccess: onSuccessBoardProfile
|
||||
} = useRequest((boardProfile: string) => getBoardProfile(boardProfile), {
|
||||
immediate: false
|
||||
});
|
||||
|
||||
onSuccessBoardProfile((event) => {
|
||||
const { loading: processingBoard, send: readBoardProfile } = useRequest(
|
||||
(boardProfile: string) => getBoardProfile(boardProfile),
|
||||
{
|
||||
immediate: false
|
||||
}
|
||||
).onSuccess((event) => {
|
||||
const response = event.data as Settings;
|
||||
updateDataValue({
|
||||
...data,
|
||||
@@ -107,29 +103,6 @@ const ApplicationSettings = () => {
|
||||
});
|
||||
});
|
||||
|
||||
// TODO replace with Alova 3
|
||||
// const { loading: processingBoard, send: readBoardProfile } = useRequest(
|
||||
// (boardProfile: string) => getBoardProfile(boardProfile),
|
||||
// {
|
||||
// immediate: false
|
||||
// }
|
||||
// ).onSuccess((event) => {
|
||||
// const response = event.data as Settings;
|
||||
// updateDataValue({
|
||||
// ...data,
|
||||
// board_profile: response.board_profile,
|
||||
// led_gpio: response.led_gpio,
|
||||
// dallas_gpio: response.dallas_gpio,
|
||||
// rx_gpio: response.rx_gpio,
|
||||
// tx_gpio: response.tx_gpio,
|
||||
// pbutton_gpio: response.pbutton_gpio,
|
||||
// phy_type: response.phy_type,
|
||||
// eth_power: response.eth_power,
|
||||
// eth_phy_addr: response.eth_phy_addr,
|
||||
// eth_clock_mode: response.eth_clock_mode
|
||||
// });
|
||||
// });
|
||||
|
||||
const { send: restartCommand } = useRequest(restart(), {
|
||||
immediate: false
|
||||
});
|
||||
|
||||
@@ -7,8 +7,7 @@ import { Button, Checkbox, MenuItem } from '@mui/material';
|
||||
import * as NTPApi from 'api/ntp';
|
||||
import { readNTPSettings } from 'api/ntp';
|
||||
|
||||
// import { updateState } from 'alova/client'; // TODO replace when Alova 3 is released
|
||||
import { updateState } from 'alova';
|
||||
import { updateState } from 'alova/client';
|
||||
import type { ValidateFieldsError } from 'async-validator';
|
||||
import {
|
||||
BlockFormControlLabel,
|
||||
|
||||
@@ -24,8 +24,7 @@ import {
|
||||
import * as SystemApi from 'api/system';
|
||||
|
||||
import { dialogStyle } from 'CustomTheme';
|
||||
// import { useRequest } from 'alova/client' // TODO replace when Alova 3 is released
|
||||
import { useRequest } from 'alova';
|
||||
import { useRequest } from 'alova/client';
|
||||
import { SectionContent, useLayoutTitle } from 'components';
|
||||
import ListMenuItem from 'components/layout/ListMenuItem';
|
||||
import { useI18nContext } from 'i18n/i18n-react';
|
||||
|
||||
@@ -14,8 +14,7 @@ import {
|
||||
} from 'api/app';
|
||||
import { getDevVersion, getStableVersion } from 'api/system';
|
||||
|
||||
// import { useRequest } from 'alova/client' // TODO replace when Alova 3 is released
|
||||
import { useRequest } from 'alova';
|
||||
import { useRequest } from 'alova/client';
|
||||
import type { APIcall } from 'app/main/types';
|
||||
import {
|
||||
FormLoader,
|
||||
@@ -32,92 +31,22 @@ const UploadDownload = () => {
|
||||
const [restarting, setRestarting] = useState<boolean>();
|
||||
const [md5, setMd5] = useState<string>();
|
||||
|
||||
const { send: sendSettings, onSuccess: onSuccessGetSettings } = useRequest(
|
||||
getSettings(),
|
||||
{
|
||||
immediate: false
|
||||
}
|
||||
);
|
||||
const { send: sendCustomizations, onSuccess: onSuccessGetCustomizations } =
|
||||
useRequest(getCustomizations(), {
|
||||
immediate: false
|
||||
});
|
||||
const { send: sendEntities, onSuccess: onSuccessGetEntities } = useRequest(
|
||||
getEntities(),
|
||||
{
|
||||
immediate: false
|
||||
}
|
||||
);
|
||||
const { send: sendSchedule, onSuccess: onSuccessGetSchedule } = useRequest(
|
||||
getSchedule(),
|
||||
{
|
||||
immediate: false
|
||||
}
|
||||
);
|
||||
const { send: getAPI, onSuccess: onGetAPI } = useRequest(
|
||||
(data: APIcall) => API(data),
|
||||
{
|
||||
immediate: false
|
||||
}
|
||||
);
|
||||
|
||||
// Alova 3 code...
|
||||
// const {
|
||||
// loading: isUploading,
|
||||
// uploading: progress,
|
||||
// send: sendUpload,
|
||||
// abort: cancelUpload
|
||||
// } = useRequest(SystemApi.uploadFile, {
|
||||
// immediate: false
|
||||
// }).onSuccess(({ data }) => {
|
||||
// if (data) {
|
||||
// setMd5(data.md5);
|
||||
// toast.success(LL.UPLOAD() + ' MD5 ' + LL.SUCCESSFUL());
|
||||
// } else {
|
||||
// setRestarting(true);
|
||||
// }
|
||||
// });
|
||||
const {
|
||||
loading: isUploading,
|
||||
uploading: progress,
|
||||
send: sendUpload,
|
||||
onSuccess: onSuccessUpload,
|
||||
abort: cancelUpload
|
||||
} = useRequest(SystemApi.uploadFile, {
|
||||
immediate: false
|
||||
});
|
||||
|
||||
onSuccessUpload(({ data }) => {
|
||||
}).onSuccess(({ data }) => {
|
||||
if (data) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
||||
setMd5(data.md5);
|
||||
setMd5(data.md5 as string);
|
||||
toast.success(LL.UPLOAD() + ' MD5 ' + LL.SUCCESSFUL());
|
||||
} else {
|
||||
setRestarting(true);
|
||||
}
|
||||
});
|
||||
onSuccessGetSettings((event) => {
|
||||
saveFile(event.data, 'settings.json');
|
||||
});
|
||||
onSuccessGetCustomizations((event) => {
|
||||
saveFile(event.data, 'customizations.json');
|
||||
});
|
||||
onSuccessGetEntities((event) => {
|
||||
saveFile(event.data, 'entities.json');
|
||||
});
|
||||
onSuccessGetSchedule((event) => {
|
||||
saveFile(event.data, 'schedule.json');
|
||||
});
|
||||
onGetAPI((event) => {
|
||||
saveFile(
|
||||
event.data,
|
||||
event.sendArgs[0].device + '_' + event.sendArgs[0].entity + '.txt'
|
||||
);
|
||||
});
|
||||
|
||||
// TODO Alova 3 code...
|
||||
/*
|
||||
|
||||
const { send: sendSettings } = useRequest(getSettings(), {
|
||||
immediate: false
|
||||
}).onSuccess((event) => {
|
||||
@@ -151,8 +80,6 @@ const UploadDownload = () => {
|
||||
);
|
||||
});
|
||||
|
||||
*/
|
||||
|
||||
const {
|
||||
data: data,
|
||||
send: loadData,
|
||||
|
||||
@@ -25,8 +25,7 @@ import {
|
||||
import * as NetworkApi from 'api/network';
|
||||
import * as SystemApi from 'api/system';
|
||||
|
||||
import { updateState, useRequest } from 'alova';
|
||||
// import { updateState, useRequest } from 'alova/client'; // TODO replace when Alova 3 is released
|
||||
import { updateState, useRequest } from 'alova/client';
|
||||
import type { ValidateFieldsError } from 'async-validator';
|
||||
import {
|
||||
BlockFormControlLabel,
|
||||
|
||||
@@ -5,8 +5,7 @@ import { Button } from '@mui/material';
|
||||
|
||||
import * as NetworkApi from 'api/network';
|
||||
|
||||
// import { updateState, useRequest } from 'alova/client'; // TODO replace when Alova 3 is released
|
||||
import { updateState, useRequest } from 'alova';
|
||||
import { updateState, useRequest } from 'alova/client';
|
||||
import { ButtonRow, FormLoader, SectionContent } from 'components';
|
||||
import { useI18nContext } from 'i18n/i18n-react';
|
||||
|
||||
@@ -21,18 +20,22 @@ const WiFiNetworkScanner = () => {
|
||||
const [errorMessage, setErrorMessage] = useState<string>();
|
||||
|
||||
// is called on page load to start network scan
|
||||
|
||||
const { send: scanNetworks, onComplete: onCompleteScanNetworks } = useRequest(
|
||||
NetworkApi.scanNetworks
|
||||
const { send: scanNetworks } = useRequest(NetworkApi.scanNetworks).onComplete(
|
||||
() => {
|
||||
pollCount.current = 0;
|
||||
setErrorMessage(undefined);
|
||||
void updateState(NetworkApi.listNetworks(), () => undefined);
|
||||
void getNetworkList();
|
||||
}
|
||||
);
|
||||
const {
|
||||
data: networkList,
|
||||
send: getNetworkList,
|
||||
onSuccess: onSuccessNetworkList
|
||||
} = useRequest(NetworkApi.listNetworks, {
|
||||
immediate: false
|
||||
});
|
||||
onSuccessNetworkList((event) => {
|
||||
|
||||
const { data: networkList, send: getNetworkList } = useRequest(
|
||||
NetworkApi.listNetworks,
|
||||
{
|
||||
immediate: false
|
||||
}
|
||||
).onSuccess((event) => {
|
||||
// is called when network scan is completed
|
||||
if (!event.data) {
|
||||
const completedPollCount = pollCount.current + 1;
|
||||
if (completedPollCount < NUM_POLLS) {
|
||||
@@ -45,42 +48,6 @@ const WiFiNetworkScanner = () => {
|
||||
}
|
||||
});
|
||||
|
||||
onCompleteScanNetworks(() => {
|
||||
pollCount.current = 0;
|
||||
setErrorMessage(undefined);
|
||||
updateState('listNetworks', () => undefined);
|
||||
void getNetworkList();
|
||||
});
|
||||
|
||||
// Alova 3 code...
|
||||
// const { send: scanNetworks } = useRequest(NetworkApi.scanNetworks).onComplete(
|
||||
// () => {
|
||||
// pollCount.current = 0;
|
||||
// setErrorMessage(undefined);
|
||||
// void updateState(NetworkApi.listNetworks(), () => undefined);
|
||||
// void getNetworkList();
|
||||
// }
|
||||
// );
|
||||
|
||||
// const { data: networkList, send: getNetworkList } = useRequest(
|
||||
// NetworkApi.listNetworks,
|
||||
// {
|
||||
// immediate: false
|
||||
// }
|
||||
// ).onSuccess((event) => {
|
||||
// // is called when network scan is completed
|
||||
// if (!event.data) {
|
||||
// const completedPollCount = pollCount.current + 1;
|
||||
// if (completedPollCount < NUM_POLLS) {
|
||||
// pollCount.current = completedPollCount;
|
||||
// setTimeout(getNetworkList, POLLING_FREQUENCY);
|
||||
// } else {
|
||||
// setErrorMessage(LL.PROBLEM_LOADING());
|
||||
// pollCount.current = 0;
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
|
||||
const renderNetworkScanner = () => {
|
||||
if (!networkList) {
|
||||
return (
|
||||
|
||||
@@ -16,8 +16,7 @@ import {
|
||||
import * as SecurityApi from 'api/security';
|
||||
|
||||
import { dialogStyle } from 'CustomTheme';
|
||||
// import { useRequest } from 'alova/client' // TODO replace when Alova 3 is released
|
||||
import { useRequest } from 'alova';
|
||||
import { useRequest } from 'alova/client';
|
||||
import { MessageBox } from 'components';
|
||||
import { useI18nContext } from 'i18n/i18n-react';
|
||||
|
||||
|
||||
@@ -16,8 +16,7 @@ import type { Theme } from '@mui/material';
|
||||
|
||||
import * as APApi from 'api/ap';
|
||||
|
||||
// import { useRequest } from 'alova/client' // TODO replace when Alova 3 is released
|
||||
import { useRequest } from 'alova';
|
||||
import { useRequest } from 'alova/client';
|
||||
import { ButtonRow, FormLoader, SectionContent, useLayoutTitle } from 'components';
|
||||
import { useI18nContext } from 'i18n/i18n-react';
|
||||
import type { APStatusType } from 'types';
|
||||
|
||||
@@ -13,8 +13,7 @@ import {
|
||||
Table
|
||||
} from '@table-library/react-table-library/table';
|
||||
import { useTheme as tableTheme } from '@table-library/react-table-library/theme';
|
||||
// import { useRequest } from 'alova/client' // TODO replace when Alova 3 is released
|
||||
import { useRequest } from 'alova';
|
||||
import { useRequest } from 'alova/client';
|
||||
import { ButtonRow, FormLoader, SectionContent, useLayoutTitle } from 'components';
|
||||
import { useI18nContext } from 'i18n/i18n-react';
|
||||
import type { Translation } from 'i18n/i18n-types';
|
||||
|
||||
@@ -20,8 +20,7 @@ import {
|
||||
|
||||
import * as SystemApi from 'api/system';
|
||||
|
||||
// import { useRequest } from 'alova/client' // TODO replace when Alova 3 is released
|
||||
import { useRequest } from 'alova';
|
||||
import { useRequest } from 'alova/client';
|
||||
import { ButtonRow, FormLoader, SectionContent, useLayoutTitle } from 'components';
|
||||
import { useI18nContext } from 'i18n/i18n-react';
|
||||
|
||||
|
||||
@@ -17,8 +17,7 @@ import type { Theme } from '@mui/material';
|
||||
|
||||
import * as MqttApi from 'api/mqtt';
|
||||
|
||||
// import { useRequest } from 'alova/client' // TODO replace when Alova 3 is released
|
||||
import { useRequest } from 'alova';
|
||||
import { useRequest } from 'alova/client';
|
||||
import { ButtonRow, FormLoader, SectionContent, useLayoutTitle } from 'components';
|
||||
import { useI18nContext } from 'i18n/i18n-react';
|
||||
import type { MqttStatusType } from 'types';
|
||||
|
||||
@@ -29,8 +29,7 @@ import type { Theme } from '@mui/material';
|
||||
import * as NTPApi from 'api/ntp';
|
||||
|
||||
import { dialogStyle } from 'CustomTheme';
|
||||
// import { useRequest } from 'alova/client' // TODO replace when Alova 3 is released
|
||||
import { useRequest } from 'alova';
|
||||
import { useRequest } from 'alova/client';
|
||||
import { ButtonRow, FormLoader, SectionContent, useLayoutTitle } from 'components';
|
||||
import { useI18nContext } from 'i18n/i18n-react';
|
||||
import type { NTPStatusType, Time } from 'types';
|
||||
|
||||
@@ -20,8 +20,7 @@ import type { Theme } from '@mui/material';
|
||||
|
||||
import * as NetworkApi from 'api/network';
|
||||
|
||||
// import { useRequest } from 'alova/client' // TODO replace when Alova 3 is released
|
||||
import { useRequest } from 'alova';
|
||||
import { useRequest } from 'alova/client';
|
||||
import { ButtonRow, FormLoader, SectionContent, useLayoutTitle } from 'components';
|
||||
import { useI18nContext } from 'i18n/i18n-react';
|
||||
import type { NetworkStatusType } from 'types';
|
||||
|
||||
@@ -2,8 +2,7 @@ import { useEffect, useRef, useState } from 'react';
|
||||
|
||||
import * as SystemApi from 'api/system';
|
||||
|
||||
// import { useRequest } from 'alova/client' // TODO replace when Alova 3 is released
|
||||
import { useRequest } from 'alova';
|
||||
import { useRequest } from 'alova/client';
|
||||
import { FormLoader } from 'components';
|
||||
import { useI18nContext } from 'i18n/i18n-react';
|
||||
|
||||
|
||||
@@ -34,8 +34,7 @@ import {
|
||||
import * as SystemApi from 'api/system';
|
||||
|
||||
import { dialogStyle } from 'CustomTheme';
|
||||
// import { useRequest } from 'alova/client' // TODO replace when Alova 3 is released
|
||||
import { useRequest } from 'alova';
|
||||
import { useRequest } from 'alova/client';
|
||||
import { busConnectionStatus } from 'app/main/types';
|
||||
import { FormLoader, SectionContent, useLayoutTitle } from 'components';
|
||||
import ListMenuItem from 'components/layout/ListMenuItem';
|
||||
|
||||
@@ -16,9 +16,7 @@ import {
|
||||
import * as SystemApi from 'api/system';
|
||||
import { fetchLogES } from 'api/system';
|
||||
|
||||
// import { useRequest, useSSE } from 'alova/client'; // TODO replace when Alova 3 is released
|
||||
import { useSSE } from '@alova/scene-react';
|
||||
import { useRequest } from 'alova';
|
||||
import { useRequest, useSSE } from 'alova/client';
|
||||
import {
|
||||
BlockFormControlLabel,
|
||||
BlockNavigation,
|
||||
|
||||
@@ -6,8 +6,7 @@ import { toast } from 'react-toastify';
|
||||
import { ACCESS_TOKEN } from 'api/endpoints';
|
||||
|
||||
import * as AuthenticationApi from 'components/routing/authentication';
|
||||
// import { useRequest } from 'alova/client' // TODO replace when Alova 3 is released
|
||||
import { useRequest } from 'alova';
|
||||
import { useRequest } from 'alova/client';
|
||||
import { LoadingSpinner } from 'components';
|
||||
import { verifyAuthorization } from 'components/routing/authentication';
|
||||
import { useI18nContext } from 'i18n/i18n-react';
|
||||
@@ -42,7 +41,6 @@ const Authentication: FC<RequiredChildrenProps> = ({ children }) => {
|
||||
AuthenticationApi.clearAccessToken();
|
||||
setMe(undefined);
|
||||
if (doRedirect) {
|
||||
// navigate('/');
|
||||
redirect('/');
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,127 +1,43 @@
|
||||
// TODO Alova 3 code....
|
||||
// import { useState } from 'react';
|
||||
// import { useBlocker } from 'react-router-dom';
|
||||
// import { toast } from 'react-toastify';
|
||||
// import type { AlovaGenerics, Method } from 'alova';
|
||||
// import { useRequest } from 'alova/client';
|
||||
// import { useI18nContext } from 'i18n/i18n-react';
|
||||
// export interface RestRequestOptions<D> {
|
||||
// read: () => Method<AlovaGenerics>;
|
||||
// update: (value: D) => Method<AlovaGenerics>;
|
||||
// }
|
||||
// export const useRest = <D>({ read, update }: RestRequestOptions<D>) => {
|
||||
// const { LL } = useI18nContext();
|
||||
// const [errorMessage, setErrorMessage] = useState<string>();
|
||||
// const [restartNeeded, setRestartNeeded] = useState<boolean>(false);
|
||||
// const [origData, setOrigData] = useState<D>();
|
||||
// const [dirtyFlags, setDirtyFlags] = useState<string[]>([]);
|
||||
// const blocker = useBlocker(dirtyFlags.length !== 0);
|
||||
// const {
|
||||
// data,
|
||||
// send: readData,
|
||||
// update: updateData
|
||||
// } = useRequest(read()).onComplete((event) => {
|
||||
// setOrigData(event.data as D);
|
||||
// });
|
||||
// const { loading: saving, send: writeData } = useRequest(
|
||||
// (newData: D) => update(newData),
|
||||
// { immediate: false }
|
||||
// ).onSuccess(() => {
|
||||
// toast.success(LL.UPDATED_OF(LL.SETTINGS(1)));
|
||||
// setDirtyFlags([]);
|
||||
// });
|
||||
// const updateDataValue = (new_data: D) => {
|
||||
// updateData({ data: new_data });
|
||||
// };
|
||||
// const loadData = async () => {
|
||||
// setDirtyFlags([]);
|
||||
// setErrorMessage(undefined);
|
||||
// await readData().catch((error: Error) => {
|
||||
// toast.error(error.message);
|
||||
// setErrorMessage(error.message);
|
||||
// });
|
||||
// };
|
||||
// const saveData = async () => {
|
||||
// if (!data) {
|
||||
// return;
|
||||
// }
|
||||
// setRestartNeeded(false);
|
||||
// setErrorMessage(undefined);
|
||||
// setDirtyFlags([]);
|
||||
// setOrigData(data as D);
|
||||
// await writeData(data as D).catch((error: Error) => {
|
||||
// if (error.message === 'Reboot required') {
|
||||
// setRestartNeeded(true);
|
||||
// } else {
|
||||
// toast.error(error.message);
|
||||
// setErrorMessage(error.message);
|
||||
// }
|
||||
// });
|
||||
// };
|
||||
// return {
|
||||
// loadData,
|
||||
// saveData,
|
||||
// saving: saving as boolean,
|
||||
// updateDataValue,
|
||||
// data: data as D, // Explicitly define the type of 'data'
|
||||
// origData: origData as D, // Explicitly define the type of 'origData' to 'D'
|
||||
// dirtyFlags,
|
||||
// setDirtyFlags,
|
||||
// setOrigData,
|
||||
// blocker,
|
||||
// errorMessage,
|
||||
// restartNeeded
|
||||
// } as const;
|
||||
// };
|
||||
import { useState } from 'react';
|
||||
import { useBlocker } from 'react-router-dom';
|
||||
import { toast } from 'react-toastify';
|
||||
|
||||
import { type Method, useRequest } from 'alova';
|
||||
import type { AlovaGenerics, Method } from 'alova';
|
||||
import { useRequest } from 'alova/client';
|
||||
import { useI18nContext } from 'i18n/i18n-react';
|
||||
|
||||
export interface RestRequestOptions<D> {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
read: () => Method<any, any, any, any, any, any, any>;
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
update: (value: D) => Method<any, any, any, any, any, any, any>;
|
||||
read: () => Method<AlovaGenerics>;
|
||||
update: (value: D) => Method<AlovaGenerics>;
|
||||
}
|
||||
|
||||
export const useRest = <D>({ read, update }: RestRequestOptions<D>) => {
|
||||
const { LL } = useI18nContext();
|
||||
|
||||
const [errorMessage, setErrorMessage] = useState<string>();
|
||||
const [restartNeeded, setRestartNeeded] = useState<boolean>(false);
|
||||
const [origData, setOrigData] = useState<D>();
|
||||
|
||||
const [dirtyFlags, setDirtyFlags] = useState<string[]>([]);
|
||||
const blocker = useBlocker(dirtyFlags.length !== 0);
|
||||
|
||||
const {
|
||||
data,
|
||||
send: readData,
|
||||
update: updateData,
|
||||
onComplete: onReadComplete
|
||||
} = useRequest(read());
|
||||
update: updateData
|
||||
} = useRequest(read()).onComplete((event) => {
|
||||
setOrigData(event.data as D);
|
||||
});
|
||||
|
||||
const {
|
||||
loading: saving,
|
||||
send: writeData,
|
||||
onSuccess: onWriteSuccess
|
||||
} = useRequest((newData: D) => update(newData), { immediate: false });
|
||||
|
||||
const updateDataValue = (new_data: D) => {
|
||||
updateData({ data: new_data });
|
||||
};
|
||||
|
||||
onWriteSuccess(() => {
|
||||
const { loading: saving, send: writeData } = useRequest(
|
||||
(newData: D) => update(newData),
|
||||
{ immediate: false }
|
||||
).onSuccess(() => {
|
||||
toast.success(LL.UPDATED_OF(LL.SETTINGS(1)));
|
||||
setDirtyFlags([]);
|
||||
});
|
||||
|
||||
onReadComplete((event) => {
|
||||
setOrigData(event.data as D);
|
||||
});
|
||||
const updateDataValue = (new_data: D) => {
|
||||
updateData({ data: new_data });
|
||||
};
|
||||
|
||||
const loadData = async () => {
|
||||
setDirtyFlags([]);
|
||||
@@ -149,7 +65,6 @@ export const useRest = <D>({ read, update }: RestRequestOptions<D>) => {
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
loadData,
|
||||
saveData,
|
||||
|
||||
Reference in New Issue
Block a user