Skip to content

Commit

Permalink
alova - update comments
Browse files Browse the repository at this point in the history
  • Loading branch information
proddy committed Jun 15, 2023
1 parent adc4760 commit f58dbf6
Show file tree
Hide file tree
Showing 20 changed files with 117 additions and 137 deletions.
1 change: 1 addition & 0 deletions interface/src/AuthenticatedRouting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const AuthenticatedRouting: FC = () => {
);

useEffect(() => {
// TODO how to replace AXIOS.interceptors.response.use ???
const axiosHandlerId = AXIOS.interceptors.response.use((response) => response, handleApiResponseError);
return () => AXIOS.interceptors.response.eject(axiosHandlerId);
}, [handleApiResponseError]);
Expand Down
2 changes: 1 addition & 1 deletion interface/src/api/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import type { Me, SignInRequest, SignInResponse } from 'types';
export const SIGN_IN_PATHNAME = 'loginPathname';
export const SIGN_IN_SEARCH = 'loginSearch';

// TODO move to Alova
export function verifyAuthorization(): AxiosPromise<void> {
return AXIOS.get('/verifyAuthorization');
}

export function signIn(request: SignInRequest): AxiosPromise<SignInResponse> {
return AXIOS.post('/signIn', request);
}
Expand Down
13 changes: 5 additions & 8 deletions interface/src/api/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,20 @@ import ReactHook from 'alova/react';
import axios from 'axios';
import { unpack } from '../api/unpack';

// TODO axios can be removed
import type { AxiosPromise, CancelToken, AxiosProgressEvent } from 'axios';

export const REST_BASE_URL = '/rest/';
export const API_BASE_URL = '/api/';

export const ACCESS_TOKEN = 'access_token';

const location = window.location;
const webProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
export const WS_BASE_URL = '/ws/';
export const ES_BASE_URL = '/es/';
export const WEB_SOCKET_ROOT = webProtocol + '//' + location.host + WS_BASE_URL;
export const EVENT_SOURCE_ROOT = location.protocol + '//' + location.host + ES_BASE_URL;
const host = window.location.host;
export const WEB_SOCKET_ROOT = 'ws://' + host + '/ws/';
export const EVENT_SOURCE_ROOT = 'http://' + host + '/es/';

export const alovaInstance = createAlova({
statesHook: ReactHook,
timeout: 5000,
timeout: 3000,
requestAdapter: xhrRequestAdapter(),
beforeRequest(method) {
if (localStorage.getItem(ACCESS_TOKEN)) {
Expand Down
1 change: 1 addition & 0 deletions interface/src/api/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { AxiosPromise } from 'axios';

import type { Features } from 'types';

// TODO move to Alova
export function readFeatures(): AxiosPromise<Features> {
return AXIOS.get('/features');
}
1 change: 1 addition & 0 deletions interface/src/api/mqtt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AXIOS } from './endpoints';
import type { AxiosPromise } from 'axios';
import type { MqttSettings, MqttStatus } from 'types';

// TODO move to alova
export function readMqttStatus(): AxiosPromise<MqttStatus> {
return AXIOS.get('/mqttStatus');
}
Expand Down
1 change: 1 addition & 0 deletions interface/src/api/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { AxiosPromise } from 'axios';

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

// TODO move to alova
export function readNetworkStatus(): AxiosPromise<NetworkStatus> {
return AXIOS.get('/networkStatus');
}
Expand Down
1 change: 1 addition & 0 deletions interface/src/api/ntp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AXIOS } from './endpoints';
import type { AxiosPromise } from 'axios';
import type { NTPSettings, NTPStatus, Time } from 'types';

// TODO move to Alova
export function readNTPStatus(): AxiosPromise<NTPStatus> {
return AXIOS.get('/ntpStatus');
}
Expand Down
1 change: 1 addition & 0 deletions interface/src/api/security.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { AxiosPromise } from 'axios';

import type { SecuritySettings, Token } from 'types';

// TODO move to Alova
export function readSecuritySettings(): AxiosPromise<SecuritySettings> {
return AXIOS.get('/securitySettings');
}
Expand Down
1 change: 1 addition & 0 deletions interface/src/api/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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 });
}
Expand Down
2 changes: 1 addition & 1 deletion interface/src/framework/mqtt/MqttStatusForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +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());
// TODO replace with const { data: data, send: loadData, error } = useRequest(APApi.readAPStatus);
const { loadData, data, errorMessage } = useRest<MqttStatus>({ read: MqttApi.readMqttStatus });

