api refactoring

This commit is contained in:
proddy
2024-08-09 14:35:33 +02:00
parent 93ed502e57
commit 251b0ea287
24 changed files with 134 additions and 150 deletions

View File

@@ -18,7 +18,6 @@ import {
useLayoutTitle
} from 'components';
import { useI18nContext } from 'i18n/i18n-react';
import { range } from 'lodash-es';
import type { APSettingsType } from 'types';
import { APProvisionMode } from 'types';
import { numberValue, updateValueDirty, useRest } from 'utils';
@@ -73,6 +72,11 @@ const APSettings = () => {
}
};
// no lodash - https://asleepace.com/blog/typescript-range-without-a-loop/
function range(a: number, b: number): number[] {
return a < b ? [a, ...range(a + 1, b)] : [b];
}
return (
<>
<ValidatedTextField

View File

@@ -16,7 +16,7 @@ import {
Typography
} from '@mui/material';
import * as SystemApi from 'api/system';
import { readHardwareStatus, restart } from 'api/system';
import { useRequest } from 'alova/client';
import RestartMonitor from 'app/status/RestartMonitor';
@@ -35,7 +35,7 @@ import { useI18nContext } from 'i18n/i18n-react';
import { numberValue, updateValueDirty, useRest } from 'utils';
import { validate } from 'validators';
import * as EMSESP from '../main/api';
import { getBoardProfile, readSettings, writeSettings } from '../../api/app';
import { BOARD_PROFILES } from '../main/types';
import type { Settings } from '../main/types';
import { createSettingsValidator } from '../main/validators';
@@ -49,7 +49,7 @@ export function boardProfileSelectItems() {
}
const ApplicationSettings = () => {
const { data: hardwareData } = useRequest(SystemApi.readHardwareStatus, {
const { data: hardwareData } = useRequest(readHardwareStatus, {
initialData: { psram: false }
});
@@ -66,8 +66,8 @@ const ApplicationSettings = () => {
errorMessage,
restartNeeded
} = useRest<Settings>({
read: EMSESP.readSettings,
update: EMSESP.writeSettings
read: readSettings,
update: writeSettings
});
const [restarting, setRestarting] = useState<boolean>();
@@ -84,7 +84,7 @@ const ApplicationSettings = () => {
const [fieldErrors, setFieldErrors] = useState<ValidateFieldsError>();
const { loading: processingBoard, send: readBoardProfile } = useRequest(
(boardProfile: string) => EMSESP.getBoardProfile(boardProfile),
(boardProfile: string) => getBoardProfile(boardProfile),
{
immediate: false
}
@@ -105,7 +105,7 @@ const ApplicationSettings = () => {
});
});
const { send: restartCommand } = useRequest(SystemApi.restart(), {
const { send: restartCommand } = useRequest(restart(), {
immediate: false
});

View File

@@ -5,8 +5,15 @@ import DownloadIcon from '@mui/icons-material/GetApp';
import { Box, Button, Divider, Link, Typography } from '@mui/material';
import * as SystemApi from 'api/system';
import {
API,
getCustomizations,
getEntities,
getSchedule,
getSettings
} from 'api/app';
import { getDevVersion, getStableVersion } from 'api/system';
import * as EMSESP from 'app/main/api';
import { useRequest } from 'alova/client';
import type { APIcall } from 'app/main/types';
import {
@@ -24,31 +31,31 @@ const UploadDownload = () => {
const [restarting, setRestarting] = useState<boolean>();
const [md5, setMd5] = useState<string>();
const { send: getSettings } = useRequest(EMSESP.getSettings(), {
const { send: sendSettings } = useRequest(getSettings(), {
immediate: false
}).onSuccess((event) => {
saveFile(event.data, 'settings.json');
});
const { send: getCustomizations } = useRequest(EMSESP.getCustomizations(), {
const { send: sendCustomizations } = useRequest(getCustomizations(), {
immediate: false
}).onSuccess((event) => {
saveFile(event.data, 'customizations.json');
});
const { send: getEntities } = useRequest(EMSESP.getEntities(), {
const { send: sendEntities } = useRequest(getEntities(), {
immediate: false
}).onSuccess((event) => {
saveFile(event.data, 'entities.json');
});
const { send: getSchedule } = useRequest(EMSESP.getSchedule(), {
const { send: sendSchedule } = useRequest(getSchedule(), {
immediate: false
}).onSuccess((event) => {
saveFile(event.data, 'schedule.json');
});
const { send: getAPI } = useRequest((data: APIcall) => EMSESP.API(data), {
const { send: getAPI } = useRequest((data: APIcall) => API(data), {
immediate: false
}).onSuccess((event) => {
saveFile(
@@ -64,8 +71,8 @@ const UploadDownload = () => {
} = useRequest(SystemApi.readHardwareStatus);
// called immediately to get the latest version, on page load
const { data: latestVersion } = useRequest(SystemApi.getStableVersion);
const { data: latestDevVersion } = useRequest(SystemApi.getDevVersion);
const { data: latestVersion } = useRequest(getStableVersion);
const { data: latestDevVersion } = useRequest(getDevVersion);
const STABLE_URL = 'https://github.com/emsesp/EMS-ESP32/releases/download/';
const STABLE_RELNOTES_URL =
@@ -131,25 +138,25 @@ const UploadDownload = () => {
};
const downloadSettings = async () => {
await getSettings().catch((error: Error) => {
await sendSettings().catch((error: Error) => {
toast.error(error.message);
});
};
const downloadCustomizations = async () => {
await getCustomizations().catch((error: Error) => {
await sendCustomizations().catch((error: Error) => {
toast.error(error.message);
});
};
const downloadEntities = async () => {
await getEntities().catch((error: Error) => {
await sendEntities().catch((error: Error) => {
toast.error(error.message);
});
};
const downloadSchedule = async () => {
await getSchedule().catch((error: Error) => {
await sendSchedule().catch((error: Error) => {
toast.error(error.message);
});
};