Skip to content

Commit

Permalink
merging to sync - still need to fix system.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
proddy committed Jun 16, 2023
1 parent f58dbf6 commit 2ae45ec
Show file tree
Hide file tree
Showing 20 changed files with 171 additions and 150 deletions.
15 changes: 4 additions & 11 deletions interface/src/api/ap.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import { AXIOS, alovaInstance } from './endpoints';
import type { AxiosPromise } from 'axios';
import { alovaInstance } from './endpoints';

import type { APSettings, APStatus } from 'types';

export const readAPStatus = () => alovaInstance.Get<APStatus>('/apStatus');

// TODO change APSettings AXIOS to Alova
export function readAPSettings(): AxiosPromise<APSettings> {
return AXIOS.get('/apSettings');
}
export function updateAPSettings(apSettings: APSettings): AxiosPromise<APSettings> {
return AXIOS.post('/apSettings', apSettings);
}
export const readAPStatus = () => alovaInstance.Get<APStatus>('/rest/apStatus');
export const readAPSettings = () => alovaInstance.Get<APSettings>('/rest/apSettings');
export const updateAPSettings = (data: APSettings) => alovaInstance.Post<APSettings>('/rest/apSettings', data);
8 changes: 8 additions & 0 deletions interface/src/api/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ export const EVENT_SOURCE_ROOT = 'http://' + host + '/es/';
export const alovaInstance = createAlova({
statesHook: ReactHook,
timeout: 3000,
localCache: {
GET: {
mode: 'placeholder',
// expire: 60 * 10 * 1000
// see https://alova.js.org/learning/response-cache/#cache-replaceholder-mode
expire: 2000
}
},
requestAdapter: xhrRequestAdapter(),
beforeRequest(method) {
if (localStorage.getItem(ACCESS_TOKEN)) {
Expand Down
18 changes: 4 additions & 14 deletions interface/src/api/mqtt.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
import { AXIOS } from './endpoints';
import type { AxiosPromise } from 'axios';
import { alovaInstance } from './endpoints';
import type { MqttSettings, MqttStatus } from 'types';

// TODO move to alova
export function readMqttStatus(): AxiosPromise<MqttStatus> {
return AXIOS.get('/mqttStatus');
}

export function readMqttSettings(): AxiosPromise<MqttSettings> {
return AXIOS.get('/mqttSettings');
}

export function updateMqttSettings(mqttSettings: MqttSettings): AxiosPromise<MqttSettings> {
return AXIOS.post('/mqttSettings', mqttSettings);
}
export const readMqttStatus = () => alovaInstance.Get<MqttStatus>('/rest/mqttStatus');
export const readMqttSettings = () => alovaInstance.Get<MqttSettings>('/rest/mqttSettings');
export const updateMqttSettings = (data: MqttSettings) => alovaInstance.Post<MqttSettings>('/rest/mqttSettings', data);
30 changes: 8 additions & 22 deletions interface/src/api/network.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
import { AXIOS } from './endpoints';
import type { AxiosPromise } from 'axios';
import { alovaInstance } from './endpoints';

import type { WiFiNetworkList, NetworkSettings, NetworkStatus } from 'types';

// TODO move to alova
export function readNetworkStatus(): AxiosPromise<NetworkStatus> {
return AXIOS.get('/networkStatus');
}

export function scanNetworks(): AxiosPromise<void> {
return AXIOS.get('/scanNetworks');
}

export function listNetworks(): AxiosPromise<WiFiNetworkList> {
return AXIOS.get('/listNetworks');
}

export function readNetworkSettings(): AxiosPromise<NetworkSettings> {
return AXIOS.get('/networkSettings');
}

export function updateNetworkSettings(wifiSettings: NetworkSettings): AxiosPromise<NetworkSettings> {
return AXIOS.post('/networkSettings', wifiSettings);
}
export const readNetworkStatus = () => alovaInstance.Get<NetworkStatus>('/rest/networkStatus');
export const scanNetworks = () => alovaInstance.Get('/rest/scanNetworks');
export const listNetworks = () => alovaInstance.Get<WiFiNetworkList>('/rest/listNetworks');
export const readNetworkSettings = () =>
alovaInstance.Get<NetworkSettings>('/rest/networkSettings', { name: 'networkSettings' });
export const updateNetworkSettings = (wifiSettings: NetworkSettings) =>
alovaInstance.Post<NetworkSettings>('/rest/networkSettings', wifiSettings);
25 changes: 8 additions & 17 deletions interface/src/api/ntp.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import { AXIOS } from './endpoints';
import type { AxiosPromise } from 'axios';
import { alovaInstance } from './endpoints';
import type { NTPSettings, NTPStatus, Time } from 'types';

// TODO move to Alova
export function readNTPStatus(): AxiosPromise<NTPStatus> {
return AXIOS.get('/ntpStatus');
}
export const readNTPStatus = () => alovaInstance.Get<NTPStatus>('/rest/ntpStatus');
export const readNTPSettings = () =>
alovaInstance.Get<NTPSettings>('/rest/ntpSettings', {
name: 'ntpSettings'
});
export const updateNTPSettings = (data: NTPSettings) => alovaInstance.Post<NTPSettings>('/rest/ntpSettings', data);

export function readNTPSettings(): AxiosPromise<NTPSettings> {
return AXIOS.get('/ntpSettings');
}

export function updateNTPSettings(ntpSettings: NTPSettings): AxiosPromise<NTPSettings> {
return AXIOS.post('/ntpSettings', ntpSettings);
}

export function updateTime(time: Time): AxiosPromise<Time> {
return AXIOS.post('/time', time);
}
export const updateTime = (data: Time) => alovaInstance.Post<Time>('/rest/time', data);
15 changes: 11 additions & 4 deletions interface/src/api/system.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { AXIOS, AXIOS_BIN, startUploadFile } from './endpoints';
import { AXIOS, AXIOS_BIN, alovaInstance, startUploadFile } from './endpoints';
import type { FileUploadConfig } from './endpoints';
import type { AxiosPromise } from 'axios';

import type { OTASettings, SystemStatus, LogSettings, LogEntries } from 'types';

// TODO move to Alova
export function readSystemStatus(timeout?: number): AxiosPromise<SystemStatus> {
return AXIOS.get('/systemStatus', { timeout });
}
// TODO fix this next!

export const readSystemStatus = (timeout?: number) =>
alovaInstance.Get<SystemStatus>('/rest/systemStatus', {
params: { timeout }
});

// export function readSystemStatus(timeout?: number): AxiosPromise<SystemStatus> {
// return AXIOS.get('/systemStatus', { timeout });
// }

export function restart(): AxiosPromise<void> {
return AXIOS.post('/restart');
Expand Down
24 changes: 17 additions & 7 deletions interface/src/framework/ap/APSettingsForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,35 @@ import {

import { useI18nContext } from 'i18n/i18n-react';
import { APProvisionMode } from 'types';
import { numberValue, updateValueDirty, useRest } from 'utils';
import { numberValue, updateValueDirty, useRest2 } from 'utils';

import { createAPSettingsValidator, validate } from 'validators';

export const isAPEnabled = ({ provision_mode }: APSettings) =>
provision_mode === APProvisionMode.AP_MODE_ALWAYS || provision_mode === APProvisionMode.AP_MODE_DISCONNECTED;

const APSettingsForm: FC = () => {
const { loadData, saving, data, setData, origData, dirtyFlags, setDirtyFlags, blocker, saveData, errorMessage } =
useRest<APSettings>({
read: APApi.readAPSettings,
update: APApi.updateAPSettings
});
const {
loadData,
saving,
data,
updateDataValue,
origData,
dirtyFlags,
setDirtyFlags,
blocker,
saveData,
errorMessage
} = useRest2<APSettings>({
read: APApi.readAPSettings,
update: APApi.updateAPSettings
});

const { LL } = useI18nContext();

const [fieldErrors, setFieldErrors] = useState<ValidateFieldsError>();

const updateFormValue = updateValueDirty(origData, dirtyFlags, setDirtyFlags, setData);
const updateFormValue = updateValueDirty(origData, dirtyFlags, setDirtyFlags, updateDataValue);

const content = () => {
if (!data) {
Expand Down
24 changes: 17 additions & 7 deletions interface/src/framework/mqtt/MqttSettingsForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,32 @@ import {
BlockNavigation
} from 'components';
import { useI18nContext } from 'i18n/i18n-react';
import { numberValue, updateValueDirty, useRest } from 'utils';
import { numberValue, updateValueDirty, useRest2 } from 'utils';

import { createMqttSettingsValidator, validate } from 'validators';

const MqttSettingsForm: FC = () => {
const { loadData, saving, data, setData, origData, dirtyFlags, setDirtyFlags, blocker, saveData, errorMessage } =
useRest<MqttSettings>({
read: MqttApi.readMqttSettings,
update: MqttApi.updateMqttSettings
});
const {
loadData,
saving,
data,
updateDataValue,
origData,
dirtyFlags,
setDirtyFlags,
blocker,
saveData,
errorMessage
} = useRest2<MqttSettings>({
read: MqttApi.readMqttSettings,
update: MqttApi.updateMqttSettings
});

const { LL } = useI18nContext();

const [fieldErrors, setFieldErrors] = useState<ValidateFieldsError>();

const updateFormValue = updateValueDirty(origData, dirtyFlags, setDirtyFlags, setData);
const updateFormValue = updateValueDirty(origData, dirtyFlags, setDirtyFlags, updateDataValue);

const content = () => {
if (!data) {
Expand Down
7 changes: 3 additions & 4 deletions interface/src/framework/mqtt/MqttStatusForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import RefreshIcon from '@mui/icons-material/Refresh';
import ReportIcon from '@mui/icons-material/Report';
import SpeakerNotesOffIcon from '@mui/icons-material/SpeakerNotesOff';
import { Avatar, Button, Divider, List, ListItem, ListItemAvatar, ListItemText, useTheme } from '@mui/material';
import { useRequest } from 'alova';
import type { Theme } from '@mui/material';
import type { FC } from 'react';

Expand All @@ -12,7 +13,6 @@ import * as MqttApi from 'api/mqtt';
import { ButtonRow, FormLoader, SectionContent } from 'components';
import { useI18nContext } from 'i18n/i18n-react';
import { MqttDisconnectReason } from 'types';
import { useRest } from 'utils';

export const mqttStatusHighlight = ({ enabled, connected }: MqttStatus, theme: Theme) => {
if (!enabled) {
Expand All @@ -38,8 +38,7 @@ export const mqttQueueHighlight = ({ mqtt_queued }: MqttStatus, theme: Theme) =>
};

const MqttStatusForm: FC = () => {
// TODO replace with const { data: data, send: loadData, error } = useRequest(APApi.readAPStatus);
const { loadData, data, errorMessage } = useRest<MqttStatus>({ read: MqttApi.readMqttStatus });
const { data: data, send: loadData, error } = useRequest(MqttApi.readMqttStatus);

const { LL } = useI18nContext();

Expand Down Expand Up @@ -80,7 +79,7 @@ const MqttStatusForm: FC = () => {

const content = () => {
if (!data) {
return <FormLoader onRetry={loadData} errorMessage={errorMessage} />;
return <FormLoader onRetry={loadData} errorMessage={error?.message} />;
}

const renderConnectionStatus = () => (
Expand Down
32 changes: 18 additions & 14 deletions interface/src/framework/network/NetworkSettingsForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
InputAdornment,
TextField
} from '@mui/material';
import { updateState, useRequest } from 'alova';
import { useContext, useEffect, useState } from 'react';
import { toast } from 'react-toastify';
import RestartMonitor from '../system/RestartMonitor';
Expand All @@ -40,7 +41,7 @@ import {
} from 'components';
import { useI18nContext } from 'i18n/i18n-react';
import * as EMSESP from 'project/api';
import { numberValue, updateValueDirty, useRest } from 'utils';
import { numberValue, updateValueDirty, useRest2 } from 'utils';

import { validate } from 'validators';
import { createNetworkSettingsValidator } from 'validators/network';
Expand All @@ -52,30 +53,35 @@ const WiFiSettingsForm: FC = () => {

const [initialized, setInitialized] = useState(false);
const [restarting, setRestarting] = useState(false);

const {
loadData,
saving,
data,
setData,
updateDataValue,
origData,
dirtyFlags,
setDirtyFlags,
blocker,
saveData,
errorMessage,
restartNeeded
} = useRest<NetworkSettings>({
} = useRest2<NetworkSettings>({
read: NetworkApi.readNetworkSettings,
update: NetworkApi.updateNetworkSettings
});

const { send: restartCommand } = useRequest(EMSESP.restart(), {
immediate: false
});

useEffect(() => {
if (!initialized && data) {
if (selectedNetwork) {
setData({
updateState('networkSettings', (current_data) => ({
ssid: selectedNetwork.ssid,
password: '',
hostname: data?.hostname,
hostname: current_data?.hostname,
static_ip_config: false,
enableIPv6: false,
bandwidth20: false,
Expand All @@ -84,13 +90,13 @@ const WiFiSettingsForm: FC = () => {
enableMDNS: true,
enableCORS: false,
CORSOrigin: '*'
});
}));
}
setInitialized(true);
}
}, [initialized, setInitialized, data, setData, selectedNetwork]);
}, [initialized, setInitialized, data, selectedNetwork]);

const updateFormValue = updateValueDirty(origData, dirtyFlags, setDirtyFlags, setData);
const updateFormValue = updateValueDirty(origData, dirtyFlags, setDirtyFlags, updateDataValue);

const [fieldErrors, setFieldErrors] = useState<ValidateFieldsError>();

Expand All @@ -112,12 +118,10 @@ const WiFiSettingsForm: FC = () => {
};

const restart = async () => {
try {
await EMSESP.restart();
setRestarting(true);
} catch (error) {
toast.error(LL.PROBLEM_UPDATING());
}
await restartCommand().catch((error) => {
toast.error(error.message);
});
setRestarting(true);
};

return (
Expand Down
7 changes: 3 additions & 4 deletions interface/src/framework/network/NetworkStatusForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import SettingsInputAntennaIcon from '@mui/icons-material/SettingsInputAntenna';
import SettingsInputComponentIcon from '@mui/icons-material/SettingsInputComponent';
import WifiIcon from '@mui/icons-material/Wifi';
import { Avatar, Button, Divider, List, ListItem, ListItemAvatar, ListItemText, useTheme } from '@mui/material';
import { useRequest } from 'alova';
import type { Theme } from '@mui/material';
import type { FC } from 'react';

Expand All @@ -15,7 +16,6 @@ import { ButtonRow, FormLoader, SectionContent } from 'components';

import { useI18nContext } from 'i18n/i18n-react';
import { NetworkConnectionStatus } from 'types';
import { useRest } from 'utils';

const isConnected = ({ status }: NetworkStatus) =>
status === NetworkConnectionStatus.WIFI_STATUS_CONNECTED ||
Expand Down Expand Up @@ -59,8 +59,7 @@ const IPs = (status: NetworkStatus) => {
};

const NetworkStatusForm: FC = () => {
// TODO replace with const { data: data, send: loadData, error } = useRequest(APApi.readAPStatus);
const { loadData, data, errorMessage } = useRest<NetworkStatus>({ read: NetworkApi.readNetworkStatus });
const { data: data, send: loadData, error } = useRequest(NetworkApi.readNetworkStatus);

const { LL } = useI18nContext();

Expand Down Expand Up @@ -91,7 +90,7 @@ const NetworkStatusForm: FC = () => {

const content = () => {
if (!data) {
return <FormLoader onRetry={loadData} errorMessage={errorMessage} />;
return <FormLoader onRetry={loadData} errorMessage={error?.message} />;
}

return (
Expand Down
Loading

0 comments on commit 2ae45ec

Please sign in to comment.