const { LL } = useI18nContext();
Expand Down
2 changes: 1 addition & 1 deletion interface/src/framework/network/NetworkStatusForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const IPs = (status: NetworkStatus) => {
};

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

const { LL } = useI18nContext();
Expand Down
2 changes: 1 addition & 1 deletion interface/src/framework/ntp/NTPStatusForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const ntpStatusHighlight = ({ status }: NTPStatus, theme: Theme) => {
};

const NTPStatusForm: FC = () => {
// TODO replace with const { data: data, send: loadData, error } = useRequest(APApi.readAPStatus());
// TODO replace with const { data: data, send: loadData, error } = useRequest(APApi.readAPStatus);
const { loadData, data, errorMessage } = useRest<NTPStatus>({ read: NTPApi.readNTPStatus });

const [localTime, setLocalTime] = useState<string>('');
Expand Down
2 changes: 1 addition & 1 deletion interface/src/framework/system/SystemLog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const levelLabel = (level: LogLevel) => {
const SystemLog: FC = () => {
const { LL } = useI18nContext();

// TODO replace with const { data: data, send: loadData, error } = useRequest(APApi.readAPStatus());
// TODO replace with const { data: data, send: loadData, error } = useRequest(APApi.readAPStatus);
const { loadData, data, setData, origData, dirtyFlags, blocker, setDirtyFlags, setOrigData } = useRest<LogSettings>({
read: SystemApi.readLogSettings
});
Expand Down
2 changes: 1 addition & 1 deletion interface/src/framework/system/SystemStatusForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const SystemStatusForm: FC = () => {
const { LL } = useI18nContext();
const [restarting, setRestarting] = useState<boolean>();

// TODO replace with const { data: data, send: loadData, error } = useRequest(APApi.readAPStatus());
// TODO replace with const { data: data, send: loadData, error } = useRequest(APApi.readAPStatus);
const { loadData, data, errorMessage } = useRest<SystemStatus>({ read: SystemApi.readSystemStatus });

const { me } = useContext(AuthenticatedContext);
Expand Down
9 changes: 2 additions & 7 deletions interface/src/project/SettingsCustomization.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,7 @@ const SettingsCustomization: FC = () => {

const { send: writeCustomEntities } = useRequest((data) => EMSESP.writeCustomEntities(data), { immediate: false });

const {
send: readDeviceEntities,
update: updateDeviceEntities,
onSuccess: onSuccess
} = useRequest((data) => EMSESP.readDeviceEntities(data), {
const { send: readDeviceEntities, onSuccess: onSuccess } = useRequest((data) => EMSESP.readDeviceEntities(data), {
initialData: [],
immediate: false,
force: true
Expand Down Expand Up @@ -236,8 +232,7 @@ const SettingsCustomization: FC = () => {
};

const maskDisabled = (set: boolean) => {
// TODO fix update!
updateDeviceEntities(
setDeviceEntities(
deviceEntities.map(function (de) {
if ((de.m & selectedFilters || !selectedFilters) && de.id.toLowerCase().includes(search.toLowerCase())) {
return {
Expand Down
115 changes: 45 additions & 70 deletions interface/src/project/SettingsEntities.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import WarningIcon from '@mui/icons-material/Warning';
import { Button, Typography, Box } from '@mui/material';
import { Table, Header, HeaderRow, HeaderCell, Body, Row, Cell } from '@table-library/react-table-library/table';
import { useTheme } from '@table-library/react-table-library/theme';
import { useState, useEffect, useCallback } from 'react';
import { updateState, useRequest } from 'alova';
import { useState, useCallback } from 'react';
import { unstable_useBlocker as useBlocker } from 'react-router-dom';

import { toast } from 'react-toastify';
Expand All @@ -18,18 +19,25 @@ import type { FC } from 'react';
import { ButtonRow, FormLoader, SectionContent, BlockNavigation } from 'components';

import { useI18nContext } from 'i18n/i18n-react';
import { extractErrorMessage } from 'utils';

const SettingsEntities: FC = () => {
const { LL } = useI18nContext();
const [numChanges, setNumChanges] = useState<number>(0);
const blocker = useBlocker(numChanges !== 0);
const [entities, setEntities] = useState<EntityItem[]>();
const [selectedEntityItem, setSelectedEntityItem] = useState<EntityItem>();
const [errorMessage, setErrorMessage] = useState<string>();
const [creating, setCreating] = useState<boolean>(false);
const [dialogOpen, setDialogOpen] = useState<boolean>(false);

const {
data: entities,
send: fetchEntities,
error
} = useRequest(EMSESP.readEntities, {
initialData: []
});

const { send: writeEntities } = useRequest((data) => EMSESP.writeEntities(data), { immediate: false });

function hasEntityChanged(ei: EntityItem) {
return (
ei.id !== ei.o_id ||
Expand All @@ -45,12 +53,6 @@ const SettingsEntities: FC = () => {
);
}

useEffect(() => {
if (entities) {
setNumChanges(entities ? entities.filter((ei) => hasEntityChanged(ei)).length : 0);
}
}, [entities]);

const entity_theme = useTheme({
Table: `
--data-table-library_grid-template-columns: repeat(1, minmax(60px, 1fr)) minmax(80px, auto) 80px 80px 80px;
Expand Down Expand Up @@ -105,62 +107,32 @@ const SettingsEntities: FC = () => {
`
});

const fetchEntities = useCallback(async () => {
try {
const response = await EMSESP.readEntities();
setEntities(
response.data.entities.map((ei) => ({
...ei,
o_id: ei.id,
o_device_id: ei.device_id,
o_type_id: ei.type_id,
o_offset: ei.offset,
o_factor: ei.factor,
o_uom: ei.uom,
o_value_type: ei.value_type,
o_name: ei.name,
o_writeable: ei.writeable,
o_deleted: ei.deleted
}))
);
} catch (error) {
setErrorMessage(extractErrorMessage(error, LL.PROBLEM_LOADING()));
}
}, [LL]);

useEffect(() => {
void fetchEntities();
}, [fetchEntities]);

const saveEntities = async () => {
if (entities) {
try {
const response = await EMSESP.writeEntities({
entities: entities
.filter((ei) => !ei.deleted)
.map((condensed_ei) => ({
id: condensed_ei.id,
name: condensed_ei.name,
device_id: condensed_ei.device_id,
type_id: condensed_ei.type_id,
offset: condensed_ei.offset,
factor: condensed_ei.factor,
uom: condensed_ei.uom,
writeable: condensed_ei.writeable,
value_type: condensed_ei.value_type
}))
});

if (response.status === 200) {
toast.success(LL.ENTITIES_UPDATED());
} else {
toast.error(LL.PROBLEM_UPDATING());
}
await writeEntities(
entities
.filter((ei) => !ei.deleted)
.map((condensed_ei) => ({
id: condensed_ei.id,
name: condensed_ei.name,
device_id: condensed_ei.device_id,
type_id: condensed_ei.type_id,
offset: condensed_ei.offset,
factor: condensed_ei.factor,
uom: condensed_ei.uom,
writeable: condensed_ei.writeable,
value_type: condensed_ei.value_type
}))
)
.then(() => {
toast.success(LL.ENTITIES_UPDATED());
})
.catch((err) => {
toast.error(err.message);
})
.finally(async () => {
await fetchEntities();
} catch (error) {
toast.error(extractErrorMessage(error, LL.PROBLEM_UPDATING()));
}
}
setNumChanges(0);
});
};

const editEntityItem = useCallback((ei: EntityItem) => {
Expand All @@ -175,11 +147,14 @@ const SettingsEntities: FC = () => {

const onDialogSave = (updatedItem: EntityItem) => {
setDialogOpen(false);
if (entities && creating) {
setEntities([...entities.filter((ei) => creating || ei.o_id !== updatedItem.o_id), updatedItem]);
} else {
setEntities(entities?.map((ei) => (ei.id === updatedItem.id ? { ...ei, ...updatedItem } : ei)));
}

updateState('entities', (data) => {
const new_data = creating
? [...data.filter((ei) => creating || ei.o_id !== updatedItem.o_id), updatedItem]
: data.map((ei) => (ei.id === updatedItem.id ? { ...ei, ...updatedItem } : ei));
setNumChanges(new_data.filter((ei) => hasEntityChanged(ei)).length);
return new_data;
});
};

const addEntityItem = () => {
Expand Down Expand Up @@ -215,7 +190,7 @@ const SettingsEntities: FC = () => {

const renderEntity = () => {
if (!entities) {
return <FormLoader errorMessage={errorMessage} />;
return <FormLoader onRetry={fetchEntities} errorMessage={error?.message} />;
}

return (
Expand Down
Loading

0 comments on commit f58dbf6

Please sign in to comment.